]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ArgumentsTest.php
Merge pull request #8269 from MrPetovan/bug/frio-more-actions
[friendica.git] / tests / src / App / ArgumentsTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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                 $this->assertEquals($assert['queryString'], $arguments->getQueryString());
32                 $this->assertEquals($assert['command'], $arguments->getCommand());
33                 $this->assertEquals($assert['argv'], $arguments->getArgv());
34                 $this->assertEquals($assert['argc'], $arguments->getArgc());
35                 $this->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                 $this->assertArguments([
46                         'queryString' => '',
47                         'command'     => '',
48                         'argv'        => ['home'],
49                         'argc'        => 1,
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                         'withQ'                => [
72                                 'assert' => [
73                                         'queryString' => 'profile/test/it?arg1=value1&arg2=value2',
74                                         'command'     => 'profile/test/it',
75                                         'argv'        => ['profile', 'test', 'it'],
76                                         'argc'        => 3,
77                                 ],
78                                 'server' => [
79                                         'QUERY_STRING' => 'q=profile/test/it?arg1=value1&arg2=value2',
80                                 ],
81                                 'get'    => [
82                                         'q' => 'profile/test/it',
83                                 ],
84                         ],
85                         'withWrongDelimiter'   => [
86                                 'assert' => [
87                                         'queryString' => 'profile/test/it?arg1=value1&arg2=value2',
88                                         'command'     => 'profile/test/it',
89                                         'argv'        => ['profile', 'test', 'it'],
90                                         'argc'        => 3,
91                                 ],
92                                 'server' => [
93                                         'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2',
94                                 ],
95                                 'get'    => [
96                                         'pagename' => 'profile/test/it',
97                                 ],
98                         ],
99                         'withUnixHomeDir'      => [
100                                 'assert' => [
101                                         'queryString' => '~test/it?arg1=value1&arg2=value2',
102                                         'command'     => 'profile/test/it',
103                                         'argv'        => ['profile', 'test', 'it'],
104                                         'argc'        => 3,
105                                 ],
106                                 'server' => [
107                                         'QUERY_STRING' => 'pagename=~test/it?arg1=value1&arg2=value2',
108                                 ],
109                                 'get'    => [
110                                         'pagename' => '~test/it',
111                                 ],
112                         ],
113                         'withDiasporaHomeDir'  => [
114                                 'assert' => [
115                                         'queryString' => 'u/test/it?arg1=value1&arg2=value2',
116                                         'command'     => 'profile/test/it',
117                                         'argv'        => ['profile', 'test', 'it'],
118                                         'argc'        => 3,
119                                 ],
120                                 'server' => [
121                                         'QUERY_STRING' => 'pagename=u/test/it?arg1=value1&arg2=value2',
122                                 ],
123                                 'get'    => [
124                                         'pagename' => 'u/test/it',
125                                 ],
126                         ],
127                         'withTrailingSlash'    => [
128                                 'assert' => [
129                                         'queryString' => 'profile/test/it?arg1=value1&arg2=value2/',
130                                         'command'     => 'profile/test/it',
131                                         'argv'        => ['profile', 'test', 'it'],
132                                         'argc'        => 3,
133                                 ],
134                                 'server' => [
135                                         'QUERY_STRING' => 'pagename=profile/test/it?arg1=value1&arg2=value2/',
136                                 ],
137                                 'get'    => [
138                                         'pagename' => 'profile/test/it',
139                                 ],
140                         ],
141                         'withWrongQueryString' => [
142                                 'assert' => [
143                                         // empty query string?!
144                                         'queryString' => '',
145                                         'command'     => 'profile/test/it',
146                                         'argv'        => ['profile', 'test', 'it'],
147                                         'argc'        => 3,
148                                 ],
149                                 'server' => [
150                                         'QUERY_STRING' => 'wrong=profile/test/it?arg1=value1&arg2=value2/',
151                                 ],
152                                 'get'    => [
153                                         'pagename' => 'profile/test/it',
154                                 ],
155                         ],
156                         'withMissingPageName'  => [
157                                 'assert' => [
158                                         'queryString' => 'notvalid/it?arg1=value1&arg2=value2/',
159                                         'command'     => App\Module::DEFAULT,
160                                         'argv'        => [App\Module::DEFAULT],
161                                         'argc'        => 1,
162                                 ],
163                                 'server' => [
164                                         'QUERY_STRING' => 'pagename=notvalid/it?arg1=value1&arg2=value2/',
165                                 ],
166                                 'get'    => [
167                                 ],
168                         ],
169                 ];
170         }
171
172         /**
173          * Test all variants of argument determination
174          *
175          * @dataProvider dataArguments
176          */
177         public function testDetermine(array $assert, array $server, array $get)
178         {
179                 $arguments = (new App\Arguments())
180                         ->determine($server, $get);
181
182                 $this->assertArguments($assert, $arguments);
183         }
184
185         /**
186          * Test if the get/has methods are working for the determined arguments
187          *
188          * @dataProvider dataArguments
189          */
190         public function testGetHas(array $assert, array $server, array $get)
191         {
192                 $arguments = (new App\Arguments())
193                         ->determine($server, $get);
194
195                 for ($i = 0; $i < $arguments->getArgc(); $i++) {
196                         $this->assertTrue($arguments->has($i));
197                         $this->assertEquals($assert['argv'][$i], $arguments->get($i));
198                 }
199
200                 $this->assertFalse($arguments->has($arguments->getArgc()));
201                 $this->assertEmpty($arguments->get($arguments->getArgc()));
202                 $this->assertEquals('default', $arguments->get($arguments->getArgc(), 'default'));
203         }
204
205         public function dataStripped()
206         {
207                 return [
208                         'strippedZRLFirst'  => [
209                                 'assert' => '?arg1=value1',
210                                 'input'  => '?zrl=nope&arg1=value1',
211                         ],
212                         'strippedZRLLast'   => [
213                                 'assert' => '?arg1=value1',
214                                 'input'  => '?arg1=value1&zrl=nope',
215                         ],
216                         'strippedZTLMiddle' => [
217                                 'assert' => '?arg1=value1&arg2=value2',
218                                 'input'  => '?arg1=value1&zrl=nope&arg2=value2',
219                         ],
220                         'strippedOWTFirst'  => [
221                                 'assert' => '?arg1=value1',
222                                 'input'  => '?owt=test&arg1=value1',
223                         ],
224                         'strippedOWTLast'   => [
225                                 'assert' => '?arg1=value1',
226                                 'input'  => '?arg1=value1&owt=test',
227                         ],
228                         'strippedOWTMiddle' => [
229                                 'assert' => '?arg1=value1&arg2=value2',
230                                 'input'  => '?arg1=value1&owt=test&arg2=value2',
231                         ],
232                 ];
233         }
234
235         /**
236          * Test the ZRL and OWT stripping
237          *
238          * @dataProvider dataStripped
239          */
240         public function testStrippedQueries(string $assert, string $input)
241         {
242                 $command = 'test/it';
243
244                 $arguments = (new App\Arguments())
245                         ->determine(['QUERY_STRING' => 'q=' . $command . $input,], ['pagename' => $command]);
246
247                 $this->assertEquals($command . $assert, $arguments->getQueryString());
248         }
249
250         /**
251          * Test that arguments are immutable
252          */
253         public function testImmutable()
254         {
255                 $argument = new App\Arguments();
256
257                 $argNew = $argument->determine([], []);
258
259                 $this->assertNotSame($argument, $argNew);
260         }
261 }