|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Flarum. |
| 5 | + * |
| 6 | + * For detailed copyright and license information, please view the |
| 7 | + * LICENSE file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Flarum\Tests\integration\forum; |
| 11 | + |
| 12 | +use Flarum\Extend; |
| 13 | +use Flarum\Testing\integration\TestCase; |
| 14 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | +use Psr\Http\Message\ResponseInterface; |
| 17 | +use Psr\Http\Message\ServerRequestInterface; |
| 18 | +use Psr\Http\Server\RequestHandlerInterface; |
| 19 | + |
| 20 | +class ErrorNegotiationTest extends TestCase |
| 21 | +{ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + // Register a forum route that always errors, so we can assert how the |
| 27 | + // forum error handler formats the response based on the Accept header |
| 28 | + // (the regression from #3850, where forum errors were always HTML). |
| 29 | + $this->extend( |
| 30 | + (new Extend\Routes('forum')) |
| 31 | + ->get('/test-error-negotiation', 'test.error-negotiation', ErrorThrowingRoute::class) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + private function error(?string $accept): ResponseInterface |
| 36 | + { |
| 37 | + $request = $this->request('GET', '/test-error-negotiation'); |
| 38 | + |
| 39 | + if ($accept !== null) { |
| 40 | + $request = $request->withHeader('Accept', $accept); |
| 41 | + } |
| 42 | + |
| 43 | + return $this->send($request); |
| 44 | + } |
| 45 | + |
| 46 | + #[Test] |
| 47 | + public function forum_errors_are_returned_as_json_for_api_requests(): void |
| 48 | + { |
| 49 | + $response = $this->error('application/vnd.api+json'); |
| 50 | + |
| 51 | + $this->assertEquals(404, $response->getStatusCode()); |
| 52 | + $this->assertStringContainsString('json', $response->getHeaderLine('Content-Type')); |
| 53 | + |
| 54 | + $body = json_decode((string) $response->getBody(), true); |
| 55 | + $this->assertIsArray($body); |
| 56 | + $this->assertArrayHasKey('errors', $body); |
| 57 | + } |
| 58 | + |
| 59 | + #[Test] |
| 60 | + public function forum_errors_are_returned_as_json_for_xhr_requests_without_an_explicit_accept(): void |
| 61 | + { |
| 62 | + // Flarum's own XHR requests send `Accept: */*`. This is the regression from #3850: |
| 63 | + // such requests previously received an HTML error page instead of JSON. |
| 64 | + $response = $this->error('*/*'); |
| 65 | + |
| 66 | + $this->assertEquals(404, $response->getStatusCode()); |
| 67 | + $this->assertStringContainsString('json', $response->getHeaderLine('Content-Type')); |
| 68 | + |
| 69 | + $body = json_decode((string) $response->getBody(), true); |
| 70 | + $this->assertIsArray($body); |
| 71 | + $this->assertArrayHasKey('errors', $body); |
| 72 | + } |
| 73 | + |
| 74 | + #[Test] |
| 75 | + public function forum_errors_are_returned_as_html_for_browser_requests(): void |
| 76 | + { |
| 77 | + $response = $this->error('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'); |
| 78 | + |
| 79 | + $this->assertEquals(404, $response->getStatusCode()); |
| 80 | + $this->assertStringContainsString('html', $response->getHeaderLine('Content-Type')); |
| 81 | + $this->assertNull(json_decode((string) $response->getBody(), true)); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class ErrorThrowingRoute implements RequestHandlerInterface |
| 86 | +{ |
| 87 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 88 | + { |
| 89 | + throw (new ModelNotFoundException())->setModel('Test'); |
| 90 | + } |
| 91 | +} |
0 commit comments