]> git.mxchange.org Git - friendica.git/blob - tests/src/Security/TwoFactor/Factory/TrustedBrowserTest.php
Add more special chars at tests
[friendica.git] / tests / src / Security / TwoFactor / Factory / TrustedBrowserTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Security\TwoFactor\Factory;
23
24 use Friendica\Security\TwoFactor\Factory\TrustedBrowser;
25 use Friendica\Test\MockedTest;
26 use Friendica\Util\DateTimeFormat;
27 use Friendica\Util\Strings;
28 use Psr\Log\NullLogger;
29
30 class TrustedBrowserTest extends MockedTest
31 {
32         public function testCreateFromTableRowSuccess()
33         {
34                 $factory = new TrustedBrowser(new NullLogger());
35
36                 $row = [
37                         'cookie_hash' => Strings::getRandomHex(),
38                         'uid' => 42,
39                         'user_agent' => 'PHPUnit',
40                         'created' => DateTimeFormat::utcNow(),
41                         'trusted' => true,
42                         'last_used' => null,
43                 ];
44
45                 $trustedBrowser = $factory->createFromTableRow($row);
46
47                 $this->assertEquals($row, $trustedBrowser->toArray());
48         }
49
50         public function testCreateFromTableRowMissingData()
51         {
52                 $this->expectException(\TypeError::class);
53
54                 $factory = new TrustedBrowser(new NullLogger());
55
56                 $row = [
57                         'cookie_hash' => null,
58                         'uid' => null,
59                         'user_agent' => null,
60                         'created' => null,
61                         'trusted' => true,
62                         'last_used' => null,
63                 ];
64
65                 $trustedBrowser = $factory->createFromTableRow($row);
66
67                 $this->assertEquals($row, $trustedBrowser->toArray());
68         }
69
70         public function testCreateForUserWithUserAgent()
71         {
72                 $factory = new TrustedBrowser(new NullLogger());
73
74                 $uid       = 42;
75                 $userAgent = 'PHPUnit';
76
77                 $trustedBrowser = $factory->createForUserWithUserAgent($uid, $userAgent, true);
78
79                 $this->assertNotEmpty($trustedBrowser->cookie_hash);
80                 $this->assertEquals($uid, $trustedBrowser->uid);
81                 $this->assertEquals($userAgent, $trustedBrowser->user_agent);
82                 $this->assertTrue($trustedBrowser->trusted);
83                 $this->assertNotEmpty($trustedBrowser->created);
84         }
85 }