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

SitesManagerTest.php « Integration « tests « SitesManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ac51890c099c5f9c91d08885485c0c316ed69a9b (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
<?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\SitesManager\tests\Integration;

use Piwik\Cache;
use Piwik\Archive\ArchiveInvalidator;
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 test_onSiteDeleted_shouldClearSiteCache()
    {
        $cache = Cache::getLazyCache();
        $cache->save($this->siteId, 'testcontent');

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

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

    public function test_onSiteDeleted_shouldRemoveRememberedArchiveReports()
    {
        $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(array($this->siteId, 4949), $remembered['2014-04-05']);

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

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

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

    /**
     * @dataProvider getTestDataForRedirectDashboard
     */
    public function test_redirectDashboardToWelcomePage_doesNothingIfModuleActionAreIncorrect($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 test_redirectDashboardToWelcomePage_doesNothingIfThereIsNoIdSiteParam()
    {
        $module = 'CoreHome';
        $action = 'index';
        $params = [];

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

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

    public function test_redirectDashboardToWelcomePage_doesNothingIfAVisitWasTrackedInThePast()
    {
        $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 test_redirectDashboardToWelcomePage_doesNothingIfAVisitWasTrackedAndWasLaterPurged()
    {
        $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 test_redirectDashboardToWelcomePage_redirectsIfThereIsNoDataAndAppropriateParams()
    {
        $module = 'CoreHome';
        $action = 'index';
        $params = [];

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

        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 array(
            'Piwik\Access' => new FakeAccess()
        );
    }
}