SlugHelperTest.php 5.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Slug;

/*
 * 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!
 */

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',
            'fallbacks' => '0'
        ],
        2 => [
            'title' => 'Deutsch',
            'enabled' => true,
            'languageId' => 2,
            'base' => '/de/',
            'typo3Language' => 'de',
            'locale' => 'de_DE.UTF-8',
            'iso-639-1' => 'de',
            'flag' => 'de',
            'fallbackType' => 'fallback',
            'fallbacks' => '0'
        ],
        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',
            'fallbacks' => '2,0'
        ],
    ];

    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']);
    }

    /**
     * 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',
                    'sys_language_uid' => 0
                ]
            ],
            'Dansk page / default parent' => [
                '/default-parent/dansk-page',
                [
                    'uid' => '13',
                    'title' => 'Dansk Page',
                    'sys_language_uid' => 1
                ],
            ],
            'german page / german parent' => [
                '/german-parent/german-page',
                [
                    'uid' => '13',
                    'title' => 'German Page',
                    'sys_language_uid' => 2
                ]
            ],
            'swiss page / german fallback parent' => [
                 '/german-parent/swiss-page',
                 [
                     'uid' => '13',
                     'title' => 'Swiss Page',
                     'sys_language_uid' => 3
                 ],
             ],
        ];
    }

    /**
     * @dataProvider generateRespectsFallbackLanguageOfParentPageSlugDataProvider
     * @param string $expected
     * @param array $page
     * @test
     */
    public function generateRespectsFallbackLanguageOfParentPageSlug(string $expected, array $page)
    {
        $slugHelper = GeneralUtility::makeInstance(
            SlugHelper::class,
            'pages',
            'slug',
            [
                'generatorOptions' => [
                    'fields' => ['title'],
                    'prefixParentPageSlug' => true,
                ],
            ]
        );

        self::assertEquals(
            $expected,
            $slugHelper->generate(
                [
                    'title' => $page['title'],
                    'uid' => $page['uid'],
                    'sys_language_uid' => $page['sys_language_uid']
                ],
                (int)$page['uid']
            )
        );
    }
}