File size: 4,500 Bytes
5b04ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\GeoIp2\tests\Integration;

use Piwik\Config;
use Piwik\Option;
use Piwik\Plugins\GeoIp2\Controller;
use Piwik\Plugins\GeoIp2\GeoIP2AutoUpdater;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group GeoIp2
 * @group ControllerTest
 * @group Plugins
 */
class ControllerTest extends IntegrationTestCase
{
    private $controller;
    private $server = [];
    private $get = [];
    private $post = [];
    private $request = [];

    public function setUp(): void
    {
        parent::setUp();

        Config::getInstance()->General['enable_geolocation_admin'] = 1;

        $this->controller = $this->getMockBuilder(Controller::class)
            ->disableOriginalConstructor()
            ->setMethods(['checkTokenInUrl'])
            ->getMock();
        $this->controller->expects($this->any())
            ->method('checkTokenInUrl');

        $this->server = $_SERVER;
        $this->get = $_GET;
        $this->post = $_POST;
        $this->request = $_REQUEST;

        Option::delete(GeoIP2AutoUpdater::LOC_URL_OPTION_NAME);
        Option::delete(GeoIP2AutoUpdater::ISP_URL_OPTION_NAME);
        Option::delete('geoip2.download_url.loc');
        Option::delete('geoip2.download_url.isp');
    }

    public function tearDown(): void
    {
        $_SERVER = $this->server;
        $_GET = $this->get;
        $_POST = $this->post;
        $_REQUEST = $this->request;

        Option::delete(GeoIP2AutoUpdater::LOC_URL_OPTION_NAME);
        Option::delete(GeoIP2AutoUpdater::ISP_URL_OPTION_NAME);
        Option::delete('geoip2.download_url.loc');
        Option::delete('geoip2.download_url.isp');

        parent::tearDown();
    }

    public function testDownloadMissingGeoIpDbShouldAbortIfConfiguredUrlChangesMidDownloadAndDeleteChunks()
    {
        $oldLocUrl = 'https://download.db-ip.com/free/dbip-city-lite-2020-01.mmdb.gz';
        $newLocUrl = 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=test&suffix=tar.gz';
        $ispUrl = 'https://download.db-ip.com/free/dbip-asn-lite-2020-01.mmdb.gz';

        Option::set(GeoIP2AutoUpdater::LOC_URL_OPTION_NAME, $oldLocUrl);
        Option::set(GeoIP2AutoUpdater::ISP_URL_OPTION_NAME, $ispUrl);
        Option::set('geoip2.download_url.loc', $oldLocUrl);
        Option::set('geoip2.download_url.isp', $ispUrl);

        $locDownloadChunk = GeoIP2AutoUpdater::getTemporaryFolder(
            GeoIP2AutoUpdater::getZippedFilenameToDownloadTo(
                $oldLocUrl,
                'loc',
                GeoIP2AutoUpdater::getGeoIPUrlExtension($oldLocUrl)
            ),
            true
        );
        $ispDownloadChunk = GeoIP2AutoUpdater::getTemporaryFolder(
            GeoIP2AutoUpdater::getZippedFilenameToDownloadTo(
                $ispUrl,
                'isp',
                GeoIP2AutoUpdater::getGeoIPUrlExtension($ispUrl)
            ),
            true
        );

        file_put_contents($locDownloadChunk, 'loc');
        file_put_contents($ispDownloadChunk, 'isp');
        Option::set($locDownloadChunk . '_expectedDownloadSize', '12');
        Option::set($ispDownloadChunk . '_expectedDownloadSize', '12');

        Option::set(GeoIP2AutoUpdater::LOC_URL_OPTION_NAME, $newLocUrl);

        $this->setPostRequest(1, 'loc');
        $continueResponse = $this->decodeJsonResponse($this->controller->downloadMissingGeoIpDb());

        $this->assertArrayHasKey('error', $continueResponse);
        $this->assertFileNotExists($locDownloadChunk);
        $this->assertFileExists($ispDownloadChunk);
        $this->assertFalse(Option::get($locDownloadChunk . '_expectedDownloadSize'));
        $this->assertSame('12', Option::get($ispDownloadChunk . '_expectedDownloadSize'));
        $this->assertFalse(Option::get('geoip2.download_url.loc'));

        @unlink($ispDownloadChunk);
        Option::delete($ispDownloadChunk . '_expectedDownloadSize');
    }

    private function setPostRequest(int $continue, string $key): void
    {
        $_SERVER['REQUEST_METHOD'] = 'POST';
        $_GET = [
            'continue' => $continue,
            'key' => $key,
        ];
        $_POST = $_GET;
        $_REQUEST = $_GET;
    }

    private function decodeJsonResponse($response): array
    {
        return (array) json_decode((string) $response, true);
    }
}