testVars = static::$fixture->getTestEnvironment(); $this->originalAssumeSecureValue = Config::getInstance()->General['assume_secure_protocol']; } public function tearDown(): void { parent::tearDown(); $this->testVars->overrideConfig('General', 'assume_secure_protocol', $this->originalAssumeSecureValue); $this->testVars->save(); } public function testIgnoreCookieSameSiteChromeSecure() { $this->testVars->overrideConfig('General', 'assume_secure_protocol', 1); $this->testVars->save(); $headers = $this->setIgnoreCookie(self::USERAGENT_CHROME); $cookie = $this->findIgnoreCookie($headers); $this->assertCookieSameSiteMatches('None', $cookie); } public function testIgnoreCookieSameSiteChromeNotSecure() { $this->testVars->overrideConfig('General', 'assume_secure_protocol', 0); $this->testVars->save(); $headers = $this->setIgnoreCookie(self::USERAGENT_CHROME); $cookie = $this->findIgnoreCookie($headers); $this->assertCookieSameSiteMatches('Lax', $cookie); } public function testIgnoreCookieSameSiteFirefox() { $headers = $this->setIgnoreCookie(self::USERAGENT_FIREFOX); $cookie = $this->findIgnoreCookie($headers); $this->assertCookieSameSiteMatches('Lax', $cookie); } public function testIgnoreCookieSameSiteSafari() { $headers = $this->setIgnoreCookie(self::USERAGENT_SAFARI); $cookie = $this->findIgnoreCookie($headers); self::assertStringNotContainsString($cookie, 'SameSite'); } private function setIgnoreCookie($userAgent) { $matomoUrl = Fixture::getTestRootUrl(); $tokenAuth = Fixture::getTokenAuth(); $params = array( 'module' => 'UsersManager', 'action' => 'setIgnoreCookie', 'idSite' => 1, 'period' => 'day', 'date' => 'yesterday', 'ignoreSalt' => md5(SettingsPiwik::getSalt()), 'token_auth' => $tokenAuth ); $url = $matomoUrl . 'index.php?' . http_build_query($params); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); return curl_exec($ch); } private function findIgnoreCookie($rawHeaders) { $ignoreCookieName = Config::getInstance()->Tracker['ignore_visits_cookie_name']; preg_match('/^Set-Cookie: ' . $ignoreCookieName . '=.*/m', $rawHeaders, $matches); return $matches ? $matches[0] : ''; } private function assertCookieSameSiteMatches($expectedSameSite, $cookieHeader) { self::assertStringContainsString('SameSite=' . $expectedSameSite, $cookieHeader); } }