SlugHelperTest.php 5.42 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Slug;

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Functional test for the SlugHelper
 */
class SlugHelperTest extends AbstractDataHandlerActionTestCase
{
    /**
     * @var string
     */
    protected $scenarioDataSetDirectory = 'typo3/sysext/core/Tests/Functional/DataHandling/Slug/DataSet/';

    /**
     * Default Site Configuration
     * @var array
     */
    protected $siteLanguageConfiguration = [
        1 => [
            'title' => 'Dansk',
            'enabled' => true,
            'languageId' => 1,
            'base' => '/dk/',
            'typo3Language' => 'dk',
            'locale' => 'da_DK.UTF-8',
            'iso-639-1' => 'da',
            'flag' => 'dk',
            'fallbackType' => 'fallback',
49
            'fallbacks' => '0',
50
51
52
53
54
55
56
57
58
59
60
        ],
        2 => [
            'title' => 'Deutsch',
            'enabled' => true,
            'languageId' => 2,
            'base' => '/de/',
            'typo3Language' => 'de',
            'locale' => 'de_DE.UTF-8',
            'iso-639-1' => 'de',
            'flag' => 'de',
            'fallbackType' => 'fallback',
61
            'fallbacks' => '0',
62
63
64
65
66
67
68
69
70
71
72
        ],
        3 => [
            'title' => 'Schweizer Deutsch',
            'enabled' => true,
            'languageId' => 3,
            'base' => '/de-ch/',
            'typo3Language' => 'ch',
            'locale' => 'de_CH.UTF-8',
            'iso-639-1' => 'ch',
            'flag' => 'ch',
            'fallbackType' => 'fallback',
73
            'fallbacks' => '2,0',
74
75
76
        ],
    ];

77
78
79
80
    protected $testExtensionsToLoad = [
        'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial',
    ];

81
82
83
84
85
86
87
88
89
    protected function setUp(): void
    {
        parent::setUp();

        $this->importScenarioDataSet('Pages');
        $this->setUpFrontendSite(1, $this->siteLanguageConfiguration);
        $this->setUpFrontendRootPage(1, ['typo3/sysext/core/Tests/Functional/Fixtures/Frontend/JsonRenderer.typoscript']);
    }

90
91
92
93
94
95
96
97
98
    /**
     * @test
     */
    public function verifyCleanReferenceIndex(): void
    {
        // The test verifies the imported data set has a clean reference index by the check in tearDown()
        self::assertTrue(true);
    }

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    /**
     * DataProvider for testing the language resolving of the parent page.
     * - If the language can be resolved, get the slug of the current language
     * - If not, consecutively try the fallback languages from the site config
     * - As a last resort, fall back to the default language.
     *
     * Example languages:
     * 0 = "Default"
     * 1 = "Dansk" - (Fallback to Default)
     * 2 = "German" - (Fallback to Default)
     * 3 = "Swiss German" - (Fallback to German)
     *
     * @return array
     */
    public function generateRespectsFallbackLanguageOfParentPageSlugDataProvider(): array
    {
        return [
            'default page / default parent' => [
                '/default-parent/default-page',
                [
                    'uid' => '13',
                    'title' => 'Default Page',
121
122
                    'sys_language_uid' => 0,
                ],
123
124
125
126
127
128
            ],
            'Dansk page / default parent' => [
                '/default-parent/dansk-page',
                [
                    'uid' => '13',
                    'title' => 'Dansk Page',
129
                    'sys_language_uid' => 1,
130
131
132
133
134
135
136
                ],
            ],
            'german page / german parent' => [
                '/german-parent/german-page',
                [
                    'uid' => '13',
                    'title' => 'German Page',
137
138
                    'sys_language_uid' => 2,
                ],
139
140
141
142
143
144
            ],
            'swiss page / german fallback parent' => [
                 '/german-parent/swiss-page',
                 [
                     'uid' => '13',
                     'title' => 'Swiss Page',
145
                     'sys_language_uid' => 3,
146
147
148
149
150
151
152
153
154
155
156
                 ],
             ],
        ];
    }

    /**
     * @dataProvider generateRespectsFallbackLanguageOfParentPageSlugDataProvider
     * @param string $expected
     * @param array $page
     * @test
     */
157
    public function generateRespectsFallbackLanguageOfParentPageSlug(string $expected, array $page): void
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    {
        $slugHelper = GeneralUtility::makeInstance(
            SlugHelper::class,
            'pages',
            'slug',
            [
                'generatorOptions' => [
                    'fields' => ['title'],
                    'prefixParentPageSlug' => true,
                ],
            ]
        );

        self::assertEquals(
            $expected,
            $slugHelper->generate(
                [
                    'title' => $page['title'],
                    'uid' => $page['uid'],
177
                    'sys_language_uid' => $page['sys_language_uid'],
178
179
180
181
182
183
                ],
                (int)$page['uid']
            )
        );
    }
}