diff options
author | sgiehl <stefan@piwik.org> | 2013-10-25 17:30:46 +0400 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2013-10-25 17:30:46 +0400 |
commit | 48fc1af5ea76de8a77237d10b1933c6c12f4cc40 (patch) | |
tree | 2a4f551c622947fd266d4dab716f17c0d2cd6f62 /tests | |
parent | 4ab8a6190ee6cb4d33cf99fc579be86606cc07fa (diff) |
moved urlhelper tests to its own test file
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Core/CommonTest.php | 174 | ||||
-rw-r--r-- | tests/PHPUnit/Core/UrlHelperTest.php | 187 | ||||
-rw-r--r-- | tests/resources/extractSearchEngineInformationFromUrlTests.yml | 14 |
3 files changed, 201 insertions, 174 deletions
diff --git a/tests/PHPUnit/Core/CommonTest.php b/tests/PHPUnit/Core/CommonTest.php index ad8647ab71..7df50fbac9 100644 --- a/tests/PHPUnit/Core/CommonTest.php +++ b/tests/PHPUnit/Core/CommonTest.php @@ -1,7 +1,6 @@ <?php use Piwik\Common; use Piwik\Filesystem; -use Piwik\UrlHelper; /** * Piwik - Open source web analytics @@ -12,42 +11,6 @@ use Piwik\UrlHelper; class Core_CommonTest extends PHPUnit_Framework_TestCase { /** - * Dataprovider for testIsUrl - */ - public function getUrls() - { - return array( - // valid urls - array('http://piwik.org', true), - array('http://www.piwik.org', true), - array('https://piwik.org', true), - array('https://piwik.org/dir/dir2/?oeajkgea7aega=&ge=a', true), - array('ftp://www.pi-wik.org', true), - array('news://www.pi-wik.org', true), - array('https://www.tëteâ.org', true), - array('http://汉语/漢語.cn', true), //chinese - // invalid urls - array('it doesnt look like url', false), - array('/index?page=test', false), - array('test.html', false), - array('/\/\/\/\/\/\\\http://test.com////', false), - array('jmleslangues.php', false), - array('http://', false), - array(' http://', false), - array('testhttp://test.com', false), - ); - } - - /** - * @dataProvider getUrls - * @group Core - */ - public function testIsUrl($url, $isValid) - { - $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url)); - } - - /** * Dataprovider for testSanitizeInputValues */ public function getInputValues() @@ -275,84 +238,6 @@ class Core_CommonTest extends PHPUnit_Framework_TestCase } /** - * Dataprovider for testGetParameterFromQueryString - */ - public function getQueryStrings() - { - return array( // querystring, parameter, expected value - array('x=1', 'x', '1'), - array('?x=1', 'x', '1'), - array('?x=y==1', 'x', 'y==1'), - array('x[]=', 'x', array('')), - array('x[]=1', 'x', array('1')), - array('x[]=y==1', 'x', array('y==1')), - array('?x[]=1&x[]=2', 'x', array('1', '2')), - array('?x%5b%5d=3&x[]=4', 'x', array('3', '4')), - array('?x%5B]=5&x[%5D=6', 'x', array('5', '6')), - array('toto=mama&mama=&tuytyt=teaoi&toto=mama second value', 'tuytyt', 'teaoi'), - - // don't unescape the value, otherwise it becomes - // ?x[]=A&y=1 - array('?x%5B%5D=A%26y%3D1', 'x', array('A%26y%3D1')), - // ?z=y&x[]=1 - array('?z=y%26x%5b%5d%3d1', 'x', null), - - // strange characters - array('toto=mama&mama=&tuytyt=Поиск в Интернете Поиск страниц на русском _*()!$!£$^!£$%&toto=mama second value', 'tuytyt', 'Поиск в Интернете Поиск страниц на русском _*()!$!£$^!£$%'), - - // twice the parameter => returns the last value in the url - array('toto=mama&mama=&tuytyt=teaoi&toto=mama second value', 'toto', 'mama second value'), - - // empty param - array('toto=mama&mama=&tuytyt=teaoi', 'mama', ''), - - // missing parameter value => returns false - array('x', 'x', false), - array('toto=mama&mama&tuytyt=teaoi', 'mama', false), - - // param not found => null - array('toto=mama&mama=titi', 'tot', null), - - // empty query string => null - array('', 'test', null), - ); - } - - /** - * @dataProvider getQueryStrings - * @group Core - */ - public function testGetParameterFromQueryString($queryString, $parameter, $expected) - { - $this->assertSame($expected, UrlHelper::getParameterFromQueryString($queryString, $parameter)); - } - - /** - * @group Core - */ - public function testGetPathAndQueryFromUrl() - { - $this->assertEquals('test/index.php?module=CoreHome', UrlHelper::getPathAndQueryFromUrl('http://piwik.org/test/index.php?module=CoreHome')); - } - - /** - * @group Core - */ - public function testGetArrayFromQueryString() - { - $expected = array( - 'a' => false, - 'b' => '', - 'c' => '1', - 'd' => array(false), - 'e' => array(''), - 'f' => array('a'), - 'g' => array('b', 'c'), - ); - $this->assertEquals(serialize($expected), serialize(UrlHelper::getArrayFromQueryString('a&b=&c=1&d[]&e[]=&f[]=a&g[]=b&g[]=c'))); - } - - /** * @group Core */ public function testIsValidFilenameValidValues() @@ -568,63 +453,4 @@ class Core_CommonTest extends PHPUnit_Framework_TestCase } } - /** - * Dataprovider for testExtractSearchEngineInformationFromUrl - */ - public function getSearchEngineUrls() - { - return Spyc::YAMLLoad(PIWIK_PATH_TEST_TO_ROOT .'/tests/resources/extractSearchEngineInformationFromUrlTests.yml'); - } - - /** - * @dataProvider getSearchEngineUrls - * @group Core - */ - public function testExtractSearchEngineInformationFromUrl($url, $engine, $keywords) - { - $this->includeDataFilesForSearchEngineTest(); - $returnedValue = UrlHelper::extractSearchEngineInformationFromUrl($url); - - $exptectedValue = false; - - if (!empty($engine)) { - $exptectedValue = array('name' => $engine, 'keywords' => $keywords); - } - - $this->assertEquals($exptectedValue, $returnedValue); - } - - /** - * Dataprovider for testGetLossyUrl - */ - public function getLossyUrls() - { - return array( - array('example.com', 'example.com'), - array('m.example.com', 'example.com'), - array('www.example.com', 'example.com'), - array('search.example.com', 'example.com'), - array('example.ca', 'example.{}'), - array('us.example.com', '{}.example.com'), - array('www.m.example.ca', 'example.{}'), - array('www.google.com.af', 'google.{}'), - array('www.google.co.uk', 'google.{}'), - array('images.de.ask.com', 'images.{}.ask.com'), - ); - } - - /** - * @dataProvider getLossyUrls - * @group Core - */ - public function testGetLossyUrl($input, $expected) - { - $this->assertEquals($expected, UrlHelper::getLossyUrl($input)); - } - - private function includeDataFilesForSearchEngineTest() - { - include "DataFiles/SearchEngines.php"; - include "DataFiles/Countries.php"; - } } diff --git a/tests/PHPUnit/Core/UrlHelperTest.php b/tests/PHPUnit/Core/UrlHelperTest.php new file mode 100644 index 0000000000..aa223335db --- /dev/null +++ b/tests/PHPUnit/Core/UrlHelperTest.php @@ -0,0 +1,187 @@ +<?php
+use Piwik\UrlHelper;
+
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+class Core_CommonTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Dataprovider for testIsUrl
+ */
+ public function getUrls()
+ {
+ return array(
+ // valid urls
+ array('http://piwik.org', true),
+ array('http://www.piwik.org', true),
+ array('https://piwik.org', true),
+ array('https://piwik.org/dir/dir2/?oeajkgea7aega=&ge=a', true),
+ array('ftp://www.pi-wik.org', true),
+ array('news://www.pi-wik.org', true),
+ array('https://www.tëteâ.org', true),
+ array('http://汉语/漢語.cn', true), //chinese
+ // invalid urls
+ array('it doesnt look like url', false),
+ array('/index?page=test', false),
+ array('test.html', false),
+ array('/\/\/\/\/\/\\\http://test.com////', false),
+ array('jmleslangues.php', false),
+ array('http://', false),
+ array(' http://', false),
+ array('testhttp://test.com', false),
+ );
+ }
+
+ /**
+ * @dataProvider getUrls
+ * @group Core
+ */
+ public function testIsUrl($url, $isValid)
+ {
+ $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url));
+ }
+
+ /**
+ * Dataprovider for testGetParameterFromQueryString
+ */
+ public function getQueryStrings()
+ {
+ return array( // querystring, parameter, expected value
+ array('x=1', 'x', '1'),
+ array('?x=1', 'x', '1'),
+ array('?x=y==1', 'x', 'y==1'),
+ array('x[]=', 'x', array('')),
+ array('x[]=1', 'x', array('1')),
+ array('x[]=y==1', 'x', array('y==1')),
+ array('?x[]=1&x[]=2', 'x', array('1', '2')),
+ array('?x%5b%5d=3&x[]=4', 'x', array('3', '4')),
+ array('?x%5B]=5&x[%5D=6', 'x', array('5', '6')),
+ array('toto=mama&mama=&tuytyt=teaoi&toto=mama second value', 'tuytyt', 'teaoi'),
+
+ // don't unescape the value, otherwise it becomes
+ // ?x[]=A&y=1
+ array('?x%5B%5D=A%26y%3D1', 'x', array('A%26y%3D1')),
+ // ?z=y&x[]=1
+ array('?z=y%26x%5b%5d%3d1', 'x', null),
+
+ // strange characters
+ array('toto=mama&mama=&tuytyt=Поиск в Интернете Поиск страниц на русском _*()!$!£$^!£$%&toto=mama second value', 'tuytyt', 'Поиск в Интернете Поиск страниц на русском _*()!$!£$^!£$%'),
+
+ // twice the parameter => returns the last value in the url
+ array('toto=mama&mama=&tuytyt=teaoi&toto=mama second value', 'toto', 'mama second value'),
+
+ // empty param
+ array('toto=mama&mama=&tuytyt=teaoi', 'mama', ''),
+
+ // missing parameter value => returns false
+ array('x', 'x', false),
+ array('toto=mama&mama&tuytyt=teaoi', 'mama', false),
+
+ // param not found => null
+ array('toto=mama&mama=titi', 'tot', null),
+
+ // empty query string => null
+ array('', 'test', null),
+ );
+ }
+
+ /**
+ * @dataProvider getQueryStrings
+ * @group Core
+ */
+ public function testGetParameterFromQueryString($queryString, $parameter, $expected)
+ {
+ $this->assertSame($expected, UrlHelper::getParameterFromQueryString($queryString, $parameter));
+ }
+
+ /**
+ * @group Core
+ */
+ public function testGetPathAndQueryFromUrl()
+ {
+ $this->assertEquals('test/index.php?module=CoreHome', UrlHelper::getPathAndQueryFromUrl('http://piwik.org/test/index.php?module=CoreHome'));
+ }
+
+ /**
+ * @group Core
+ */
+ public function testGetArrayFromQueryString()
+ {
+ $expected = array(
+ 'a' => false,
+ 'b' => '',
+ 'c' => '1',
+ 'd' => array(false),
+ 'e' => array(''),
+ 'f' => array('a'),
+ 'g' => array('b', 'c'),
+ );
+ $this->assertEquals(serialize($expected), serialize(UrlHelper::getArrayFromQueryString('a&b=&c=1&d[]&e[]=&f[]=a&g[]=b&g[]=c')));
+ }
+
+ /**
+ * Dataprovider for testExtractSearchEngineInformationFromUrl
+ */
+ public function getSearchEngineUrls()
+ {
+ return Spyc::YAMLLoad(PIWIK_PATH_TEST_TO_ROOT .'/tests/resources/extractSearchEngineInformationFromUrlTests.yml');
+ }
+
+ /**
+ * @dataProvider getSearchEngineUrls
+ * @group Core
+ */
+ public function testExtractSearchEngineInformationFromUrl($url, $engine, $keywords)
+ {
+ $this->includeDataFilesForSearchEngineTest();
+ $returnedValue = UrlHelper::extractSearchEngineInformationFromUrl($url);
+
+ $exptectedValue = false;
+
+ if (!empty($engine)) {
+ $exptectedValue = array('name' => $engine, 'keywords' => $keywords);
+ }
+
+ $this->assertEquals($exptectedValue, $returnedValue);
+ }
+
+ /**
+ * Dataprovider for testGetLossyUrl
+ */
+ public function getLossyUrls()
+ {
+ return array(
+ array('example.com', 'example.com'),
+ array('m.example.com', 'example.com'),
+ array('www.example.com', 'example.com'),
+ array('search.example.com', 'example.com'),
+ array('example.ca', 'example.{}'),
+ array('us.example.com', '{}.example.com'),
+ array('www.m.example.ca', 'example.{}'),
+ array('www.google.com.af', 'google.{}'),
+ array('www.google.co.uk', 'google.{}'),
+ array('images.de.ask.com', 'images.{}.ask.com'),
+ );
+ }
+
+ /**
+ * @dataProvider getLossyUrls
+ * @group Core
+ */
+ public function testGetLossyUrl($input, $expected)
+ {
+ $this->assertEquals($expected, UrlHelper::getLossyUrl($input));
+ }
+
+
+
+ private function includeDataFilesForSearchEngineTest()
+ {
+ include "DataFiles/SearchEngines.php";
+ include "DataFiles/Countries.php";
+ }
+}
\ No newline at end of file diff --git a/tests/resources/extractSearchEngineInformationFromUrlTests.yml b/tests/resources/extractSearchEngineInformationFromUrlTests.yml index c4309205cb..1832657c97 100644 --- a/tests/resources/extractSearchEngineInformationFromUrlTests.yml +++ b/tests/resources/extractSearchEngineInformationFromUrlTests.yml @@ -7,6 +7,11 @@ # @category Piwik_Plugins ############### +# empty url +- url: '' + engine: false + keywords: false + # normal case - url: 'http://uk.search.yahoo.com/search?p=piwik&ei=UTF-8&fr=moz2' engine: 'Yahoo!' @@ -162,6 +167,11 @@ engine: 'Google' keywords: 'piwik security' +# Google Video +- url: 'https://www.google.com/search?tbm=vid&hl=de&source=hp&biw=1920&bih=1099&q=piwik&gbv=2&oq=piwik&gs_l=video-hp.3..0l5.2388.2959.0.3120.5.4.0.1.1.0.72.230.4.4.0....0...1ac.1.24.video-hp..0.5.243.6qReggj7ElY' + engine: 'Google Video' + keywords: 'piwik' + # Bing (subdomains) - url: 'http://ca.bing.com/search?q=piwik+web+analytics&go=&form=QBLH&filt=all&qs=n&sk=' engine: 'Bing' @@ -247,6 +257,10 @@ engine: 'Yahoo!' keywords: 'piwik' +- url: 'http://de.images.search.yahoo.com/search/images;_ylt=A0PDode7bWpSCSIAgtM0CQx.?p=piwik&ei=utf-8&iscqry=&fr=sfp' + engine: 'Yahoo! Images' + keywords: 'piwik' + # Babylon - url: 'http://search.babylon.com/?q=piwik' engine: 'Babylon' |