friendica/tests/src/App/ArgumentsTest.php

286 lines
7.6 KiB
PHP
Raw Normal View History

2019-08-12 18:13:58 +02:00
<?php
2020-02-09 15:45:36 +01:00
/**
* @copyright Copyright (C) 2010-2024, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2019-08-12 18:13:58 +02:00
namespace Friendica\Test\src\App;
use Friendica\App;
use PHPUnit\Framework\TestCase;
class ArgumentsTest extends TestCase
{
private function assertArguments(array $assert, App\Arguments $arguments)
{
self::assertEquals($assert['queryString'], $arguments->getQueryString());
self::assertEquals($assert['command'], $arguments->getCommand());
self::assertEquals($assert['argv'], $arguments->getArgv());
self::assertEquals($assert['argc'], $arguments->getArgc());
2022-01-02 22:21:41 +01:00
self::assertEquals($assert['method'], $arguments->getMethod());
self::assertCount($assert['argc'], $arguments->getArgv());
2019-08-12 18:13:58 +02:00
}
/**
* Test the default argument without any determinations
*/
public function testDefault()
{
$arguments = new App\Arguments();
self::assertArguments([
2019-08-12 18:13:58 +02:00
'queryString' => '',
'command' => '',
'argv' => [],
'argc' => 0,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET
2019-08-12 18:13:58 +02:00
],
$arguments);
}
public function dataArguments()
{
return [
'withPagename' => [
'assert' => [
'queryString' => 'profile/test/it?arg1=value1&arg2=value2',
'command' => 'profile/test/it',
'argv' => ['profile', 'test', 'it'],
'argc' => 3,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2',
2019-08-12 18:13:58 +02:00
],
'get' => [
'pagename' => 'profile/test/it',
],
],
'withUnixHomeDir' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'queryString' => '~test/it?arg1=value1&arg2=value2',
'command' => '~test/it',
'argv' => ['~test', 'it'],
'argc' => 2,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'pagename=~test/it&arg1=value1&arg2=value2',
2019-08-12 18:13:58 +02:00
],
'get' => [
'pagename' => '~test/it',
2019-08-12 18:13:58 +02:00
],
],
'withDiasporaHomeDir' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'queryString' => 'u/test/it?arg1=value1&arg2=value2',
'command' => 'u/test/it',
'argv' => ['u', 'test', 'it'],
2019-08-12 18:13:58 +02:00
'argc' => 3,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'pagename=u/test/it&arg1=value1&arg2=value2',
2019-08-12 18:13:58 +02:00
],
'get' => [
'pagename' => 'u/test/it',
2019-08-12 18:13:58 +02:00
],
],
'withTrailingSlash' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'queryString' => 'profile/test/it?arg1=value1&arg2=value2%2F',
2019-08-12 18:13:58 +02:00
'command' => 'profile/test/it',
'argv' => ['profile', 'test', 'it'],
'argc' => 3,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'pagename=profile/test/it&arg1=value1&arg2=value2/',
2019-08-12 18:13:58 +02:00
],
'get' => [
'pagename' => 'profile/test/it',
2019-08-12 18:13:58 +02:00
],
],
'withWrongQueryString' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'queryString' => 'profile/test/it?wrong=profile%2Ftest%2Fit&arg1=value1&arg2=value2%2F',
2019-08-12 18:13:58 +02:00
'command' => 'profile/test/it',
'argv' => ['profile', 'test', 'it'],
'argc' => 3,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'wrong=profile/test/it&arg1=value1&arg2=value2/',
2019-08-12 18:13:58 +02:00
],
'get' => [
'pagename' => 'profile/test/it',
2019-08-12 18:13:58 +02:00
],
],
'withMissingPageName' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'queryString' => 'notvalid/it?arg1=value1&arg2=value2%2F',
'command' => 'notvalid/it',
'argv' => ['notvalid', 'it'],
'argc' => 2,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'pagename=notvalid/it&arg1=value1&arg2=value2/',
2019-08-12 18:13:58 +02:00
],
'get' => [
],
],
'withNothing' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'queryString' => '?arg1=value1&arg2=value2%2F',
'command' => '',
'argv' => [],
'argc' => 0,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'arg1=value1&arg2=value2/',
2019-08-12 18:13:58 +02:00
],
'get' => [
],
],
'withFileExtension' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'queryString' => 'api/call.json',
'command' => 'api/call.json',
'argv' => ['api', 'call.json'],
'argc' => 2,
2022-01-02 22:21:41 +01:00
'method' => App\Router::GET,
2019-08-12 18:13:58 +02:00
],
'server' => [
'QUERY_STRING' => 'pagename=api/call.json',
2019-08-12 18:13:58 +02:00
],
'get' => [
'pagename' => 'api/call.json'
2019-08-12 18:13:58 +02:00
],
],
2022-01-02 22:21:41 +01:00
'withHTTPMethod' => [
'assert' => [
'queryString' => 'api/call.json',
'command' => 'api/call.json',
'argv' => ['api', 'call.json'],
'argc' => 2,
'method' => App\Router::POST,
],
'server' => [
'QUERY_STRING' => 'pagename=api/call.json',
'REQUEST_METHOD' => App\Router::POST,
],
'get' => [
'pagename' => 'api/call.json'
],
],
2019-08-12 18:13:58 +02:00
];
}
/**
* Test all variants of argument determination
*
* @dataProvider dataArguments
*/
public function testDetermine(array $assert, array $server, array $get)
{
$arguments = (new App\Arguments())
->determine($server, $get);
self::assertArguments($assert, $arguments);
2019-08-12 18:13:58 +02:00
}
/**
* Test if the get/has methods are working for the determined arguments
*
* @dataProvider dataArguments
*/
public function testGetHas(array $assert, array $server, array $get)
{
$arguments = (new App\Arguments())
->determine($server, $get);
for ($i = 0; $i < $arguments->getArgc(); $i++) {
self::assertTrue($arguments->has($i));
self::assertEquals($assert['argv'][$i], $arguments->get($i));
2019-08-12 18:13:58 +02:00
}
self::assertFalse($arguments->has($arguments->getArgc()));
self::assertEmpty($arguments->get($arguments->getArgc()));
self::assertEquals('default', $arguments->get($arguments->getArgc(), 'default'));
2019-08-12 18:13:58 +02:00
}
public function dataStripped()
{
return [
'strippedZRLFirst' => [
'assert' => '?arg1=value1',
'input' => '&zrl=nope&arg1=value1',
2019-08-12 18:13:58 +02:00
],
'strippedZRLLast' => [
'assert' => '?arg1=value1',
'input' => '&arg1=value1&zrl=nope',
2019-08-12 18:13:58 +02:00
],
'strippedZTLMiddle' => [
'assert' => '?arg1=value1&arg2=value2',
'input' => '&arg1=value1&zrl=nope&arg2=value2',
2019-08-12 18:13:58 +02:00
],
'strippedOWTFirst' => [
'assert' => '?arg1=value1',
'input' => '&owt=test&arg1=value1',
2019-08-12 18:13:58 +02:00
],
'strippedOWTLast' => [
'assert' => '?arg1=value1',
'input' => '&arg1=value1&owt=test',
2019-08-12 18:13:58 +02:00
],
'strippedOWTMiddle' => [
'assert' => '?arg1=value1&arg2=value2',
'input' => '&arg1=value1&owt=test&arg2=value2',
2019-08-12 18:13:58 +02:00
],
];
}
/**
* Test the ZRL and OWT stripping
*
* @dataProvider dataStripped
*/
public function testStrippedQueries(string $assert, string $input)
{
$command = 'test/it';
$arguments = (new App\Arguments())
->determine(['QUERY_STRING' => 'pagename=' . $command . $input,], ['pagename' => $command]);
2019-08-12 18:13:58 +02:00
self::assertEquals($command . $assert, $arguments->getQueryString());
2019-08-12 18:13:58 +02:00
}
/**
* Test that arguments are immutable
*/
public function testImmutable()
{
$argument = new App\Arguments();
$argNew = $argument->determine([], []);
self::assertNotSame($argument, $argNew);
2019-08-12 18:13:58 +02:00
}
}