1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lochmueller\LanguageDetection\Tests\Unit\Detect; |
6
|
|
|
|
7
|
|
|
use Lochmueller\LanguageDetection\Detect\GeoPluginDetect; |
8
|
|
|
use Lochmueller\LanguageDetection\Domain\Collection\LocaleCollection; |
9
|
|
|
use Lochmueller\LanguageDetection\Event\DetectUserLanguagesEvent; |
10
|
|
|
use Lochmueller\LanguageDetection\Service\IpLocation; |
11
|
|
|
use Lochmueller\LanguageDetection\Service\LanguageService; |
12
|
|
|
use Lochmueller\LanguageDetection\Service\LocaleCollectionSortService; |
13
|
|
|
use Lochmueller\LanguageDetection\Service\SiteConfigurationService; |
14
|
|
|
use Lochmueller\LanguageDetection\Tests\Unit\AbstractUnitTest; |
15
|
|
|
use TYPO3\CMS\Core\Http\ServerRequest; |
16
|
|
|
use TYPO3\CMS\Core\Site\Entity\Site; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers \Lochmueller\LanguageDetection\Detect\GeoPluginDetect |
20
|
|
|
* |
21
|
|
|
* @internal |
22
|
|
|
*/ |
23
|
|
|
class GeoPluginDetectTest extends AbstractUnitTest |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @covers \Lochmueller\LanguageDetection\Domain\Collection\LocaleCollection |
27
|
|
|
* @covers \Lochmueller\LanguageDetection\Domain\Model\Dto\LocaleValueObject |
28
|
|
|
* @covers \Lochmueller\LanguageDetection\Domain\Model\Dto\SiteConfiguration |
29
|
|
|
* @covers \Lochmueller\LanguageDetection\Event\DetectUserLanguagesEvent |
30
|
|
|
* @covers \Lochmueller\LanguageDetection\Service\LanguageService |
31
|
|
|
* @covers \Lochmueller\LanguageDetection\Service\LocaleCollectionSortService |
32
|
|
|
* @covers \Lochmueller\LanguageDetection\Service\SiteConfigurationService |
33
|
|
|
* |
34
|
|
|
* @dataProvider data |
35
|
|
|
* |
36
|
|
|
* @param string[] $result |
37
|
|
|
* @param ?string $ipLocationConfiguration |
38
|
|
|
*/ |
39
|
|
|
public function testAddIpLanguageConfiguration(string $addIpLocationToBrowserLanguage, array $result, ?string $ipLocationConfiguration): void |
40
|
|
|
{ |
41
|
|
|
$ipLocation = $this->createStub(IpLocation::class); |
42
|
|
|
$ipLocation->method('getCountryCode')->willReturn($ipLocationConfiguration); |
43
|
|
|
|
44
|
|
|
$serverRequest = new ServerRequest(null, null, 'php://input', ['user-agent' => 'AdsBot-Google']); |
45
|
|
|
|
46
|
|
|
$site = $this->createStub(Site::class); |
47
|
|
|
$site->method('getConfiguration')->willReturn(['addIpLocationToBrowserLanguage' => $addIpLocationToBrowserLanguage]); |
48
|
|
|
|
49
|
|
|
$event = new DetectUserLanguagesEvent($site, $serverRequest); |
50
|
|
|
$event->setUserLanguages(LocaleCollection::fromArray(['default'])); |
51
|
|
|
|
52
|
|
|
$ipLanguage = new GeoPluginDetect($ipLocation, new LanguageService(), new SiteConfigurationService(), new LocaleCollectionSortService()); |
53
|
|
|
$ipLanguage($event); |
54
|
|
|
|
55
|
|
|
self::assertSame($result, array_map(fn($locale): string => (string)$locale, $event->getUserLanguages()->toArray())); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array<string, array<string|string[]|string|null>> |
60
|
|
|
*/ |
61
|
|
|
public static function data(): array |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
'Empty LD configuration with country result' => ['', ['default'], 'DE'], |
65
|
|
|
'After LD configuration with no country result' => ['after', ['default'], null], |
66
|
|
|
'After LD configuration with DE country result' => ['after', ['default', 'de_DE'], 'DE'], |
67
|
|
|
'Before LD configuration with DE country result' => ['before', ['de_DE', 'default'], 'DE'], |
68
|
|
|
'Replace LD configuration with DE country result' => ['replace', ['de_DE'], 'DE'], |
69
|
|
|
'Wrong LD configuration with DE country result' => ['wrong', ['default'], 'DE'], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|