3 * @copyright Copyright (C) 2020, Friendica
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 $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());
39 * Test the default argument without any determinations
41 public function testDefault()
43 $arguments = new App\Arguments();
45 $this->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',
73 'queryString' => 'profile/test/it?arg1=value1&arg2=value2',
74 'command' => 'profile/test/it',
75 'argv' => ['profile', 'test', 'it'],
79 'QUERY_STRING' => 'q=profile/test/it?arg1=value1&arg2=value2',
82 'q' => 'profile/test/it',
85 'withWrongDelimiter' => [
87 'queryString' => 'profile/test/it?arg1=value1&arg2=value2',
88 'command' => 'profile/test/it',
89 'argv' => ['profile', 'test', 'it'],
93 'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2',
96 'pagename' => 'profile/test/it',
99 'withUnixHomeDir' => [
101 'queryString' => '~test/it?arg1=value1&arg2=value2',
102 'command' => 'profile/test/it',
103 'argv' => ['profile', 'test', 'it'],
107 'QUERY_STRING' => 'pagename=~test/it?arg1=value1&arg2=value2',
110 'pagename' => '~test/it',
113 'withDiasporaHomeDir' => [
115 'queryString' => 'u/test/it?arg1=value1&arg2=value2',
116 'command' => 'profile/test/it',
117 'argv' => ['profile', 'test', 'it'],
121 'QUERY_STRING' => 'pagename=u/test/it?arg1=value1&arg2=value2',
124 'pagename' => 'u/test/it',
127 'withTrailingSlash' => [
129 'queryString' => 'profile/test/it?arg1=value1&arg2=value2/',
130 'command' => 'profile/test/it',
131 'argv' => ['profile', 'test', 'it'],
135 'QUERY_STRING' => 'pagename=profile/test/it?arg1=value1&arg2=value2/',
138 'pagename' => 'profile/test/it',
141 'withWrongQueryString' => [
143 // empty query string?!
145 'command' => 'profile/test/it',
146 'argv' => ['profile', 'test', 'it'],
150 'QUERY_STRING' => 'wrong=profile/test/it?arg1=value1&arg2=value2/',
153 'pagename' => 'profile/test/it',
156 'withMissingPageName' => [
158 'queryString' => 'notvalid/it?arg1=value1&arg2=value2/',
159 'command' => App\Module::DEFAULT,
160 'argv' => [App\Module::DEFAULT],
164 'QUERY_STRING' => 'pagename=notvalid/it?arg1=value1&arg2=value2/',
173 * Test all variants of argument determination
175 * @dataProvider dataArguments
177 public function testDetermine(array $assert, array $server, array $get)
179 $arguments = (new App\Arguments())
180 ->determine($server, $get);
182 $this->assertArguments($assert, $arguments);
186 * Test if the get/has methods are working for the determined arguments
188 * @dataProvider dataArguments
190 public function testGetHas(array $assert, array $server, array $get)
192 $arguments = (new App\Arguments())
193 ->determine($server, $get);
195 for ($i = 0; $i < $arguments->getArgc(); $i++) {
196 $this->assertTrue($arguments->has($i));
197 $this->assertEquals($assert['argv'][$i], $arguments->get($i));
200 $this->assertFalse($arguments->has($arguments->getArgc()));
201 $this->assertEmpty($arguments->get($arguments->getArgc()));
202 $this->assertEquals('default', $arguments->get($arguments->getArgc(), 'default'));
205 public function dataStripped()
208 'strippedZRLFirst' => [
209 'assert' => '?arg1=value1',
210 'input' => '?zrl=nope&arg1=value1',
212 'strippedZRLLast' => [
213 'assert' => '?arg1=value1',
214 'input' => '?arg1=value1&zrl=nope',
216 'strippedZTLMiddle' => [
217 'assert' => '?arg1=value1&arg2=value2',
218 'input' => '?arg1=value1&zrl=nope&arg2=value2',
220 'strippedOWTFirst' => [
221 'assert' => '?arg1=value1',
222 'input' => '?owt=test&arg1=value1',
224 'strippedOWTLast' => [
225 'assert' => '?arg1=value1',
226 'input' => '?arg1=value1&owt=test',
228 'strippedOWTMiddle' => [
229 'assert' => '?arg1=value1&arg2=value2',
230 'input' => '?arg1=value1&owt=test&arg2=value2',
236 * Test the ZRL and OWT stripping
238 * @dataProvider dataStripped
240 public function testStrippedQueries(string $assert, string $input)
242 $command = 'test/it';
244 $arguments = (new App\Arguments())
245 ->determine(['QUERY_STRING' => 'q=' . $command . $input,], ['pagename' => $command]);
247 $this->assertEquals($command . $assert, $arguments->getQueryString());
251 * Test that arguments are immutable
253 public function testImmutable()
255 $argument = new App\Arguments();
257 $argNew = $argument->determine([], []);
259 $this->assertNotSame($argument, $argNew);