diff options
author | sgiehl <stefan@matomo.org> | 2022-02-25 17:52:19 +0300 |
---|---|---|
committer | sgiehl <stefan@matomo.org> | 2022-02-25 17:52:19 +0300 |
commit | fd272e63affedce7db96075f71b33697eba85bf2 (patch) | |
tree | 6871c01214f8a9fa1f48dfda1fffeafa4c834808 | |
parent | dcfa2d2c21ae14b8c47773418d32a3a83eb3514d (diff) |
restructure serializing testsremove-upgradephp
-rw-r--r-- | tests/PHPUnit/Unit/CommonTest.php | 449 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/CookieTest.php | 176 |
2 files changed, 311 insertions, 314 deletions
diff --git a/tests/PHPUnit/Unit/CommonTest.php b/tests/PHPUnit/Unit/CommonTest.php index d01315778e..1438bcaadd 100644 --- a/tests/PHPUnit/Unit/CommonTest.php +++ b/tests/PHPUnit/Unit/CommonTest.php @@ -2,7 +2,7 @@ /** * Matomo - free/libre analytics platform * - * @link https://matomo.org + * @link https://matomo.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -20,7 +20,7 @@ use Piwik\Tests\Framework\Mock\FakeLogger; /** * @backupGlobals enabled - * @group Common + * @group Core */ class CommonTest extends TestCase { @@ -49,75 +49,82 @@ class CommonTest extends TestCase */ public function getInputValues() { - return array( // input, output + return [ + // input, output // sanitize an array OK - array( - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52), - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52) - ), - array( - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52, - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52), - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52), - array(array(array(array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52))) - )), - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52, - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52), - array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52), - array(array(array(array('test1' => 't1', 't45', "teatae", 4568, array('test'), 1.52))) - )) - ), + [ + ['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52], + ['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52], + ], + [ + [ + 'test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52, + ['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52], + ['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52], + [ + [[['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52]]], + ], + ], + [ + 'test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52, + ['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52], + ['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52], + [ + [[['test1' => 't1', 't45', "teatae", 4568, ['test'], 1.52]]], + ], + ], + ], // sanitize an array with bad value level1 - array( - array('test1' => 't1', 't45', 'tea1"ta"e', 568, 1 => array('t<e"st'), 1.52), - array('test1' => 't1', 't45', 'tea1"ta"e', 568, 1 => array('t<e"st'), 1.52) - ), + [ + ['test1' => 't1', 't45', 'tea1"ta"e', 568, 1 => ['t<e"st'], 1.52], + ['test1' => 't1', 't45', 'tea1"ta"e', 568, 1 => ['t<e"st'], 1.52], + ], // sanitize an array with bad value level2 - array( - array('tea1"ta"e' => array('t<e"st' => array('tgeag454554"t')), 1.52), - array('tea1"ta"e' => array('t<e"st' => array('tgeag454554"t')), 1.52) - ), + [ + ['tea1"ta"e' => ['t<e"st' => ['tgeag454554"t']], 1.52], + ['tea1"ta"e' => ['t<e"st' => ['tgeag454554"t']], 1.52], + ], // sanitize a string unicode => no change - array( + [ " Поиск в Интернете Поgqegиск страниц на рgeqg8978усском", - " Поиск в Интернете Поgqegиск страниц на рgeqg8978усском" - ), + " Поиск в Интернете Поgqegиск страниц на рgeqg8978усском", + ], // sanitize a bad string - array( + [ '& " < > 123abc\'', - '& " < > 123abc'' - ), + '& " < > 123abc'', + ], // test filter - expect new line and null byte to be filtered out - array( + [ "Null\0Byte", - 'NullByte' - ), + 'NullByte', + ], // double encoded - no change (document as user error) - array( + [ + '%48%45%4C%00%4C%4F+%57%4F%52%4C%44', '%48%45%4C%00%4C%4F+%57%4F%52%4C%44', - '%48%45%4C%00%4C%4F+%57%4F%52%4C%44' - ), + ], // sanitize an integer - array('121564564', '121564564'), - array('121564564.0121', '121564564.0121'), - array(121564564.0121, 121564564.0121), - array(12121, 12121), + ['121564564', '121564564'], + ['121564564.0121', '121564564.0121'], + [121564564.0121, 121564564.0121], + [12121, 12121], // sanitize HTML - array( + [ "<test toto='mama' piwik=\"cool\">Piwik!!!!!</test>", - "<test toto='mama' piwik="cool">Piwik!!!!!</test>" - ), + "<test toto='mama' piwik="cool">Piwik!!!!!</test>", + ], // sanitize a SQL query - array( + [ "SELECT piwik FROM piwik_tests where test= 'super\"value' AND cool=toto #comment here", - "SELECT piwik FROM piwik_tests where test= 'super"value' AND cool=toto #comment here" - ), + "SELECT piwik FROM piwik_tests where test= 'super"value' AND cool=toto #comment here", + ], // sanitize php variables - array(true, true), - array(false, false), - array(null, null), - array("", ""), - ); + [true, true], + [false, false], + [null, null], + ["", ""], + ]; } /** @@ -160,14 +167,14 @@ class CommonTest extends TestCase public function testGetRequestVar_GetStringFloatGiven() { $_GET['test'] = 1413.431413; - $value = Common::getRequestVar('test', null, 'string'); + $value = Common::getRequestVar('test', null, 'string'); $this->assertEquals('1413.431413', $value); } public function testGetRequestVar_GetStringIntegerGiven() { $_GET['test'] = 1413; - $value = Common::getRequestVar('test', null, 'string'); + $value = Common::getRequestVar('test', null, 'string'); $this->assertEquals('1413', $value); } @@ -196,47 +203,46 @@ class CommonTest extends TestCase */ public function getRequestVarValues() { - return array( // value of request var, default value, var type, expected - array(1413.431413, 2, 'int', 2), // withdefault Withtype WithValue => value casted as type - array(null, 'default', null, 'default'), // withdefault Notype NoValue => default value - array(null, 'default', 'string', 'default'), // withdefault Withtype NoValue =>default value casted as type - // integer as a default value / types - array('', 45, 'int', 45), - array(1413.431413, 45, 'int', 45), - array('', 45, 'integer', 45), - array('', 45.0, 'float', 45.0), - array('', 45.25, 'float', 45.25), - // string as a default value / types - array('1413.431413', 45, 'int', 45), - array('1413.431413', 45, 'string', '1413.431413'), - array('', 45, 'string', '45'), - array('', 'geaga', 'string', 'geaga'), - array('', ''}{}}{}{}'', 'string', ''}{}}{}{}''), - array('', 'http://url?arg1=val1&arg2=val2', 'string', 'http://url?arg1=val1&arg2=val2'), - array('http://url?arg1=val1&arg2=val2', 'http://url?arg1=val1&arg2=val4', 'string', 'http://url?arg1=val1&arg2=val2'), - array(array("test", 1345524, array("gaga")), array(), 'array', array("test", 1345524, array("gaga"))), // array as a default value / types - array(array("test", 1345524, array("gaga")), 45, 'string', "45"), - array(array("test", 1345524, array("gaga")), array(1), 'array', array("test", 1345524, array("gaga"))), - array(array("test", 1345524, "Start of hello\nworld\n\t", array("gaga")), array(1), 'array', array("test", 1345524, "Start of hello\nworld\n\t", array("gaga"))), - array(array("test", 1345524, array("gaga")), 4, 'int', 4), - array('', array(1), 'array', array(1)), - array('', array(), 'array', array()), - // we give a number in a string and request for a number => it should give the string casted as a number - array('45645646', 1, 'int', 45645646), - array('45645646', 45, 'integer', 45645646), - array('45645646', '45454', 'string', '45645646'), - array('45645646', array(), 'array', array()), - ); + return [ // value of request var, default value, var type, expected + [1413.431413, 2, 'int', 2], // withdefault Withtype WithValue => value casted as type + [null, 'default', null, 'default'], // withdefault Notype NoValue => default value + [null, 'default', 'string', 'default'], // withdefault Withtype NoValue =>default value casted as type + // integer as a default value / types + ['', 45, 'int', 45], + [1413.431413, 45, 'int', 45], + ['', 45, 'integer', 45], + ['', 45.0, 'float', 45.0], + ['', 45.25, 'float', 45.25], + // string as a default value / types + ['1413.431413', 45, 'int', 45], + ['1413.431413', 45, 'string', '1413.431413'], + ['', 45, 'string', '45'], + ['', 'geaga', 'string', 'geaga'], + ['', ''}{}}{}{}'', 'string', ''}{}}{}{}''], + ['', 'http://url?arg1=val1&arg2=val2', 'string', 'http://url?arg1=val1&arg2=val2'], + ['http://url?arg1=val1&arg2=val2', 'http://url?arg1=val1&arg2=val4', 'string', 'http://url?arg1=val1&arg2=val2'], + [["test", 1345524, ["gaga"]], [], 'array', ["test", 1345524, ["gaga"]]], // array as a default value / types + [["test", 1345524, ["gaga"]], 45, 'string', "45"], + [["test", 1345524, ["gaga"]], [1], 'array', ["test", 1345524, ["gaga"]]], + [["test", 1345524, "Start of hello\nworld\n\t", ["gaga"]], [1], 'array', ["test", 1345524, "Start of hello\nworld\n\t", ["gaga"]]], + [["test", 1345524, ["gaga"]], 4, 'int', 4], + ['', [1], 'array', [1]], + ['', [], 'array', []], + // we give a number in a string and request for a number => it should give the string casted as a number + ['45645646', 1, 'int', 45645646], + ['45645646', 45, 'integer', 45645646], + ['45645646', '45454', 'string', '45645646'], + ['45645646', [], 'array', []], + ]; } /** * @dataProvider getRequestVarValues - * @group Core */ public function testGetRequestVar($varValue, $default, $type, $expected) { $_GET['test'] = $varValue; - $return = Common::getRequestVar('test', $default, $type); + $return = Common::getRequestVar('test', $default, $type); $this->assertEquals($expected, $return); // validate correct type switch ($type) { @@ -258,13 +264,13 @@ class CommonTest extends TestCase public function testIsValidFilenameValidValues() { - $valid = array( + $valid = [ "test", "test.txt", "test.......", "en-ZHsimplified", '0', - ); + ]; foreach ($valid as $toTest) { $this->assertTrue(Filesystem::isValidFilename($toTest), $toTest . " not valid!"); } @@ -272,7 +278,7 @@ class CommonTest extends TestCase public function testIsValidFilenameNotValidValues() { - $notvalid = array( + $notvalid = [ "../test", "/etc/htpasswd", '$var', @@ -283,7 +289,7 @@ class CommonTest extends TestCase ".htaccess", "very long long eogaioge ageja geau ghaeihieg heiagie aiughaeui hfilename", "WHITE SPACE", - ); + ]; foreach ($notvalid as $toTest) { self::assertFalse(Filesystem::isValidFilename($toTest), $toTest . " valid but shouldn't!"); } @@ -292,29 +298,122 @@ class CommonTest extends TestCase public function testSafeUnserialize() { // should unserialize an allowed class - $this->assertTrue(Common::safe_unserialize('O:12:"Piwik\Common":0:{}', ['Piwik\Common']) instanceof Common); + self::assertInstanceOf(Common::class, Common::safe_unserialize('O:12:"Piwik\Common":0:{}', ['Piwik\Common'])); // not allowed classed should result in an incomplete class - $this->assertTrue(Common::safe_unserialize('O:12:"Piwik\Common":0:{}') instanceof \__PHP_Incomplete_Class); + self::assertInstanceOf(\__PHP_Incomplete_Class::class, Common::safe_unserialize('O:12:"Piwik\Common":0:{}')); // strings not unserializable should return false and trigger a debug log $logger = $this->createFakeLogger(); self::assertFalse(Common::safe_unserialize('{1:somebroken}')); self::assertStringContainsString('Unable to unserialize a string: unserialize(): Error at offset 0 of 14 bytes', $logger->output); + + /* + * serialize() uses its internal machine representation when floats expressed in E-notation, + * which may vary between php versions, OS, and hardware platforms + */ + $testData = -5.0E+142; + self::assertSame($testData, Common::safe_unserialize(serialize($testData))); + + $unserialized = [ + 'announcement' => true, + 'source' => [ + [ + 'filename' => 'php-5.3.3.tar.bz2', + 'name' => 'PHP 5.3.3 (tar.bz2)', + 'md5' => '21ceeeb232813c10283a5ca1b4c87b48', + 'date' => '22 July 2010', + ], + [ + 'filename' => 'php-5.3.3.tar.gz', + 'name' => 'PHP 5.3.3 (tar.gz)', + 'md5' => '5adf1a537895c2ec933fddd48e78d8a2', + 'date' => '22 July 2010', + ], + ], + 'date' => '22 July 2010', + 'version' => '5.3.3', + ]; + $serialized = 'a:4:{s:12:"announcement";b:1;s:6:"source";a:2:{i:0;a:4:{s:8:"filename";s:17:"php-5.3.3.tar.bz2";s:4:"name";s:19:"PHP 5.3.3 (tar.bz2)";s:3:"md5";s:32:"21ceeeb232813c10283a5ca1b4c87b48";s:4:"date";s:12:"22 July 2010";}i:1;a:4:{s:8:"filename";s:16:"php-5.3.3.tar.gz";s:4:"name";s:18:"PHP 5.3.3 (tar.gz)";s:3:"md5";s:32:"5adf1a537895c2ec933fddd48e78d8a2";s:4:"date";s:12:"22 July 2010";}}s:4:"date";s:12:"22 July 2010";s:7:"version";s:5:"5.3.3";}'; + + self::assertEquals($serialized, serialize($unserialized)); + self::assertSame($unserialized, Common::safe_unserialize($serialized)); + self::assertSame($unserialized, Common::safe_unserialize(serialize($unserialized))); + self::assertEquals($serialized, serialize(Common::safe_unserialize($serialized))); + + $a = 'a:1:{i:0;O:12:"Piwik\Common":0:{}}'; + $ua = Common::safe_unserialize($a); + self::assertIsArray($ua); + self::assertInstanceOf(\__PHP_Incomplete_Class::class, $ua[0]); + + $a = 'a:1:{i:0;O:12:"Piwik\Common":0:{}}'; + $ua = Common::safe_unserialize($a, ['Piwik\Common']); + self::assertIsArray($ua); + self::assertInstanceOf(Common::class, $ua[0]); + + $a = 'a:2:{i:0;s:4:"test";i:1;O:12:"Piwik\Common":0:{}}'; + $ua = Common::safe_unserialize($a); + self::assertIsArray($ua); + self::assertSame('test', $ua[0]); + self::assertInstanceOf(\__PHP_Incomplete_Class::class, $ua[1]); + + $a = 'O:28:"Test_Piwik_Cookie_Mock_Class":1:{s:18:"' . "\0" . 'Piwik\Common' . "\0" . 'name";s:4:"test";}'; + $ua = Common::safe_unserialize($a); + self::assertInstanceOf(\__PHP_Incomplete_Class::class, $ua); + + // arrays and objects cannot be used as keys + $a = 'a:2:{i:0;a:0:{}O:28:"Test_Piwik_Cookie_Mock_Class":0:{}s:4:"test";'; + $this->assertFalse(Common::safe_unserialize($a), "test: unserializing with illegal key"); + } + + /** + * Dataprovider for testSafeSerialize + */ + public function getSafeSerializeData() + { + return [ + ['null', null], + ['bool false', false], + ['bool true', true], + ['negative int', -42], + ['zero', 0], + ['positive int', 42], + ['float', 1.25], + ['empty string', ''], + ['nul in string', "\0"], + ['carriage return in string', "first line\r\nsecond line"], + ['utf7 in string', 'hello, world'], + ['utf8 in string', '是'], + ['empty array', []], + ['single element array', ["test"]], + ['associative array', ["alpha", 2 => "beta"]], + ['mixed keys', ['first' => 'john', 'last' => 'doe', 10 => 'age']], + ['nested arrays', ['top' => ['middle' => 2, ['bottom'], 'last'], 'the end' => true]], + ['array confusion', ['"', "'", '}', ';', ':']], + ]; + } + + /** + * @dataProvider getSafeSerializeData + */ + public function testSafeSerialize($id, $testData) + { + $this->assertEquals($testData, unserialize(serialize($testData)), $id); + $this->assertSame($testData, Common::safe_unserialize(serialize($testData)), $id); } private function createFakeLogger() { $logger = new FakeLogger(); - $newEnv = new Environment('test', array( - 'Psr\Log\LoggerInterface' => $logger, + $newEnv = new Environment('test', [ + 'Psr\Log\LoggerInterface' => $logger, 'Tests.log.allowAllHandlers' => true, - )); + ]); $newEnv->init(); $newMonologLogger = $newEnv->getContainer()->make('Psr\Log\LoggerInterface'); - $oldLogger = new Log($newMonologLogger); + $oldLogger = new Log($newMonologLogger); Log::setSingletonInstance($oldLogger); return $logger; @@ -325,40 +424,40 @@ class CommonTest extends TestCase */ public function getBrowserLanguageData() { - return array( // user agent, browser language - array("en-gb", "en-gb"), + return [ + // user agent, browser language + ["en-gb", "en-gb"], // filter quality attribute - array("en-us,en;q=0.5", "en-us,en"), + ["en-us,en;q=0.5", "en-us,en"], // bad user agents - array("en-us,chrome://global/locale/intl.properties", "en-us"), + ["en-us,chrome://global/locale/intl.properties", "en-us"], // unregistered language tag - array("en,en-securid", "en"), - array("en-securid,en", "en"), - array("en-us,en-securid,en", "en-us,en"), + ["en,en-securid", "en"], + ["en-securid,en", "en"], + ["en-us,en-securid,en", "en-us,en"], // accept private sub tags - array("en-us,x-en-securid", "en-us,x-en-securid"), - array("en-us,en-x-securid", "en-us,en-x-securid"), + ["en-us,x-en-securid", "en-us,x-en-securid"], + ["en-us,en-x-securid", "en-us,en-x-securid"], // filter arbitrary white space - array("en-us, en", "en-us,en"), - array("en-ca, en-us ,en", "en-ca,en-us,en"), + ["en-us, en", "en-us,en"], + ["en-ca, en-us ,en", "en-ca,en-us,en"], // handle comments - array(" ( comment ) en-us (another comment) ", "en-us"), + [" ( comment ) en-us (another comment) ", "en-us"], // handle quoted pairs (embedded in comments) - array(" ( \( start ) en-us ( \) end ) ", "en-us"), - array(" ( \) en-ca, \( ) en-us ( \) ,en ) ", "en-us"), - ); + [" ( \( start ) en-us ( \) end ) ", "en-us"], + [" ( \) en-ca, \( ) en-us ( \) ,en ) ", "en-us"], + ]; } /** * @dataProvider getBrowserLanguageData - * @group Core */ public function testGetBrowserLanguage($useragent, $browserLanguage) { @@ -374,25 +473,24 @@ class CommonTest extends TestCase /** @var RegionDataProvider $regionDataProvider */ $regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider'); - return array( // browser language, valid countries, expected result - array("", array(), "xx"), - array("", array("us" => 'amn'), "xx"), - array("en", array("us" => 'amn'), "xx"), - array("en-us", array("us" => 'amn'), "us"), - array("en-ca", array("us" => 'amn'), "xx"), - array("en-ca", array("us" => 'amn', "ca" => 'amn'), "ca"), - array("fr-fr,fr-ca", array("us" => 'amn', "ca" => 'amn'), "ca"), - array("fr-fr;q=1.0,fr-ca;q=0.9", array("us" => 'amn', "ca" => 'amn'), "ca"), - array("fr-ca,fr;q=0.1", array("us" => 'amn', "ca" => 'amn'), "ca"), - array("en-us,en;q=0.5", $regionDataProvider->getCountryList(), "us"), - array("fr-ca,fr;q=0.1", array("fr" => 'eur', "us" => 'amn', "ca" => 'amn'), "ca"), - array("fr-fr,fr-ca", array("fr" => 'eur', "us" => 'amn', "ca" => 'amn'), "fr") - ); + return [ // browser language, valid countries, expected result + ["", [], "xx"], + ["", ["us" => 'amn'], "xx"], + ["en", ["us" => 'amn'], "xx"], + ["en-us", ["us" => 'amn'], "us"], + ["en-ca", ["us" => 'amn'], "xx"], + ["en-ca", ["us" => 'amn', "ca" => 'amn'], "ca"], + ["fr-fr,fr-ca", ["us" => 'amn', "ca" => 'amn'], "ca"], + ["fr-fr;q=1.0,fr-ca;q=0.9", ["us" => 'amn', "ca" => 'amn'], "ca"], + ["fr-ca,fr;q=0.1", ["us" => 'amn', "ca" => 'amn'], "ca"], + ["en-us,en;q=0.5", $regionDataProvider->getCountryList(), "us"], + ["fr-ca,fr;q=0.1", ["fr" => 'eur', "us" => 'amn', "ca" => 'amn'], "ca"], + ["fr-fr,fr-ca", ["fr" => 'eur', "us" => 'amn', "ca" => 'amn'], "fr"], + ]; } /** * @dataProvider getCountryCodeTestData - * @group Core */ public function testExtractCountryCodeFromBrowserLanguage($browserLanguage, $validCountries, $expected) { @@ -406,17 +504,16 @@ class CommonTest extends TestCase public function getCountryCodeTestDataInfer() { - return array( // browser language, valid countries, expected result (non-guess vs guess) - array("fr,en-us", array("us" => 'amn', "ca" => 'amn'), "us", "fr"), - array("fr,en-us", array("fr" => 'eur', "us" => 'amn', "ca" => 'amn'), "us", "fr"), - array("fr,fr-fr,en-us", array("fr" => 'eur', "us" => 'amn', "ca" => 'amn'), "fr", "fr"), - array("fr-fr,fr,en-us", array("fr" => 'eur', "us" => 'amn', "ca" => 'amn'), "fr", "fr") - ); + return [ // browser language, valid countries, expected result (non-guess vs guess) + ["fr,en-us", ["us" => 'amn', "ca" => 'amn'], "us", "fr"], + ["fr,en-us", ["fr" => 'eur', "us" => 'amn', "ca" => 'amn'], "us", "fr"], + ["fr,fr-fr,en-us", ["fr" => 'eur', "us" => 'amn', "ca" => 'amn'], "fr", "fr"], + ["fr-fr,fr,en-us", ["fr" => 'eur', "us" => 'amn', "ca" => 'amn'], "fr", "fr"], + ]; } /** * @dataProvider getCountryCodeTestDataInfer - * @group Core */ public function testExtractCountryCodeFromBrowserLanguageInfer($browserLanguage, $validCountries, $expected, $expectedInfer) { @@ -432,29 +529,29 @@ class CommonTest extends TestCase */ public function getLanguageDataToExtractLanguageRegionCode() { - return array( + return [ // browser language, valid languages (with optional region), expected result - array("fr-ca", array("fr"), "fr-ca"), - array("fr-ca", array("ca"), "xx"), - array("", array(), "xx"), - array("", array("en"), "xx"), - array("fr", array("en"), "xx"), - array("en", array("en"), "en"), - array("en", array("en-ca"), "xx"), - array("en-ca", array("en-ca"), "en-ca"), - array("en-ca", array("en"), "en-ca"), - array("fr,en-us", array("fr", "en"), "fr"), - array("fr,en-us", array("en", "fr"), "fr"), - array("fr-fr,fr-ca", array("fr"), "fr-fr"), - array("fr-fr,fr-ca", array("fr-ca"), "fr-ca"), - array("-ca", array("fr","ca"), "xx"), - array("fr-fr;q=1.0,fr-ca;q=0.9", array("fr-ca"), "fr-ca"), - array("es,en,fr;q=0.7,de;q=0.3", array("fr", "es", "de", "en"), "es"), - array("zh-sg,de;q=0.3", array("zh", "es", "de"), "zh-sg"), - array("fr-ca,fr;q=0.1", array("fr-ca"), "fr-ca"), - array("r5,fr;q=1,de", array("fr", "de"), "fr"), - array("Zen§gq1", array("en"), "xx"), - ); + ["fr-ca", ["fr"], "fr-ca"], + ["fr-ca", ["ca"], "xx"], + ["", [], "xx"], + ["", ["en"], "xx"], + ["fr", ["en"], "xx"], + ["en", ["en"], "en"], + ["en", ["en-ca"], "xx"], + ["en-ca", ["en-ca"], "en-ca"], + ["en-ca", ["en"], "en-ca"], + ["fr,en-us", ["fr", "en"], "fr"], + ["fr,en-us", ["en", "fr"], "fr"], + ["fr-fr,fr-ca", ["fr"], "fr-fr"], + ["fr-fr,fr-ca", ["fr-ca"], "fr-ca"], + ["-ca", ["fr", "ca"], "xx"], + ["fr-fr;q=1.0,fr-ca;q=0.9", ["fr-ca"], "fr-ca"], + ["es,en,fr;q=0.7,de;q=0.3", ["fr", "es", "de", "en"], "es"], + ["zh-sg,de;q=0.3", ["zh", "es", "de"], "zh-sg"], + ["fr-ca,fr;q=0.1", ["fr-ca"], "fr-ca"], + ["r5,fr;q=1,de", ["fr", "de"], "fr"], + ["Zen§gq1", ["en"], "xx"], + ]; } /** @@ -471,24 +568,24 @@ class CommonTest extends TestCase */ public function getLanguageDataToExtractLanguageCode() { - return array( + return [ // browser language, valid languages, expected result - array("fr-ca", array("fr"), "fr"), - array("fr-ca", array("ca"), "xx"), - array("", array("en"), "xx"), - array("fr", array("en"), "xx"), - array("en", array("en"), "en"), - array("en", array("en-ca"), "xx"), - array("en-ca", array("en"), "en"), - array("fr,en-us", array("fr", "en"), "fr"), - array("fr,en-us", array("en", "fr"), "fr"), - array("fr-fr,fr-ca", array("fr"), "fr"), - array("-ca", array("fr","ca"), "xx"), - array("es,en,fr;q=0.7,de;q=0.3", array("fr", "es", "de", "en"), "es"), - array("zh-sg,de;q=0.3", array("zh", "es", "de"), "zh"), - array("r5,fr;q=1,de", array("fr", "de"), "fr"), - array("Zen§gq1", array("en"), "xx"), - ); + ["fr-ca", ["fr"], "fr"], + ["fr-ca", ["ca"], "xx"], + ["", ["en"], "xx"], + ["fr", ["en"], "xx"], + ["en", ["en"], "en"], + ["en", ["en-ca"], "xx"], + ["en-ca", ["en"], "en"], + ["fr,en-us", ["fr", "en"], "fr"], + ["fr,en-us", ["en", "fr"], "fr"], + ["fr-fr,fr-ca", ["fr"], "fr"], + ["-ca", ["fr", "ca"], "xx"], + ["es,en,fr;q=0.7,de;q=0.3", ["fr", "es", "de", "en"], "es"], + ["zh-sg,de;q=0.3", ["zh", "es", "de"], "zh"], + ["r5,fr;q=1,de", ["fr", "de"], "fr"], + ["Zen§gq1", ["en"], "xx"], + ]; } /** diff --git a/tests/PHPUnit/Unit/CookieTest.php b/tests/PHPUnit/Unit/CookieTest.php index 6d31abb1a3..9e0ba780d0 100644 --- a/tests/PHPUnit/Unit/CookieTest.php +++ b/tests/PHPUnit/Unit/CookieTest.php @@ -2,7 +2,7 @@ /** * Matomo - free/libre analytics platform * - * @link https://matomo.org + * @link https://matomo.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ @@ -13,6 +13,9 @@ use Piwik\Common; use Piwik\Cookie; use Piwik\SettingsPiwik; +/** + * @group Core + */ class CookieTest extends \PHPUnit\Framework\TestCase { const TEST_COOKIE_NAME = 'fooBarTest'; @@ -42,7 +45,7 @@ class CookieTest extends \PHPUnit\Framework\TestCase public function test_loadContentFromCookie() { $_COOKIE[self::TEST_COOKIE_NAME] = 'hello=1.2:ignore=Kg==:foo=:bar=dGVzdDp2YWx1ZQ=='; - $this->cookie = $this->makeCookie(); + $this->cookie = $this->makeCookie(); $this->assertEquals('1.2', $this->cookie->get('hello')); $this->assertEquals('*', $this->cookie->get('ignore')); $this->assertEquals('', $this->cookie->get('foo')); @@ -51,18 +54,18 @@ class CookieTest extends \PHPUnit\Framework\TestCase public function test_loadContentFromCookie_wontUnserialiseContentIfNotSigned() { - $val = serialize(['foobar']); + $val = serialize(['foobar']); $_COOKIE[self::TEST_COOKIE_NAME] = 'hello=' . base64_encode($val) . ':_=foobar'; - $this->cookie = $this->makeCookie(); + $this->cookie = $this->makeCookie(); $this->assertEquals(Common::sanitizeInputValues($val), $this->cookie->get('hello')); } public function test_loadContentFromCookie_willUnserialiseContentIfSigned() { - $val = serialize(['foobar']); - $cookieStr = 'hello=' . base64_encode($val) . ':_='; + $val = serialize(['foobar']); + $cookieStr = 'hello=' . base64_encode($val) . ':_='; $_COOKIE[self::TEST_COOKIE_NAME] = $cookieStr . sha1($cookieStr . SettingsPiwik::getSalt()); - $this->cookie = $this->makeCookie(); + $this->cookie = $this->makeCookie(); $this->assertEquals(['foobar'], $this->cookie->get('hello')); } @@ -75,7 +78,7 @@ class CookieTest extends \PHPUnit\Framework\TestCase public function test_delete_unsetsValues() { $_COOKIE[self::TEST_COOKIE_NAME] = 'hello=1.2'; - $this->cookie = $this->makeCookie(); + $this->cookie = $this->makeCookie(); $this->assertEquals('1.2', $this->cookie->get('hello')); $this->cookie->delete(); @@ -111,7 +114,7 @@ class CookieTest extends \PHPUnit\Framework\TestCase { $this->expectException(\Exception::class); $this->expectExceptionMessage('Only strings and numbers can be used in cookies. Value is of type array'); - $this->cookie->set('ignore', array('foo')); + $this->cookie->set('ignore', ['foo']); $this->cookie->generateContentString(); } @@ -120,31 +123,29 @@ class CookieTest extends \PHPUnit\Framework\TestCase */ public function getJsonSerializeData() { - return array( - array('null', null), - array('bool false', false), - array('bool true', true), - array('negative int', -42), - array('zero', 0), - array('positive int', 42), - array('float', 1.25), - array('empty string', ''), - array('nul in string', "\0"), - array('carriage return in string', "first line\r\nsecond line"), - array('utf7 in string', 'hello, world'), - array('utf8 in string', '是'), - array('empty array', array()), - array('single element array', array("test")), - array('associative array', array("alpha", 2 => "beta")), - array('mixed keys', array('first' => 'john', 'last' => 'doe', 10 => 'age')), - array('nested arrays', array('top' => array('middle' => 2, array('bottom'), 'last'), 'the end' => true)), - array('array confusion', array('"', "'", '}', ';', ':')), - ); + return [ + ['null', null], + ['bool false', false], + ['bool true', true], + ['negative int', -42], + ['zero', 0], + ['positive int', 42], + ['float', 1.25], + ['empty string', ''], + ['nul in string', "\0"], + ['carriage return in string', "first line\r\nsecond line"], + ['utf7 in string', 'hello, world'], + ['utf8 in string', '是'], + ['empty array', []], + ['single element array', ["test"]], + ['associative array', ["alpha", 2 => "beta"]], + ['mixed keys', ['first' => 'john', 'last' => 'doe', 10 => 'age']], + ['nested arrays', ['top' => ['middle' => 2, ['bottom'], 'last'], 'the end' => true]], + ['array confusion', ['"', "'", '}', ';', ':']], + ]; } /** - * @group Core - * * @dataProvider getJsonSerializeData */ public function testJsonSerialize($id, $testData) @@ -152,106 +153,6 @@ class CookieTest extends \PHPUnit\Framework\TestCase $this->assertEquals($testData, json_decode(json_encode($testData), $assoc = true), $id); } - /** - * Dataprovider for testSafeSerialize - */ - public function getSafeSerializeData() - { - return array( - array('null', null), - array('bool false', false), - array('bool true', true), - array('negative int', -42), - array('zero', 0), - array('positive int', 42), - array('float', 1.25), - array('empty string', ''), - array('nul in string', "\0"), - array('carriage return in string', "first line\r\nsecond line"), - array('utf7 in string', 'hello, world'), - array('utf8 in string', '是'), - array('empty array', array()), - array('single element array', array("test")), - array('associative array', array("alpha", 2 => "beta")), - array('mixed keys', array('first' => 'john', 'last' => 'doe', 10 => 'age')), - array('nested arrays', array('top' => array('middle' => 2, array('bottom'), 'last'), 'the end' => true)), - array('array confusion', array('"', "'", '}', ';', ':')), - ); - } - - /** - * @group Core - * - * @dataProvider getSafeSerializeData - */ - public function testSafeSerialize($id, $testData) - { - $this->assertEquals($testData, unserialize(serialize($testData)), $id); - $this->assertSame($testData, Common::safe_unserialize(serialize($testData)), $id); - } - - /** - * @group Core - */ - public function testSafeUnserialize() - { - /* - * serialize() uses its internal machine representation when floats expressed in E-notation, - * which may vary between php versions, OS, and hardware platforms - */ - $testData = -5.0E+142; - $this->assertEquals($testData, unserialize(serialize($testData))); - $this->assertSame($testData, Common::safe_unserialize(serialize($testData))); - - $unserialized = array( - 'announcement' => true, - 'source' => array( - array( - 'filename' => 'php-5.3.3.tar.bz2', - 'name' => 'PHP 5.3.3 (tar.bz2)', - 'md5' => '21ceeeb232813c10283a5ca1b4c87b48', - 'date' => '22 July 2010', - ), - array( - 'filename' => 'php-5.3.3.tar.gz', - 'name' => 'PHP 5.3.3 (tar.gz)', - 'md5' => '5adf1a537895c2ec933fddd48e78d8a2', - 'date' => '22 July 2010', - ), - ), - 'date' => '22 July 2010', - 'version' => '5.3.3', - ); - $serialized = 'a:4:{s:12:"announcement";b:1;s:6:"source";a:2:{i:0;a:4:{s:8:"filename";s:17:"php-5.3.3.tar.bz2";s:4:"name";s:19:"PHP 5.3.3 (tar.bz2)";s:3:"md5";s:32:"21ceeeb232813c10283a5ca1b4c87b48";s:4:"date";s:12:"22 July 2010";}i:1;a:4:{s:8:"filename";s:16:"php-5.3.3.tar.gz";s:4:"name";s:18:"PHP 5.3.3 (tar.gz)";s:3:"md5";s:32:"5adf1a537895c2ec933fddd48e78d8a2";s:4:"date";s:12:"22 July 2010";}}s:4:"date";s:12:"22 July 2010";s:7:"version";s:5:"5.3.3";}'; - - $this->assertSame($unserialized, unserialize($serialized)); - $this->assertEquals($serialized, serialize($unserialized)); - - $this->assertSame($unserialized, Common::safe_unserialize($serialized)); - $this->assertEquals($serialized, serialize($unserialized)); - $this->assertSame($unserialized, Common::safe_unserialize(serialize($unserialized))); - $this->assertEquals($serialized, serialize(Common::safe_unserialize($serialized))); - - $a = 'O:31:"Test_Piwik_Cookie_Phantom_Class":0:{}'; - $this->assertFalse(Common::safe_unserialize($a), "test: unserializing an object where class not (yet) defined"); - - $a = 'O:28:"Test_Piwik_Cookie_Mock_Class":0:{}'; - $this->assertFalse(Common::safe_unserialize($a), "test: unserializing an object where class is defined"); - - $a = 'a:1:{i:0;O:28:"Test_Piwik_Cookie_Mock_Class":0:{}}'; - $this->assertFalse(Common::safe_unserialize($a), "test: unserializing nested object where class is defined"); - - $a = 'a:2:{i:0;s:4:"test";i:1;O:28:"Test_Piwik_Cookie_Mock_Class":0:{}}'; - $this->assertFalse(Common::safe_unserialize($a), "test: unserializing another nested object where class is defined"); - - $a = 'O:28:"Test_Piwik_Cookie_Mock_Class":1:{s:34:"' . "\0" . 'Test_Piwik_Cookie_Mock_Class' . "\0" . 'name";s:4:"test";}'; - $this->assertFalse(Common::safe_unserialize($a), "test: unserializing object with member where class is defined"); - - // arrays and objects cannot be used as keys, i.e., generates "Warning: Illegal offset type ..." - $a = 'a:2:{i:0;a:0:{}O:28:"Test_Piwik_Cookie_Mock_Class":0:{}s:4:"test";'; - $this->assertFalse(Common::safe_unserialize($a), "test: unserializing with illegal key"); - } - public function test_isCookieInRequest_ReturnsTrueIfCookieExists() { $_COOKIE['abc'] = 'value'; @@ -267,26 +168,25 @@ class CookieTest extends \PHPUnit\Framework\TestCase { //assert + 30 years $checkTime = $this->cookie->formatExpireTime("+ 30 years"); - $years = $this->diffInYears($checkTime); + $years = $this->diffInYears($checkTime); $this->assertTrue($years >= 29); // assert Empty $checkTime = $this->cookie->formatExpireTime(); - $years = $this->diffInYears($checkTime); + $years = $this->diffInYears($checkTime); $this->assertTrue($years >= 1); // assert timestamp - $checkTime = $this->cookie->formatExpireTime(time()+(86400 * 365 * 3)); - $years = $this->diffInYears($checkTime); + $checkTime = $this->cookie->formatExpireTime(time() + (86400 * 365 * 3)); + $years = $this->diffInYears($checkTime); $this->assertTrue($years >= 2); - } private function diffInYears($checkTime) { $today = new DateTime(); - $time = DateTime::createFromFormat('l, d-M-Y H:i:s T', $checkTime); - $diff = $time->diff($today); + $time = DateTime::createFromFormat('l, d-M-Y H:i:s T', $checkTime); + $diff = $time->diff($today); return $diff->format('%y'); } } |