File: SitesManagerTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (191 lines) | stat: -rw-r--r-- 5,673 bytes parent folder | download
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
<?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\SitesManager\tests\Integration;

use Piwik\Cache;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Db;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\SitesManager;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * Class Plugins_SitesManagerTest
 *
 * @group Plugins
 */
class SitesManagerTest extends IntegrationTestCase
{
    /**
     * @var SitesManager
     */
    private $manager;

    private $siteId;

    public function setUp(): void
    {
        parent::setUp();

        // setup the access layer
        FakeAccess::$superUser = true;

        $this->manager = new SitesManager();
        $this->siteId  = Fixture::createWebsite('2014-03-03 00:00:00');
    }

    public function testOnSiteDeletedShouldClearSiteCache()
    {
        $cache = Cache::getLazyCache();
        $cache->save($this->siteId, 'testcontent');

        $this->manager->onSiteDeleted($this->siteId);

        $this->assertFalse($cache->contains($this->siteId));
    }

    public function testOnSiteDeletedShouldRemoveRememberedArchiveReports()
    {
        $archive = StaticContainer::get('Piwik\Archive\ArchiveInvalidator');
        $archive->rememberToInvalidateArchivedReportsLater($this->siteId, Date::factory('2014-04-05'));
        $archive->rememberToInvalidateArchivedReportsLater($this->siteId, Date::factory('2014-04-06'));
        $archive->rememberToInvalidateArchivedReportsLater(4949, Date::factory('2014-04-05'));

        $remembered = $archive->getRememberedArchivedReportsThatShouldBeInvalidated();
        $this->assertCount(2, $remembered);

        sort($remembered['2014-04-05']);
        $this->assertSame([$this->siteId, 4949], $remembered['2014-04-05']);

        sort($remembered['2014-04-06']);
        $this->assertSame([$this->siteId], $remembered['2014-04-06']);

        $this->manager->onSiteDeleted($this->siteId);

        $expected = [
            '2014-04-05' => [4949],
        ];
        $this->assertEquals($expected, $archive->getRememberedArchivedReportsThatShouldBeInvalidated());
    }

    /**
     * @dataProvider getTestDataForRedirectDashboard
     */
    public function testRedirectDashboardToWelcomePageDoesNothingIfModuleActionAreIncorrect($module, $action)
    {
        $originalModule = $module;
        $originalAction = $action;
        $params = [];

        Piwik::postEvent('Request.dispatch', [&$module, &$action, &$params]);

        $this->assertEquals($originalModule, $module);
        $this->assertEquals($originalAction, $action);
    }

    public function getTestDataForRedirectDashboard()
    {
        return [
            ['CoreHome', 'someothermethod'],
            ['SitesManager', 'index'],
        ];
    }

    public function testRedirectDashboardToWelcomePageDoesNothingIfThereIsNoIdSiteParam()
    {
        $module = 'CoreHome';
        $action = 'index';
        $params = [];

        Piwik::postEvent('Request.dispatch', [&$module, &$action, &$params]);

        $this->assertEquals('CoreHome', $module);
        $this->assertEquals('index', $action);
    }

    public function testRedirectDashboardToWelcomePageDoesNothingIfAVisitWasTrackedInThePast()
    {
        $module = 'CoreHome';
        $action = 'index';
        $params = [];

        $_GET['idSite'] = $this->siteId;

        $tracker = Fixture::getTracker($this->siteId, '2015-02-04 04:12:35');
        $tracker->setUrl('http://example.com/');
        Fixture::checkResponse($tracker->doTrackPageView('a test title'));

        $this->assertEquals(false, Option::get('SitesManagerHadTrafficInPast_' . $this->siteId));

        Piwik::postEvent('Request.dispatch', [&$module, &$action, &$params]);

        $this->assertEquals('1', Option::get('SitesManagerHadTrafficInPast_' . $this->siteId));

        $this->assertEquals('CoreHome', $module);
        $this->assertEquals('index', $action);
    }

    public function testRedirectDashboardToWelcomePageDoesNothingIfAVisitWasTrackedAndWasLaterPurged()
    {
        $module = 'CoreHome';
        $action = 'index';
        $params = [];

        $_GET['idSite'] = $this->siteId;

        $tracker = Fixture::getTracker($this->siteId, '2015-02-04 04:12:35');
        $tracker->setUrl('http://example.com/');
        Fixture::checkResponse($tracker->doTrackPageView('a test title'));

        Piwik::postEvent('Request.dispatch', [&$module, &$action, &$params]);

        Db::exec('TRUNCATE ' . Common::prefixTable('log_visit'));

        $module = 'CoreHome';
        $action = 'index';

        $this->assertEquals('CoreHome', $module);
        $this->assertEquals('index', $action);
    }

    public function testRedirectDashboardToWelcomePageRedirectsIfThereIsNoDataAndAppropriateParams()
    {
        $module = 'CoreHome';
        $action = 'index';
        $params = [];

        $_GET['idSite'] = $this->siteId;

        \Zend_Session::$_unitTestEnabled = true;

        Piwik::postEvent('Request.dispatch', [&$module, &$action, &$params]);

        $this->assertEquals('SitesManager', $module);
        $this->assertEquals('siteWithoutData', $action);
    }

    protected static function configureFixture($fixture)
    {
        parent::configureFixture($fixture);
        $fixture->createSuperUser = true;
    }

    public function provideContainerConfig()
    {
        return [
            'Piwik\Access' => new FakeAccess(),
        ];
    }
}