Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AttributeHistoricalDataWithLocationsTest.php « System « tests « UserCountry « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b0429c7c9afb1c1c3ebb512c45747a27c40c1d2 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://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\Plugin;
use Piwik\Plugins\UserCountry\Commands\AttributeHistoricalDataWithLocations;
use Piwik\Tests\Fixtures\ManyVisitsWithGeoIP;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Translate;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

/**
 * Class AttributeHistoricalDataWithLocationsTest
 * @package Piwik\Plugins\UserCountry\Test\Integration
 *
 * @group UserCountry
 * @group AttributeHistoricalDataWithLocations
 */
class AttributeHistoricalDataWithLocationsTest extends IntegrationTestCase
{
    /**
     * @var ManyVisitsWithGeoIP
     */
    public static $fixture = null;

    public function setUp()
    {
        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('GeoIPCity.dat');
    }

    /**
     * @expectedException \RuntimeException
     * @expectedExceptionMessage  Not enough arguments
     */
    public function testExecute_ShouldThrowException_IfArgumentIsMissing()
    {
        $this->executeCommand(null);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage General_ExceptionInvalidDateFormat
     */
    public function testExecute_ShouldReturnMessage_IfDatesAreInvalid()
    {
        $this->executeCommand('test');
    }

    public function testExecute_ShouldReturnEmptyWorkingProcessLogs_IfThereIsNoData()
    {
        $this->assertRegExp(
            '/Re-attribution for date range: 2014-06-01 to 2014-06-06. 0 visits to process with provider "geoip_php"./',
            $this->executeCommand('2014-06-01,2014-06-06')
        );
    }

    public function testExecute_ShouldReturnLogAfterWorkingWithSomeData()
    {
        $result = $this->executeCommand('2010-01-03,2010-06-03');

        $this->assertContains(
            'Re-attribution for date range: 2010-01-03 to 2010-06-03. 35 visits to process with provider "geoip_php".',
            $result
        );

        $this->assertRegExp('/100% processed. Time elapsed: [0-9.]+s/', $result);

        $queryParams = array(
            'idSite'  => self::$fixture->idSite,
            'date'    => self::$fixture->dateTime,
            'period'  => 'month'
        );

        // 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.
        Translate::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();