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