TYPO3 CMS  TYPO3_7-6
Typo3DbBackendTest.php
Go to the documentation of this file.
1 <?php
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 
22 
27 {
31  protected static $dataMapper;
32 
36  public static function setUpBeforeClass()
37  {
38  self::$dataMapper = new DataMapper();
39  }
40 
45  {
46  $mockValueObject = $this->getMock(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class, ['_getProperties'], [], '', false);
47  $mockValueObject->expects($this->once())->method('_getProperties')->will($this->returnValue(['propertyName' => 'propertyValue']));
48  $mockColumnMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, ['isPersistableProperty', 'getColumnName'], [], '', false);
49  $mockColumnMap->expects($this->any())->method('getColumnName')->will($this->returnValue('column_name'));
50  $tableName = 'tx_foo_table';
51  $mockDataMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, ['isPersistableProperty', 'getColumnMap', 'getTableName'], [], '', false);
52  $mockDataMap->expects($this->any())->method('isPersistableProperty')->will($this->returnValue(true));
53  $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($mockColumnMap));
54  $mockDataMap->expects($this->any())->method('getTableName')->will($this->returnValue($tableName));
55  $mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['getDataMap', 'getPlainValue'], [], '', false);
56  $mockDataMapper->expects($this->once())->method('getDataMap')->will($this->returnValue($mockDataMap));
57  $mockDataMapper->expects($this->once())->method('getPlainValue')->will($this->returnValue('plainPropertyValue'));
58  $expectedStatement = 'SELECT * FROM tx_foo_table WHERE column_name=?';
59  $expectedParameters = ['plainPropertyValue'];
60  $expectedUid = 52;
61  $mockDataBaseHandle = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, ['sql_query', 'sql_fetch_assoc'], [], '', false);
62  $mockDataBaseHandle->expects($this->once())->method('sql_query')->will($this->returnValue('resource'));
63  $mockDataBaseHandle->expects($this->any())->method('sql_fetch_assoc')->with('resource')->will($this->returnValue(['uid' => $expectedUid]));
64  $mockTypo3DbBackend = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, ['checkSqlErrors', 'replacePlaceholders', 'addVisibilityConstraintStatement'], [], '', false);
65  $mockTypo3DbBackend->expects($this->once())->method('addVisibilityConstraintStatement')->with($this->isInstanceOf(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class), $tableName, $this->isType('array'));
66  $mockTypo3DbBackend->expects($this->once())->method('replacePlaceholders')->with($expectedStatement, $expectedParameters);
67  $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
68  $mockTypo3DbBackend->_set('databaseHandle', $mockDataBaseHandle);
69  $result = $mockTypo3DbBackend->_callRef('getUidOfAlreadyPersistedValueObject', $mockValueObject);
70  $this->assertSame($expectedUid, $result);
71  }
72 
76  public function doLanguageAndWorkspaceOverlayChangesUidIfInPreview()
77  {
78  $comparisonRow = [
79  'uid' => '42',
80  'pid' => '42',
81  '_ORIG_pid' => '-1',
82  '_ORIG_uid' => '43'
83  ];
84  $row = [
85  'uid' => '42',
86  'pid' => '42'
87  ];
88  $workspaceVersion = [
89  'uid' => '43',
90  'pid' => '-1'
91  ];
93  $mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, ['dummy'], [], '', false);
94 
95  $workspaceUid = 2;
96  $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
98  $pageRepositoryMock = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, ['movePlhOL', 'getWorkspaceVersionOfRecord']);
99  $pageRepositoryMock->versioningPreview = true;
100  $pageRepositoryMock->expects($this->once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->will($this->returnValue($workspaceVersion));
101  $mockTypo3DbBackend = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, ['dummy'], [], '', false);
102  $mockTypo3DbBackend->_set('pageRepository', $pageRepositoryMock);
103  $this->assertSame([$comparisonRow], $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, [$row], $mockQuerySettings, $workspaceUid));
104  }
105 
110  {
111  return [
112  'string' => ['bar', '123', '123'],
113  'array' => ['bar', [1, 2, 3], '1,2,3'],
114  ];
115  }
116 
124  public function resolveParameterPlaceholdersReplacesValues($parameter, $value, $expected)
125  {
126  $mock = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, ['quoteTextValueCallback']);
127  $mock->expects($this->any())->method('quoteTextValueCallback')->will($this->returnArgument(0));
128  $mock->_set('dataMapper', self::$dataMapper);
129  $stmtParts = ['tables' => ['foo'], 'where' => $parameter];
130  $parameters = [$parameter => $value];
131  $result = $mock->_call('resolveParameterPlaceholders', $stmtParts, $parameters);
132  $this->assertSame($expected, $result['where']);
133  }
134 
141  {
142  $querySettingsProphecy = $this->prophesize(QuerySettingsInterface::class);
143  $queryInterfaceProphecy = $this->prophesize(QueryInterface::class);
144  $queryParserProphecy = $this->prophesize(Typo3DbQueryParser::class);
145  $queryParserProphecy->preparseQuery($queryInterfaceProphecy->reveal())->willReturn([123, []]);
146  $queryParserProphecy->parseQuery($queryInterfaceProphecy->reveal())->willReturn(
147  ['tables' => ['tt_content']]
148  );
149  $queryParserProphecy->addDynamicQueryParts(\Prophecy\Argument::cetera())->willReturn();
150  $queryInterfaceProphecy->getQuerySettings()->willReturn($querySettingsProphecy->reveal());
151  $queryInterfaceProphecy->getConstraint()->willReturn();
152  $queryInterfaceProphecy->getLimit()->willReturn();
153  $queryInterfaceProphecy->getOffset()->willReturn(10);
154 
155  $typo3DbQueryParser = new Typo3DbBackend();
156  $typo3DbQueryParser->injectQueryParser($queryParserProphecy->reveal());
157  $typo3DbQueryParser->getObjectCountByQuery($queryInterfaceProphecy->reveal());
158  }
159 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)