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

ManyVisitsWithGeoIP.php « Fixtures « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9458ccb12ebcfc08ce428313f37469e46c48d129 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link    http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/MockLocationProvider.php';

/**
 * Adds one new website and tracks 35 visits from 18 visitors with geolocation using
 * free GeoIP databases. The GeoIP databases are downloaded if they do not exist already.
 */
class Test_Piwik_Fixture_ManyVisitsWithGeoIP extends Test_Piwik_BaseFixture
{
    const GEOIP_IMPL_TO_TEST = 'geoip_php';

    public $idSite = 1;
    public $dateTime = '2010-01-03 11:22:33';

    public $ips = array(
        '194.57.91.215', // in Besançon, FR (unicode city name)
        '::ffff:137.82.130.49', // in British Columbia (mapped ipv4)
        '137.82.130.0', // anonymization tests
        '137.82.0.0',
        '2001:db8:85a3:0:0:8a2e:370:7334', // ipv6 (geoip lookup not supported)
        '113.62.1.1', // in Lhasa, Tibet
        '151.100.101.92', // in Rome, Italy (using country DB, so only Italy will show)
        '103.29.196.229', // in Indonesia (Bali), (only Indonesia will show up)
    );

    protected $idGoal;
    protected $idGoal2;

    public function setUp()
    {
        $this->setUpWebsitesAndGoals();
        self::downloadGeoIpDbs();

        $this->setMockLocationProvider();
        $this->trackVisits(9, false);

        $this->setLocationProvider('GeoIPCity.dat');
        $this->trackVisits(2, true, $useLocal = false);
        $this->trackVisits(4, true, $useLocal = false, $doBulk = true);

        $this->setLocationProvider('GeoIP.dat');
        $this->trackVisits(2, true);

        $this->trackOtherVisits();

        $this->setLocationProvider('GeoIPCity.dat');
    }

    public function tearDown()
    {
        $this->unsetLocationProvider();
    }

    private function setUpWebsitesAndGoals()
    {
        self::createWebsite($this->dateTime, 0, "Site 1");
        $this->idGoal = Piwik_Goals_API::getInstance()->addGoal($this->idSite, 'all', 'url', 'http', 'contains', false, 5);
        $this->idGoal2 = Piwik_Goals_API::getInstance()->addGoal($this->idSite, 'two', 'url', 'xxxxxxxxxxxxx', 'contains', false, 5);
    }

    private function trackVisits($visitorCount, $setIp = false, $useLocal = true, $doBulk = false)
    {
        static $calledCounter = 0;
        $calledCounter++;

        $dateTime = $this->dateTime;
        $idSite = $this->idSite;

        // use local tracker so mock location provider can be used
        $t = self::getTracker($idSite, $dateTime, $defaultInit = true, $useLocal);
        if ($doBulk) {
            $t->enableBulkTracking();
            $t->setTokenAuth(self::getTokenAuth());
        }
        for ($i = 0; $i != $visitorCount; ++$i) {
            $t->setVisitorId( substr(md5($i + $calledCounter * 1000), 0, $t::LENGTH_VISITOR_ID));
            if ($setIp) {
                $t->setIp(current($this->ips));
                next($this->ips);
            } else {
                $t->setIp("1.2.4.$i");
            }

            // first visit
            $date = Piwik_Date::factory($dateTime)->addDay($i);
            $t->setForceVisitDateTime($date->getDatetime());
            $t->setUrl("http://piwik.net/grue/lair");
            $t->setCustomVariable(1, 'Cvar 1 name', 'Cvar1 value is ' .$i , 'visit');
            $t->setCustomVariable(5, 'Cvar 5 name', 'Cvar5 value is ' .$i , 'visit');
            $t->setCustomVariable(2, 'Cvar 2 PAGE name', 'Cvar2 PAGE value is ' .$i, 'page');
            $t->setCustomVariable(5, 'Cvar 5 PAGE name', 'Cvar5 PAGE value is ' .$i, 'page');

            $r = $t->doTrackPageView('It\'s <script> pitch black...');
            if (!$doBulk) {
                self::checkResponse($r);
            }

            // second visit
            $date = $date->addHour(1);
            $t->setForceVisitDateTime($date->getDatetime());
            $t->setUrl("http://piwik.net/space/quest/iv");

            // Manually record some data
            $t->setDebugStringAppend(
                '&_idts='. $date->subDay(100)->getTimestampUTC(). // first visit timestamp
                '&_ects='. $date->subDay(50)->getTimestampUTC(). // Timestamp ecommerce
                '&_viewts='. $date->subDay(10)->getTimestampUTC(). // Last visit timestamp
                '&_idvc=5' // Visit count
            );
            $r = $t->doTrackPageView("Space Quest XII");

            if (!$doBulk) {
                self::checkResponse($r);
            }

            // Track site search (for AutoSuggestAPI test)
            // Only for half visitors so they don't all have a "site search" as last action and some of them have a standard page view as last action
            if( ($i % 2) == 0) {
                $date = $date->addHour(0.1);
                $t->setForceVisitDateTime($date->getDatetime());
                $r = $t->doTrackSiteSearch('Bring on the party', 'CAT');
                if (!$doBulk) {
                    self::checkResponse($r);
                }
            }

            $date = $date->addHour(0.2);
            $t->setForceVisitDateTime($date->getDatetime());
            $r = $t->doTrackGoal($this->idGoal2);
            if (!$doBulk) {
                self::checkResponse($r);
            }
        }
        if ($doBulk) {
            self::checkResponse($t->doBulkTrack());
        }
    }

    private function trackOtherVisits()
    {
        $dateTime = $this->dateTime;
        $idSite = $this->idSite;

        $t = self::getTracker($idSite, $dateTime, $defaultInit = true);
        $t->setVisitorId('fed33392d3a48ab2');
        $t->setTokenAuth(self::getTokenAuth());
        $t->setForceVisitDateTime(Piwik_Date::factory($dateTime)->addDay(20)->getDatetime());
        $t->setIp('194.57.91.215');
        $t->setCountry('us');
        $t->setRegion('CA');
        $t->setCity('not a city');
        $t->setLatitude(1);
        $t->setLongitude(2);
        $t->setUrl("http://piwik.net/grue/lair");
        $t->setUrlReferrer('http://google.com/?q=Wikileaks FTW');
        self::checkResponse($t->doTrackPageView('It\'s pitch black...'));
    }

    private function setLocationProvider($file)
    {
        Piwik_UserCountry_LocationProvider_GeoIp::$dbNames['loc'] = array($file);
        Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
        Piwik_UserCountry_LocationProvider::$providers = null;
        Piwik_UserCountry_LocationProvider::setCurrentProvider(self::GEOIP_IMPL_TO_TEST);

        if (Piwik_UserCountry_LocationProvider::getCurrentProviderId() !== self::GEOIP_IMPL_TO_TEST) {
            throw new Exception("Failed to set the current location provider to '" . self::GEOIP_IMPL_TO_TEST . "'.");
        }

        $possibleFiles = Piwik_UserCountry_LocationProvider_GeoIp::$dbNames['loc'];
        if (Piwik_UserCountry_LocationProvider_GeoIp::getPathToGeoIpDatabase($possibleFiles) === false) {
            throw new Exception("The GeoIP location provider cannot find the '$file' file! Tests will fail.");
        }
    }

    private function setMockLocationProvider()
    {
        Piwik_UserCountry_LocationProvider::$providers = null;
        Piwik_UserCountry_LocationProvider::setCurrentProvider('mock_provider');
        MockLocationProvider::$locations = array(
            self::makeLocation('Stratford-upon-Avon', 'P3', 'gb', 123.456, 21.321), // template location

            // same region, different city, same country
            self::makeLocation('Nuneaton and Bedworth', 'P3', 'gb', $isp = 'comcast.net'),

            // same region, city & country (different lat/long)
            self::makeLocation('Stratford-upon-Avon', 'P3', 'gb', 124.456, 22.231, $isp = 'comcast.net'),

            // same country, different region & city
            self::makeLocation('London', 'H9', 'gb'),

            // same country, different region, same city
            self::makeLocation('Stratford-upon-Avon', 'G5', 'gb', $lat = null, $long = null, $isp = 'awesomeisp.com'),

            // different country, diff region, same city
            self::makeLocation('Stratford-upon-Avon', '66', 'ru'),

            // different country, diff region (same as last), different city
            self::makeLocation('Hluboká nad Vltavou', '66', 'ru'),

            // different country, diff region (same as last), same city
            self::makeLocation('Stratford-upon-Avon', '66', 'mk'),

            // unknown location
            self::makeLocation(null, null, null),
        );
    }

    private function unsetLocationProvider()
    {
        Piwik_UserCountry_LocationProvider::setCurrentProvider('default');
    }

}