Skip to content

Commit

Permalink
[TASK] Add option to simulate IP address for test purposes, bugfix fo…
Browse files Browse the repository at this point in the history
…r multiple IP judges
  • Loading branch information
kranz committed Apr 10, 2022
1 parent 6465cd8 commit 30f830a
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 21 deletions.
16 changes: 3 additions & 13 deletions Classes/FactProvider/AbstractFactProvider.php
Expand Up @@ -27,10 +27,12 @@ abstract class AbstractFactProvider

/**
* @param string $basename The basename for the factsArray. This name comes from configuration.
* @param array $configuration TypoScript configuration array for this fact provider
*/
public function __construct(string $basename = '')
public function __construct(string $basename = '', array $configuration = [])
{
$this->basename = $basename;
$this->configuration = $configuration;
}

public function isMultiple(): bool
Expand Down Expand Up @@ -65,18 +67,6 @@ protected function getFactPropertyName(string $property): string
return mb_strtolower($property);
}

/**
* @return array|mixed
*/
public function getSubject()
{
if (count($this->facts) > 1) {
return $this->facts;
}

return array_shift($this->facts);
}

/**
* Priority is only set if there are multiple facts (e.g. for browser accept languages)
*/
Expand Down
4 changes: 2 additions & 2 deletions Classes/FactProvider/BrowserAcceptedLanguage.php
Expand Up @@ -48,9 +48,9 @@ public function process(): self
public function isGuilty($prosecution): bool
{
LocateUtility::mainstreamValue($prosecution);
$this->priority = (int)($this->getSubject()[$prosecution] ?? 0);
$this->priority = (int)($this->facts[$prosecution] ?? 0);

return isset($this->getSubject()[$prosecution]);
return isset($this->facts[$prosecution]);
}

protected function getAcceptedLanguages(): array
Expand Down
6 changes: 4 additions & 2 deletions Classes/FactProvider/IP2Country.php
Expand Up @@ -33,7 +33,9 @@ public function getBasename(): string
*/
public function process(): self
{
$iso2 = GeneralUtility::makeInstance(LocateUtility::class)->getCountryIso2FromIP() ?? '';
$simulateIp = $this->configuration['settings']['simulateIp'] ? : null;

$iso2 = GeneralUtility::makeInstance(LocateUtility::class)->getCountryIso2FromIP($simulateIp) ?? '';
LocateUtility::mainstreamValue($iso2);
$this->facts[$this->getBasename()] = $iso2;

Expand All @@ -48,6 +50,6 @@ public function isGuilty($prosecution): bool
$prosecution = (string)$prosecution;
LocateUtility::mainstreamValue($prosecution);

return $this->getSubject() === $prosecution;
return $this->facts[$this->getBasename()] === $prosecution;
}
}
2 changes: 1 addition & 1 deletion Classes/Judge/Condition.php
Expand Up @@ -24,7 +24,7 @@ public function adjudicate(AbstractFactProvider $factProvider, int $priority = A
{
$prosecution = $this->configuration['prosecution'] ?? $this->configuration['prosecution.'] ?? null;

if ($prosecution !== null && $factProvider->isGuilty($prosecution) && isset($this->configuration['verdict'])) {
if ($prosecution !== null && isset($this->configuration['verdict']) && $factProvider->isGuilty($prosecution)) {
$this->decision = (new Decision())->withVerdictName($this->configuration['verdict']);
$this->decision->setPriority($priority);

Expand Down
1 change: 1 addition & 0 deletions Classes/Middleware/LanguageRedirectMiddleware.php
Expand Up @@ -43,6 +43,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
'overrideSessionValue' => (bool)($locateSetup['overrideSessionValue'] ?? 0),
'sessionHandling' => (bool)($locateSetup['sessionHandling'] ?? 0),
'excludeBots' => (bool)($locateSetup['excludeBots'] ?? 1),
'simulateIp' => $locateSetup['simulateIp'] ? : null,
],
];

Expand Down
2 changes: 1 addition & 1 deletion Classes/Processor/Court.php
Expand Up @@ -95,7 +95,7 @@ protected function processFacts(): void
}

/* @var $factProvider AbstractFactProvider */
$factProvider = GeneralUtility::makeInstance($className);
$factProvider = GeneralUtility::makeInstance($className, $key, $this->configuration);

if (!$factProvider instanceof AbstractFactProvider) {
throw new IllegalFactProviderException(
Expand Down
Expand Up @@ -16,6 +16,9 @@ config.tx_locate {
# URL Parameter which has to be true when overrideCookie is allowed within action and cookieHandling is enabled
overrideQueryParameter = setLang

# Simulate your IP address for countryByIP fact provider (for test purposes only), 109.10.163.98 is a french IP address
simulateIp = 109.10.163.98

verdicts {
redirectToMainlandChina = Leuchtfeuer\Locate\Verdict\Redirect
redirectToMainlandChina.url = https://www.example.cn
Expand Down
7 changes: 5 additions & 2 deletions Configuration/TypoScript/setup.typoscript
Expand Up @@ -16,13 +16,16 @@ config.tx_locate {
# URL Parameter which has to be true when overrideCookie is allowed within action and cookieHandling is enabled
overrideQueryParameter = setLang

# Simulate your IP address for countryByIP fact provider (for test purposes only)
simulateIp =

# See example file for configuration
verdicts {
}

facts {
browserAcceptLanguage = Leuchtfeuer\Locate\FactProvider\BrowserAcceptedLanguage
countryByIP = Leuchtfeuer\Locate\FactProvider\IP2Country
#browserAcceptLanguage = Leuchtfeuer\Locate\FactProvider\BrowserAcceptedLanguage
#countryByIP = Leuchtfeuer\Locate\FactProvider\IP2Country
}

# See example file for configuration
Expand Down

0 comments on commit 30f830a

Please sign in to comment.