3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\App;
25 use PHPUnit\Framework\TestCase;
27 class ArgumentsTest extends TestCase
29 private function assertArguments(array $assert, App\Arguments $arguments)
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());
39 * Test the default argument without any determinations
41 public function testDefault()
43 $arguments = new App\Arguments();
45 self::assertArguments([
54 public function dataArguments()
59 'queryString' => 'profile/test/it?arg1=value1&arg2=value2',
60 'command' => 'profile/test/it',
61 'argv' => ['profile', 'test', 'it'],
65 'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2',
68 'pagename' => 'profile/test/it',
71 'withUnixHomeDir' => [
73 'queryString' => '~test/it?arg1=value1&arg2=value2',
74 'command' => '~test/it',
75 'argv' => ['~test', 'it'],
79 'QUERY_STRING' => 'pagename=~test/it&arg1=value1&arg2=value2',
82 'pagename' => '~test/it',
85 'withDiasporaHomeDir' => [
87 'queryString' => 'u/test/it?arg1=value1&arg2=value2',
88 'command' => 'u/test/it',
89 'argv' => ['u', 'test', 'it'],
93 'QUERY_STRING' => 'pagename=u/test/it&arg1=value1&arg2=value2',
96 'pagename' => 'u/test/it',
99 'withTrailingSlash' => [
101 'queryString' => 'profile/test/it?arg1=value1&arg2=value2%2F',
102 'command' => 'profile/test/it',
103 'argv' => ['profile', 'test', 'it'],
107 'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2/',
110 'pagename' => 'profile/test/it',
113 'withWrongQueryString' => [
115 'queryString' => 'profile/test/it?wrong=profile%2Ftest%2Fit&arg1=value1&arg2=value2%2F',
116 'command' => 'profile/test/it',
117 'argv' => ['profile', 'test', 'it'],
121 'QUERY_STRING' => 'wrong=profile/test/it&arg1=value1&arg2=value2/',
124 'pagename' => 'profile/test/it',
127 'withMissingPageName' => [
129 'queryString' => 'notvalid/it?arg1=value1&arg2=value2%2F',
130 'command' => 'notvalid/it',
131 'argv' => ['notvalid', 'it'],
135 'QUERY_STRING' => 'pagename=notvalid/it&arg1=value1&arg2=value2/',
142 'queryString' => '?arg1=value1&arg2=value2%2F',
148 'QUERY_STRING' => 'arg1=value1&arg2=value2/',
153 'withFileExtension' => [
155 'queryString' => 'api/call.json',
156 'command' => 'api/call.json',
157 'argv' => ['api', 'call.json'],
161 'QUERY_STRING' => 'pagename=api/call.json',
164 'pagename' => 'api/call.json'
171 * Test all variants of argument determination
173 * @dataProvider dataArguments
175 public function testDetermine(array $assert, array $server, array $get)
177 $arguments = (new App\Arguments())
178 ->determine($server, $get);
180 self::assertArguments($assert, $arguments);
184 * Test if the get/has methods are working for the determined arguments
186 * @dataProvider dataArguments
188 public function testGetHas(array $assert, array $server, array $get)
190 $arguments = (new App\Arguments())
191 ->determine($server, $get);
193 for ($i = 0; $i < $arguments->getArgc(); $i++) {
194 self::assertTrue($arguments->has($i));
195 self::assertEquals($assert['argv'][$i], $arguments->get($i));
198 self::assertFalse($arguments->has($arguments->getArgc()));
199 self::assertEmpty($arguments->get($arguments->getArgc()));
200 self::assertEquals('default', $arguments->get($arguments->getArgc(), 'default'));
203 public function dataStripped()
206 'strippedZRLFirst' => [
207 'assert' => '?arg1=value1',
208 'input' => '&zrl=nope&arg1=value1',
210 'strippedZRLLast' => [
211 'assert' => '?arg1=value1',
212 'input' => '&arg1=value1&zrl=nope',
214 'strippedZTLMiddle' => [
215 'assert' => '?arg1=value1&arg2=value2',
216 'input' => '&arg1=value1&zrl=nope&arg2=value2',
218 'strippedOWTFirst' => [
219 'assert' => '?arg1=value1',
220 'input' => '&owt=test&arg1=value1',
222 'strippedOWTLast' => [
223 'assert' => '?arg1=value1',
224 'input' => '&arg1=value1&owt=test',
226 'strippedOWTMiddle' => [
227 'assert' => '?arg1=value1&arg2=value2',
228 'input' => '&arg1=value1&owt=test&arg2=value2',
234 * Test the ZRL and OWT stripping
236 * @dataProvider dataStripped
238 public function testStrippedQueries(string $assert, string $input)
240 $command = 'test/it';
242 $arguments = (new App\Arguments())
243 ->determine(['QUERY_STRING' => 'pagename=' . $command . $input,], ['pagename' => $command]);
245 self::assertEquals($command . $assert, $arguments->getQueryString());
249 * Test that arguments are immutable
251 public function testImmutable()
253 $argument = new App\Arguments();
255 $argNew = $argument->determine([], []);
257 self::assertNotSame($argument, $argNew);