Skip to content

Commit

Permalink
Fix #34 - Add complete locale to GeoPluginDetect
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Nov 10, 2023
1 parent 66a5877 commit be085a2
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 48 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["8.0", "8.1"]
typo3: ["^11.5", ]
php: ["8.1", "8.2"]
typo3: ["^11.5", "^12.4"]
include:
- php: "8.1"
typo3: "^12.0"
- php: "8.0"
typo3: "^11.5"
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
10 changes: 5 additions & 5 deletions Classes/Detect/GeoPluginDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ public function __invoke(DetectUserLanguagesEvent $event): void
return;
}

$language = $this->getLanguage($event->getRequest());
if ($language === null) {
$locale = $this->getLocale($event->getRequest());
if ($locale === null) {
return;
}

$event->setUserLanguages($this->localeCollectionSortService->addLocaleByMode($event->getUserLanguages(), new LocaleValueObject($language), $addIp));
$event->setUserLanguages($this->localeCollectionSortService->addLocaleByMode($event->getUserLanguages(), new LocaleValueObject($locale), $addIp));
}

public function getLanguage(ServerRequestInterface $request): ?string
public function getLocale(ServerRequestInterface $request): ?string
{
$countryCode = $this->ipLocation->getCountryCode($request->getServerParams()['REMOTE_ADDR'] ?? '');
if ($countryCode === null) {
return null;
}

return $this->languageService->getLanguageByCountry($countryCode);
return $this->languageService->getLanguageByCountry($countryCode) . '_' . $countryCode;
}
}
11 changes: 11 additions & 0 deletions Tests/Unit/AbstractUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Lochmueller\LanguageDetection\Tests\Unit;

use TYPO3\CMS\Core\Http\Client\GuzzleClientFactory;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

abstract class AbstractUnitTest extends UnitTestCase
Expand All @@ -24,4 +27,12 @@ public static function assertExecutionMemoryLessThenOrEqual(float $memoryInKb, c
$memoryUsage = memory_get_usage() - $beforeMemory;
self::assertLessThanOrEqual(1024 * $memoryInKb, $memoryUsage, 'Execution memory of this workload should be less then ' . $memoryInKb . 'KB.');
}

public function getRequestFactory(): RequestFactory
{
if ((new Typo3Version())->getMajorVersion() >= 12) {
return new RequestFactory(new GuzzleClientFactory());
}
return new RequestFactory();
}
}
6 changes: 3 additions & 3 deletions Tests/Unit/Detect/GeoPluginDetectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function data(): array
return [
'Empty LD configuration with country result' => ['', ['default'], 'DE'],
'After LD configuration with no country result' => ['after', ['default'], null],
'After LD configuration with DE country result' => ['after', ['default', 'de'], 'DE'],
'Before LD configuration with DE country result' => ['before', ['de', 'default'], 'DE'],
'Replace LD configuration with DE country result' => ['replace', ['de'], 'DE'],
'After LD configuration with DE country result' => ['after', ['default', 'de_DE'], 'DE'],
'Before LD configuration with DE country result' => ['before', ['de_DE', 'default'], 'DE'],
'Replace LD configuration with DE country result' => ['replace', ['de_DE'], 'DE'],
'Wrong LD configuration with DE country result' => ['wrong', ['default'], 'DE'],
];
}
Expand Down
7 changes: 3 additions & 4 deletions Tests/Unit/Service/IpLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Lochmueller\LanguageDetection\Service\IpLocation;
use Lochmueller\LanguageDetection\Tests\Unit\AbstractUnitTest;
use TYPO3\CMS\Core\Http\RequestFactory;

/**
* @internal
Expand All @@ -20,7 +19,7 @@ class IpLocationTest extends AbstractUnitTest
*/
public function testGetLocationForValidIp(): void
{
$locationService = new IpLocation(new RequestFactory());
$locationService = new IpLocation($this->getRequestFactory());
$result = $locationService->getCountryCode('8.8.8.8');

self::assertEquals('US', $result);
Expand All @@ -31,7 +30,7 @@ public function testGetLocationForValidIp(): void
*/
public function testEmptyIpDirectNull(): void
{
$locationService = new IpLocation(new RequestFactory());
$locationService = new IpLocation($this->getRequestFactory());

self::assertNull($locationService->getCountryCode(''));
}
Expand All @@ -41,7 +40,7 @@ public function testEmptyIpDirectNull(): void
*/
public function testGetLocationForInvalidIp(): void
{
$locationService = new IpLocation(new RequestFactory());
$locationService = new IpLocation($this->getRequestFactory());
self::assertNull($locationService->getCountryCode('0.0.0.0'));
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"license": "GPL-2.0-or-later",
"require-dev": {
"ext-json": "*",
"typo3/testing-framework": "^6.16",
"typo3/testing-framework": "^7.0",
"friendsofphp/php-cs-fixer": "^3.11",
"rector/rector": "^0.14",
"phpstan/phpstan": "^1.8",
Expand Down
47 changes: 16 additions & 31 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap=".Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTestsBootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="random"
resolveDependencies="true"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="false"
failOnRisky="true"
stopOnSkipped="false"
failOnWarning="true"
colors="true"
verbose="true">
<testsuites>
<testsuite name="language_detection tests">
<directory>Tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">Classes</directory>
</include>
<report>
<clover outputFile=".phpunit.cache/clover.xml" />
</report>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap=".Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTestsBootstrap.php" executionOrder="random" resolveDependencies="true" beStrictAboutOutputDuringTests="true" failOnRisky="true" stopOnSkipped="false" failOnWarning="true" colors="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="true" beStrictAboutCoverageMetadata="true">
<testsuites>
<testsuite name="language_detection tests">
<directory>Tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<clover outputFile=".phpunit.cache/clover.xml"/>
</report>
</coverage>
<source>
<include>
<directory suffix=".php">Classes</directory>
</include>
</source>
</phpunit>

0 comments on commit be085a2

Please sign in to comment.