d7fd8d5a554c463252c7afa5dc1480c14d76b00c
[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 TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
18 use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
19 use TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend;
20 use TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser;
21 use TYPO3\CMS\Extbase\Persistence\QueryInterface;
22
23 /**
24 * Test case
25 */
26 class Typo3DbBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
27 {
28 /**
29 * @var DataMapper
30 */
31 protected static $dataMapper;
32
33 /**
34 * Setup DataMapper
35 */
36 public static function setUpBeforeClass()
37 {
38 self::$dataMapper = new DataMapper();
39 }
40
41 /**
42 * @test
43 */
44 public function uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly()
45 {
46 $mockValueObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class)
47 ->setMethods(array('_getProperties'))
48 ->disableOriginalConstructor()
49 ->getMock();
50 $mockValueObject->expects($this->once())->method('_getProperties')->will($this->returnValue(array('propertyName' => 'propertyValue')));
51 $mockColumnMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class)
52 ->setMethods(array('isPersistableProperty', 'getColumnName'))
53 ->disableOriginalConstructor()
54 ->getMock();
55 $mockColumnMap->expects($this->any())->method('getColumnName')->will($this->returnValue('column_name'));
56 $tableName = 'tx_foo_table';
57 $mockDataMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class)
58 ->setMethods(array('isPersistableProperty', 'getColumnMap', 'getTableName'))
59 ->disableOriginalConstructor()
60 ->getMock();
61 $mockDataMap->expects($this->any())->method('isPersistableProperty')->will($this->returnValue(true));
62 $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($mockColumnMap));
63 $mockDataMap->expects($this->any())->method('getTableName')->will($this->returnValue($tableName));
64 $mockDataMapper = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class)
65 ->setMethods(array('getDataMap', 'getPlainValue'))
66 ->disableOriginalConstructor()
67 ->getMock();
68 $mockDataMapper->expects($this->once())->method('getDataMap')->will($this->returnValue($mockDataMap));
69 $mockDataMapper->expects($this->once())->method('getPlainValue')->will($this->returnValue('plainPropertyValue'));
70 $expectedStatement = 'SELECT * FROM tx_foo_table WHERE column_name=?';
71 $expectedParameters = array('plainPropertyValue');
72 $expectedUid = 52;
73 $mockDataBaseHandle = $this->getMockBuilder(\TYPO3\CMS\Core\Database\DatabaseConnection::class)
74 ->setMethods(array('sql_query', 'sql_fetch_assoc'))
75 ->disableOriginalConstructor()
76 ->getMock();
77 $mockDataBaseHandle->expects($this->once())->method('sql_query')->will($this->returnValue('resource'));
78 $mockDataBaseHandle->expects($this->any())->method('sql_fetch_assoc')->with('resource')->will($this->returnValue(array('uid' => $expectedUid)));
79 $mockTypo3DbBackend = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, array('checkSqlErrors', 'replacePlaceholders', 'addVisibilityConstraintStatement'), array(), '', false);
80 $mockTypo3DbBackend->expects($this->once())->method('addVisibilityConstraintStatement')->with($this->isInstanceOf(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class), $tableName, $this->isType('array'));
81 $mockTypo3DbBackend->expects($this->once())->method('replacePlaceholders')->with($expectedStatement, $expectedParameters);
82 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
83 $mockTypo3DbBackend->_set('databaseHandle', $mockDataBaseHandle);
84 $result = $mockTypo3DbBackend->_callRef('getUidOfAlreadyPersistedValueObject', $mockValueObject);
85 $this->assertSame($expectedUid, $result);
86 }
87
88 /**
89 * @test
90 */
91 public function doLanguageAndWorkspaceOverlayChangesUidIfInPreview()
92 {
93 $comparisonRow = array(
94 'uid' => '42',
95 'pid' => '42',
96 '_ORIG_pid' => '-1',
97 '_ORIG_uid' => '43'
98 );
99 $row = array(
100 'uid' => '42',
101 'pid' => '42'
102 );
103 $workspaceVersion = array(
104 'uid' => '43',
105 'pid' => '-1'
106 );
107 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
108 $mockQuerySettings = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class)
109 ->setMethods(array('dummy'))
110 ->disableOriginalConstructor()
111 ->getMock();
112
113 $workspaceUid = 2;
114 $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
115 /** @var $pageRepositoryMock \TYPO3\CMS\Frontend\Page\PageRepository|\PHPUnit_Framework_MockObject_MockObject */
116 $pageRepositoryMock = $this->getMockBuilder(\TYPO3\CMS\Frontend\Page\PageRepository::class)
117 ->setMethods(array('movePlhOL', 'getWorkspaceVersionOfRecord'))
118 ->getMock();
119 $pageRepositoryMock->versioningPreview = true;
120 $pageRepositoryMock->expects($this->once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->will($this->returnValue($workspaceVersion));
121 $mockTypo3DbBackend = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, array('dummy'), array(), '', false);
122 $mockTypo3DbBackend->_set('pageRepository', $pageRepositoryMock);
123 $this->assertSame(array($comparisonRow), $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, array($row), $mockQuerySettings, $workspaceUid));
124 }
125
126 /**
127 * @return array
128 */
129 public function resolveParameterPlaceholdersReplacesValuesDataProvider()
130 {
131 return array(
132 'string' => array('bar', '123', '123'),
133 'array' => array('bar', array(1,2,3), '1,2,3'),
134 );
135 }
136
137 /**
138 * @param $parameter
139 * @param $value
140 * @param $expected
141 * @test
142 * @dataProvider resolveParameterPlaceholdersReplacesValuesDataProvider
143 */
144 public function resolveParameterPlaceholdersReplacesValues($parameter, $value, $expected)
145 {
146 $mock = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, array('quoteTextValueCallback'));
147 $mock->expects($this->any())->method('quoteTextValueCallback')->will($this->returnArgument(0));
148 $mock->_set('dataMapper', self::$dataMapper);
149 $stmtParts = array('tables' => array('foo'), 'where' => $parameter);
150 $parameters = array($parameter => $value);
151 $result = $mock->_call('resolveParameterPlaceholders', $stmtParts, $parameters);
152 $this->assertSame($expected, $result['where']);
153 }
154
155 /**
156 * @test
157 * @return void
158 */
159 public function getObjectCountByQueryThrowsExceptionIfOffsetWithoutLimitIsUsed()
160 {
161 $querySettingsProphecy = $this->prophesize(QuerySettingsInterface::class);
162 $queryInterfaceProphecy = $this->prophesize(QueryInterface::class);
163 $queryParserProphecy = $this->prophesize(Typo3DbQueryParser::class);
164 $queryParserProphecy->preparseQuery($queryInterfaceProphecy->reveal())->willReturn([123, []]);
165 $queryParserProphecy->parseQuery($queryInterfaceProphecy->reveal())->willReturn(
166 ['tables' => ['tt_content']]
167 );
168 $queryParserProphecy->addDynamicQueryParts(\Prophecy\Argument::cetera())->willReturn();
169 $queryInterfaceProphecy->getQuerySettings()->willReturn($querySettingsProphecy->reveal());
170 $queryInterfaceProphecy->getConstraint()->willReturn();
171 $queryInterfaceProphecy->getLimit()->willReturn();
172 $queryInterfaceProphecy->getOffset()->willReturn(10);
173
174 $this->expectException(\InvalidArgumentException::class);
175 $this->expectExceptionCode(1465223252);
176
177 $typo3DbQueryParser = new Typo3DbBackend();
178 $typo3DbQueryParser->injectQueryParser($queryParserProphecy->reveal());
179 $typo3DbQueryParser->getObjectCountByQuery($queryInterfaceProphecy->reveal());
180 }
181 }