Skip to content

Commit 94deee2

Browse files
committed
Add tests
1 parent a6b3ddb commit 94deee2

File tree

4 files changed

+92
-10
lines changed

4 files changed

+92
-10
lines changed

src/Http/Controllers/PreviewController.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function createToken(Tokens $tokens, Request $request): JsonResponse|Redi
4444
};
4545

4646
$token = $tokens->createPreviewToken([
47-
'preview/preview', [
47+
action(self::preview(...), absolute: false), [
4848
'elementType' => $data['elementType'],
4949
'canonicalId' => (int) $canonicalId,
5050
'siteId' => (int) $data['siteId'],
@@ -123,11 +123,6 @@ public function preview(Request $request, Kernel $kernel): mixed
123123

124124
/** @var \Illuminate\Support\Uri $originalUri */
125125
$originalUri = Context::pullHidden(HandleTokenRequest::ORIGINAL_URI_KEY);
126-
$originalUri = $originalUri->withoutQuery([
127-
'token',
128-
'x-craft-preview',
129-
'x-craft-live-preview',
130-
]);
131126

132127
$newRequest = $request->duplicateWithUri(
133128
newUri: $originalUri->value(),

src/Http/Middleware/HandleTokenRequest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ public function handle(Request $request, Closure $next): mixed
5050
return $next($request);
5151
}
5252

53-
Context::addHidden(self::ORIGINAL_URI_KEY, $request->uri());
53+
Context::addHidden(self::ORIGINAL_URI_KEY, $request->uri()->withoutQuery([
54+
'token',
55+
'x-craft-preview',
56+
'x-craft-live-preview',
57+
]));
5458

55-
$route = $request->actionSegmentsToRoute(explode('/', (string) $tokenRoute[0]));
56-
57-
$newRequest = $request->duplicateWithUri($route, $tokenRoute[1] ?? []);
59+
$newRequest = $request->duplicateWithUri((string) $tokenRoute[0], $tokenRoute[1] ?? []);
5860

5961
return $next($newRequest);
6062
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CraftCms\Cms\Cms;
6+
use CraftCms\Cms\Http\Middleware\HandleTokenRequest;
7+
use CraftCms\Cms\Support\Str;
8+
use CraftCms\Cms\Token\Tokens;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Context;
11+
12+
beforeEach(function () {
13+
$this->middleware = app(HandleTokenRequest::class);
14+
});
15+
16+
it('does nothing if there is no token or token header', function () {
17+
expect($this->middleware->handle(Request::create('foo'), fn () => 'bar'))->toBe('bar');
18+
});
19+
20+
it('throws if an invalid token is passed', function () {
21+
$this->expectExceptionMessage('Invalid token');
22+
23+
$this->middleware->handle(Request::create('foo', parameters: [
24+
Cms::config()->tokenParam => 'invalid token',
25+
]), fn () => 'bar');
26+
});
27+
28+
it('adds the token to the context', function () {
29+
$this->middleware->handle(Request::create('foo', parameters: [
30+
Cms::config()->tokenParam => Str::random(32),
31+
]), fn () => 'bar');
32+
33+
expect(Context::getHidden(HandleTokenRequest::TOKEN_KEY))
34+
->not()
35+
->toBeNull();
36+
});
37+
38+
it('does nothing more when the token does not return a route', function () {
39+
$result = $this->middleware->handle(Request::create('foo', parameters: [
40+
Cms::config()->tokenParam => Str::random(32),
41+
]), fn () => 'bar');
42+
43+
expect($result)->toBe('bar');
44+
});
45+
46+
it('returns the response of the token route', function () {
47+
$token = app(Tokens::class)->createToken('token/route');
48+
49+
$result = $this->middleware->handle(Request::create('foo', parameters: [
50+
Cms::config()->tokenParam => $token,
51+
]), function (?Request $request = null) {
52+
if (! is_null($request)) {
53+
return $request->path();
54+
}
55+
56+
return 'bar';
57+
});
58+
59+
expect($result)->toBe('token/route');
60+
61+
/** @var ?\Illuminate\Support\Uri $originalUri */
62+
$originalUri = Context::getHidden(HandleTokenRequest::ORIGINAL_URI_KEY);
63+
64+
expect($originalUri)->not()->toBeNull();
65+
expect($originalUri->value())->toBe(url('foo'));
66+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CraftCms\Cms\Http\Middleware\HandleTokenRequest;
6+
use CraftCms\Cms\Http\Middleware\RequireToken;
7+
use Illuminate\Support\Facades\Context;
8+
9+
it('throws if no token is found', function () {
10+
$this->expectExceptionMessage('Valid token required');
11+
12+
app(RequireToken::class)->handle(Request::create('foo'), fn () => 'bar');
13+
});
14+
15+
it('returns next if token is found', function () {
16+
Context::addHidden(HandleTokenRequest::TOKEN_KEY, 'token');
17+
18+
expect(app(RequireToken::class)->handle(Request::create('foo'), fn () => 'bar'))->toBe('bar');
19+
});

0 commit comments

Comments
 (0)