a8c8114e84d68164e89b05af2d7e5879d51df369
[Packages/TYPO3.CMS.git] / typo3 / sysext / redirects / Tests / Unit / FormDataProvider / ValuePickerItemDataProviderTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Redirects\Tests\Unit\FormDataProvider;
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
18 use Doctrine\DBAL\Driver\Statement;
19 use Prophecy\Prophecy\ObjectProphecy;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
22 use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
23 use TYPO3\CMS\Core\Site\SiteFinder;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Redirects\FormDataProvider\ValuePickerItemDataProvider;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28 class ValuePickerItemDataProviderTest extends UnitTestCase
29 {
30 protected $sysRedirectResultSet = [
31 'tableName' => 'sys_redirect',
32 'processedTca' => [
33 'columns' => [
34 'source_host' => [
35 'config' => [
36 'valuePicker' => [
37 'items' => []
38 ]
39 ]
40 ]
41 ]
42 ]
43 ];
44
45 /**
46 * @test
47 */
48 public function addDataDoesNothingIfNoRedirectDataGiven()
49 {
50 $result = [
51 'tableName' => 'tt_content',
52 ];
53
54 $siteFinderProphecy = $this->prophesize(SiteFinder::class);
55 $siteFinderProphecy->getAllSites()->willReturn([]);
56 $valuePickerItemDataProvider = new ValuePickerItemDataProvider($siteFinderProphecy->reveal());
57 $actualResult = $valuePickerItemDataProvider->addData($result);
58 self::assertSame($result, $actualResult);
59 }
60
61 /**
62 * @test
63 */
64 public function addDataAddsDomainNameAsKeyAndValueToRedirectValuePicker()
65 {
66 $statementProphecy = $this->setUpDatabase();
67 $statementProphecy->fetchAll()->willReturn(
68 [
69 ['domainName' => 'bar.test'],
70 ['domainName' => 'foo.test'],
71 ]
72 );
73 // no results for now
74 $siteFinderProphecy = $this->prophesize(SiteFinder::class);
75 $siteFinderProphecy->getAllSites()->willReturn([]);
76 $valuePickerItemDataProvider = new ValuePickerItemDataProvider($siteFinderProphecy->reveal());
77 $actualResult = $valuePickerItemDataProvider->addData($this->sysRedirectResultSet);
78 $expected = $this->sysRedirectResultSet;
79 $expected['processedTca']['columns']['source_host']['config']['valuePicker']['items'] = [
80 ['bar.test', 'bar.test'],
81 ['foo.test', 'foo.test'],
82 ];
83 self::assertSame($expected, $actualResult);
84 }
85
86 /**
87 * @test
88 */
89 public function addDataDoesNotChangeResultSetIfNoSysDomainsAreFound()
90 {
91 $statementProphecy = $this->setUpDatabase();
92 $statementProphecy->fetchAll()->willReturn([]);
93 $siteFinderProphecy = $this->prophesize(SiteFinder::class);
94 $siteFinderProphecy->getAllSites()->willReturn([]);
95 $valuePickerItemDataProvider = new ValuePickerItemDataProvider($siteFinderProphecy->reveal());
96 $actualResult = $valuePickerItemDataProvider->addData($this->sysRedirectResultSet);
97
98 self::assertSame($this->sysRedirectResultSet, $actualResult);
99 }
100
101 /**
102 * @return \Doctrine\DBAL\Driver\Statement|\Prophecy\Prophecy\ObjectProphecy
103 */
104 private function setUpDatabase(): ObjectProphecy
105 {
106 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
107 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
108 $connectionPoolProphecy->getQueryBuilderForTable('sys_domain')->willReturn($queryBuilderProphecy->reveal());
109 $queryRestrictionContainerProphecy = $this->prophesize(QueryRestrictionContainerInterface::class);
110 $queryBuilderProphecy->getRestrictions()->willReturn($queryRestrictionContainerProphecy->reveal());
111 $queryBuilderProphecy->select('domainName')->willReturn($queryBuilderProphecy->reveal());
112 $queryBuilderProphecy->from('sys_domain')->willReturn($queryBuilderProphecy->reveal());
113 $statementProphecy = $this->prophesize(Statement::class);
114 $queryBuilderProphecy->execute()->willReturn($statementProphecy->reveal());
115 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
116 return $statementProphecy;
117 }
118 }