init
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Test\Unit\Model;
|
||||
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\HtmlLinkProcessor;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\UrlPathConverter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HtmlLinkProcessorTest extends TestCase
|
||||
{
|
||||
public function testConvertsOnlyNavigationalAnchorUrls(): void
|
||||
{
|
||||
$converter = $this->createMock(UrlPathConverter::class);
|
||||
$converter->expects(self::exactly(3))
|
||||
->method('toLanguageUrl')
|
||||
->willReturnCallback(static function (string $url, string $language): string {
|
||||
self::assertSame('ko', $language);
|
||||
|
||||
return [
|
||||
'/news' => '/ko/news',
|
||||
'https://example.com/about-us?from=footer' =>
|
||||
'https://example.com/ko/about-us?from=footer',
|
||||
'https://external.example.net/page' => 'https://external.example.net/page',
|
||||
][$url];
|
||||
});
|
||||
|
||||
$html = '<html><body>'
|
||||
. '<a href="/news">News</a>'
|
||||
. '<a href="https://example.com/about-us?from=footer">About</a>'
|
||||
. '<a href="https://external.example.net/page">External</a>'
|
||||
. '<a href="#details">Details</a>'
|
||||
. '<a href="mailto:hello@example.com">Email</a>'
|
||||
. '<a href="tel:+123456789">Phone</a>'
|
||||
. '<a href="javascript:void(0)">Toggle</a>'
|
||||
. '</body></html>';
|
||||
|
||||
$processor = new HtmlLinkProcessor($converter);
|
||||
$result = $processor->process($html, 'ko');
|
||||
|
||||
self::assertStringContainsString('href="/ko/news"', $result);
|
||||
self::assertStringContainsString(
|
||||
'href="https://example.com/ko/about-us?from=footer"',
|
||||
$result
|
||||
);
|
||||
self::assertStringContainsString('href="https://external.example.net/page"', $result);
|
||||
self::assertStringContainsString('href="#details"', $result);
|
||||
self::assertStringContainsString('href="mailto:hello@example.com"', $result);
|
||||
self::assertStringContainsString('href="tel:+123456789"', $result);
|
||||
self::assertStringContainsString('href="javascript:void(0)"', $result);
|
||||
}
|
||||
|
||||
public function testPreservesSingleQuotesAndEscapesQuerySeparator(): void
|
||||
{
|
||||
$converter = $this->createMock(UrlPathConverter::class);
|
||||
$converter->expects(self::once())
|
||||
->method('toLanguageUrl')
|
||||
->with('/search?q=resin&size=1kg', 'de')
|
||||
->willReturn('/de/search?q=resin&size=1kg');
|
||||
|
||||
$processor = new HtmlLinkProcessor($converter);
|
||||
|
||||
self::assertSame(
|
||||
"<a href='/de/search?q=resin&size=1kg'>Search</a>",
|
||||
$processor->process("<a href='/search?q=resin&size=1kg'>Search</a>", 'de')
|
||||
);
|
||||
}
|
||||
|
||||
public function testReturnsMarkupWithoutAnchorsUnchanged(): void
|
||||
{
|
||||
$converter = $this->createMock(UrlPathConverter::class);
|
||||
$converter->expects(self::never())->method('toLanguageUrl');
|
||||
|
||||
$processor = new HtmlLinkProcessor($converter);
|
||||
$html = '<html><body><p>No links</p></body></html>';
|
||||
|
||||
self::assertSame($html, $processor->process($html, 'ko'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Test\Unit\Model;
|
||||
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Store\Model\Website;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\LanguageResolver;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\RequestPathProcessor;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\StoreCodeMapper;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\StoreViewRegistry;
|
||||
|
||||
class LanguageAndPathTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var StoreViewRegistry|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $storeViewRegistry;
|
||||
|
||||
/** @var StoreManagerInterface|\PHPUnit\Framework\MockObject\MockObject */
|
||||
private $storeManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->storeViewRegistry = $this->createMock(StoreViewRegistry::class);
|
||||
$this->storeViewRegistry->method('isKnownLanguage')->willReturnCallback(static function (string $language): bool {
|
||||
return in_array($language, ['en', 'de'], true);
|
||||
});
|
||||
$this->storeViewRegistry->method('isKnownStoreCode')->willReturnCallback(static function (string $storeCode): bool {
|
||||
return in_array($storeCode, ['en_US', 'en_DE', 'de_US', 'de_DE'], true);
|
||||
});
|
||||
$this->storeViewRegistry->method('getLanguageByStoreCode')->willReturnCallback(static function (string $storeCode): ?string {
|
||||
$map = [
|
||||
'en_US' => 'en',
|
||||
'en_DE' => 'en',
|
||||
'de_US' => 'de',
|
||||
'de_DE' => 'de',
|
||||
];
|
||||
|
||||
return $map[$storeCode] ?? null;
|
||||
});
|
||||
$this->storeViewRegistry->method('getDefaultStoreCodeForLanguage')->willReturnCallback(
|
||||
static function (string $language): ?string {
|
||||
$map = [
|
||||
'en' => 'en_US',
|
||||
'de' => 'de_US',
|
||||
];
|
||||
|
||||
return $map[$language] ?? null;
|
||||
}
|
||||
);
|
||||
$this->storeViewRegistry->method('isLanguageInWebsite')->willReturnCallback(
|
||||
static fn(string $language, int $websiteId): bool =>
|
||||
$websiteId === 1 && in_array($language, ['en', 'de'], true)
|
||||
);
|
||||
$this->storeViewRegistry->method('isStoreCodeInWebsite')->willReturnCallback(
|
||||
static fn(string $storeCode, int $websiteId): bool =>
|
||||
$websiteId === 1 && in_array($storeCode, ['en_US', 'en_DE', 'de_US', 'de_DE'], true)
|
||||
);
|
||||
$this->storeViewRegistry->method('getStoreCodeForLanguageInWebsite')->willReturnCallback(
|
||||
static function (string $language, int $websiteId): ?string {
|
||||
if ($websiteId !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ['en' => 'en_US', 'de' => 'de_US'][$language] ?? null;
|
||||
}
|
||||
);
|
||||
|
||||
$defaultStore = $this->createMock(Store::class);
|
||||
$defaultStore->method('getCode')->willReturn('en_US');
|
||||
$website = $this->createMock(Website::class);
|
||||
$website->method('getId')->willReturn(1);
|
||||
$website->method('getDefaultStore')->willReturn($defaultStore);
|
||||
$this->storeManager = $this->createMock(StoreManagerInterface::class);
|
||||
$this->storeManager->method('getWebsite')->willReturn($website);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider productPathProvider
|
||||
*/
|
||||
public function testResolvesLanguageFromProductPath(string $pathInfo, ?string $expectedLanguage): void
|
||||
{
|
||||
$resolver = new LanguageResolver($this->storeViewRegistry, $this->storeManager);
|
||||
|
||||
self::assertSame($expectedLanguage, $resolver->resolveFromPath($pathInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function productPathProvider(): array
|
||||
{
|
||||
return [
|
||||
'english product path' => ['/en/product-a', 'en'],
|
||||
'german product path' => ['/de/product-a', 'de'],
|
||||
'english language root' => ['/en', 'en'],
|
||||
'real store code is not language' => ['/en_US/product-a', null],
|
||||
'unsupported prefix' => ['/fr/product-a', null],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stripProductPathProvider
|
||||
*/
|
||||
public function testStripsLanguagePrefixBeforeUrlRewrite(string $pathInfo, string $expectedPathInfo): void
|
||||
{
|
||||
$processor = new RequestPathProcessor(new LanguageResolver($this->storeViewRegistry, $this->storeManager));
|
||||
|
||||
self::assertSame($expectedPathInfo, $processor->stripLanguagePrefix($pathInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function stripProductPathProvider(): array
|
||||
{
|
||||
return [
|
||||
'english product path' => ['/en/product-a', '/product-a'],
|
||||
'german product path' => ['/de/product-a', '/product-a'],
|
||||
'nested product path' => ['/de/category/product-a', '/category/product-a'],
|
||||
'language root' => ['/en', '/'],
|
||||
'non language path unchanged' => ['/product-a', '/product-a'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider replaceProductPathProvider
|
||||
*/
|
||||
public function testReplacesLanguagePrefixForMarketSwitch(string $pathInfo, string $language, string $expectedPathInfo): void
|
||||
{
|
||||
$processor = new RequestPathProcessor(new LanguageResolver($this->storeViewRegistry, $this->storeManager));
|
||||
|
||||
self::assertSame($expectedPathInfo, $processor->replaceLanguagePrefix($pathInfo, $language));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function replaceProductPathProvider(): array
|
||||
{
|
||||
return [
|
||||
'german product path switches to english' => [
|
||||
'/de/privacy-policy-cookie-restriction-mode/',
|
||||
'en',
|
||||
'/en/privacy-policy-cookie-restriction-mode/',
|
||||
],
|
||||
'english product path switches to german' => [
|
||||
'/en/product-a',
|
||||
'de',
|
||||
'/de/product-a',
|
||||
],
|
||||
'language root keeps trailing slash' => [
|
||||
'/de/',
|
||||
'en',
|
||||
'/en/',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider storeMappingProvider
|
||||
*/
|
||||
public function testMapsProductPathLanguageAndMarketToRealStoreCode(
|
||||
string $language,
|
||||
?string $market,
|
||||
?string $expectedStoreCode
|
||||
): void {
|
||||
$mapper = new StoreCodeMapper($this->storeViewRegistry, $this->storeManager);
|
||||
|
||||
self::assertSame($expectedStoreCode, $mapper->map($language, $market));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function storeMappingProvider(): array
|
||||
{
|
||||
return [
|
||||
'english US product path' => ['en', 'en_US', 'en_US'],
|
||||
'english DE product path' => ['en', 'en_DE', 'en_DE'],
|
||||
'german US product path' => ['de', 'de_US', 'de_US'],
|
||||
'german DE product path' => ['de', 'de_DE', 'de_DE'],
|
||||
'english path ignores german market and uses default' => ['en', 'de_DE', 'en_US'],
|
||||
'unknown language has no default' => ['fr', null, null],
|
||||
];
|
||||
}
|
||||
|
||||
public function testUsesLanguageSpecificDefaultStoreFromRegistry(): void
|
||||
{
|
||||
$mapper = new StoreCodeMapper($this->storeViewRegistry, $this->storeManager);
|
||||
|
||||
self::assertSame('en_US', $mapper->getDefaultStoreCodeForLanguage('en'));
|
||||
self::assertSame('de_US', $mapper->getDefaultStoreCodeForLanguage('de'));
|
||||
}
|
||||
|
||||
public function testUsesCurrentWebsiteWhenLanguageExistsInMultipleWebsites(): void
|
||||
{
|
||||
$registry = $this->createMock(StoreViewRegistry::class);
|
||||
$registry->expects(self::once())
|
||||
->method('getStoreCodeForLanguageInWebsite')
|
||||
->with('de', 1)
|
||||
->willReturn('de_EU');
|
||||
|
||||
$mapper = new StoreCodeMapper($registry, $this->storeManager);
|
||||
|
||||
self::assertSame('de_EU', $mapper->getDefaultStoreCodeForLanguage('de'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Test\Unit\Model;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Store\Model\Website;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\MarketCookie;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\MarketResolver;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\StoreCodeMapper;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\StoreViewRegistry;
|
||||
|
||||
class MarketResolverTest extends TestCase
|
||||
{
|
||||
public function testResolvesStoreCodeMarketForProductPathLanguage(): void
|
||||
{
|
||||
$resolver = $this->createResolver(null);
|
||||
$request = $this->createRequest('en_DE');
|
||||
|
||||
self::assertSame('en_DE', $resolver->resolveForLanguage($request, 'en'));
|
||||
}
|
||||
|
||||
public function testIgnoresCrossLanguageCookieForProductPath(): void
|
||||
{
|
||||
$resolver = $this->createResolver('de_DE');
|
||||
$request = $this->createRequest(null);
|
||||
|
||||
self::assertSame('en_US', $resolver->resolveForLanguage($request, 'en'));
|
||||
}
|
||||
|
||||
public function testUsesGermanDefaultForGermanProductPath(): void
|
||||
{
|
||||
$resolver = $this->createResolver(null);
|
||||
$request = $this->createRequest(null);
|
||||
|
||||
self::assertSame('de_US', $resolver->resolveForLanguage($request, 'de'));
|
||||
}
|
||||
|
||||
public function testIgnoresMissingCookieStoreCode(): void
|
||||
{
|
||||
$resolver = $this->createResolver('fr_FR');
|
||||
$request = $this->createRequest(null);
|
||||
|
||||
self::assertSame('en_US', $resolver->resolveForLanguage($request, 'en'));
|
||||
}
|
||||
|
||||
public function testNormalizeMarketRejectsUnknownStoreCode(): void
|
||||
{
|
||||
$resolver = $this->createResolver(null);
|
||||
|
||||
self::assertNull($resolver->normalizeMarket('fr_FR'));
|
||||
}
|
||||
|
||||
private function createResolver(?string $cookieMarket): MarketResolver
|
||||
{
|
||||
$cookie = $this->createMock(MarketCookie::class);
|
||||
$cookie->method('get')->willReturn($cookieMarket);
|
||||
|
||||
return new MarketResolver(
|
||||
$cookie,
|
||||
new StoreCodeMapper($this->createRegistry(), $this->createStoreManager())
|
||||
);
|
||||
}
|
||||
|
||||
private function createRegistry(): StoreViewRegistry
|
||||
{
|
||||
$registry = $this->createMock(StoreViewRegistry::class);
|
||||
$registry->method('isKnownStoreCode')->willReturnCallback(static function (string $storeCode): bool {
|
||||
return in_array($storeCode, ['en_US', 'en_DE', 'de_US', 'de_DE'], true);
|
||||
});
|
||||
$registry->method('getLanguageByStoreCode')->willReturnCallback(static function (string $storeCode): ?string {
|
||||
$map = [
|
||||
'en_US' => 'en',
|
||||
'en_DE' => 'en',
|
||||
'de_US' => 'de',
|
||||
'de_DE' => 'de',
|
||||
];
|
||||
|
||||
return $map[$storeCode] ?? null;
|
||||
});
|
||||
$registry->method('getDefaultStoreCodeForLanguage')->willReturnCallback(
|
||||
static function (string $language): ?string {
|
||||
$map = [
|
||||
'en' => 'en_US',
|
||||
'de' => 'de_US',
|
||||
];
|
||||
|
||||
return $map[$language] ?? null;
|
||||
}
|
||||
);
|
||||
$registry->method('isStoreCodeInWebsite')->willReturnCallback(
|
||||
static fn(string $storeCode, int $websiteId): bool =>
|
||||
$websiteId === 1 && in_array($storeCode, ['en_US', 'en_DE', 'de_US', 'de_DE'], true)
|
||||
);
|
||||
$registry->method('getStoreCodeForLanguageInWebsite')->willReturnCallback(
|
||||
static function (string $language, int $websiteId): ?string {
|
||||
if ($websiteId !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ['en' => 'en_US', 'de' => 'de_US'][$language] ?? null;
|
||||
}
|
||||
);
|
||||
|
||||
return $registry;
|
||||
}
|
||||
|
||||
private function createStoreManager(): StoreManagerInterface
|
||||
{
|
||||
$website = $this->createMock(Website::class);
|
||||
$website->method('getId')->willReturn(1);
|
||||
$storeManager = $this->createMock(StoreManagerInterface::class);
|
||||
$storeManager->method('getWebsite')->willReturn($website);
|
||||
|
||||
return $storeManager;
|
||||
}
|
||||
|
||||
private function createRequest(?string $market): RequestInterface
|
||||
{
|
||||
$request = $this->createMock(RequestInterface::class);
|
||||
$request->method('getParam')->with('market')->willReturn($market);
|
||||
|
||||
return $request;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Test\Unit\Model;
|
||||
|
||||
use ArrayIterator;
|
||||
use Magento\Store\Model\ResourceModel\Store\Collection;
|
||||
use Magento\Store\Model\ResourceModel\Store\CollectionFactory;
|
||||
use Magento\Store\Model\Store;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\StoreViewRegistry;
|
||||
|
||||
class StoreViewRegistryTest extends TestCase
|
||||
{
|
||||
public function testGroupsActiveStoreViewsByLanguageAndUsesSortedDefaults(): void
|
||||
{
|
||||
$registry = $this->createRegistry([
|
||||
$this->createStore(1, 'english_us', 1),
|
||||
$this->createStore(2, 'english_de', 2),
|
||||
$this->createStore(3, 'german_us', 1),
|
||||
$this->createStore(4, 'german_de', 2),
|
||||
]);
|
||||
|
||||
self::assertSame(
|
||||
[
|
||||
'en' => ['english_us', 'english_de'],
|
||||
'de' => ['german_us', 'german_de'],
|
||||
],
|
||||
$registry->getStoresByLanguage()
|
||||
);
|
||||
self::assertSame('english_us', $registry->getDefaultStoreCodeForLanguage('en'));
|
||||
self::assertSame('german_us', $registry->getDefaultStoreCodeForLanguage('de'));
|
||||
self::assertSame('en', $registry->getLanguageByStoreCode('english_de'));
|
||||
self::assertTrue($registry->isKnownStoreCode('german_de'));
|
||||
self::assertTrue($registry->isKnownLanguage('de'));
|
||||
self::assertSame('german_de', $registry->getStoreCodeForLanguageInWebsite('de', 2));
|
||||
self::assertFalse($registry->isStoreCodeInWebsite('german_de', 1));
|
||||
}
|
||||
|
||||
public function testSkipsUnsupportedStoreCodeFormat(): void
|
||||
{
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects(self::once())->method('warning');
|
||||
|
||||
$registry = $this->createRegistry(
|
||||
[
|
||||
$this->createStore(1, 'english_us', 1),
|
||||
$this->createStore(5, 'bad_locale', 1),
|
||||
],
|
||||
$logger
|
||||
);
|
||||
|
||||
self::assertSame(['en' => ['english_us']], $registry->getStoresByLanguage());
|
||||
self::assertFalse($registry->isKnownStoreCode('bad_locale'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Store[] $stores
|
||||
*/
|
||||
private function createRegistry(array $stores, ?LoggerInterface $logger = null): StoreViewRegistry
|
||||
{
|
||||
$collection = $this->getMockBuilder(Collection::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['setWithoutDefaultFilter', 'addStatusFilter', 'setOrder', 'getIterator'])
|
||||
->getMock();
|
||||
$collection->method('setWithoutDefaultFilter')->willReturnSelf();
|
||||
$collection->method('addStatusFilter')->willReturnSelf();
|
||||
$collection->method('setOrder')->willReturnSelf();
|
||||
$collection->method('getIterator')->willReturn(new ArrayIterator($stores));
|
||||
|
||||
$factory = $this->getMockBuilder(CollectionFactory::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['create'])
|
||||
->getMock();
|
||||
$factory->method('create')->willReturn($collection);
|
||||
|
||||
$scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
|
||||
$scopeConfig->method('getValue')->willReturnCallback(static function ($path, $scope, $storeCode) {
|
||||
$locales = [
|
||||
'english_us' => 'en_US',
|
||||
'english_de' => 'en_GB',
|
||||
'german_us' => 'de_DE',
|
||||
'german_de' => 'de_DE',
|
||||
'bad_locale' => '',
|
||||
];
|
||||
|
||||
return $locales[$storeCode] ?? '';
|
||||
});
|
||||
|
||||
return new StoreViewRegistry(
|
||||
$factory,
|
||||
$logger ?: $this->createMock(LoggerInterface::class),
|
||||
$scopeConfig
|
||||
);
|
||||
}
|
||||
|
||||
private function createStore(int $storeId, string $storeCode, int $websiteId): Store
|
||||
{
|
||||
$store = $this->getMockBuilder(Store::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getId', 'getCode', 'getWebsiteId'])
|
||||
->getMock();
|
||||
$store->method('getId')->willReturn($storeId);
|
||||
$store->method('getCode')->willReturn($storeCode);
|
||||
$store->method('getWebsiteId')->willReturn($websiteId);
|
||||
|
||||
return $store;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Test\Unit\Model;
|
||||
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Store\Model\Website;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\Config;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\StoreCodeMapper;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\StoreViewRegistry;
|
||||
use Esun3d\LanguageUrlStoreResolver\Model\UrlPathConverter;
|
||||
|
||||
class UrlPathConverterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider productUrlProvider
|
||||
*/
|
||||
public function testConvertsGeneratedProductUrlsToLanguageUrls(
|
||||
string $url,
|
||||
string $language,
|
||||
string $expectedUrl
|
||||
): void {
|
||||
$converter = new UrlPathConverter(
|
||||
$this->createConfig(),
|
||||
new StoreCodeMapper($this->createRegistry(), $this->createStoreManager('https://example.com/')),
|
||||
$this->createRegistry(),
|
||||
$this->createStoreManager('https://example.com/')
|
||||
);
|
||||
|
||||
self::assertSame($expectedUrl, $converter->toLanguageUrl($url, $language));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public static function productUrlProvider(): array
|
||||
{
|
||||
return [
|
||||
'real english US store code product url' => [
|
||||
'https://example.com/en_US/product-a',
|
||||
'en',
|
||||
'https://example.com/en/product-a',
|
||||
],
|
||||
'real german DE store code product url' => [
|
||||
'https://example.com/de_DE/product-a',
|
||||
'de',
|
||||
'https://example.com/de/product-a',
|
||||
],
|
||||
'plain product url gets language prefix' => [
|
||||
'https://example.com/product-a',
|
||||
'en',
|
||||
'https://example.com/en/product-a',
|
||||
],
|
||||
'language product url is not duplicated' => [
|
||||
'https://example.com/en/product-a',
|
||||
'en',
|
||||
'https://example.com/en/product-a',
|
||||
],
|
||||
'query and fragment survive conversion' => [
|
||||
'https://example.com/en_US/product-a?q=printer#reviews',
|
||||
'en',
|
||||
'https://example.com/en/product-a?q=printer#reviews',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testSkipsExternalProductUrl(): void
|
||||
{
|
||||
$converter = new UrlPathConverter(
|
||||
$this->createConfig(),
|
||||
new StoreCodeMapper($this->createRegistry(), $this->createStoreManager('https://example.com/')),
|
||||
$this->createRegistry(),
|
||||
$this->createStoreManager('https://example.com/')
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
'https://cdn.example.net/en_US/product-a',
|
||||
$converter->toLanguageUrl('https://cdn.example.net/en_US/product-a', 'en')
|
||||
);
|
||||
}
|
||||
|
||||
public function testSkipsStaticProductAssetPath(): void
|
||||
{
|
||||
$converter = new UrlPathConverter(
|
||||
$this->createConfig(),
|
||||
new StoreCodeMapper($this->createRegistry(), $this->createStoreManager('https://example.com/')),
|
||||
$this->createRegistry(),
|
||||
$this->createStoreManager('https://example.com/')
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
'https://example.com/static/version123/frontend/product-a.jpg',
|
||||
$converter->toLanguageUrl('https://example.com/static/version123/frontend/product-a.jpg', 'en')
|
||||
);
|
||||
}
|
||||
|
||||
public function testOmitsPrefixForWebsiteDefaultLanguage(): void
|
||||
{
|
||||
$mapper = $this->createMock(StoreCodeMapper::class);
|
||||
$mapper->method('isRealStoreCodeInScope')->with('en_US')->willReturn(true);
|
||||
$mapper->method('languageFromStoreCode')->with('en_US')->willReturn('en');
|
||||
$mapper->method('isLanguageAllowed')->willReturn(false);
|
||||
$mapper->method('isDefaultLanguage')->with('en')->willReturn(true);
|
||||
|
||||
$converter = new UrlPathConverter(
|
||||
$this->createConfig(),
|
||||
$mapper,
|
||||
$this->createRegistry(),
|
||||
$this->createStoreManager('https://example.com/')
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
'https://example.com/product-a',
|
||||
$converter->toLanguageUrl('https://example.com/en_US/product-a', 'en')
|
||||
);
|
||||
}
|
||||
|
||||
private function createConfig(): Config
|
||||
{
|
||||
$config = $this->createMock(Config::class);
|
||||
$config->method('shouldSkipPath')->willReturnCallback(static function (string $path): bool {
|
||||
$path = strtolower(trim($path, '/'));
|
||||
$skipPrefixes = [
|
||||
'admin',
|
||||
'rest',
|
||||
'graphql',
|
||||
'soap',
|
||||
'static',
|
||||
'media',
|
||||
'pub',
|
||||
'customer/section/load',
|
||||
];
|
||||
|
||||
foreach ($skipPrefixes as $prefix) {
|
||||
if ($path === $prefix || strpos($path, $prefix . '/') === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
private function createRegistry(): StoreViewRegistry
|
||||
{
|
||||
$registry = $this->createMock(StoreViewRegistry::class);
|
||||
$registry->method('isKnownLanguage')->willReturnCallback(static function (string $language): bool {
|
||||
return in_array($language, ['en', 'de'], true);
|
||||
});
|
||||
$registry->method('isKnownStoreCode')->willReturnCallback(static function (string $storeCode): bool {
|
||||
return in_array($storeCode, ['en_US', 'en_DE', 'de_US', 'de_DE'], true);
|
||||
});
|
||||
$registry->method('getLanguageByStoreCode')->willReturnCallback(static function (string $storeCode): ?string {
|
||||
$map = [
|
||||
'en_US' => 'en',
|
||||
'en_DE' => 'en',
|
||||
'de_US' => 'de',
|
||||
'de_DE' => 'de',
|
||||
];
|
||||
|
||||
return $map[$storeCode] ?? null;
|
||||
});
|
||||
$registry->method('isStoreCodeInWebsite')->willReturnCallback(
|
||||
static fn(string $storeCode, int $websiteId): bool =>
|
||||
$websiteId === 1 && in_array($storeCode, ['en_US', 'en_DE', 'de_US', 'de_DE'], true)
|
||||
);
|
||||
$registry->method('isLanguageInWebsite')->willReturnCallback(
|
||||
static fn(string $language, int $websiteId): bool =>
|
||||
$websiteId === 1 && in_array($language, ['en', 'de'], true)
|
||||
);
|
||||
|
||||
return $registry;
|
||||
}
|
||||
|
||||
private function createStoreManager(string $baseUrl): StoreManagerInterface
|
||||
{
|
||||
$store = $this->getMockBuilder(Store::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getBaseUrl'])
|
||||
->getMock();
|
||||
$store->method('getBaseUrl')->willReturn($baseUrl);
|
||||
|
||||
$storeManager = $this->createMock(StoreManagerInterface::class);
|
||||
$storeManager->method('getStore')->willReturn($store);
|
||||
$website = $this->createMock(Website::class);
|
||||
$website->method('getId')->willReturn(1);
|
||||
$website->method('getDefaultStore')->willReturn($store);
|
||||
$storeManager->method('getWebsite')->willReturn($website);
|
||||
|
||||
return $storeManager;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user