1c91cc99c83d3e90e809b7a1c14e918585f091c0
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / AddTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 class AddTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
24 */
25 protected $persistentManager;
26
27 /**
28 * @var array
29 */
30 protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
31
32 /**
33 * @var array
34 */
35 protected $coreExtensionsToLoad = ['extbase', 'fluid'];
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
39 */
40 protected $objectManager;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Persistence\Repository
44 */
45 protected $blogRepository;
46
47 /**
48 * Sets up this test suite.
49 */
50 protected function setUp()
51 {
52 parent::setUp();
53
54 $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
55 $this->persistentManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
56 $this->blogRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository::class);
57 }
58
59 /**
60 * @test
61 */
62 public function addSimpleObjectTest()
63 {
64 $newBlogTitle = 'aDi1oogh';
65 $newBlog = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Model\Blog::class);
66 $newBlog->setTitle($newBlogTitle);
67
68 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository $blogRepository */
69 $this->blogRepository->add($newBlog);
70 $this->persistentManager->persistAll();
71
72 $queryBuilder = (new ConnectionPool())->getQueryBuilderForTable('tx_blogexample_domain_model_blog');
73 $queryBuilder->getRestrictions()
74 ->removeAll();
75 $newBlogCount = $queryBuilder
76 ->count('*')
77 ->from('tx_blogexample_domain_model_blog')
78 ->where(
79 $queryBuilder->expr()->eq(
80 'title',
81 $queryBuilder->createNamedParameter($newBlogTitle, \PDO::PARAM_STR)
82 )
83 )
84 ->execute()
85 ->fetchColumn(0);
86 $this->assertEquals(1, $newBlogCount);
87 }
88
89 /**
90 * @test
91 */
92 public function addObjectSetsDefaultLanguageTest()
93 {
94 $newBlogTitle = 'aDi1oogh';
95 $newBlog = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Model\Blog::class);
96 $newBlog->setTitle($newBlogTitle);
97
98 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository $blogRepository */
99 $this->blogRepository->add($newBlog);
100 $this->persistentManager->persistAll();
101
102 $queryBuilder = (new ConnectionPool())->getQueryBuilderForTable('tx_blogexample_domain_model_blog');
103 $queryBuilder->getRestrictions()
104 ->removeAll();
105 $newBlogRecord = $queryBuilder
106 ->select('*')
107 ->from('tx_blogexample_domain_model_blog')
108 ->where(
109 $queryBuilder->expr()->eq(
110 'title',
111 $queryBuilder->createNamedParameter($newBlogTitle, \PDO::PARAM_STR)
112 )
113 )
114 ->execute()
115 ->fetch();
116 $this->assertEquals(0, $newBlogRecord['sys_language_uid']);
117 }
118
119 /**
120 * @test
121 */
122 public function addObjectSetsDefinedLanguageTest()
123 {
124 $newBlogTitle = 'aDi1oogh';
125 $newBlog = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Model\Blog::class);
126 $newBlog->setTitle($newBlogTitle);
127 $newBlog->_setProperty('_languageUid', -1);
128
129 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository $blogRepository */
130 $this->blogRepository->add($newBlog);
131 $this->persistentManager->persistAll();
132
133 $queryBuilder = (new ConnectionPool())->getQueryBuilderForTable('tx_blogexample_domain_model_blog');
134 $queryBuilder->getRestrictions()
135 ->removeAll();
136 $newBlogRecord = $queryBuilder
137 ->select('*')
138 ->from('tx_blogexample_domain_model_blog')
139 ->where(
140 $queryBuilder->expr()->eq(
141 'title',
142 $queryBuilder->createNamedParameter($newBlogTitle, \PDO::PARAM_STR)
143 )
144 )
145 ->execute()
146 ->fetch();
147 $this->assertEquals(-1, $newBlogRecord['sys_language_uid']);
148 }
149 }