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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<?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\System;
use Piwik\Option;
use Piwik\Plugins\GeoIp2\Commands\ConvertRegionCodesToIso;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @group UserCountry
* @group ConvertRegionCodesToIso
*/
class ConvertRegionCodesToIsoTest extends IntegrationTestCase
{
/**
* @var Fixture
*/
public static $fixture = null;
protected static $idSite;
public function setUp(): void
{
parent::setUp();
self::$idSite = Fixture::createWebsite('2016-01-01');
Fixture::createSuperUser(true);
LocationProvider::$providers = null;
LocationProvider::setCurrentProvider('geoip2php');
}
protected static function trackVisit($country, $region)
{
$t = Fixture::getTracker(self::$idSite, '2017-05-05 12:36:00', $defaultInit = true);
$t->setForceNewVisit();
$t->setVisitorId('fed33392d3a48ab2');
$t->setForceVisitDateTime('2017-05-10 12:36:00');
$t->setTokenAuth(Fixture::getTokenAuth());
$t->setIp(rand(1, 256) . '.' . rand(1, 256) . '.' . rand(1, 256) . '.' . rand(1, 256));
$t->setUserId('userid.email@example.org');
$t->setCountry($country);
$t->setRegion($region);
$t->setCity('any city');
$t->setLatitude(1);
$t->setLongitude(2);
$t->setUrl("http://piwik.net/grue/lair");
$t->setUrlReferrer('http://google.com/?q=Wikileaks FTW');
$t->setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3 (.NET CLR 3.5.30729)");
Fixture::checkResponse($t->doTrackPageView('It\'s pitch black...'));
}
protected static function trackVisitAfterSwitch($country, $region)
{
$t = Fixture::getTracker(self::$idSite, '2017-05-15 12:36:00', $defaultInit = true);
$t->setForceNewVisit();
$t->setVisitorId('fed33392d3a48ab2');
$t->setForceVisitDateTime('2017-05-15 12:36:00');
$t->setTokenAuth(Fixture::getTokenAuth());
$t->setIp(rand(1, 256) . '.' . rand(1, 256) . '.' . rand(1, 256) . '.' . rand(1, 256));
$t->setUserId('userid.email@example.org');
$t->setCountry($country);
$t->setRegion($region);
$t->setCity('any city');
$t->setLatitude(1);
$t->setLongitude(2);
$t->setUrl("http://piwik.net/grue/lair");
$t->setUrlReferrer('http://google.com/?q=Wikileaks FTW');
$t->setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3 (.NET CLR 3.5.30729)");
Fixture::checkResponse($t->doTrackPageView('It\'s pitch black...'));
}
public function tearDown(): void
{
parent::tearDown();
Option::delete(ConvertRegionCodesToIso::OPTION_NAME);
}
public function testExecuteAlreadyConverted()
{
Option::set(ConvertRegionCodesToIso::OPTION_NAME, true);
$result = $this->executeCommand();
$this->assertRegExp('/Converting region codes already done/', $result);
}
public function testExecuteShouldConvertRegionCodes()
{
Option::set(GeoIp2::SWITCH_TO_ISO_REGIONS_OPTION_NAME, mktime(0, 0, 0, 5, 12, 2017));
self::trackVisit('gr', '14'); // should become A
self::trackVisit('ir', '03'); // should become 08
self::trackVisit('ir', '15'); // should become 10
self::trackVisit('ir', '10'); // should become 05
self::trackVisit('ad', '05'); // should not change
self::trackVisit('bm', '04'); // should become empty, as not mappable
self::trackVisit('gb', 'C5'); // should become empty, as not mappable
self::trackVisit('jm', '10'); // should become 14
self::trackVisit('ti', '1'); // should become cn / xz
self::trackVisit('eu', ''); // should become `unknown` as country code is invalid
self::trackVisitAfterSwitch('jm', '10');
$result = $this->executeCommand();
self::assertStringContainsString('All region codes converted', $result);
$queryParams = array(
'idSite' => self::$idSite,
'date' => '2017-05-05',
'period' => 'month',
'hideColumns' => 'sum_visit_length', // for unknown reasons this field is different in MySQLI only for this system test
);
// we need to manually reload the translations since they get reset for some reason in IntegrationTestCase::tearDown();
Fixture::loadAllTranslations();
$this->assertApiResponseEqualsExpected("UserCountry.getRegion", $queryParams);
$this->assertApiResponseEqualsExpected("UserCountry.getCountry", $queryParams);
}
/**
* @return string
*/
private function executeCommand()
{
$command = new ConvertRegionCodesToIso();
$application = new Application();
$application->add($command);
$commandTester = new CommandTester($command);
$params = array();
$params['command'] = $command->getName();
$commandTester->execute($params);
$result = $commandTester->getDisplay();
return $result;
}
public static function getPathToTestDirectory()
{
return __DIR__;
}
}
ConvertRegionCodesToIsoTest::$fixture = new Fixture();
|