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;
24 use Friendica\App\Router;
25 use Friendica\Core\Cache\Capability\ICanCache;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Lock\Capability\ICanLock;
29 use Friendica\Network\HTTPException\MethodNotAllowedException;
30 use Friendica\Network\HTTPException\NotFoundException;
32 use Mockery\MockInterface;
33 use PHPUnit\Framework\TestCase;
35 class RouterTest extends TestCase
37 /** @var L10n|MockInterface */
48 protected function setUp() : void
52 $this->l10n = Mockery::mock(L10n::class);
53 $this->l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
55 $this->cache = Mockery::mock(ICanCache::class);
56 $this->cache->shouldReceive('get')->andReturn(null);
57 $this->cache->shouldReceive('set')->andReturn(false);
59 $this->lock = Mockery::mock(ICanLock::class);
60 $this->lock->shouldReceive('acquire')->andReturn(true);
61 $this->lock->shouldReceive('isLocked')->andReturn(false);
64 public function testGetModuleClass()
66 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
68 $routeCollector = $router->getRouteCollector();
69 $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName');
70 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
71 $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
72 $routeCollector->addRoute([Router::GET], '/test/sub', 'TestSubModuleClassName');
73 $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
74 $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
75 $routeCollector->addRoute([Router::GET], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
77 self::assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
78 self::assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
79 self::assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
80 self::assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
81 self::assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
82 self::assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
83 self::assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
84 self::assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
85 self::assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
88 public function testPostModuleClass()
90 $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache, $this->lock);
92 $routeCollector = $router->getRouteCollector();
93 $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName');
94 $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
95 $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
96 $routeCollector->addRoute([Router::POST], '/test/sub', 'TestSubModuleClassName');
97 $routeCollector->addRoute([Router::POST], '/optional[/option]', 'OptionalModuleClassName');
98 $routeCollector->addRoute([Router::POST], '/variable/{var}', 'VariableModuleClassName');
99 $routeCollector->addRoute([Router::POST], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
101 self::assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
102 self::assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
103 self::assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
104 self::assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
105 self::assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
106 self::assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
107 self::assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
108 self::assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
109 self::assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
112 public function testGetModuleClassNotFound()
114 $this->expectException(NotFoundException::class);
116 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
118 $router->getModuleClass('/unsupported');
121 public function testGetModuleClassNotFoundTypo()
123 $this->expectException(NotFoundException::class);
125 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
127 $routeCollector = $router->getRouteCollector();
128 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
130 $router->getModuleClass('/tes');
133 public function testGetModuleClassNotFoundOptional()
135 $this->expectException(NotFoundException::class);
137 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
139 $routeCollector = $router->getRouteCollector();
140 $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
142 $router->getModuleClass('/optional/opt');
145 public function testGetModuleClassNotFoundVariable()
147 $this->expectException(NotFoundException::class);
149 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
151 $routeCollector = $router->getRouteCollector();
152 $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
154 $router->getModuleClass('/variable');
157 public function testGetModuleClassMethodNotAllowed()
159 $this->expectException(MethodNotAllowedException::class);
161 $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache, $this->lock);
163 $routeCollector = $router->getRouteCollector();
164 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
166 $router->getModuleClass('/test');
169 public function testPostModuleClassMethodNotAllowed()
171 $this->expectException(MethodNotAllowedException::class);
173 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
175 $routeCollector = $router->getRouteCollector();
176 $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
178 $router->getModuleClass('/test');
181 public function dataRoutes()
186 '/' => [Module\Home::class, [Router::GET]],
188 '/route' => [Module\Friendica::class, [Router::GET]],
194 '/route' => [Module\Xrd::class, [Router::GET]],
198 '/it' => [Module\WellKnown\NodeInfo::class, [Router::POST]],
200 '/double' => [Module\Profile\Index::class, [Router::GET, Router::POST]]
207 * @dataProvider dataRoutes
209 public function testGetRoutes(array $routes)
211 $router = (new Router(
212 ['REQUEST_METHOD' => Router::GET],
217 ))->loadRoutes($routes);
219 self::assertEquals(Module\Home::class, $router->getModuleClass('/'));
220 self::assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
221 self::assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
222 self::assertEquals(Module\Profile\Index::class, $router->getModuleClass('/double'));
226 * @dataProvider dataRoutes
228 public function testPostRouter(array $routes)
230 $router = (new Router([
231 'REQUEST_METHOD' => Router::POST
232 ], '', $this->l10n, $this->cache, $this->lock))->loadRoutes($routes);
235 self::assertEquals(Module\WellKnown\NodeInfo::class, $router->getModuleClass('/post/it'));
236 self::assertEquals(Module\Profile\Index::class, $router->getModuleClass('/double'));