[TASK] Migrate Extbase Typo3DbBackend to Context API
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / Storage / Typo3DbBackendTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage;
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 Doctrine\DBAL\Driver\Statement;
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Context\Context;
20 use TYPO3\CMS\Core\Context\WorkspaceAspect;
21 use TYPO3\CMS\Core\Database\ConnectionPool;
22 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
24 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
25 use TYPO3\CMS\Extbase\Object\ObjectManager;
26 use TYPO3\CMS\Extbase\Service\EnvironmentService;
27 use TYPO3\CMS\Frontend\Page\PageRepository;
28
29 /**
30 * Test case
31 */
32 class Typo3DbBackendTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
33 {
34 /**
35 * Subject is not notice free, disable E_NOTICES
36 */
37 protected static $suppressNotices = true;
38
39 /**
40 * Due to nested PageRepository / FrontendRestriction Container issues, the Context object is set
41 * @var bool
42 */
43 protected $resetSingletonInstances = true;
44
45 public function setUp()
46 {
47 parent::setUp();
48 $GLOBALS['TSFE'] = new \stdClass();
49 }
50
51 /**
52 * @return array
53 */
54 public function uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider(): array
55 {
56 return [
57 'isFrontendEnvironment' => [true],
58 'isBackendEnvironment' => [false],
59 ];
60 }
61
62 /**
63 * @test
64 * @dataProvider uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider
65 */
66 public function uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment)
67 {
68 $mockValueObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class)
69 ->setMethods(['_getProperties'])
70 ->disableOriginalConstructor()
71 ->getMock();
72 $mockValueObject->expects($this->once())->method('_getProperties')
73 ->will($this->returnValue(['propertyName' => 'propertyValue']));
74 $mockColumnMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class)
75 ->setMethods(['isPersistableProperty', 'getColumnName'])
76 ->disableOriginalConstructor()
77 ->getMock();
78 $mockColumnMap->expects($this->any())->method('getColumnName')->will($this->returnValue('column_name'));
79 $tableName = 'tx_foo_table';
80 $mockDataMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class)
81 ->setMethods(['isPersistableProperty', 'getColumnMap', 'getTableName'])
82 ->disableOriginalConstructor()
83 ->getMock();
84 $mockDataMap->expects($this->any())->method('isPersistableProperty')->will($this->returnValue(true));
85 $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($mockColumnMap));
86 $mockDataMap->expects($this->any())->method('getTableName')->will($this->returnValue($tableName));
87 $mockDataMapper = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class)
88 ->setMethods(['getDataMap', 'getPlainValue'])
89 ->disableOriginalConstructor()
90 ->getMock();
91 $mockDataMapper->expects($this->once())->method('getDataMap')
92 ->will($this->returnValue($mockDataMap));
93 $mockDataMapper->expects($this->once())->method('getPlainValue')
94 ->will($this->returnValue('plainPropertyValue'));
95 $expectedUid = 52;
96
97 $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
98 $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
99 $queryResultProphet = $this->prophesize(Statement::class);
100 $queryResultProphet->fetchColumn(Argument::cetera())->willReturn($expectedUid);
101 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
102 $queryBuilderProphet->execute()->willReturn($queryResultProphet->reveal());
103 $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
104 $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
105 $queryBuilderProphet->select('uid')->willReturn($queryBuilderProphet->reveal());
106 $queryBuilderProphet->from($tableName)->willReturn($queryBuilderProphet->reveal());
107 $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
108 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
109 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
110
111 $environmentServiceProphet = $this->prophesize(EnvironmentService::class);
112 $environmentServiceProphet->isEnvironmentInFrontendMode()->willReturn($isFrontendEnvironment);
113
114 if ($isFrontendEnvironment) {
115 $queryBuilderProphet->setRestrictions(Argument::type(FrontendRestrictionContainer::class))
116 ->shouldBeCalled();
117 }
118
119 $mockTypo3DbBackend = $this->getAccessibleMock(
120 \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class,
121 ['dummy'],
122 [],
123 '',
124 false
125 );
126 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
127 $mockTypo3DbBackend->_set('connectionPool', $connectionPoolProphet->reveal());
128 $mockTypo3DbBackend->_set('environmentService', $environmentServiceProphet->reveal());
129 $result = $mockTypo3DbBackend->_callRef('getUidOfAlreadyPersistedValueObject', $mockValueObject);
130 $this->assertSame($expectedUid, $result);
131 }
132
133 /**
134 * @test
135 */
136 public function doLanguageAndWorkspaceOverlayChangesUidIfInPreview()
137 {
138 $comparisonRow = [
139 'uid' => '42',
140 'pid' => '42',
141 '_ORIG_pid' => '-1',
142 '_ORIG_uid' => '43'
143 ];
144 $row = [
145 'uid' => '42',
146 'pid' => '42'
147 ];
148 $workspaceVersion = [
149 'uid' => '43',
150 'pid' => '-1'
151 ];
152 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
153 $mockQuerySettings = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class)
154 ->setMethods(['dummy'])
155 ->disableOriginalConstructor()
156 ->getMock();
157
158 $workspaceUid = 2;
159
160 $objectManagerMock = $this->getMockBuilder(ObjectManager::class)
161 ->disableOriginalConstructor()
162 ->getMock();
163 $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
164 /** @var $pageRepositoryMock PageRepository|\PHPUnit_Framework_MockObject_MockObject */
165 $pageRepositoryMock = $this->getMockBuilder(PageRepository::class)
166 ->setMethods(['movePlhOL', 'getWorkspaceVersionOfRecord'])
167 ->disableOriginalConstructor()
168 ->getMock();
169 $pageRepositoryMock->expects($this->once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->will($this->returnValue($workspaceVersion));
170 $pageRepositoryMock->versioningWorkspaceId = $workspaceUid;
171 $context = new Context([
172 'workspace' => new WorkspaceAspect($workspaceUid)
173 ]);
174 $objectManagerMock->expects($this->at(0))->method('get')->with(Context::class)->willReturn($context);
175 $objectManagerMock->expects($this->at(1))->method('get')->with(PageRepository::class, $context)->willReturn($pageRepositoryMock);
176 $mockTypo3DbBackend = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, ['dummy'], [], '', false);
177 $mockTypo3DbBackend->injectObjectManager($objectManagerMock);
178 $this->assertSame([$comparisonRow], $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, [$row], $mockQuerySettings));
179 }
180 }