‪TYPO3CMS  9.5
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 
17 use Doctrine\DBAL\Driver\Statement;
18 use Prophecy\Argument;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪Typo3DbBackendTest extends UnitTestCase
34 {
39  protected ‪$resetSingletonInstances = true;
40 
41  public function ‪setUp()
42  {
43  parent::setUp();
44  ‪$GLOBALS['TSFE'] = new \stdClass();
45  }
46 
51  {
52  return [
53  'isFrontendEnvironment' => [true],
54  'isBackendEnvironment' => [false],
55  ];
56  }
57 
62  public function ‪uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment)
63  {
64  $mockValueObject = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\DomainObject\AbstractValueObject::class)
65  ->setMethods(['_getProperties'])
66  ->disableOriginalConstructor()
67  ->getMock();
68  $mockValueObject->expects($this->once())->method('_getProperties')
69  ->will($this->returnValue(['propertyName' => 'propertyValue']));
70  $mockColumnMap = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Mapper\DataMap::class)
71  ->setMethods(['isPersistableProperty', 'getColumnName'])
72  ->disableOriginalConstructor()
73  ->getMock();
74  $mockColumnMap->expects($this->any())->method('getColumnName')->will($this->returnValue('column_name'));
75  $tableName = 'tx_foo_table';
76  $mockDataMap = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Mapper\DataMap::class)
77  ->setMethods(['isPersistableProperty', 'getColumnMap', 'getTableName'])
78  ->disableOriginalConstructor()
79  ->getMock();
80  $mockDataMap->expects($this->any())->method('isPersistableProperty')->will($this->returnValue(true));
81  $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($mockColumnMap));
82  $mockDataMap->expects($this->any())->method('getTableName')->will($this->returnValue($tableName));
83  $mockDataMapper = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Mapper\DataMapper::class)
84  ->setMethods(['getDataMap', 'getPlainValue'])
85  ->disableOriginalConstructor()
86  ->getMock();
87  $mockDataMapper->expects($this->once())->method('getDataMap')
88  ->will($this->returnValue($mockDataMap));
89  $mockDataMapper->expects($this->once())->method('getPlainValue')
90  ->will($this->returnValue('plainPropertyValue'));
91  $expectedUid = 52;
92 
93  $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
94  $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
95  $queryResultProphet = $this->prophesize(Statement::class);
96  $queryResultProphet->fetchColumn(Argument::cetera())->willReturn($expectedUid);
97  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
98  $queryBuilderProphet->execute()->willReturn($queryResultProphet->reveal());
99  $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
100  $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
101  $queryBuilderProphet->select('uid')->willReturn($queryBuilderProphet->reveal());
102  $queryBuilderProphet->from($tableName)->willReturn($queryBuilderProphet->reveal());
103  $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
104  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
105  $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
106 
107  $environmentServiceProphet = $this->prophesize(EnvironmentService::class);
108  $environmentServiceProphet->isEnvironmentInFrontendMode()->willReturn($isFrontendEnvironment);
109 
110  if ($isFrontendEnvironment) {
111  $queryBuilderProphet->setRestrictions(Argument::type(FrontendRestrictionContainer::class))
112  ->shouldBeCalled();
113  }
114 
115  $mockTypo3DbBackend = $this->getAccessibleMock(
116  \‪TYPO3\CMS\‪Extbase\Persistence\Generic\Storage\Typo3DbBackend::class,
117  ['dummy'],
118  [],
119  '',
120  false
121  );
122  $mockObjectManager = $this->createMock(ObjectManager::class);
123  $mockObjectManager->expects($this->any())
124  ->method('get')
125  ->with(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Mapper\DataMapper::class)
126  ->will($this->returnValue($mockDataMapper));
127 
128  $mockTypo3DbBackend->_set('objectManager', $mockObjectManager);
129  $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
130  $mockTypo3DbBackend->_set('connectionPool', $connectionPoolProphet->reveal());
131  $mockTypo3DbBackend->_set('environmentService', $environmentServiceProphet->reveal());
132  $result = $mockTypo3DbBackend->_callRef('getUidOfAlreadyPersistedValueObject', $mockValueObject);
133  $this->assertSame($expectedUid, $result);
134  }
135 
140  {
141  $comparisonRow = [
142  'uid' => '42',
143  'pid' => '42',
144  '_ORIG_pid' => '-1',
145  '_ORIG_uid' => '43'
146  ];
147  $row = [
148  'uid' => '42',
149  'pid' => '42'
150  ];
151  $workspaceVersion = [
152  'uid' => '43',
153  'pid' => '-1'
154  ];
156  $mockQuerySettings = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Typo3QuerySettings::class)
157  ->setMethods(['dummy'])
158  ->disableOriginalConstructor()
159  ->getMock();
160 
161  $workspaceUid = 2;
162 
163  $objectManagerMock = $this->getMockBuilder(ObjectManager::class)
164  ->disableOriginalConstructor()
165  ->getMock();
166  $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
167  $context = new Context([
168  'workspace' => new WorkspaceAspect($workspaceUid)
169  ]);
171  $pageRepositoryMock = $this->getMockBuilder(PageRepository::class)
172  ->setMethods(['movePlhOL', 'getWorkspaceVersionOfRecord'])
173  ->setConstructorArgs([$context])
174  ->getMock();
175  $pageRepositoryMock->expects($this->once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->will($this->returnValue($workspaceVersion));
176  $objectManagerMock->expects($this->at(0))->method('get')->with(Context::class)->willReturn($context);
177  $objectManagerMock->expects($this->at(1))->method('get')->with(PageRepository::class, $context)->willReturn($pageRepositoryMock);
178  $mockTypo3DbBackend = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Storage\Typo3DbBackend::class, ['dummy'], [], '', false);
179  $mockTypo3DbBackend->injectObjectManager($objectManagerMock);
180  $this->assertSame([$comparisonRow], $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, [$row], $mockQuerySettings));
181  }
182 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: Typo3DbBackendTest.php:38
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:29
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly
‪uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment)
Definition: Typo3DbBackendTest.php:61
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage
Definition: Typo3DbBackendTest.php:2
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\doLanguageAndWorkspaceOverlayChangesUidIfInPreview
‪doLanguageAndWorkspaceOverlayChangesUidIfInPreview()
Definition: Typo3DbBackendTest.php:138
‪TYPO3\CMS\Frontend\Page\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Extbase\Service\EnvironmentService
Definition: EnvironmentService.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider
‪array uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider()
Definition: Typo3DbBackendTest.php:49
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest
Definition: Typo3DbBackendTest.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\setUp
‪setUp()
Definition: Typo3DbBackendTest.php:40
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:29
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25