File size: 2,880 Bytes
968114b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?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\AnonymousPiwikUsageMeasurement\tests\Integration\Tracker;

use Piwik\Plugins\AnonymousPiwikUsageMeasurement\SystemSettings;
use Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker\Trackers;
use Piwik\Plugins\AnonymousPiwikUsageMeasurement\Tracker;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

class TestTargets extends Tracker\Targets
{
    private $targets = array();

    public function __construct($targets)
    {
        $this->targets = $targets;
    }

    public function getTargets()
    {
        return $this->targets;
    }
}

/**
 * @group AnonymousPiwikUsageMeasurement
 * @group Trackers
 * @group TrackersTest
 * @group Plugins
 */
class TrackersTest extends IntegrationTestCase
{
    /**
     * All your actual test methods should start with the name "test"
     */
    public function test_makeTrackers_NoTrackersGiven()
    {
        $targets  = new TestTargets(array());
        $trackers = new Trackers($targets);
        $trackers = $trackers->makeTrackers();

        $this->assertSame(array(), $trackers);
    }

    /**
     * All your actual test methods should start with the name "test"
     */
    public function test_makeTrackers_FromDefaultSettings()
    {
        $settings = new SystemSettings();
        $targets  = new Tracker\Targets($settings);
        $trackers = new Trackers($targets);
        $trackers = $trackers->makeTrackers();

        $this->assertCount(0, $trackers);
    }

    /**
     * All your actual test methods should start with the name "test"
     */
    public function test_makeTrackers_CanCreateMultipleTrackers_AndSavesUrlAndIdSiteCorrectInTheInstance_AndAnonymizesUrlAndReferrer()
    {
        $targets = array(
            array('url' => 'http://apache.piwik/piwik.php', 'idSite' => 98, 'useAnonymization' => true),
            array('url' => 'http://apache.piwik/piwik/piwik.php', 'idSite' => 101, 'useAnonymization' => true),
        );

        $targets  = new TestTargets($targets);
        $trackers = new Trackers($targets);
        $trackers = $trackers->makeTrackers();

        $this->assertCount(2, $trackers);

        // verify url anonymized, referrer removed, idSite + url applied
        $this->assertStringStartsWith('http://apache.piwik/piwik.php?idsite=98&rec=1', $trackers[0]->getUrlTrackPageView());
        $this->assertStringEndsWith('url=https%3A%2F%2Fdemo-anonymous.matomo.org&urlref=', $trackers[0]->getUrlTrackPageView());

        $this->assertStringStartsWith('http://apache.piwik/piwik/piwik.php?idsite=101&rec=1', $trackers[1]->getUrlTrackPageView());
        $this->assertStringEndsWith('url=https%3A%2F%2Fdemo-anonymous.matomo.org&urlref=', $trackers[1]->getUrlTrackPageView());
    }
}