]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Session/UserSessionTest.php
Add more special chars at tests
[friendica.git] / tests / src / Core / Session / UserSessionTest.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\Core\Session;
23
24 use Friendica\Core\Session\Model\UserSession;
25 use Friendica\Core\Session\Type\ArraySession;
26 use Friendica\Test\MockedTest;
27
28 class UserSessionTest extends MockedTest
29 {
30         public function dataLocalUserId()
31         {
32                 return [
33                         'standard' => [
34                                 'data' => [
35                                         'authenticated' => true,
36                                         'uid'           => 21,
37                                 ],
38                                 'expected' => 21,
39                         ],
40                         'not_auth' => [
41                                 'data' => [
42                                         'authenticated' => false,
43                                         'uid'           => 21,
44                                 ],
45                                 'expected' => false,
46                         ],
47                         'no_uid' => [
48                                 'data' => [
49                                         'authenticated' => true,
50                                 ],
51                                 'expected' => false,
52                         ],
53                         'no_auth' => [
54                                 'data' => [
55                                         'uid' => 21,
56                                 ],
57                                 'expected' => false,
58                         ],
59                         'invalid' => [
60                                 'data' => [
61                                         'authenticated' => false,
62                                         'uid'           => 'test',
63                                 ],
64                                 'expected' => false,
65                         ],
66                 ];
67         }
68
69         /**
70          * @dataProvider dataLocalUserId
71          */
72         public function testGetLocalUserId(array $data, $expected)
73         {
74                 $userSession = new UserSession(new ArraySession($data));
75                 $this->assertEquals($expected, $userSession->getLocalUserId());
76         }
77
78         public function testPublicContactId()
79         {
80                 $this->markTestSkipped('Needs Contact::getIdForURL testable first');
81         }
82
83         public function dataGetRemoteUserId()
84         {
85                 return [
86                         'standard' => [
87                                 'data' => [
88                                         'authenticated' => true,
89                                         'visitor_id'    => 21,
90                                 ],
91                                 'expected' => 21,
92                         ],
93                         'not_auth' => [
94                                 'data' => [
95                                         'authenticated' => false,
96                                         'visitor_id'    => 21,
97                                 ],
98                                 'expected' => false,
99                         ],
100                         'no_visitor_id' => [
101                                 'data' => [
102                                         'authenticated' => true,
103                                 ],
104                                 'expected' => false,
105                         ],
106                         'no_auth' => [
107                                 'data' => [
108                                         'visitor_id' => 21,
109                                 ],
110                                 'expected' => false,
111                         ],
112                         'invalid' => [
113                                 'data' => [
114                                         'authenticated' => false,
115                                         'visitor_id'    => 'test',
116                                 ],
117                                 'expected' => false,
118                         ],
119                 ];
120         }
121
122         /**
123          * @dataProvider dataGetRemoteUserId
124          */
125         public function testGetRemoteUserId(array $data, $expected)
126         {
127                 $userSession = new UserSession(new ArraySession($data));
128                 $this->assertEquals($expected, $userSession->getRemoteUserId());
129         }
130
131         /// @fixme Add more data when Contact::getIdForUrl is a dynamic class
132         public function dataGetRemoteContactId()
133         {
134                 return [
135                         'remote_exists' => [
136                                 'uid'  => 1,
137                                 'data' => [
138                                         'remote' => ['1' => '21'],
139                                 ],
140                                 'expected' => 21,
141                         ],
142                 ];
143         }
144
145         /**
146          * @dataProvider dataGetRemoteContactId
147          */
148         public function testGetRemoteContactId(int $uid, array $data, $expected)
149         {
150                 $userSession = new UserSession(new ArraySession($data));
151                 $this->assertEquals($expected, $userSession->getRemoteContactID($uid));
152         }
153
154         public function dataGetUserIdForVisitorContactID()
155         {
156                 return [
157                         'standard' => [
158                                 'cid'  => 21,
159                                 'data' => [
160                                         'remote' => ['3' => '21'],
161                                 ],
162                                 'expected' => 3,
163                         ],
164                         'missing' => [
165                                 'cid'  => 2,
166                                 'data' => [
167                                         'remote' => ['3' => '21'],
168                                 ],
169                                 'expected' => false,
170                         ],
171                         'empty' => [
172                                 'cid'  => 21,
173                                 'data' => [
174                                 ],
175                                 'expected' => false,
176                         ],
177                 ];
178         }
179
180         /** @dataProvider dataGetUserIdForVisitorContactID */
181         public function testGetUserIdForVisitorContactID(int $cid, array $data, $expected)
182         {
183                 $userSession = new UserSession(new ArraySession($data));
184                 $this->assertEquals($expected, $userSession->getUserIDForVisitorContactID($cid));
185         }
186
187         public function dataAuthenticated()
188         {
189                 return [
190                         'authenticated' => [
191                                 'data' => [
192                                         'authenticated' => true,
193                                 ],
194                                 'expected' => true,
195                         ],
196                         'not_authenticated' => [
197                                 'data' => [
198                                         'authenticated' => false,
199                                 ],
200                                 'expected' => false,
201                         ],
202                         'missing' => [
203                                 'data' => [
204                                 ],
205                                 'expected' => false,
206                         ],
207                 ];
208         }
209
210         /**
211          * @dataProvider dataAuthenticated
212          */
213         public function testIsAuthenticated(array $data, $expected)
214         {
215                 $userSession = new UserSession(new ArraySession($data));
216                 $this->assertEquals($expected, $userSession->isAuthenticated());
217         }
218 }