Files
kupshop/bundles/KupShop/I18nBundle/Tests/Locale/LanguageAwareUrlGeneratorTest.php
2025-08-02 16:30:27 +02:00

80 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\I18nBundle\Tests\Locale;
use KupShop\I18nBundle\Util\Locale\LanguageAwareUrlGenerator;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Contexts;
class LanguageAwareUrlGeneratorTest extends \DatabaseTestCase
{
private LanguageAwareUrlGenerator $urlGenerator;
protected function setUp(): void
{
parent::setUp();
$this->urlGenerator = $this->get(LanguageAwareUrlGenerator::class);
$this->initializeRequestStack();
}
/** @dataProvider data_testLocaleChange */
public function testLocaleChange(string $language, string $path, string $expectedPath): void
{
$this->assertEquals(
$expectedPath,
$this->urlGenerator->generateInLanguage($language, $path)
);
}
public function data_testLocaleChange(): iterable
{
// products
yield 'Locale change for product without translation' => ['sk', '/iphone-wpj_z1/', '/iphone-wpj_z1/'];
yield 'Locale change for product with translation' => ['sk', '/produkt-s-prekladem_z2/', '/produkt-s-sk-prekladem_z2/'];
yield 'Locale change for product hidden in translation' => ['sk', '/produkt-skryty-v-prekladu_z3/', '/'];
// sections
yield 'Locale change for section without translation with custom URL' => ['sk', '/test-sekce-bez-prekladu/', '/test-sekce-bez-prekladu/'];
yield 'Locale change for section with translation with custom URL' => ['sk', '/test-sekce-s-prekladem/', '/test-sekce-s-sk-prekladem/'];
yield 'Locale change for section hidden in translation with custom URL' => ['sk', '/test-sekce-skryta-v-prekladu/', '/'];
yield 'Locale change for section with translation' => ['sk', '/test-sekce-s-prekladem-bez-custom-url_k4/', '/test-sekce-s-prekladem-bez-custom-url-sk_k4/'];
yield 'Locale change for section with filter parameters' => ['sk', '/test-sekce-s-prekladem/f/cervena/', '/test-sekce-s-sk-prekladem/'];
// articles
yield 'Locale change for article without translation' => ['sk', '/clanek/1/lorem-ipsum/', '/clanok/1/lorem-ipsum/'];
yield 'Locale change for article with translation' => ['sk', '/clanek/2/lorem-ipsum-s-prekladem/', '/clanok/2/lorem-ipsum-sk/'];
yield 'Locale change for article hidden in translation' => ['sk', '/clanek/3/lorem-ipsum-skryty-v-prekladu/', '/'];
// dynamic pages (menu links)
yield 'Locale change for page without translation' => ['sk', '/kontakt/', '/kontakt/'];
yield 'Locale change for page with translation' => ['sk', '/stranka-s-prekladem/', '/sk-stranka-s-prekladem/'];
yield 'Locale change for page hidden in translation' => ['sk', '/stranka-skryta-v-prekladu/', '/'];
// static pages
yield 'Locale change for registration page' => ['sk', '/registrace/', '/registracia/'];
yield 'Locale change for login page' => ['sk', '/prihlaseni/', '/prihlasenie/'];
}
public function testPathAfterLocaleChange(): void
{
$languageContext = Contexts::get(LanguageContext::class);
$articlePath = path('kupshop_content_articles_article', ['IDa' => 1]);
$this->assertEquals('/clanek/1/', $articlePath, 'cs language active - URL should be in cs lang');
$languageContext->activate('sk');
$articlePath = path('kupshop_content_articles_article', ['IDa' => 1]);
$this->assertEquals('/clanok/1/', $articlePath, 'sk language active - URL should be in sk lang');
}
protected function getDataSet()
{
return $this->getJsonDataSetFromFile();
}
}