File size: 4,473 Bytes
2ec30d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

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

namespace Piwik\Plugins\Bandwidth\tests\Fixtures;

use Piwik\Date;
use Piwik\Filesystem;
use Piwik\Plugin;
use Piwik\Tests\Framework\Fixture;

/**
 * Track actions with bandwidth
 */
class OneVisitWithBandwidth extends Fixture
{
    public $dateTime = '2010-01-03 11:22:33';
    public $idSite = 1;

    /**
     * @var \MatomoTracker
     */
    private $tracker;

    public function setUp(): void
    {
        Plugin\Manager::getInstance()->loadPlugin('Bandwidth');
        Plugin\Manager::getInstance()->installLoadedPlugins();
        Filesystem::deleteAllCacheOnUpdate();

        $this->setUpWebsiteAndUser();
        $this->trackVisits();
    }

    private function setUpWebsiteAndUser()
    {
        // tests run in UTC, the Tracker in UTC
        if (!self::siteCreated($idSite = 1)) {
            self::createWebsite($this->dateTime);
        }

        if (!self::siteCreated($idSite = 2)) {
            self::createWebsite($this->dateTime);
        }

        self::createSuperUser();
    }

    public function trackVisits()
    {
        $this->tracker = self::getTracker($this->idSite, $this->dateTime, $useDefault = true, $uselocal = false);
        $this->tracker->setUrl('http://www.example.org/page');

        $this->trackPageview('Test Title', 550);
        $this->trackPageview('Test 2', 200928);
        $this->trackPageview('Test Title');

        $this->moveTimeForward(2);
        $this->trackDownload('/test/xyz.png', 23929);
        $this->trackDownload('/app.apk', 194948483939);
        $this->trackPageview('Test Title 4', 10);
        $this->trackPageview('Test Title');
        $this->moveTimeForward(3);
        $this->trackPageview('Test 3', 52);
        $this->trackPageview('Test Title', 20);
        $this->trackPageview('Test 3', 13);
        $this->moveTimeForward(4);
        $this->trackPageview('Test Title 4', 6889);
        $this->trackDownload('/app.apk', 194948483939);

        $this->trackPageview('No bandwidth');

        $this->moveTimeForward(4.5);
        $this->trackPageview('Test Title', 95);
        $this->trackPageview('Index', 19493);
        $this->trackPageview('Index', 204948283939);
        $this->trackPageview('No bandwidth');
        $this->trackPageview('Index', 455);


        // we want to track pageviews for a different day in the same month year to verify it will still show
        // bandwidth columns on that day even there were no bytes tracked but there were some tracked in the same month
        $this->tracker = self::getTracker($this->idSite, '2010-01-05 23:23:23', $useDefault = true, $uselocal = false);
        $this->trackPageview('Test Title');
        $this->trackPageview('Test Title');
        $this->trackPageview('Test Title 2');
        $this->trackPageview('Index');

        // we want to track some more pageviews with no bytes to make sure columns are not shown here as there are no
        // tracked bytes for this month
        $this->tracker = self::getTracker($this->idSite, '2010-02-05 23:23:23', $useDefault = true, $uselocal = false);
        $this->trackPageview('Test Title');
        $this->trackPageview('Test Title');
        $this->trackPageview('Index');
        $this->trackDownload('/test/xyz.png');
        $this->trackDownload('/app.apk');
    }

    private function trackPageview($title, $bytes = null)
    {
        $this->setBandwidthTrackerParam($bytes);
        $url = str_replace(' ', '/', strtolower($title));
        $this->tracker->setUrl('http://www.example.org/' . $url);
        self::checkResponse($this->tracker->doTrackPageView($title));
    }

    private function trackDownload($path, $bytes = null)
    {
        $this->setBandwidthTrackerParam($bytes);
        self::checkResponse($this->tracker->doTrackAction('http://www.example.org' . $path, 'download'));
    }

    private function setBandwidthTrackerParam($bytes)
    {
        if ($bytes !== null) {
            $this->tracker->setDebugStringAppend('bw_bytes=' . $bytes);
        } else {
            $this->tracker->setDebugStringAppend('');
        }
    }

    private function moveTimeForward($minutes)
    {
        $hour = $minutes / 60;
        $this->tracker->setForceVisitDateTime(Date::factory($this->dateTime)->addHour($hour)->getDatetime());
    }

    public function tearDown(): void
    {
    }
}