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

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

use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Period;

require_once PIWIK_INCLUDE_PATH . '/plugins/Referrers/Referrers.php';

class ReferrersTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Dataprovider serving all search engine data
     */
    public function getSearchEngines()
    {
        include PIWIK_PATH_TEST_TO_ROOT . '/core/DataFiles/SearchEngines.php';

        $searchEngines = array();
        foreach ($GLOBALS['Piwik_SearchEngines'] as $url => $searchEngine) {
            $searchEngines[] = array($url, $searchEngine);
        }
        return $searchEngines;
    }

    /**
     * search engine has at least one keyword
     *
     * @group Plugins
     *
     * @dataProvider getSearchEngines
     */
    public function testMissingSearchEngineKeyword($url, $searchEngine)
    {
        // Get list of search engines and first appearing URL
        static $searchEngines = array();

        $name = parse_url('http://' . $url);
        if (!array_key_exists($searchEngine[0], $searchEngines)) {
            $searchEngines[$searchEngine[0]] = $url;

            $this->assertTrue(!empty($searchEngine[1]), $name['host']);
        }
    }

    /**
     * search engine is defined in DataFiles/SearchEngines.php but there's no favicon
     *
     * @group Plugins
     *
     * @dataProvider getSearchEngines
     */
    public function testMissingSearchEngineIcons($url, $searchEngine)
    {
        // Get list of existing favicons
        $favicons = scandir(PIWIK_PATH_TEST_TO_ROOT . '/plugins/Referrers/images/searchEngines/');

        // Get list of search engines and first appearing URL
        static $searchEngines = array();

        $name = parse_url('http://' . $url);
        if (!array_key_exists($searchEngine[0], $searchEngines)) {
            $searchEngines[$searchEngine[0]] = $url;

            $this->assertTrue(in_array($name['host'] . '.png', $favicons), $name['host']);
        }
    }

    /**
     * favicon exists but there's no corresponding search engine defined in DataFiles/SearchEngines.php
     *
     * @group Plugins
     */
    public function testObsoleteSearchEngineIcons()
    {
        include PIWIK_PATH_TEST_TO_ROOT . '/core/DataFiles/SearchEngines.php';

        // Get list of search engines and first appearing URL
        $searchEngines = array();
        foreach ($GLOBALS['Piwik_SearchEngines'] as $url => $searchEngine) {
            $name = parse_url('http://' . $url);
            if (!array_key_exists($name['host'], $searchEngines)) {
                $searchEngines[$name['host']] = true;
            }
        }

        // Get list of existing favicons
        $favicons = scandir(PIWIK_PATH_TEST_TO_ROOT . '/plugins/Referrers/images/searchEngines/');
        foreach ($favicons as $name) {
            if ($name[0] == '.' || strpos($name, 'xx.') === 0) {
                continue;
            }

            $host = substr($name, 0, -4);
            $this->assertTrue(array_key_exists($host, $searchEngines), $host);
        }
    }

    /**
     * get search engine host from url
     *
     * @group Plugins
     */
    public function testGetSearchEngineHostFromUrl()
    {
        $data = array(
            'http://www.google.com/cse' => array('www.google.com', 'www.google.com/cse'),
            'http://www.google.com'     => array('www.google.com', 'www.google.com'),
        );

        foreach ($data as $url => $expected) {
            $this->assertEquals($expected[0], \Piwik\Plugins\Referrers\getSearchEngineHostFromUrl($url));
            $this->assertEquals($expected[1], \Piwik\Plugins\Referrers\getSearchEngineHostPathFromUrl($url));
        }
    }

    /**
     * Dataprovider for testGetSearchEngineUrlFromUrlAndKeyword
     */
    public function getSearchEngineUrlFromUrlAndKeywordTestData()
    {
        return array(
            array('http://apollo.lv/portal/search/', 'piwik', 'http://apollo.lv/portal/search/?cof=FORID%3A11&q=piwik&search_where=www'),
            array('http://bing.com/images/search', 'piwik', 'http://bing.com/images/search/?q=piwik'),
            array('http://google.com', 'piwik', 'http://google.com/search?q=piwik'),
        );
    }

    /**
     * get search engine url from name and keyword
     *
     * @group Plugins
     *
     * @dataProvider getSearchEngineUrlFromUrlAndKeywordTestData
     */
    public function testGetSearchEngineUrlFromUrlAndKeyword($url, $keyword, $expected)
    {
        include PIWIK_PATH_TEST_TO_ROOT . '/core/DataFiles/SearchEngines.php';
        $this->assertEquals($expected, \Piwik\Plugins\Referrers\getSearchEngineUrlFromUrlAndKeyword($url, $keyword));
    }

    /**
     * Dataprovider for getSocialNetworkFromDomainTestData
     */
    public function getSocialNetworkFromDomainTestData()
    {
        return array(
            array('http://www.facebook.com', 'Facebook'),
            array('http://www.facebook.com/piwik', 'Facebook'),
            array('http://m.facebook.com', 'Facebook'),
            array('https://m.facebook.com', 'Facebook'),
            array('m.facebook.com', 'Facebook'),
            array('http://lastfm.com.tr', 'Last.fm'),
            array('http://t.co/test', 'Twitter'),
            array('http://xxt.co/test', \Piwik\Piwik::translate('General_Unknown')),
            array('asdfasdfadsf.com', \Piwik\Piwik::translate('General_Unknown')),
            array('http://xwayn.com', \Piwik\Piwik::translate('General_Unknown')),
            array('http://live.com/test', \Piwik\Piwik::translate('General_Unknown')),
        );
    }

    /**
     * @group Plugins
     *
     * @dataProvider getSocialNetworkFromDomainTestData
     */
    public function testGetSocialNetworkFromDomain($url, $expected)
    {
        include PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php';
        $this->assertEquals($expected, \Piwik\Plugins\Referrers\getSocialNetworkFromDomain($url));
    }

    public function getSocialsLogoFromUrlTestData()
    {
        return array(
            array('http://www.facebook.com', 'facebook.com.png'),
            array('www.facebook.com', 'facebook.com.png',),
            array('http://lastfm.com.tr', 'last.fm.png'),
            array('http://asdfasdf.org/test', 'xx.png'),
            array('http://www.google.com', 'xx.png'),
        );
    }

    /**
     * @group Plugins
     *
     * @dataProvider getSocialsLogoFromUrlTestData
     */
    public function testGetSocialsLogoFromUrl($url, $expected)
    {
        include PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php';
        $this->assertContains($expected, \Piwik\Plugins\Referrers\getSocialsLogoFromUrl($url));
    }

    public function isSocialUrlTestData()
    {
        return array(
            array('http://www.facebook.com', 'Facebook', true),
            array('http://www.facebook.com', 'Twitter', false),
            array('http://m.facebook.com', false, true),
            array('http://lastfm.com.tr', 'Last.fm', true),
            array('http://asdfasdf.org/test', false, false),
            array('http://asdfasdf.com/test', 'Facebook', false),
        );
    }

    /**
     * @group Plugins
     *
     * @dataProvider isSocialUrlTestData
     */
    public function testIsSocialUrl($url, $assumedSocial, $expected)
    {
        include PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php';
        $this->assertEquals($expected, \Piwik\Plugins\Referrers\isSocialUrl($url, $assumedSocial));
    }

    public function removeUrlProtocolTestData()
    {
        return array(
            array('http://www.facebook.com', 'www.facebook.com'),
            array('https://bla.fr', 'bla.fr'),
            array('ftp://bla.fr', 'bla.fr'),
            array('udp://bla.fr', 'bla.fr'),
            array('bla.fr', 'bla.fr'),
            array('ASDasdASDDasd', 'ASDasdASDDasd'),
        );
    }

    /**
     * @group Plugins
     *
     * @dataProvider removeUrlProtocolTestData
     */
    public function testRemoveUrlProtocol($url, $expected)
    {
        $this->assertEquals($expected, \Piwik\Plugins\Referrers\removeUrlProtocol($url));
    }
}