2019-04-14 03:49:54 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2019-04-14 03:49:54 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-09-18 03:01:22 -05:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\GeoLite;
|
2019-04-14 03:49:54 -05:00
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
use MaxMind\Db\Reader\Metadata;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-22 07:27:07 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-04-14 03:49:54 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
2022-09-18 03:01:22 -05:00
|
|
|
use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdater;
|
2022-09-18 03:31:14 -05:00
|
|
|
use Shlinkio\Shlink\CLI\GeoLite\GeolocationResult;
|
2021-05-30 05:30:03 -05:00
|
|
|
use Shlinkio\Shlink\Core\Options\TrackingOptions;
|
2022-09-18 10:00:03 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\DbUpdateException;
|
2023-11-23 02:31:02 -06:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\MissingLicenseException;
|
2019-08-10 06:42:37 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
2019-07-20 05:11:07 -05:00
|
|
|
use Symfony\Component\Lock;
|
2019-04-14 03:49:54 -05:00
|
|
|
use Throwable;
|
|
|
|
|
2023-11-29 05:34:13 -06:00
|
|
|
use function array_map;
|
2019-04-14 03:49:54 -05:00
|
|
|
use function range;
|
|
|
|
|
|
|
|
class GeolocationDbUpdaterTest extends TestCase
|
|
|
|
{
|
2022-10-24 12:59:03 -05:00
|
|
|
private MockObject & DbUpdaterInterface $dbUpdater;
|
|
|
|
private MockObject & Reader $geoLiteDbReader;
|
|
|
|
private MockObject & Lock\LockInterface $lock;
|
2019-04-14 03:49:54 -05:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2019-04-14 03:49:54 -05:00
|
|
|
{
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->dbUpdater = $this->createMock(DbUpdaterInterface::class);
|
|
|
|
$this->geoLiteDbReader = $this->createMock(Reader::class);
|
2024-02-17 03:46:29 -06:00
|
|
|
$this->lock = $this->createMock(Lock\SharedLockInterface::class);
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->lock->method('acquire')->with($this->isTrue())->willReturn(true);
|
2019-04-14 03:49:54 -05:00
|
|
|
}
|
|
|
|
|
2023-11-23 02:31:02 -06:00
|
|
|
#[Test]
|
|
|
|
public function properResultIsReturnedWhenLicenseIsMissing(): void
|
|
|
|
{
|
|
|
|
$mustBeUpdated = fn () => self::assertTrue(true);
|
|
|
|
|
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->willReturn(false);
|
|
|
|
$this->dbUpdater->expects($this->once())->method('downloadFreshCopy')->willThrowException(
|
|
|
|
new MissingLicenseException(''),
|
|
|
|
);
|
|
|
|
$this->geoLiteDbReader->expects($this->never())->method('metadata');
|
|
|
|
|
|
|
|
$result = $this->geolocationDbUpdater()->checkDbUpdate($mustBeUpdated);
|
|
|
|
self::assertEquals(GeolocationResult::LICENSE_MISSING, $result);
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-04-14 03:49:54 -05:00
|
|
|
public function exceptionIsThrownWhenOlderDbDoesNotExistAndDownloadFails(): void
|
|
|
|
{
|
2020-10-03 17:35:14 -05:00
|
|
|
$mustBeUpdated = fn () => self::assertTrue(true);
|
2022-09-18 10:00:03 -05:00
|
|
|
$prev = new DbUpdateException('');
|
2019-07-23 15:04:01 -05:00
|
|
|
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->willReturn(false);
|
|
|
|
$this->dbUpdater->expects($this->once())->method('downloadFreshCopy')->with(
|
|
|
|
$this->isNull(),
|
|
|
|
)->willThrowException($prev);
|
|
|
|
$this->geoLiteDbReader->expects($this->never())->method('metadata');
|
2019-04-14 03:49:54 -05:00
|
|
|
|
|
|
|
try {
|
2022-09-17 05:57:04 -05:00
|
|
|
$this->geolocationDbUpdater()->checkDbUpdate($mustBeUpdated);
|
2022-10-22 07:27:07 -05:00
|
|
|
self::fail();
|
2019-04-14 03:49:54 -05:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
/** @var GeolocationDbUpdateFailedException $e */
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
|
|
|
self::assertSame($prev, $e->getPrevious());
|
|
|
|
self::assertFalse($e->olderDbExists());
|
2019-04-14 03:49:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideBigDays')]
|
2019-04-14 03:49:54 -05:00
|
|
|
public function exceptionIsThrownWhenOlderDbIsTooOldAndDownloadFails(int $days): void
|
|
|
|
{
|
2022-09-18 10:00:03 -05:00
|
|
|
$prev = new DbUpdateException('');
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->willReturn(true);
|
|
|
|
$this->dbUpdater->expects($this->once())->method('downloadFreshCopy')->with(
|
|
|
|
$this->isNull(),
|
|
|
|
)->willThrowException($prev);
|
|
|
|
$this->geoLiteDbReader->expects($this->once())->method('metadata')->with()->willReturn(
|
|
|
|
$this->buildMetaWithBuildEpoch(Chronos::now()->subDays($days)->getTimestamp()),
|
|
|
|
);
|
2019-04-14 03:49:54 -05:00
|
|
|
|
|
|
|
try {
|
2022-09-17 05:57:04 -05:00
|
|
|
$this->geolocationDbUpdater()->checkDbUpdate();
|
2022-10-22 07:27:07 -05:00
|
|
|
self::fail();
|
2019-04-14 03:49:54 -05:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
/** @var GeolocationDbUpdateFailedException $e */
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
|
|
|
self::assertSame($prev, $e->getPrevious());
|
|
|
|
self::assertTrue($e->olderDbExists());
|
2019-04-14 03:49:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideBigDays(): iterable
|
2019-04-14 03:49:54 -05:00
|
|
|
{
|
2019-04-14 06:18:03 -05:00
|
|
|
yield [36];
|
|
|
|
yield [50];
|
|
|
|
yield [75];
|
2019-04-14 03:49:54 -05:00
|
|
|
yield [100];
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideSmallDays')]
|
2022-09-18 03:31:14 -05:00
|
|
|
public function databaseIsNotUpdatedIfItIsNewEnough(string|int $buildEpoch): void
|
2019-04-14 03:49:54 -05:00
|
|
|
{
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->willReturn(true);
|
|
|
|
$this->dbUpdater->expects($this->never())->method('downloadFreshCopy');
|
|
|
|
$this->geoLiteDbReader->expects($this->once())->method('metadata')->with()->willReturn(
|
|
|
|
$this->buildMetaWithBuildEpoch($buildEpoch),
|
|
|
|
);
|
2019-04-14 03:49:54 -05:00
|
|
|
|
2022-09-18 03:31:14 -05:00
|
|
|
$result = $this->geolocationDbUpdater()->checkDbUpdate();
|
2019-04-14 03:49:54 -05:00
|
|
|
|
2022-09-18 03:31:14 -05:00
|
|
|
self::assertEquals(GeolocationResult::DB_IS_UP_TO_DATE, $result);
|
2019-04-14 03:49:54 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideSmallDays(): iterable
|
2019-04-14 03:49:54 -05:00
|
|
|
{
|
2021-02-06 14:49:49 -06:00
|
|
|
$generateParamsWithTimestamp = static function (int $days) {
|
|
|
|
$timestamp = Chronos::now()->subDays($days)->getTimestamp();
|
|
|
|
return [$days % 2 === 0 ? $timestamp : (string) $timestamp];
|
|
|
|
};
|
|
|
|
|
2023-11-29 05:34:13 -06:00
|
|
|
return array_map($generateParamsWithTimestamp, range(0, 34));
|
2021-02-06 14:49:49 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-02-06 14:49:49 -06:00
|
|
|
public function exceptionIsThrownWhenCheckingExistingDatabaseWithInvalidBuildEpoch(): void
|
|
|
|
{
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->willReturn(true);
|
|
|
|
$this->dbUpdater->expects($this->never())->method('downloadFreshCopy');
|
|
|
|
$this->geoLiteDbReader->expects($this->once())->method('metadata')->with()->willReturn(
|
|
|
|
$this->buildMetaWithBuildEpoch('invalid'),
|
|
|
|
);
|
2021-02-06 14:49:49 -06:00
|
|
|
|
|
|
|
$this->expectException(GeolocationDbUpdateFailedException::class);
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
'Build epoch with value "invalid" from existing geolocation database, could not be parsed to integer.',
|
|
|
|
);
|
|
|
|
|
2022-09-17 05:57:04 -05:00
|
|
|
$this->geolocationDbUpdater()->checkDbUpdate();
|
2021-02-06 14:49:49 -06:00
|
|
|
}
|
|
|
|
|
2021-08-05 12:47:17 -05:00
|
|
|
private function buildMetaWithBuildEpoch(string|int $buildEpoch): Metadata
|
2021-02-06 14:49:49 -06:00
|
|
|
{
|
|
|
|
return new Metadata([
|
|
|
|
'binary_format_major_version' => '',
|
|
|
|
'binary_format_minor_version' => '',
|
|
|
|
'build_epoch' => $buildEpoch,
|
|
|
|
'database_type' => '',
|
|
|
|
'languages' => '',
|
|
|
|
'description' => '',
|
|
|
|
'ip_version' => '',
|
|
|
|
'node_count' => 1,
|
|
|
|
'record_size' => 4,
|
|
|
|
]);
|
2019-04-14 03:49:54 -05:00
|
|
|
}
|
2021-05-30 05:36:58 -05:00
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideTrackingOptions')]
|
2022-09-17 05:57:04 -05:00
|
|
|
public function downloadDbIsSkippedIfTrackingIsDisabled(TrackingOptions $options): void
|
2021-05-30 05:36:58 -05:00
|
|
|
{
|
2022-09-18 03:31:14 -05:00
|
|
|
$result = $this->geolocationDbUpdater($options)->checkDbUpdate();
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->dbUpdater->expects($this->never())->method('databaseFileExists');
|
|
|
|
$this->geoLiteDbReader->expects($this->never())->method('metadata');
|
2021-05-30 05:36:58 -05:00
|
|
|
|
2022-09-18 03:31:14 -05:00
|
|
|
self::assertEquals(GeolocationResult::CHECK_SKIPPED, $result);
|
2021-05-30 05:36:58 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideTrackingOptions(): iterable
|
2021-05-30 05:36:58 -05:00
|
|
|
{
|
2022-09-17 05:57:04 -05:00
|
|
|
yield 'disableTracking' => [new TrackingOptions(disableTracking: true)];
|
|
|
|
yield 'disableIpTracking' => [new TrackingOptions(disableIpTracking: true)];
|
|
|
|
yield 'both' => [new TrackingOptions(disableTracking: true, disableIpTracking: true)];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function geolocationDbUpdater(?TrackingOptions $options = null): GeolocationDbUpdater
|
|
|
|
{
|
2022-10-22 07:27:07 -05:00
|
|
|
$locker = $this->createMock(Lock\LockFactory::class);
|
|
|
|
$locker->method('createLock')->with($this->isType('string'))->willReturn($this->lock);
|
2022-09-17 05:57:04 -05:00
|
|
|
|
|
|
|
return new GeolocationDbUpdater(
|
2022-10-22 07:27:07 -05:00
|
|
|
$this->dbUpdater,
|
2024-03-09 02:30:05 -06:00
|
|
|
fn () => $this->geoLiteDbReader,
|
2022-10-22 07:27:07 -05:00
|
|
|
$locker,
|
2022-09-17 05:57:04 -05:00
|
|
|
$options ?? new TrackingOptions(),
|
|
|
|
);
|
2021-05-30 05:36:58 -05:00
|
|
|
}
|
2019-04-14 03:49:54 -05:00
|
|
|
}
|