[!!!][TASK] Remove auto-unpublishing of workspaces
[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
17 use Doctrine\DBAL\Driver\Statement;
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Context\Context;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
23 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
24 use TYPO3\CMS\Extbase\Object\ObjectManager;
25 use TYPO3\CMS\Extbase\Service\EnvironmentService;
26 use TYPO3\CMS\Frontend\Page\PageRepository;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28
29 /**
30 * Test case
31 */
32 class Typo3DbBackendTest extends UnitTestCase
33 {
34 /**
35 * Due to nested PageRepository / FrontendRestriction Container issues, the Context object is set
36 * @var bool
37 */
38 protected $resetSingletonInstances = true;
39
40 public function setUp()
41 {
42 parent::setUp();
43 $GLOBALS['TSFE'] = new \stdClass();
44 }
45
46 /**
47 * @return array
48 */
49 public function uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider(): array
50 {
51 return [
52 'isFrontendEnvironment' => [true],
53 'isBackendEnvironment' => [false],
54 ];
55 }
56
57 /**
58 * @test
59 * @dataProvider uidOfAlreadyPersistedValueObjectIsDeterminedCorrectlyDataProvider
60 */
61 public function uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly(bool $isFrontendEnvironment)
62 {
63 $mockValueObject = $this->getMockBuilder(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class)
64 ->setMethods(['_getProperties'])
65 ->disableOriginalConstructor()
66 ->getMock();
67 $mockValueObject->expects($this->once())->method('_getProperties')
68 ->will($this->returnValue(['propertyName' => 'propertyValue']));
69 $mockColumnMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class)
70 ->setMethods(['isPersistableProperty', 'getColumnName'])
71 ->disableOriginalConstructor()
72 ->getMock();
73 $mockColumnMap->expects($this->any())->method('getColumnName')->will($this->returnValue('column_name'));
74 $tableName = 'tx_foo_table';
75 $mockDataMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class)
76 ->setMethods(['isPersistableProperty', 'getColumnMap', 'getTableName'])
77 ->disableOriginalConstructor()
78 ->getMock();
79 $mockDataMap->expects($this->any())->method('isPersistableProperty')->will($this->returnValue(true));
80 $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($mockColumnMap));
81 $mockDataMap->expects($this->any())->method('getTableName')->will($this->returnValue($tableName));
82 $mockDataMapper = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class)
83 ->setMethods(['getDataMap', 'getPlainValue'])
84 ->disableOriginalConstructor()
85 ->getMock();
86 $mockDataMapper->expects($this->once())->method('getDataMap')
87 ->will($this->returnValue($mockDataMap));
88 $mockDataMapper->expects($this->once())->method('getPlainValue')
89 ->will($this->returnValue('plainPropertyValue'));
90 $expectedUid = 52;
91
92 $expressionBuilderProphet = $this->prophesize(ExpressionBuilder::class);
93 $expressionBuilderProphet->eq(Argument::cetera())->willReturn('1 = 1');
94 $queryResultProphet = $this->prophesize(Statement::class);
95 $queryResultProphet->fetchColumn(Argument::cetera())->willReturn($expectedUid);
96 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
97 $queryBuilderProphet->execute()->willReturn($queryResultProphet->reveal());
98 $queryBuilderProphet->expr()->willReturn($expressionBuilderProphet->reveal());
99 $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturnArgument(0);
100 $queryBuilderProphet->select('uid')->willReturn($queryBuilderProphet->reveal());
101 $queryBuilderProphet->from($tableName)->willReturn($queryBuilderProphet->reveal());
102 $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
103 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
104 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
105
106 $environmentServiceProphet = $this->prophesize(EnvironmentService::class);
107 $environmentServiceProphet->isEnvironmentInFrontendMode()->willReturn($isFrontendEnvironment);
108
109 if ($isFrontendEnvironment) {
110 $queryBuilderProphet->setRestrictions(Argument::type(FrontendRestrictionContainer::class))
111 ->shouldBeCalled();
112 }
113
114 $mockTypo3DbBackend = $this->getAccessibleMock(
115 \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend::class,
116 ['dummy'],
117 [],
118 '',
119 false
120 );
121 $mockObjectManager = $this->createMock(ObjectManager::class);
122 $mockObjectManager->expects($this->any())
123 ->method('get')
124 ->with(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class)
125 ->will($this->returnValue($mockDataMapper));
126
127 $mockTypo3DbBackend->_set('objectManager', $mockObjectManager);
128 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
129 $mockTypo3DbBackend->_set('connectionPool', $connectionPoolProphet->reveal());
130 $mockTypo3DbBackend->_set('environmentService', $environmentServiceProphet->reveal());
131 $result = $mockTypo3DbBackend->_callRef('getUidOfAlreadyPersistedValueObject', $mockValueObject);
132 $this->assertSame($expectedUid, $result);
133 }
134 }