‪TYPO3CMS  10.4
Typo3DbBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\Driver\Statement;
19 use Prophecy\Argument;
36 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
37 
41 class ‪Typo3DbBackendTest extends UnitTestCase
42 {
47  protected ‪$resetSingletonInstances = true;
48 
53  {
54  return [
55  'isFrontendEnvironment' => [true],
56  'isBackendEnvironment' => [false],
57  ];
58  }
59 
64  public function ‪uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment)
65  {
66  $mockValueObject = $this->getMockBuilder(AbstractValueObject::class)
67  ->setMethods(['_getProperties'])
68  ->disableOriginalConstructor()
69  ->getMock();
70  $mockValueObject->expects(self::once())->method('_getProperties')
71  ->willReturn(['propertyName' => 'propertyValue']);
72  $mockColumnMap = $this->getMockBuilder(DataMap::class)
73  ->setMethods(['isPersistableProperty', 'getColumnName'])
74  ->disableOriginalConstructor()
75  ->getMock();
76  $mockColumnMap->expects(self::any())->method('getColumnName')->willReturn('column_name');
77  $tableName = 'tx_foo_table';
78  $mockDataMap = $this->getMockBuilder(DataMap::class)
79  ->setMethods(['isPersistableProperty', 'getColumnMap', 'getTableName'])
80  ->disableOriginalConstructor()
81  ->getMock();
82  $mockDataMap->expects(self::any())->method('isPersistableProperty')->willReturn(true);
83  $mockDataMap->expects(self::any())->method('getColumnMap')->willReturn($mockColumnMap);
84  $mockDataMap->expects(self::any())->method('getTableName')->willReturn($tableName);
85  $mockDataMapper = $this->getMockBuilder(DataMapper::class)
86  ->setMethods(['getDataMap', 'getPlainValue'])
87  ->disableOriginalConstructor()
88  ->getMock();
89  $mockDataMapper->expects(self::once())->method('getDataMap')
90  ->willReturn($mockDataMap);
91  $mockDataMapper->expects(self::once())->method('getPlainValue')
92  ->willReturn('plainPropertyValue');
93  $expectedUid = 52;
94 
95  $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
96  $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
97  $queryResultProphet = $this->prophesize(Statement::class);
98  $queryResultProphet->fetchColumn(Argument::cetera())->willReturn($expectedUid);
99  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
100  $queryBuilderProphet->execute()->willReturn($queryResultProphet->reveal());
101  $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
102  $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
103  $queryBuilderProphet->select('uid')->willReturn($queryBuilderProphet->reveal());
104  $queryBuilderProphet->from($tableName)->willReturn($queryBuilderProphet->reveal());
105  $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
106  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
107  $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
108 
109  $environmentServiceProphet = $this->prophesize(EnvironmentService::class);
110  $environmentServiceProphet->isEnvironmentInFrontendMode()->willReturn($isFrontendEnvironment);
111 
112  if ($isFrontendEnvironment) {
113  $queryBuilderProphet->setRestrictions(Argument::type(FrontendRestrictionContainer::class))
114  ->shouldBeCalled();
115  }
116 
117  $mockTypo3DbBackend = $this->getAccessibleMock(
118  Typo3DbBackend::class,
119  ['dummy'],
120  [],
121  '',
122  false
123  );
124  $mockObjectManager = $this->createMock(ObjectManager::class);
125  $mockObjectManager->expects(self::any())
126  ->method('get')
127  ->with(DataMapper::class)
128  ->willReturn($mockDataMapper);
129 
130  $mockTypo3DbBackend->_set('objectManager', $mockObjectManager);
131  $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
132  $mockTypo3DbBackend->_set('connectionPool', $connectionPoolProphet->reveal());
133  $mockTypo3DbBackend->_set('environmentService', $environmentServiceProphet->reveal());
134  $result = $mockTypo3DbBackend->getUidOfAlreadyPersistedValueObject($mockValueObject);
135  self::assertSame($expectedUid, $result);
136  }
137 
142  {
143  $comparisonRow = [
144  'uid' => '42',
145  'pid' => '42',
146  '_ORIG_pid' => '42',
147  '_ORIG_uid' => '43'
148  ];
149  $row = [
150  'uid' => '42',
151  'pid' => '42'
152  ];
153  $workspaceVersion = [
154  'uid' => '43',
155  'pid' => '42'
156  ];
157  $mockQuerySettings = $this->getMockBuilder(Typo3QuerySettings::class)
158  ->setMethods(['dummy'])
159  ->disableOriginalConstructor()
160  ->getMock();
161 
162  $workspaceUid = 2;
163 
164  $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
165  $context = new ‪Context([
166  'workspace' => new ‪WorkspaceAspect($workspaceUid)
167  ]);
168  $pageRepositoryMock = $this->getMockBuilder(PageRepository::class)
169  ->setMethods(['movePlhOL', 'getWorkspaceVersionOfRecord'])
170  ->setConstructorArgs([$context])
171  ->getMock();
172  $query = new ‪Query('random');
173  $query->setQuerySettings($mockQuerySettings);
174  $pageRepositoryMock->expects(self::once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->willReturn($workspaceVersion);
175  $environmentService = new ‪EnvironmentService();
176  $environmentService->setFrontendMode(true);
177  GeneralUtility::setSingletonInstance(Context::class, $context);
178  GeneralUtility::addInstance(PageRepository::class, $pageRepositoryMock);
179  $mockTypo3DbBackend = $this->getAccessibleMock(Typo3DbBackend::class, ['dummy'], [], '', false);
180  $mockTypo3DbBackend->injectEnvironmentService($environmentService);
181  self::assertSame([$comparisonRow], $mockTypo3DbBackend->_call('overlayLanguageAndWorkspace', $sourceMock, [$row], $query));
182  }
183 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: Typo3DbBackendTest.php:46
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:35
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend
Definition: Typo3DbBackend.php:55
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap
Definition: DataMap.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly
‪uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment)
Definition: Typo3DbBackendTest.php:63
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage
Definition: Typo3DbBackendTest.php:16
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:52
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\overlayLanguageAndWorkspaceChangesUidIfInPreview
‪overlayLanguageAndWorkspaceChangesUidIfInPreview()
Definition: Typo3DbBackendTest.php:140
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Extbase\DomainObject\AbstractValueObject
Definition: AbstractValueObject.php:24
‪TYPO3\CMS\Extbase\Service\EnvironmentService
Definition: EnvironmentService.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider
‪array uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider()
Definition: Typo3DbBackendTest.php:51
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest
Definition: Typo3DbBackendTest.php:42
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:38
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:31
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings
Definition: Typo3QuerySettings.php:29