summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-11-21 12:17:28 +0100
committermsquare <msquare@notrademark.de>2019-06-12 10:21:20 +0200
commit2e51fbff9d8472a0e98af39aff52d30f0b67706b (patch)
treea41eaf8fa3296fb2892b97fde1074edc566be4c3
parente948091066e4893b1b823fc80db1c1ebba174b53 (diff)
Added / route with redirects
-rw-r--r--config/routes.php4
-rw-r--r--src/Http/Exceptions/HttpPermanentRedirect.php17
-rw-r--r--src/Http/Exceptions/HttpRedirect.php23
-rw-r--r--src/Http/Exceptions/HttpTemporaryRedirect.php17
-rw-r--r--src/Middleware/LegacyMiddleware.php3
-rw-r--r--tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php20
-rw-r--r--tests/Unit/Http/Exceptions/HttpRedirectTest.php26
-rw-r--r--tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php20
-rw-r--r--tests/Unit/Middleware/LegacyMiddlewareTest.php30
9 files changed, 137 insertions, 23 deletions
diff --git a/config/routes.php b/config/routes.php
index b9e3646a..6f61ec71 100644
--- a/config/routes.php
+++ b/config/routes.php
@@ -1,10 +1,14 @@
<?php
+use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
use FastRoute\RouteCollector;
/** @var RouteCollector $route */
// Pages
+$route->get('/', function () {
+ throw new HttpTemporaryRedirect(auth()->user() ? config('home_site') : 'login');
+});
$route->get('/credits', 'CreditsController@index');
// Stats
diff --git a/src/Http/Exceptions/HttpPermanentRedirect.php b/src/Http/Exceptions/HttpPermanentRedirect.php
new file mode 100644
index 00000000..a1aa986f
--- /dev/null
+++ b/src/Http/Exceptions/HttpPermanentRedirect.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace Engelsystem\Http\Exceptions;
+
+class HttpPermanentRedirect extends HttpRedirect
+{
+ /**
+ * @param string $url
+ * @param array $headers
+ */
+ public function __construct(
+ string $url,
+ array $headers = []
+ ) {
+ parent::__construct($url, 301, $headers);
+ }
+}
diff --git a/src/Http/Exceptions/HttpRedirect.php b/src/Http/Exceptions/HttpRedirect.php
new file mode 100644
index 00000000..0e7d0250
--- /dev/null
+++ b/src/Http/Exceptions/HttpRedirect.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Engelsystem\Http\Exceptions;
+
+class HttpRedirect extends HttpException
+{
+ /**
+ * @param string $url
+ * @param int $statusCode
+ * @param array $headers
+ */
+ public function __construct(
+ string $url,
+ int $statusCode = 302,
+ array $headers = []
+ ) {
+ $headers = array_merge([
+ 'Location' => $url,
+ ], $headers);
+
+ parent::__construct($statusCode, '', $headers);
+ }
+}
diff --git a/src/Http/Exceptions/HttpTemporaryRedirect.php b/src/Http/Exceptions/HttpTemporaryRedirect.php
new file mode 100644
index 00000000..ece8d607
--- /dev/null
+++ b/src/Http/Exceptions/HttpTemporaryRedirect.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace Engelsystem\Http\Exceptions;
+
+class HttpTemporaryRedirect extends HttpRedirect
+{
+ /**
+ * @param string $url
+ * @param array $headers
+ */
+ public function __construct(
+ string $url,
+ array $headers = []
+ ) {
+ parent::__construct($url, 302, $headers);
+ }
+}
diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php
index de16e557..bd3987d2 100644
--- a/src/Middleware/LegacyMiddleware.php
+++ b/src/Middleware/LegacyMiddleware.php
@@ -69,9 +69,6 @@ class LegacyMiddleware implements MiddlewareInterface
$page = $appRequest->path();
$page = str_replace('-', '_', $page);
}
- if ($page == '/') {
- $page = $this->auth->user() ? config('home_site') : 'login';
- }
$title = $content = '';
if (
diff --git a/tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php b/tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php
new file mode 100644
index 00000000..3f6a832c
--- /dev/null
+++ b/tests/Unit/Http/Exceptions/HttpPermanentRedirectTest.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Http;
+
+use Engelsystem\Http\Exceptions\HttpPermanentRedirect;
+use Engelsystem\Http\Exceptions\HttpRedirect;
+use PHPUnit\Framework\TestCase;
+
+class HttpPermanentRedirectTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Http\Exceptions\HttpPermanentRedirect::__construct
+ */
+ public function testConstruct()
+ {
+ $exception = new HttpPermanentRedirect('https://lorem.ipsum/foo/bar');
+ $this->assertInstanceOf(HttpRedirect::class, $exception);
+ $this->assertEquals(301, $exception->getStatusCode());
+ }
+}
diff --git a/tests/Unit/Http/Exceptions/HttpRedirectTest.php b/tests/Unit/Http/Exceptions/HttpRedirectTest.php
new file mode 100644
index 00000000..04190f28
--- /dev/null
+++ b/tests/Unit/Http/Exceptions/HttpRedirectTest.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Http;
+
+use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
+use Engelsystem\Http\Exceptions\HttpRedirect;
+use PHPUnit\Framework\TestCase;
+
+class HttpRedirectTest extends TestCase
+{
+ use ArraySubsetAsserts;
+
+ /**
+ * @covers \Engelsystem\Http\Exceptions\HttpRedirect::__construct
+ */
+ public function testConstruct()
+ {
+ $exception = new HttpRedirect('https://lorem.ipsum/foo/bar');
+ $this->assertEquals(302, $exception->getStatusCode());
+ $this->assertArraySubset(['Location' => 'https://lorem.ipsum/foo/bar'], $exception->getHeaders());
+
+ $exception = new HttpRedirect('/test', 301, ['lorem' => 'ipsum']);
+ $this->assertEquals(301, $exception->getStatusCode());
+ $this->assertArraySubset(['lorem' => 'ipsum'], $exception->getHeaders());
+ }
+}
diff --git a/tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php b/tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php
new file mode 100644
index 00000000..6096830f
--- /dev/null
+++ b/tests/Unit/Http/Exceptions/HttpTemporaryRedirectTest.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Http;
+
+use Engelsystem\Http\Exceptions\HttpRedirect;
+use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
+use PHPUnit\Framework\TestCase;
+
+class HttpTemporaryRedirectTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Http\Exceptions\HttpTemporaryRedirect::__construct
+ */
+ public function testConstruct()
+ {
+ $exception = new HttpTemporaryRedirect('https://lorem.ipsum/foo/bar');
+ $this->assertInstanceOf(HttpRedirect::class, $exception);
+ $this->assertEquals(302, $exception->getStatusCode());
+ }
+}
diff --git a/tests/Unit/Middleware/LegacyMiddlewareTest.php b/tests/Unit/Middleware/LegacyMiddlewareTest.php
index 0589340a..f14a38ed 100644
--- a/tests/Unit/Middleware/LegacyMiddlewareTest.php
+++ b/tests/Unit/Middleware/LegacyMiddlewareTest.php
@@ -46,37 +46,29 @@ class LegacyMiddlewareTest extends TestCase
->disableOriginalConstructor()
->getMock();
- $middleware->expects($this->exactly(2))
+ $middleware->expects($this->once())
->method('loadPage')
- ->withConsecutive(['user_worklog'], ['login'])
- ->willReturnOnConsecutiveCalls(
- ['title', 'content'],
- ['title2', 'content2']
- );
+ ->with('user_worklog')
+ ->willReturn(['title', 'content']);
- $middleware->expects($this->exactly(3))
+ $middleware->expects($this->exactly(2))
->method('renderPage')
->withConsecutive(
['user_worklog', 'title', 'content'],
- ['404', 'Page not found', 'It\'s not available!'],
- ['login', 'title2', 'content2']
+ ['404', 'Page not found', 'It\'s not available!']
)
->willReturn($response);
- $container->expects($this->exactly(4))
+ $container->expects($this->exactly(3))
->method('get')
- ->withConsecutive(['request'], ['request'], ['translator'], ['request'])
+ ->withConsecutive(['request'], ['request'], ['translator'])
->willReturnOnConsecutiveCalls(
$defaultRequest,
$defaultRequest,
- $translator,
- $defaultRequest
+ $translator
);
$auth->expects($this->atLeastOnce())
- ->method('user')
- ->willReturn(false);
- $auth->expects($this->atLeastOnce())
->method('can')
->willReturn(false);
@@ -92,17 +84,15 @@ class LegacyMiddlewareTest extends TestCase
->method('path')
->willReturn('user-worklog');
- $parameters->expects($this->exactly(3))
+ $parameters->expects($this->exactly(2))
->method('get')
->with('p')
->willReturnOnConsecutiveCalls(
null,
- 'foo',
- '/'
+ 'foo'
);
$middleware->process($request, $handler);
$middleware->process($request, $handler);
- $middleware->process($request, $handler);
}
}