diff options
author | Lukas Winkler <git@lw1.at> | 2020-11-02 22:12:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-02 22:12:09 +0300 |
commit | fd1d68cbaac5c8a44aaab335700eba8f3723c754 (patch) | |
tree | 40e24a0ddd6fd7d7e2bbd1f471637fd5ee979fc7 /tests/PHPUnit/Unit | |
parent | 7f58bb49a82ef12f2c1e95834d5e32cc4129086c (diff) |
simplified cookies (#14444)
Diffstat (limited to 'tests/PHPUnit/Unit')
-rw-r--r-- | tests/PHPUnit/Unit/CookieTest.php | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/PHPUnit/Unit/CookieTest.php b/tests/PHPUnit/Unit/CookieTest.php index aa04caf47f..bd7425836e 100644 --- a/tests/PHPUnit/Unit/CookieTest.php +++ b/tests/PHPUnit/Unit/CookieTest.php @@ -8,10 +8,101 @@ namespace Piwik\Tests\Unit; +use Piwik\Common; use Piwik\Cookie; +use Piwik\SettingsPiwik; class CookieTest extends \PHPUnit\Framework\TestCase { + const TEST_COOKIE_NAME = 'fooBarTest'; + + /** + * @var Cookie + */ + private $cookie; + + public function setUp(): void + { + parent::setUp(); + $this->cookie = $this->makeCookie(); + } + + public function tearDown(): void + { + unset($_COOKIE[self::TEST_COOKIE_NAME]); + parent::tearDown(); + } + + private function makeCookie() + { + return new Cookie(self::TEST_COOKIE_NAME); + } + + public function test_loadContentFromCookie() + { + $_COOKIE[self::TEST_COOKIE_NAME] = 'hello=1.2:ignore=Kg==:foo=:bar=dGVzdDp2YWx1ZQ=='; + $this->cookie = $this->makeCookie(); + $this->assertEquals('1.2', $this->cookie->get('hello')); + $this->assertEquals('*', $this->cookie->get('ignore')); + $this->assertEquals('', $this->cookie->get('foo')); + $this->assertEquals('test:value', $this->cookie->get('bar')); + } + + public function test_loadContentFromCookie_wontUnserialiseContentIfNotSigned() + { + $val = safe_serialize(['foobar']); + $_COOKIE[self::TEST_COOKIE_NAME] = 'hello=' . base64_encode($val) . ':_=foobar'; + $this->cookie = $this->makeCookie(); + $this->assertEquals(Common::sanitizeInputValues($val), $this->cookie->get('hello')); + } + + public function test_loadContentFromCookie_willUnserialiseContentIfSigned() + { + $val = safe_serialize(['foobar']); + $cookieStr = 'hello=' . base64_encode($val) . ':_='; + $_COOKIE[self::TEST_COOKIE_NAME] = $cookieStr . sha1($cookieStr . SettingsPiwik::getSalt()); + $this->cookie = $this->makeCookie(); + $this->assertEquals(['foobar'], $this->cookie->get('hello')); + } + + public function test_get_set() + { + $this->cookie->set('ignore', '*f1'); + $this->assertEquals('*f1', $this->cookie->get('ignore')); + } + + public function test_generateContentString_usesBase64encode_string() + { + $this->cookie->set('ignore', '*'); + $this->assertEquals('ignore=Kg==', $this->cookie->generateContentString()); + } + + public function test_generateContentString_usesPlainTextNumber() + { + $this->cookie->set('hello', '1.2'); + $this->assertEquals('hello=1.2', $this->cookie->generateContentString()); + + $this->cookie->set('hello', 1.2); + $this->assertEquals('hello=1.2', $this->cookie->generateContentString()); + } + + public function test_generateContentString_multipleFields() + { + $this->cookie->set('hello', '1.2'); + $this->cookie->set('ignore', '*'); + $this->cookie->set('foo', ''); + $this->cookie->set('bar', 'test:value'); + $this->assertEquals('hello=1.2:ignore=Kg==:foo=:bar=dGVzdDp2YWx1ZQ==', $this->cookie->generateContentString()); + } + + public function test_generateContentString_throwsExceptionWhenNotStringOrNumber() + { + $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->generateContentString(); + } + /** * Dataprovider for testJsonSerialize */ |