2019-04-14 11:49:54 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-04-14 11:49:54 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-09-18 11:01:22 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\GeoLite;
|
2019-04-14 11:49:54 +03:00
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
use MaxMind\Db\Reader\Metadata;
|
2022-10-22 15:27:07 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-04-14 11:49:54 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
2022-09-18 11:01:22 +03:00
|
|
|
use Shlinkio\Shlink\CLI\GeoLite\GeolocationDbUpdater;
|
2022-09-18 11:31:14 +03:00
|
|
|
use Shlinkio\Shlink\CLI\GeoLite\GeolocationResult;
|
2021-05-30 13:30:03 +03:00
|
|
|
use Shlinkio\Shlink\Core\Options\TrackingOptions;
|
2022-09-18 18:00:03 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\DbUpdateException;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
2019-07-20 13:11:07 +03:00
|
|
|
use Symfony\Component\Lock;
|
2019-04-14 11:49:54 +03:00
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
use function Functional\map;
|
|
|
|
use function range;
|
|
|
|
|
|
|
|
class GeolocationDbUpdaterTest extends TestCase
|
|
|
|
{
|
2022-10-24 20:59:03 +03:00
|
|
|
private MockObject & DbUpdaterInterface $dbUpdater;
|
|
|
|
private MockObject & Reader $geoLiteDbReader;
|
|
|
|
private MockObject & Lock\LockInterface $lock;
|
2019-04-14 11:49:54 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2019-04-14 11:49:54 +03:00
|
|
|
{
|
2022-10-22 15:27:07 +03:00
|
|
|
$this->dbUpdater = $this->createMock(DbUpdaterInterface::class);
|
|
|
|
$this->geoLiteDbReader = $this->createMock(Reader::class);
|
|
|
|
$this->lock = $this->createMock(Lock\LockInterface::class);
|
|
|
|
$this->lock->method('acquire')->with($this->isTrue())->willReturn(true);
|
2019-04-14 11:49:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function exceptionIsThrownWhenOlderDbDoesNotExistAndDownloadFails(): void
|
|
|
|
{
|
2020-10-04 01:35:14 +03:00
|
|
|
$mustBeUpdated = fn () => self::assertTrue(true);
|
2022-09-18 18:00:03 +03:00
|
|
|
$prev = new DbUpdateException('');
|
2019-07-23 23:04:01 +03:00
|
|
|
|
2022-10-22 15:27:07 +03: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 11:49:54 +03:00
|
|
|
|
|
|
|
try {
|
2022-09-17 13:57:04 +03:00
|
|
|
$this->geolocationDbUpdater()->checkDbUpdate($mustBeUpdated);
|
2022-10-22 15:27:07 +03:00
|
|
|
self::fail();
|
2019-04-14 11:49:54 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
/** @var GeolocationDbUpdateFailedException $e */
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
|
|
|
self::assertSame($prev, $e->getPrevious());
|
|
|
|
self::assertFalse($e->olderDbExists());
|
2019-04-14 11:49:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-04-14 14:18:03 +03:00
|
|
|
* @dataProvider provideBigDays
|
2019-04-14 11:49:54 +03:00
|
|
|
*/
|
|
|
|
public function exceptionIsThrownWhenOlderDbIsTooOldAndDownloadFails(int $days): void
|
|
|
|
{
|
2022-09-18 18:00:03 +03:00
|
|
|
$prev = new DbUpdateException('');
|
2022-10-22 15:27:07 +03: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 11:49:54 +03:00
|
|
|
|
|
|
|
try {
|
2022-09-17 13:57:04 +03:00
|
|
|
$this->geolocationDbUpdater()->checkDbUpdate();
|
2022-10-22 15:27:07 +03:00
|
|
|
self::fail();
|
2019-04-14 11:49:54 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
/** @var GeolocationDbUpdateFailedException $e */
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
|
|
|
self::assertSame($prev, $e->getPrevious());
|
|
|
|
self::assertTrue($e->olderDbExists());
|
2019-04-14 11:49:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideBigDays(): iterable
|
2019-04-14 11:49:54 +03:00
|
|
|
{
|
2019-04-14 14:18:03 +03:00
|
|
|
yield [36];
|
|
|
|
yield [50];
|
|
|
|
yield [75];
|
2019-04-14 11:49:54 +03:00
|
|
|
yield [100];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-04-14 14:18:03 +03:00
|
|
|
* @dataProvider provideSmallDays
|
2019-04-14 11:49:54 +03:00
|
|
|
*/
|
2022-09-18 11:31:14 +03:00
|
|
|
public function databaseIsNotUpdatedIfItIsNewEnough(string|int $buildEpoch): void
|
2019-04-14 11:49:54 +03:00
|
|
|
{
|
2022-10-22 15:27:07 +03: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 11:49:54 +03:00
|
|
|
|
2022-09-18 11:31:14 +03:00
|
|
|
$result = $this->geolocationDbUpdater()->checkDbUpdate();
|
2019-04-14 11:49:54 +03:00
|
|
|
|
2022-09-18 11:31:14 +03:00
|
|
|
self::assertEquals(GeolocationResult::DB_IS_UP_TO_DATE, $result);
|
2019-04-14 11:49:54 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideSmallDays(): iterable
|
2019-04-14 11:49:54 +03:00
|
|
|
{
|
2021-02-06 23:49:49 +03:00
|
|
|
$generateParamsWithTimestamp = static function (int $days) {
|
|
|
|
$timestamp = Chronos::now()->subDays($days)->getTimestamp();
|
|
|
|
return [$days % 2 === 0 ? $timestamp : (string) $timestamp];
|
|
|
|
};
|
|
|
|
|
|
|
|
return map(range(0, 34), $generateParamsWithTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function exceptionIsThrownWhenCheckingExistingDatabaseWithInvalidBuildEpoch(): void
|
|
|
|
{
|
2022-10-22 15:27:07 +03: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 23:49:49 +03: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 13:57:04 +03:00
|
|
|
$this->geolocationDbUpdater()->checkDbUpdate();
|
2021-02-06 23:49:49 +03:00
|
|
|
}
|
|
|
|
|
2021-08-05 20:47:17 +03:00
|
|
|
private function buildMetaWithBuildEpoch(string|int $buildEpoch): Metadata
|
2021-02-06 23:49:49 +03: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 11:49:54 +03:00
|
|
|
}
|
2021-05-30 13:36:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideTrackingOptions
|
|
|
|
*/
|
2022-09-17 13:57:04 +03:00
|
|
|
public function downloadDbIsSkippedIfTrackingIsDisabled(TrackingOptions $options): void
|
2021-05-30 13:36:58 +03:00
|
|
|
{
|
2022-09-18 11:31:14 +03:00
|
|
|
$result = $this->geolocationDbUpdater($options)->checkDbUpdate();
|
2022-10-22 15:27:07 +03:00
|
|
|
$this->dbUpdater->expects($this->never())->method('databaseFileExists');
|
|
|
|
$this->geoLiteDbReader->expects($this->never())->method('metadata');
|
2021-05-30 13:36:58 +03:00
|
|
|
|
2022-09-18 11:31:14 +03:00
|
|
|
self::assertEquals(GeolocationResult::CHECK_SKIPPED, $result);
|
2021-05-30 13:36:58 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideTrackingOptions(): iterable
|
2021-05-30 13:36:58 +03:00
|
|
|
{
|
2022-09-17 13:57:04 +03: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 15:27:07 +03:00
|
|
|
$locker = $this->createMock(Lock\LockFactory::class);
|
|
|
|
$locker->method('createLock')->with($this->isType('string'))->willReturn($this->lock);
|
2022-09-17 13:57:04 +03:00
|
|
|
|
|
|
|
return new GeolocationDbUpdater(
|
2022-10-22 15:27:07 +03:00
|
|
|
$this->dbUpdater,
|
|
|
|
$this->geoLiteDbReader,
|
|
|
|
$locker,
|
2022-09-17 13:57:04 +03:00
|
|
|
$options ?? new TrackingOptions(),
|
|
|
|
);
|
2021-05-30 13:36:58 +03:00
|
|
|
}
|
2019-04-14 11:49:54 +03:00
|
|
|
}
|