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: 039bc1fc6eb2b8523f68d815a9c1b084c48af243 (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
<?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 function getSearchEngineUrls()
    {
        return Spyc::YAMLLoad(PIWIK_PATH_TEST_TO_ROOT .'/tests/resources/extractSearchEngineInformationFromUrlTests.yml');
    }

    public static function setUpBeforeClass()
    {
        // inject definitions to avoid database usage
        $yml = file_get_contents(PIWIK_INCLUDE_PATH . SearchEngine::DEFINITION_FILE);
        SearchEngine::getInstance()->loadYmlData($yml);

        parent::setUpBeforeClass();
    }

    /**
     * @dataProvider getSearchEngineUrls
     * @group Core
     */
    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()->getSearchEngineDefinitions() 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));
    }
}