[CLEANUP] Remove redundant code in Persistence
[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 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
17
18 /**
19 * Test case
20 */
21 class Typo3DbBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
22
23 /**
24 * @var DataMapper
25 */
26 protected static $dataMapper;
27
28 /**
29 * Setup DataMapper
30 */
31 public static function setUpBeforeClass() {
32 self::$dataMapper = new DataMapper();
33 }
34
35 /**
36 * @test
37 */
38 public function uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly() {
39 $mockValueObject = $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject', array('_getProperties'), array(), '', FALSE);
40 $mockValueObject->expects($this->once())->method('_getProperties')->will($this->returnValue(array('propertyName' => 'propertyValue')));
41 $mockColumnMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('isPersistableProperty', 'getColumnName'), array(), '', FALSE);
42 $mockColumnMap->expects($this->any())->method('getColumnName')->will($this->returnValue('column_name'));
43 $tableName = 'tx_foo_table';
44 $mockDataMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('isPersistableProperty', 'getColumnMap', 'getTableName'), array(), '', FALSE);
45 $mockDataMap->expects($this->any())->method('isPersistableProperty')->will($this->returnValue(TRUE));
46 $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($mockColumnMap));
47 $mockDataMap->expects($this->any())->method('getTableName')->will($this->returnValue($tableName));
48 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('getDataMap', 'getPlainValue'), array(), '', FALSE);
49 $mockDataMapper->expects($this->once())->method('getDataMap')->will($this->returnValue($mockDataMap));
50 $mockDataMapper->expects($this->once())->method('getPlainValue')->will($this->returnValue('plainPropertyValue'));
51 $expectedStatement = 'SELECT * FROM tx_foo_table WHERE column_name=?';
52 $expectedParameters = array('plainPropertyValue');
53 $expectedUid = 52;
54 $mockDataBaseHandle = $this->getMock('TYPO3\CMS\Core\Database\DatabaseConnection', array('sql_query', 'sql_fetch_assoc'), array(), '', FALSE);
55 $mockDataBaseHandle->expects($this->once())->method('sql_query')->will($this->returnValue('resource'));
56 $mockDataBaseHandle->expects($this->any())->method('sql_fetch_assoc')->with('resource')->will($this->returnValue(array('uid' => $expectedUid)));
57 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('checkSqlErrors', 'replacePlaceholders', 'addVisibilityConstraintStatement'), array(), '', FALSE);
58 $mockTypo3DbBackend->expects($this->once())->method('addVisibilityConstraintStatement')->with($this->isInstanceOf('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface'), $tableName, $this->isType('array'));
59 $mockTypo3DbBackend->expects($this->once())->method('replacePlaceholders')->with($expectedStatement, $expectedParameters);
60 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
61 $mockTypo3DbBackend->_set('databaseHandle', $mockDataBaseHandle);
62 $result = $mockTypo3DbBackend->_callRef('getUidOfAlreadyPersistedValueObject', $mockValueObject);
63 $this->assertSame($expectedUid, $result);
64 }
65
66 /**
67 * @test
68 */
69 public function doLanguageAndWorkspaceOverlayChangesUidIfInPreview() {
70 $comparisonRow = array(
71 'uid' => '43',
72 'pid' => '42',
73 '_ORIG_pid' => '-1',
74 '_ORIG_uid' => '43'
75 );
76 $row = array(
77 'uid' => '42',
78 'pid' => '42'
79 );
80 $workspaceVersion = array(
81 'uid' => '43',
82 'pid' => '-1'
83 );
84 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
85 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'), array(), '', FALSE);
86
87 $workspaceUid = 2;
88 $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
89 /** @var $pageRepositoryMock \TYPO3\CMS\Frontend\Page\PageRepository|\PHPUnit_Framework_MockObject_MockObject */
90 $pageRepositoryMock = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('movePlhOL', 'getWorkspaceVersionOfRecord'));
91 $pageRepositoryMock->versioningPreview = TRUE;
92 $pageRepositoryMock->expects($this->once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->will($this->returnValue($workspaceVersion));
93 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
94 $mockTypo3DbBackend->_set('pageRepository', $pageRepositoryMock);
95 $this->assertSame(array($comparisonRow), $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, array($row), $mockQuerySettings, $workspaceUid));
96 }
97
98 /**
99 * @return array
100 */
101 public function resolveParameterPlaceholdersReplacesValuesDataProvider() {
102 return array(
103 'string' => array('bar', '123', '123'),
104 'array' => array('bar', array(1,2,3), '1,2,3'),
105 );
106 }
107
108 /**
109 * @param $parameter
110 * @param $value
111 * @param $expected
112 * @test
113 * @dataProvider resolveParameterPlaceholdersReplacesValuesDataProvider
114 */
115 public function resolveParameterPlaceholdersReplacesValues($parameter, $value, $expected) {
116 $mock = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('quoteTextValueCallback'));
117 $mock->expects($this->any())->method('quoteTextValueCallback')->will($this->returnArgument(0));
118 $mock->_set('dataMapper', self::$dataMapper);
119 $stmtParts = array('tables' => array('foo'), 'where' => $parameter);
120 $parameters = array($parameter => $value);
121 $result = $mock->_call('resolveParameterPlaceholders', $stmtParts, $parameters);
122 $this->assertSame($expected, $result['where']);
123 }
124 }