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

SiteUrlsTest.php « Integration « tests « SitesManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3db34199fbf93bdaa3b0df798d1b91ee875e9f60 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.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\Plugins\SitesManager\API;
use Piwik\Plugins\SitesManager\SiteUrls;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group SitesManager
 * @group SiteUrlsTest
 * @group Plugins
 */
class SiteUrlsTest extends IntegrationTestCase
{
    /**
     * @var SiteUrls
     */
    private $siteUrls;

    /**
     * @var API
     */
    private $api;

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

        $this->siteUrls = new SiteUrls();
        $this->api = API::getInstance();

        SiteUrls::clearSitesCache();
    }

    public function testGetAllSiteUrls_shouldReturnAnEmptyArray_IfThereAreNoSites()
    {
        $this->assertSiteUrls(array());
    }

    public function testGetAllSiteUrls_shouldReturnUrlsForEachSiteId()
    {
        $this->addSite('http://www.example.com'); // only one main URL
        $this->assertSiteUrls(array(1 => array('http://www.example.com')));

        $this->addSite('http://www.example.com', 'http://www.piwik.org'); // main URL and alias URL
        $this->assertSiteUrls(array(1 => array('http://www.example.com'), 2 => array('http://www.example.com', 'http://www.piwik.org')));

        $this->api->addSiteAliasUrls(2, 'http://piwik.org');
        $this->assertSiteUrls(array(1 => array('http://www.example.com'), 2 => array('http://www.example.com', 'http://piwik.org', 'http://www.piwik.org')));

        $this->api->setSiteAliasUrls(2, array());
        $this->assertSiteUrls(array(1 => array('http://www.example.com'), 2 => array('http://www.example.com')));
    }

    public function testGetAllCachedSiteUrls_shouldReturnAnEmptyArray_IfThereAreNoSites()
    {
        $this->assertCachedSiteUrls(array());
    }

    public function testGetAllCachedSiteUrls_ShouldReturnCorrectResultEvenIfItIsCachedAsWeClearTheCacheOnAnyChange()
    {
        $this->addSite('http://www.example.com'); // only one main URL
        $this->assertCachedSiteUrls(array(1 => array('http://www.example.com')));

        $this->addSite('http://www.example.com', 'http://www.piwik.org'); // main URL and alias URL
        $this->assertCachedSiteUrls(array(1 => array('http://www.example.com'), 2 => array('http://www.example.com', 'http://www.piwik.org')));

        $this->api->addSiteAliasUrls(2, 'http://piwik.org');
        $this->assertCachedSiteUrls(array(1 => array('http://www.example.com'), 2 => array('http://www.example.com', 'http://piwik.org', 'http://www.piwik.org')));

        $this->api->setSiteAliasUrls(2, array());
        $this->assertCachedSiteUrls(array(1 => array('http://www.example.com'), 2 => array('http://www.example.com')));

        $this->api->updateSite(1, 'siteName3', array('http://updated.example.com', 'http://2.example.com'));
        $this->assertCachedSiteUrls(array(1 => array('http://updated.example.com', 'http://2.example.com'), 2 => array('http://www.example.com')));
    }

    public function testGetAllCachedSiteUrls_ShouldWriteACacheFile()
    {
        // make sure cache is empty
        $this->assertValueInCache(false);

        $this->addSite('http://www.example.com');
        $this->siteUrls->getAllCachedSiteUrls();

        // make sure we have a cached result
        $this->assertValueInCache(array(1 => array('http://www.example.com')));
    }

    public function test_clearSitesCache_ShouldActuallyDeleteACache()
    {
        $this->addSite('http://www.example.com');
        $this->siteUrls->getAllCachedSiteUrls();

        // make sure we have a cached result
        $this->assertValueInCache(array(1 => array('http://www.example.com')));

        SiteUrls::clearSitesCache();

        // make sure is empty now
        $this->assertValueInCache(false);
    }

    public function testGetAllCachedSiteUrls_ShouldReadFromTheCacheFile()
    {
        $urlsToFake = array(1 => 'Whatever');
        $cache      = $this->buildCache();
        $cache->save('allSiteUrlsPerSite', $urlsToFake, 600);

        $actual = $this->siteUrls->getAllCachedSiteUrls();

        $this->assertEquals($urlsToFake, $actual);
    }

    public function test_groupUrlsByHost_shouldReturnEmptyArray_WhenNoUrlsGiven()
    {
        $this->assertSame(array(), $this->siteUrls->groupUrlsByHost(array()));
        $this->assertSame(array(), $this->siteUrls->groupUrlsByHost(null));
    }

    public function test_groupUrlsByHost_shouldGroupByHost_WithOneSiteAndDifferentDomains_shouldRemoveWwwAndDefaultToPathSlash()
    {
        $idSite = 1;
        $oneSite = array(
            $idSite => array(
                'http://apache.piwik',
                'http://www.example.com',  // should remove www.
                'https://example.org',     // should handle https or other protocol
                'http://apache.piwik/',    // same as initial one but with slash at the end, should not add idsite twice
                'http://third.www.com'     // should not remove www. in the middle of a domain
            )
        );

        $expected = array(
            'apache.piwik'  => array('/' => array($idSite)),
            'example.com'   => array('/' => array($idSite)),
            'example.org'   => array('/' => array($idSite)),
            'third.www.com' => array('/' => array($idSite)),
        );

        $this->assertSame($expected, $this->siteUrls->groupUrlsByHost($oneSite));
    }

    public function test_groupUrlsByHost_shouldGroupByHost_WithDifferentDomainsAndPathsShouldListPathByNumberOfDirectoriesAndConvertToLowerCase()
    {
        $idSite = 1;
        $idSite2 = 2;
        $idSite3 = 3;
        $idSite4 = 4;
        $idSite5 = 5;

        $urls = array(
            $idSite => array(
                'http://apache.piwik/test', 'http://apache.piWik', 'http://apache.piwik/foo/bAr/', 'http://apache.piwik/Foo/SECOND'
            ),
            $idSite2 => array(
                'http://apache.piwik/test/', 'http://example.oRg', 'http://apache.piwik/foo/secOnd'
            ),
            $idSite3 => array(
                'http://apache.piwik/', 'http://apache.piwik/third', 'http://exampLe.com', 'http://example.org/foo/test/two'
            ),
            $idSite4 => array(),
            $idSite5 => array('invalidUrl', 'ftp://example.org/'),
        );

        $expected = array(
            'apache.piwik' => array(
                '/foo/second/' => array($idSite, $idSite2),
                '/foo/bar/' => array($idSite),
                '/third/' => array($idSite3),
                '/test/' => array($idSite, $idSite2),
                '/' => array($idSite, $idSite3)
            ),
            'example.org' => array(
                '/foo/test/two/' => array($idSite3),
                '/' => array($idSite2, $idSite5)
            ),
            'example.com' => array(
                '/' => array($idSite3)
            ),
        );

        $this->assertSame($expected, $this->siteUrls->groupUrlsByHost($urls));
    }

    /**
     * @dataProvider getTestIdSitesMatchingUrl
     */
    public function test_getIdSitesMatchingUrl($expectedMatchSites, $parsedUrl)
    {
        $urlsGroupedByHost = array(
            'apache.piwik' => array(
                '/foo/second/' => array(2),
                '/foo/sec/' => array(4),
                '/foo/bar/' => array(1),
                '/third/' => array(3),
                '/test/' => array(1, 2),
                '/' => array(1, 3)
            ),
            'example.org' => array(
                '/foo/test/two/' => array(3),
                '/foo/second/' => array(6),
                '/' => array(2, 5)
            ),
            'example.com' => array(
                '/' => array(3)
            ),
        );
        $matchedSites = $this->siteUrls->getIdSitesMatchingUrl($parsedUrl, $urlsGroupedByHost);

        $this->assertSame($expectedMatchSites, $matchedSites);
    }

    public function getTestIdSitesMatchingUrl()
    {
        return array(
            array(array(1,3), array('host' => 'apache.piwik')),
            array(array(1,3), array('host' => 'apache.piwik', 'path' => '/')),
            array(array(1,3), array('host' => 'apache.piwik', 'path' => 'nomatch')), // no other URL matches a site so we fall back to domain match
            array(array(1,3), array('host' => 'apache.piwik', 'path' => '/nomatch')),
            array(array(2), array('host' => 'apache.piwik', 'path' => '/foo/second')),
            array(array(2), array('host' => 'apache.piwik', 'path' => '/foo/second/')), // it shouldn't matter if slash is at end or not
            array(array(2), array('host' => 'apache.piwik', 'path' => '/foo/second/test')), // it should find best match
            array(array(4), array('host' => 'apache.piwik', 'path' => '/foo/sec/test')), // it should not use /foo/second for these
            array(array(4), array('host' => 'apache.piwik', 'path' => '/foo/sec/')),
            array(array(4), array('host' => 'apache.piwik', 'path' => '/foo/sec')),
            array(array(1,3), array('host' => 'apache.piwik', 'path' => '/foo')),
            array(array(2,5), array('host' => 'example.org')),
            array(array(2,5), array('host' => 'example.org', 'path' => '/')),
            array(array(2,5), array('host' => 'example.org', 'path' => 'any/nonmatching/path')),
            array(array(6), array('host' => 'example.org', 'path' => '/foo/second')),
            array(array(6), array('host' => 'example.org', 'path' => '/foo/second/test')),
            array(array(3), array('host' => 'example.com')),
            array(null, array('host' => 'example.pro')),
            array(null, array('host' => 'example.pro', 'path' => '/any')),
        );
    }

    /**
     * @dataProvider getTestPathMatchingUrl
     */
    public function test_getPathMatchingUrl($expectedMatchSites, $parsedUrl)
    {
        $urlsGroupedByHost = array(
            'apache.piwik' => array(
                '/foo/second/' => array(2),
                '/foo/sec/' => array(4),
                '/foo/bar/' => array(1),
                '/third/' => array(3),
                '/test/' => array(1, 2),
                '/' => array(1, 3)
            ),
            'example.org' => array(
                '/foo/test/two/' => array(3),
                '/foo/second/' => array(6),
                '/' => array(2, 5)
            ),
        );
        $matchedSites = $this->siteUrls->getPathMatchingUrl($parsedUrl, $urlsGroupedByHost);

        $this->assertSame($expectedMatchSites, $matchedSites);
    }

    public function getTestPathMatchingUrl()
    {
        return array(
            array('/', array('host' => 'apache.piwik')),
            array('/', array('host' => 'apache.piwik', 'path' => '/')),
            array('/', array('host' => 'apache.piwik', 'path' => '')),
            array(null, array('host' => 'test.piwik')),
            array(null, array('host' => 'test.apache.piwik')), // we do not match subdomains, only exact domain match

            array('/foo/bar/', array('host' => 'apache.piwik', 'path' => '/foo/bar')),
            array('/foo/bar/', array('host' => 'apache.piwik', 'path' => '/foo/bar/')),
            array('/foo/bar/', array('host' => 'apache.piwik', 'path' => '/foo/bar/baz/')),
            array('/', array('host' => 'apache.piwik', 'path' => '/foo/baz/bar/')),
            array('/third/', array('host' => 'apache.piwik', 'path' => '/third/bar/baz/')),

            array('/foo/second/', array('host' => 'example.org', 'path' => '/foo/second/')),
            array('/', array('host' => 'example.org', 'path' => '/foo/secon')),
            array(null, array('host' => 'example.pro', 'path' => '/foo/second/')),
        );
    }

    private function assertSiteUrls($expectedUrls)
    {
        $urls = $this->siteUrls->getAllSiteUrls();
        $this->assertEquals($expectedUrls, $urls);
    }

    private function assertCachedSiteUrls($expectedUrls)
    {
        $urls = $this->siteUrls->getAllCachedSiteUrls();
        $this->assertEquals($expectedUrls, $urls);
    }

    private function addSite($urls)
    {
        $this->api->addSite('siteName', func_get_args());
    }

    private function assertValueInCache($value)
    {
        $cache    = $this->buildCache();
        $siteUrls = $cache->fetch('allSiteUrlsPerSite');

        $this->assertEquals($value, $siteUrls);
    }

    private function buildCache()
    {
        return Cache::getLazyCache();
    }
}