1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-22 06:57:53 +00:00
Files
matomo/plugins/UserCountry/tests/System/AttributeHistoricalDataWithLocationsTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

157 خطوط
5.0 KiB
PHP

<?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\UserCountry\tests\System;
use Piwik\Common;
use Piwik\Db;
use Piwik\Plugins\UserCountry\Commands\AttributeHistoricalDataWithLocations;
use Piwik\Tests\Fixtures\ManyVisitsWithGeoIP;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
/**
* Class AttributeHistoricalDataWithLocationsTest
*
* @group UserCountry
* @group AttributeHistoricalDataWithLocations
*/
class AttributeHistoricalDataWithLocationsTest extends IntegrationTestCase
{
/**
* @var ManyVisitsWithGeoIP
*/
public static $fixture = null;
public function setUp(): void
{
parent::setUp();
$tablesToUpdate = array('log_visit', 'log_conversion');
$columnsToUpdate = array(
'location_country' => '"xx"',
'location_region' => 'NULL',
'location_city' => 'NULL',
'location_latitude' => 'NULL',
'location_longitude' => 'NULL'
);
foreach ($tablesToUpdate as $table) {
$sql = "UPDATE `" . Common::prefixTable($table) . "` SET ";
$sets = array();
foreach ($columnsToUpdate as $column => $defaultValue) {
$sets[] = $column . ' = ' . $defaultValue;
}
$sql .= implode(', ', $sets);
Db::query($sql);
}
self::$fixture->setLocationProvider('GeoIP2-City.mmdb');
}
public function testExecuteShouldThrowExceptionIfArgumentIsMissing()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments');
$this->executeCommand(null);
}
public function testExecuteShouldReturnMessageIfDatesAreInvalid()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('General_ExceptionInvalidDateFormat');
$this->executeCommand('test');
}
public function testExecuteShouldReturnEmptyWorkingProcessLogsIfThereIsNoData()
{
$this->assertRegExp(
'/Re-attribution for date range: 2014-06-01 to 2014-06-06. 0 visits to process with provider "geoip2php"./',
$this->executeCommand('2014-06-01,2014-06-06')
);
}
public function testExecuteShouldReturnLogAfterWorkingWithSomeData()
{
$result = $this->executeCommand('2010-01-03,2010-06-03');
self::assertStringContainsString(
'Re-attribution for date range: 2010-01-03 to 2010-06-03. 35 visits to process with provider "geoip2php".',
$result
);
$this->assertRegExp('/100% processed. Time elapsed: [0-9.]+s/', $result);
$queryParams = array(
'idSite' => self::$fixture->idSite,
'date' => self::$fixture->dateTime,
'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();
// if we do not load translations, a DataTable\Map containing multiple periods will contain only one DataTable having
// the label `General_DateRangeFromTo` instead of many like `From 2010-01-04 to 2010-01-11`, ' `From 2010-01-11 to 2010-01-18`
// As those data tables would all have the same prettyfied period label they would overwrite each other.
Fixture::loadAllTranslations();
$this->assertApiResponseEqualsExpected("UserCountry.getCountry", $queryParams);
$this->assertApiResponseEqualsExpected("UserCountry.getContinent", $queryParams);
$this->assertApiResponseEqualsExpected("UserCountry.getRegion", $queryParams);
$this->assertApiResponseEqualsExpected("UserCountry.getCity", $queryParams);
}
/**
* @param string|null $dates
*
* @return string
*/
private function executeCommand($dates)
{
$command = new AttributeHistoricalDataWithLocations();
$application = new Application();
$application->add($command);
$commandTester = new CommandTester($command);
if (is_null($dates)) {
$params = array();
} else {
$params = array(AttributeHistoricalDataWithLocations::DATES_RANGE_ARGUMENT => $dates);
}
$params['command'] = $command->getName();
$commandTester->execute($params);
$result = $commandTester->getDisplay();
return $result;
}
public static function configureFixture($fixture)
{
// empty (undo IntegrationTestCase configuring)
$fixture->extraTestEnvVars['loadRealTranslations'] = false;
}
public static function getPathToTestDirectory()
{
return __DIR__;
}
}
AttributeHistoricalDataWithLocationsTest::$fixture = new ManyVisitsWithGeoIP();