<commit_before><?php
/**
 * @copyright Copyright (C) 2010-2022, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Test\src\Security\TwoFactor\Factory;

use Friendica\Security\TwoFactor\Factory\TrustedBrowser;
use Friendica\Test\MockedTest;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
use Psr\Log\NullLogger;

class TrustedBrowserTest extends MockedTest
{
	public function testCreateFromTableRowSuccess()
	{
		$factory = new TrustedBrowser(new NullLogger());

		$row = [
			'cookie_hash' => Strings::getRandomHex(),
			'uid' => 42,
			'user_agent' => 'PHPUnit',
			'created' => DateTimeFormat::utcNow(),
			'trusted' => true,
			'last_used' => null,
		];

		$trustedBrowser = $factory->createFromTableRow($row);

		$this->assertEquals($row, $trustedBrowser->toArray());
	}

	public function testCreateFromTableRowMissingData()
	{
		$this->expectException(\TypeError::class);

		$factory = new TrustedBrowser(new NullLogger());

		$row = [
			'cookie_hash' => null,
			'uid' => null,
			'user_agent' => null,
			'created' => null,
			'trusted' => true,
			'last_used' => null,
		];

		$trustedBrowser = $factory->createFromTableRow($row);

		$this->assertEquals($row, $trustedBrowser->toArray());
	}

	public function testCreateForUserWithUserAgent()
	{
		$factory = new TrustedBrowser(new NullLogger());

		$uid = 42;
		$userAgent = 'PHPUnit';

		$trustedBrowser = $factory->createForUserWithUserAgent($uid, $userAgent, true);
<commit_msg>add anything ..<commit_after><?php
/**
 * @copyright Copyright (C) 2010-2022, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Test\src\Security\TwoFactor\Factory;

use Friendica\Security\TwoFactor\Factory\TrustedBrowser;
use Friendica\Test\MockedTest;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
use Psr\Log\NullLogger;

class TrustedBrowserTest extends MockedTest
{
	public function testCreateFromTableRowSuccess()
	{
		$factory = new TrustedBrowser(new NullLogger());

		$row = [
			'cookie_hash' => Strings::getRandomHex(),
			'uid' => 42,
			'user_agent' => 'PHPUnit',
			'created' => DateTimeFormat::utcNow(),
			'trusted' => true,
			'last_used' => null,
		];

		$trustedBrowser = $factory->createFromTableRow($row);

		$this->assertEquals($row, $trustedBrowser->toArray());
	}

	public function testCreateFromTableRowMissingData()
	{
		$this->expectException(\TypeError::class);

		$factory = new TrustedBrowser(new NullLogger());

		$row = [
			'cookie_hash' => null,
			'uid' => null,
			'user_agent' => null,
			'created' => null,
			'trusted' => true,
			'last_used' => null,
		];

		$trustedBrowser = $factory->createFromTableRow($row);

		$this->assertEquals($row, $trustedBrowser->toArray());
	}

	public function testCreateForUserWithUserAgent()
	{
		$factory = new TrustedBrowser(new NullLogger());

		$uid       = 42;
		$userAgent = 'PHPUnit';

		$trustedBrowser = $factory->createForUserWithUserAgent($uid, $userAgent, true);

{'fraction_non_alphanumeric': 0.12162780064014632, 'fraction_numerical': 0.006401463191586648, 'mean_word_length': 4.407911001236094, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5934301', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1299, 'n_words': 400}