ea38695b489a8ab067c8ab39bc946e94bdf45472
[Packages/TYPO3.CMS.git] / typo3 / sysext / seo / Tests / Functional / Canonical / CanonicalGeneratorTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Seo\Tests\Functional\Canonical;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Log\NullLogger;
20 use TYPO3\CMS\Core\TypoScript\TemplateService;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24 use TYPO3\CMS\Frontend\Page\PageRepository;
25 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\AbstractTestCase;
26 use TYPO3\CMS\Seo\Canonical\CanonicalGenerator;
27
28 /**
29 * Test case
30 */
31 class CanonicalGeneratorTest extends AbstractTestCase
32 {
33 /**
34 * @var string[]
35 */
36 protected $coreExtensionsToLoad = [
37 'core', 'frontend', 'seo'
38 ];
39
40 protected function setUp(): void
41 {
42 parent::setUp();
43 $this->importDataSet('EXT:seo/Tests/Functional/Fixtures/pages-canonical.xml');
44 $this->writeSiteConfiguration(
45 'website-local',
46 $this->buildSiteConfiguration(1, 'http://localhost/')
47 );
48 }
49
50 protected function initTypoScriptFrontendController(int $uid): TypoScriptFrontendController
51 {
52 $typoScriptFrontendController = new TypoScriptFrontendController(null, $uid, 0);
53 $typoScriptFrontendController->cObj = new ContentObjectRenderer();
54 $typoScriptFrontendController->cObj->setLogger(new NullLogger());
55 $typoScriptFrontendController->sys_page = GeneralUtility::makeInstance(PageRepository::class);
56 $typoScriptFrontendController->tmpl = GeneralUtility::makeInstance(TemplateService::class);
57 $typoScriptFrontendController->getPageAndRootlineWithDomain(1);
58 $GLOBALS['TSFE'] = $typoScriptFrontendController;
59 return $typoScriptFrontendController;
60 }
61
62 public function generateDataProvider(): array
63 {
64 return [
65 'uid: 1 with canonical_link' => [1, '<link rel="canonical" href="http://localhost/"/>' . LF],
66 'uid: 2 with canonical_link' => [2, '<link rel="canonical" href="http://localhost/dummy-1-2"/>' . LF],
67 'uid: 3 with canonical_link AND content_from_pid = 2' => [3, '<link rel="canonical" href="http://localhost/dummy-1-2"/>' . LF],
68 'uid: 4 without canonical_link AND content_from_pid = 2' => [4, '<link rel="canonical" href="http://localhost/dummy-1-2"/>' . LF],
69 'uid: 5 without canonical_link AND without content_from_pid set' => [5, '<link rel="canonical" href="http://localhost/dummy-1-2-5"/>' . LF],
70 'uid: 6 without canonical_link AND content_from_pid = 7 (but target page is deleted)' => [6, '<link rel="canonical" href="http://localhost/dummy-1-2-6"/>' . LF],
71 'uid: 8 without canonical_link AND content_from_pid = 9 (but target page is hidden)' => [8, '<link rel="canonical" href="http://localhost/dummy-1-2-8"/>' . LF],
72 ];
73 }
74
75 /**
76 * @test
77 * @dataProvider generateDataProvider
78 * @param int $uid
79 * @param string $expectedCanonicalUrl
80 */
81 public function generate(int $uid, string $expectedCanonicalUrl): void
82 {
83 $typoScriptFrontendController = $this->initTypoScriptFrontendController($uid);
84 $this->assertSame($expectedCanonicalUrl, (new CanonicalGenerator($typoScriptFrontendController))->generate());
85 }
86 }