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