‪TYPO3CMS  11.5
Typo3DbBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Result;
21 use Prophecy\Argument;
22 use Prophecy\PhpUnit\ProphecyTrait;
23 use Psr\Container\ContainerInterface;
29 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
46 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
47 
51 class ‪Typo3DbBackendTest extends UnitTestCase
52 {
53  use ProphecyTrait;
54 
59  protected ‪$resetSingletonInstances = true;
60 
65  {
66  return [
67  'isFrontendEnvironment' => [true],
68  'isBackendEnvironment' => [false],
69  ];
70  }
71 
77  public function ‪uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment): void
78  {
79  $mockColumnMap = $this->getMockBuilder(DataMap::class)
80  ->onlyMethods(['isPersistableProperty'])
81  ->addMethods(['getColumnName'])
82  ->disableOriginalConstructor()
83  ->getMock();
84  $mockColumnMap->method('getColumnName')->willReturn('column_name');
85  $tableName = 'tx_foo_table';
86  $mockDataMap = $this->getMockBuilder(DataMap::class)
87  ->onlyMethods(['isPersistableProperty', 'getColumnMap', 'getTableName'])
88  ->disableOriginalConstructor()
89  ->getMock();
90  $mockDataMap->method('isPersistableProperty')->willReturn(true);
91  $mockDataMap->method('getColumnMap')->willReturn($mockColumnMap);
92  $mockDataMap->method('getTableName')->willReturn($tableName);
93  $mockDataMapper = $this->getMockBuilder(DataMapper::class)
94  ->onlyMethods(['getDataMap', 'getPlainValue'])
95  ->disableOriginalConstructor()
96  ->getMock();
97  $mockDataMapper->expects(self::once())->method('getDataMap')
98  ->willReturn($mockDataMap);
99  $mockDataMapper->expects(self::once())->method('getPlainValue')
100  ->willReturn('plainPropertyValue');
101  $expectedUid = 52;
102 
103  $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
104  $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
105  $queryResultProphet = $this->prophesize(Result::class);
106  $queryResultProphet->fetchOne(Argument::cetera())->willReturn($expectedUid);
107  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
108  $queryBuilderProphet->executeQuery()->willReturn($queryResultProphet->reveal());
109  $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
110  $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
111  $queryBuilderProphet->select('uid')->willReturn($queryBuilderProphet->reveal());
112  $queryBuilderProphet->from($tableName)->willReturn($queryBuilderProphet->reveal());
113  $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
114  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
115  $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
116 
117  if ($isFrontendEnvironment) {
118  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())
119  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
120  } else {
121  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())
122  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
123  }
124 
125  if ($isFrontendEnvironment) {
126  $queryBuilderProphet->setRestrictions(Argument::type(FrontendRestrictionContainer::class))
127  ->shouldBeCalled();
128  }
129 
130  $domainObject = new class () extends ‪AbstractValueObject {
131  protected string $propertyName = 'propertyValue';
132  };
133 
134  $reflectionServiceProphecy = $this->prophesize(ReflectionService::class);
135  $reflectionServiceProphecy
136  ->getClassSchema(Argument::exact(get_class($domainObject)))
137  ->willReturn(new ‪ClassSchema(get_class($domainObject)));
138 
139  GeneralUtility::addInstance(DataMapper::class, $mockDataMapper);
140  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
141  $subject = new ‪Typo3DbBackend(
142  $this->prophesize(CacheService::class)->reveal(),
143  $reflectionServiceProphecy->reveal()
144  );
145  $result = $subject->getUidOfAlreadyPersistedValueObject($domainObject);
146  self::assertSame($expectedUid, $result);
147  }
148 
154  {
155  $comparisonRow = [
156  'uid' => '42',
157  'pid' => '42',
158  '_ORIG_uid' => '43',
159  ];
160  $row = [
161  'uid' => '42',
162  'pid' => '42',
163  't3ver_oid' => '42',
164  ];
165  $workspaceVersion = [
166  'uid' => '43',
167  'pid' => '42',
168  ];
169  $mockQuerySettings = $this->getMockBuilder(Typo3QuerySettings::class)
170  ->addMethods(['dummy'])
171  ->disableOriginalConstructor()
172  ->getMock();
173 
174  $workspaceUid = 2;
175 
176  $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
177  $context = new ‪Context([
178  'workspace' => new ‪WorkspaceAspect($workspaceUid),
179  ]);
180  $pageRepositoryMock = $this->getMockBuilder(PageRepository::class)
181  ->onlyMethods(['getWorkspaceVersionOfRecord'])
182  ->setConstructorArgs([$context])
183  ->getMock();
184  $container = $this->createMock(ContainerInterface::class);
185  $query = new ‪Query($this->createMock(DataMapFactory::class), $this->createMock(PersistenceManagerInterface::class), $this->createMock(QueryObjectModelFactory::class), $container);
186  $query->setQuerySettings($mockQuerySettings);
187  $pageRepositoryMock->expects(self::once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->willReturn($workspaceVersion);
188  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())
189  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
190  GeneralUtility::setSingletonInstance(Context::class, $context);
191  GeneralUtility::addInstance(PageRepository::class, $pageRepositoryMock);
192  $mockTypo3DbBackend = $this->getAccessibleMock(Typo3DbBackend::class, ['dummy'], [], '', false);
193  self::assertSame([$comparisonRow], $mockTypo3DbBackend->_call('overlayLanguageAndWorkspace', $sourceMock, [$row], $query));
194  }
195 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: Typo3DbBackendTest.php:57
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:36
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend
Definition: Typo3DbBackend.php:56
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap
Definition: DataMap.php:23
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
Definition: QueryObjectModelFactory.php:27
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly
‪uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment)
Definition: Typo3DbBackendTest.php:75
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage
Definition: Typo3DbBackendTest.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:51
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\overlayLanguageAndWorkspaceChangesUidIfInPreview
‪overlayLanguageAndWorkspaceChangesUidIfInPreview()
Definition: Typo3DbBackendTest.php:151
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:39
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:28
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
‪TYPO3\CMS\Extbase\DomainObject\AbstractValueObject
Definition: AbstractValueObject.php:24
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest\uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider
‪array uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider()
Definition: Typo3DbBackendTest.php:62
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage\Typo3DbBackendTest
Definition: Typo3DbBackendTest.php:52
‪TYPO3\CMS\Extbase\Service\CacheService
Definition: CacheService.php:35
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:43
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:54
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_FE
‪const REQUESTTYPE_FE
Definition: SystemEnvironmentBuilder.php:43
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:31
‪TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings
Definition: Typo3QuerySettings.php:29