]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ArgumentsTest.php
Replaced most "api_get_user" calls with newer BaseApi calls
[friendica.git] / tests / src / App / ArgumentsTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\App;
23
24 use Friendica\App;
25 use PHPUnit\Framework\TestCase;
26
27 class ArgumentsTest extends TestCase
28 {
29         private function assertArguments(array $assert, App\Arguments $arguments)
30         {
31                 self::assertEquals($assert['queryString'], $arguments->getQueryString());
32                 self::assertEquals($assert['command'], $arguments->getCommand());
33                 self::assertEquals($assert['argv'], $arguments->getArgv());
34                 self::assertEquals($assert['argc'], $arguments->getArgc());
35                 self::assertCount($assert['argc'], $arguments->getArgv());
36         }
37
38         /**
39          * Test the default argument without any determinations
40          */
41         public function testDefault()
42         {
43                 $arguments = new App\Arguments();
44
45                 self::assertArguments([
46                         'queryString' => '',
47                         'command'     => '',
48                         'argv'        => [],
49                         'argc'        => 0,
50                 ],
51                         $arguments);
52         }
53
54         public function dataArguments()
55         {
56                 return [
57                         'withPagename'         => [
58                                 'assert' => [
59                                         'queryString' => 'profile/test/it?arg1=value1&arg2=value2',
60                                         'command'     => 'profile/test/it',
61                                         'argv'        => ['profile', 'test', 'it'],
62                                         'argc'        => 3,
63                                 ],
64                                 'server' => [
65                                         'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2',
66                                 ],
67                                 'get'    => [
68                                         'pagename' => 'profile/test/it',
69                                 ],
70                         ],
71                         'withUnixHomeDir'      => [
72                                 'assert' => [
73                                         'queryString' => '~test/it?arg1=value1&arg2=value2',
74                                         'command'     => '~test/it',
75                                         'argv'        => ['~test', 'it'],
76                                         'argc'        => 2,
77                                 ],
78                                 'server' => [
79                                         'QUERY_STRING' => 'pagename=~test/it&arg1=value1&arg2=value2',
80                                 ],
81                                 'get'    => [
82                                         'pagename' => '~test/it',
83                                 ],
84                         ],
85                         'withDiasporaHomeDir'  => [
86                                 'assert' => [
87                                         'queryString' => 'u/test/it?arg1=value1&arg2=value2',
88                                         'command'     => 'u/test/it',
89                                         'argv'        => ['u', 'test', 'it'],
90                                         'argc'        => 3,
91                                 ],
92                                 'server' => [
93                                         'QUERY_STRING' => 'pagename=u/test/it&arg1=value1&arg2=value2',
94                                 ],
95                                 'get'    => [
96                                         'pagename' => 'u/test/it',
97                                 ],
98                         ],
99                         'withTrailingSlash'    => [
100                                 'assert' => [
101                                         'queryString' => 'profile/test/it?arg1=value1&arg2=value2%2F',
102                                         'command'     => 'profile/test/it',
103                                         'argv'        => ['profile', 'test', 'it'],
104                                         'argc'        => 3,
105                                 ],
106                                 'server' => [
107                                         'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2/',
108                                 ],
109                                 'get'    => [
110                                         'pagename' => 'profile/test/it',
111                                 ],
112                         ],
113                         'withWrongQueryString' => [
114                                 'assert' => [
115                                         'queryString' => 'profile/test/it?wrong=profile%2Ftest%2Fit&arg1=value1&arg2=value2%2F',
116                                         'command'     => 'profile/test/it',
117                                         'argv'        => ['profile', 'test', 'it'],
118                                         'argc'        => 3,
119                                 ],
120                                 'server' => [
121                                         'QUERY_STRING' => 'wrong=profile/test/it&arg1=value1&arg2=value2/',
122                                 ],
123                                 'get'    => [
124                                         'pagename' => 'profile/test/it',
125                                 ],
126                         ],
127                         'withMissingPageName'  => [
128                                 'assert' => [
129                                         'queryString' => 'notvalid/it?arg1=value1&arg2=value2%2F',
130                                         'command'     => 'notvalid/it',
131                                         'argv'        => ['notvalid', 'it'],
132                                         'argc'        => 2,
133                                 ],
134                                 'server' => [
135                                         'QUERY_STRING' => 'pagename=notvalid/it&arg1=value1&arg2=value2/',
136                                 ],
137                                 'get'    => [
138                                 ],
139                         ],
140                         'withNothing'  => [
141                                 'assert' => [
142                                         'queryString' => '?arg1=value1&arg2=value2%2F',
143                                         'command'     => '',
144                                         'argv'        => [],
145                                         'argc'        => 0,
146                                 ],
147                                 'server' => [
148                                         'QUERY_STRING' => 'arg1=value1&arg2=value2/',
149                                 ],
150                                 'get'    => [
151                                 ],
152                         ],
153                         'withFileExtension'  => [
154                                 'assert' => [
155                                         'queryString' => 'api/call.json',
156                                         'command'     => 'api/call.json',
157                                         'argv'        => ['api', 'call.json'],
158                                         'argc'        => 2,
159                                 ],
160                                 'server' => [
161                                         'QUERY_STRING' => 'pagename=api/call.json',
162                                 ],
163                                 'get'    => [
164                                         'pagename' => 'api/call.json'
165                                 ],
166                         ],
167                 ];
168         }
169
170         /**
171          * Test all variants of argument determination
172          *
173          * @dataProvider dataArguments
174          */
175         public function testDetermine(array $assert, array $server, array $get)
176         {
177                 $arguments = (new App\Arguments())
178                         ->determine($server, $get);
179
180                 self::assertArguments($assert, $arguments);
181         }
182
183         /**
184          * Test if the get/has methods are working for the determined arguments
185          *
186          * @dataProvider dataArguments
187          */
188         public function testGetHas(array $assert, array $server, array $get)
189         {
190                 $arguments = (new App\Arguments())
191                         ->determine($server, $get);
192
193                 for ($i = 0; $i < $arguments->getArgc(); $i++) {
194                         self::assertTrue($arguments->has($i));
195                         self::assertEquals($assert['argv'][$i], $arguments->get($i));
196                 }
197
198                 self::assertFalse($arguments->has($arguments->getArgc()));
199                 self::assertEmpty($arguments->get($arguments->getArgc()));
200                 self::assertEquals('default', $arguments->get($arguments->getArgc(), 'default'));
201         }
202
203         public function dataStripped()
204         {
205                 return [
206                         'strippedZRLFirst'  => [
207                                 'assert' => '?arg1=value1',
208                                 'input'  => '&zrl=nope&arg1=value1',
209                         ],
210                         'strippedZRLLast'   => [
211                                 'assert' => '?arg1=value1',
212                                 'input'  => '&arg1=value1&zrl=nope',
213                         ],
214                         'strippedZTLMiddle' => [
215                                 'assert' => '?arg1=value1&arg2=value2',
216                                 'input'  => '&arg1=value1&zrl=nope&arg2=value2',
217                         ],
218                         'strippedOWTFirst'  => [
219                                 'assert' => '?arg1=value1',
220                                 'input'  => '&owt=test&arg1=value1',
221                         ],
222                         'strippedOWTLast'   => [
223                                 'assert' => '?arg1=value1',
224                                 'input'  => '&arg1=value1&owt=test',
225                         ],
226                         'strippedOWTMiddle' => [
227                                 'assert' => '?arg1=value1&arg2=value2',
228                                 'input'  => '&arg1=value1&owt=test&arg2=value2',
229                         ],
230                 ];
231         }
232
233         /**
234          * Test the ZRL and OWT stripping
235          *
236          * @dataProvider dataStripped
237          */
238         public function testStrippedQueries(string $assert, string $input)
239         {
240                 $command = 'test/it';
241
242                 $arguments = (new App\Arguments())
243                         ->determine(['QUERY_STRING' => 'pagename=' . $command . $input,], ['pagename' => $command]);
244
245                 self::assertEquals($command . $assert, $arguments->getQueryString());
246         }
247
248         /**
249          * Test that arguments are immutable
250          */
251         public function testImmutable()
252         {
253                 $argument = new App\Arguments();
254
255                 $argNew = $argument->determine([], []);
256
257                 self::assertNotSame($argument, $argNew);
258         }
259 }