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