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

SearchEngineTest.php « Unit « tests « Referrers « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1b5cab4dbcd5148dd727bde6d9fd8513b1d5dbe8 (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
<?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\Plugins\Referrers\SearchEngine;
use Spyc;

/**
 * @group SearchEngine
 */
class SearchEngineTest extends \PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        // inject definitions to avoid database usage
        $yml = file_get_contents(PIWIK_PATH_TEST_TO_ROOT . SearchEngine::DEFINITION_FILE);
        SearchEngine::getInstance()->loadYmlData($yml);
        parent::setUpBeforeClass();
    }

    public function getSearchEngineUrls()
    {
        return Spyc::YAMLLoad(PIWIK_PATH_TEST_TO_ROOT .'/tests/resources/extractSearchEngineInformationFromUrlTests.yml');
    }

    /**
     * @dataProvider getSearchEngineUrls
     */
    public function testExtractInformationFromUrl($url, $engine, $keywords)
    {
        $returnedValue = SearchEngine::getInstance()->extractInformationFromUrl($url);

        $expectedValue = false;

        if (!empty($engine)) {
            $expectedValue = array('name' => $engine, 'keywords' => $keywords);
        }

        $this->assertEquals($expectedValue, $returnedValue);
    }

    public function testSearchEnginesDefinedCorrectly()
    {
        $searchEngines = array();
        foreach (SearchEngine::getInstance()->getDefinitions() as $host => $info) {
            if (isset($info['backlink']) && $info['backlink'] !== false) {
                $this->assertTrue(strrpos($info['backlink'], "{k}") !== false, $host . " search URL is not defined correctly, must contain the macro {k}");
            }

            if (!array_key_exists($info['name'], $searchEngines)) {
                $searchEngines[$info['name']] = true;

                $this->assertTrue(strpos($host, '{}') === false, $host . " search URL is the master record and should not contain {}");
            }

            if (isset($info['charsets']) && $info['charsets'] !== false) {
                $this->assertTrue(is_array($info['charsets']) || is_string($info['charsets']), $host . ' charsets must be either a string or an array');

                if (is_string($info['charsets'])) {
                    $this->assertTrue(trim($info['charsets']) !== '', $host . ' charsets cannot be an empty string');
                    $this->assertTrue(strpos($info['charsets'], ' ') === false, $host . ' charsets cannot contain spaces');

                }

                if (is_array($info['charsets'])) {
                    $this->assertTrue(count($info['charsets']) > 0, $host . ' charsets cannot be an empty array');
                    $this->assertTrue(strpos(serialize($info['charsets']), '""') === false, $host . ' charsets in array cannot be empty stringss');
                    $this->assertTrue(strpos(serialize($info['charsets']), ' ') === false, $host . ' charsets in array cannot contain spaces');
                }
            }
        }
    }

    /**
     * Dataprovider for testGetBackLinkFromUrlAndKeyword
     */
    public function getBackLinkFromUrlAndKeywordTestData()
    {
        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
     *
     * @dataProvider getBackLinkFromUrlAndKeywordTestData
     */
    public function testGetBackLinkFromUrlAndKeyword($url, $keyword, $expected)
    {
        $this->assertEquals($expected, SearchEngine::getInstance()->getBackLinkFromUrlAndKeyword($url, $keyword));
    }

    /**
     * Dataprovider serving all search engine data
     */
    public function getAllSearchEngines()
    {
        $yml = file_get_contents(PIWIK_PATH_TEST_TO_ROOT . SearchEngine::DEFINITION_FILE);
        SearchEngine::getInstance()->loadYmlData($yml);
        $searchEngines = array();
        foreach (SearchEngine::getInstance()->getDefinitions() as $url => $searchEngine) {
            $searchEngines[] = array($url, $searchEngine);
        }
        return $searchEngines;
    }

    /**
     * search engine has at least one keyword
     *
     * @dataProvider getAllSearchEngines
     */
    public function testMissingSearchEngineKeyword($url, $searchEngine)
    {
        $name = parse_url('http://' . $url);
        $this->assertTrue(!empty($searchEngine['params']) || !empty($searchEngine['hiddenkeyword']), $name['host']);
    }

    /**
     * search engine is defined but there's no favicon
     *
     * @dataProvider getAllSearchEngines
     */
    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['name'], $searchEngines)) {
            $searchEngines[$searchEngine['name']] = $url;

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

    /**
     * favicon exists but there's no corresponding search engine defined
     */
    public function testObsoleteSearchEngineIcons()
    {
        // Get list of search engines and first appearing URL
        $searchEngines = array();
        foreach (SearchEngine::getInstance()->getDefinitions() 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);
        }
    }
}