[BUGFIX] Show domains from Site configs in redirect picker 56/59256/5
authorBenni Mack <benni@typo3.org>
Fri, 21 Dec 2018 13:00:28 +0000 (14:00 +0100)
committerBenni Mack <benni@typo3.org>
Fri, 21 Dec 2018 22:09:40 +0000 (23:09 +0100)
The FormEngine sys_redirect value picker for "host" was built on
top of sys_domain, however the value picker should
include domains (hosts) from site configurations
as well.

Resolves: #87258
Releases: master, 9.5
Change-Id: I74bf5a8ce3bf91065b68269d6481eb89286585fa
Reviewed-on: https://review.typo3.org/59256
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/redirects/Classes/FormDataProvider/ValuePickerItemDataProvider.php
typo3/sysext/redirects/Tests/Unit/FormDataProvider/ValuePickerItemDataProviderTest.php

index 40a5075..13963f6 100644 (file)
@@ -17,15 +17,28 @@ namespace TYPO3\CMS\Redirects\FormDataProvider;
 
 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
 use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
+use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
- * Inject sys_domain records into valuepicker form
+ * Inject available domain hosts into a valuepicker form
  * @internal
  */
 class ValuePickerItemDataProvider implements FormDataProviderInterface
 {
+    /**
+     * @var SiteFinder
+     */
+    protected $siteFinder;
+
+    /**
+     * ValuePickerItemDataProvider constructor.
+     * @param SiteFinder|null $siteFinder
+     */
+    public function __construct(SiteFinder $siteFinder = null)
+    {
+        $this->siteFinder = $siteFinder ?? GeneralUtility::makeInstance(SiteFinder::class);
+    }
 
     /**
      * Add sys_domains into $result data array
@@ -39,29 +52,49 @@ class ValuePickerItemDataProvider implements FormDataProviderInterface
             $domains = $this->getDomains();
             foreach ($domains as $domain) {
                 $result['processedTca']['columns']['source_host']['config']['valuePicker']['items'][] =
-                [
-                    $domain['domainName'],
-                    $domain['domainName'],
-                ];
+                    [
+                        $domain,
+                        $domain,
+                    ];
             }
         }
         return $result;
     }
 
     /**
-     * Get sys_domain records from database
+     * Get sys_domain records from database, and all from pseudo-sites
      *
      * @return array domain records
      */
-    public function getDomains(): array
+    protected function getDomains(): array
     {
+        $domains = $this->getDomainsFromAllSites();
+
         $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain');
-        $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
-        $domains = $queryBuilder
+        $sysDomainRecords = $queryBuilder
             ->select('domainName')
             ->from('sys_domain')
             ->execute()
             ->fetchAll();
+        foreach ($sysDomainRecords as $domainRecord) {
+            $domains[] = $domainRecord['domainName'];
+        }
+        $domains = array_unique($domains);
+        sort($domains, SORT_NATURAL);
+        return $domains;
+    }
+
+    /**
+     * @return array
+     */
+    protected function getDomainsFromAllSites(): array
+    {
+        $domains = [];
+        foreach ($this->siteFinder->getAllSites() as $site) {
+            foreach ($site->getAllLanguages() as $language) {
+                $domains[] = $language->getBase()->getHost();
+            }
+        }
         return $domains;
     }
 }
index b76976a..a8c8114 100644 (file)
@@ -20,6 +20,7 @@ use Prophecy\Prophecy\ObjectProphecy;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
 use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
+use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Redirects\FormDataProvider\ValuePickerItemDataProvider;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
@@ -50,7 +51,9 @@ class ValuePickerItemDataProviderTest extends UnitTestCase
             'tableName' => 'tt_content',
         ];
 
-        $valuePickerItemDataProvider = new ValuePickerItemDataProvider();
+        $siteFinderProphecy = $this->prophesize(SiteFinder::class);
+        $siteFinderProphecy->getAllSites()->willReturn([]);
+        $valuePickerItemDataProvider = new ValuePickerItemDataProvider($siteFinderProphecy->reveal());
         $actualResult = $valuePickerItemDataProvider->addData($result);
         self::assertSame($result, $actualResult);
     }
@@ -63,16 +66,19 @@ class ValuePickerItemDataProviderTest extends UnitTestCase
         $statementProphecy = $this->setUpDatabase();
         $statementProphecy->fetchAll()->willReturn(
             [
-                ['domainName' => 'foo.test'],
                 ['domainName' => 'bar.test'],
+                ['domainName' => 'foo.test'],
             ]
         );
-        $valuePickerItemDataProvider = new ValuePickerItemDataProvider();
+        // no results for now
+        $siteFinderProphecy = $this->prophesize(SiteFinder::class);
+        $siteFinderProphecy->getAllSites()->willReturn([]);
+        $valuePickerItemDataProvider = new ValuePickerItemDataProvider($siteFinderProphecy->reveal());
         $actualResult = $valuePickerItemDataProvider->addData($this->sysRedirectResultSet);
         $expected = $this->sysRedirectResultSet;
         $expected['processedTca']['columns']['source_host']['config']['valuePicker']['items'] = [
-            ['foo.test', 'foo.test'],
             ['bar.test', 'bar.test'],
+            ['foo.test', 'foo.test'],
         ];
         self::assertSame($expected, $actualResult);
     }
@@ -84,7 +90,9 @@ class ValuePickerItemDataProviderTest extends UnitTestCase
     {
         $statementProphecy = $this->setUpDatabase();
         $statementProphecy->fetchAll()->willReturn([]);
-        $valuePickerItemDataProvider = new ValuePickerItemDataProvider();
+        $siteFinderProphecy = $this->prophesize(SiteFinder::class);
+        $siteFinderProphecy->getAllSites()->willReturn([]);
+        $valuePickerItemDataProvider = new ValuePickerItemDataProvider($siteFinderProphecy->reveal());
         $actualResult = $valuePickerItemDataProvider->addData($this->sysRedirectResultSet);
 
         self::assertSame($this->sysRedirectResultSet, $actualResult);