|
10 | 10 |
|
11 | 11 | namespace UserFrosting\Tests\Learn\Controller; |
12 | 12 |
|
| 13 | +use UserFrosting\Config\Config; |
13 | 14 | use UserFrosting\Learn\Recipe; |
14 | 15 | use UserFrosting\Testing\TestCase; |
| 16 | +use UserFrosting\UniformResourceLocator\ResourceLocatorInterface; |
| 17 | +use UserFrosting\UniformResourceLocator\ResourceStream; |
15 | 18 |
|
16 | 19 | /** |
17 | | - * Tests for AppController Class. |
18 | | - * |
19 | | - * N.B.: This file is sage to edit or delete. |
| 20 | + * Tests for DocumentationController class. |
20 | 21 | */ |
21 | 22 | class DocumentationControllerTest extends TestCase |
22 | 23 | { |
23 | 24 | protected string $mainSprinkle = Recipe::class; |
24 | 25 |
|
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + parent::setUp(); |
| 29 | + |
| 30 | + /** @var Config $config */ |
| 31 | + $config = $this->ci->get(Config::class); |
| 32 | + $config->set('learn.versions.latest', '6.0'); |
| 33 | + $config->set('learn.versions.available', ['6.0' => '6.0 Beta']); |
| 34 | + |
| 35 | + /** @var ResourceLocatorInterface $locator */ |
| 36 | + $locator = $this->ci->get(ResourceLocatorInterface::class); |
| 37 | + $locator->removeStream('pages'); |
| 38 | + $locator->addStream(new ResourceStream('pages', shared: true, readonly: true, path: __DIR__ . '/../pages')); |
| 39 | + } |
| 40 | + |
25 | 41 | /** |
26 | | - * Test index (`/`) page. |
| 42 | + * Test a documentation page renders successfully (non-versioned route). |
27 | 43 | */ |
28 | 44 | public function testPageIndex(): void |
29 | 45 | { |
30 | | - // Create request with method and url and fetch response |
31 | | - // $request = $this->createRequest('GET', '/'); TEMP FIX BELOW |
32 | | - $request = $this->createRequest('GET', '/quick-start'); |
| 46 | + $request = $this->createRequest('GET', '/first'); |
33 | 47 | $response = $this->handleRequest($request); |
34 | 48 |
|
35 | | - // Asserts |
36 | 49 | $this->assertResponseStatus(200, $response); |
37 | 50 | $this->assertNotSame('', (string) $response->getBody()); |
38 | 51 | } |
| 52 | + |
| 53 | + /** |
| 54 | + * Test a documentation page renders successfully (versioned route). |
| 55 | + */ |
| 56 | + public function testPageVersioned(): void |
| 57 | + { |
| 58 | + $request = $this->createRequest('GET', '/6.0/first'); |
| 59 | + $response = $this->handleRequest($request); |
| 60 | + |
| 61 | + $this->assertResponseStatus(200, $response); |
| 62 | + $this->assertNotSame('', (string) $response->getBody()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test serving a JPEG image via the non-versioned route (delegates to imageVersioned). |
| 67 | + */ |
| 68 | + public function testImage(): void |
| 69 | + { |
| 70 | + $request = $this->createRequest('GET', '/images/test.jpg'); |
| 71 | + $response = $this->handleRequest($request); |
| 72 | + |
| 73 | + $this->assertResponseStatus(200, $response); |
| 74 | + $this->assertSame('image/jpeg', $response->getHeaderLine('Content-Type')); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test serving a JPEG image (jpg extension). |
| 79 | + */ |
| 80 | + public function testImageVersionedJpeg(): void |
| 81 | + { |
| 82 | + $request = $this->createRequest('GET', '/6.0/images/test.jpg'); |
| 83 | + $response = $this->handleRequest($request); |
| 84 | + |
| 85 | + $this->assertResponseStatus(200, $response); |
| 86 | + $this->assertSame('image/jpeg', $response->getHeaderLine('Content-Type')); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test serving a PNG image. |
| 91 | + */ |
| 92 | + public function testImageVersionedPng(): void |
| 93 | + { |
| 94 | + $request = $this->createRequest('GET', '/6.0/images/test.png'); |
| 95 | + $response = $this->handleRequest($request); |
| 96 | + |
| 97 | + $this->assertResponseStatus(200, $response); |
| 98 | + $this->assertSame('image/png', $response->getHeaderLine('Content-Type')); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test serving a GIF image. |
| 103 | + */ |
| 104 | + public function testImageVersionedGif(): void |
| 105 | + { |
| 106 | + $request = $this->createRequest('GET', '/6.0/images/test.gif'); |
| 107 | + $response = $this->handleRequest($request); |
| 108 | + |
| 109 | + $this->assertResponseStatus(200, $response); |
| 110 | + $this->assertSame('image/gif', $response->getHeaderLine('Content-Type')); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Test serving an SVG image. |
| 115 | + */ |
| 116 | + public function testImageVersionedSvg(): void |
| 117 | + { |
| 118 | + $request = $this->createRequest('GET', '/6.0/images/test.svg'); |
| 119 | + $response = $this->handleRequest($request); |
| 120 | + |
| 121 | + $this->assertResponseStatus(200, $response); |
| 122 | + $this->assertSame('image/svg+xml', $response->getHeaderLine('Content-Type')); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test serving a WebP image. |
| 127 | + */ |
| 128 | + public function testImageVersionedWebp(): void |
| 129 | + { |
| 130 | + $request = $this->createRequest('GET', '/6.0/images/test.webp'); |
| 131 | + $response = $this->handleRequest($request); |
| 132 | + |
| 133 | + $this->assertResponseStatus(200, $response); |
| 134 | + $this->assertSame('image/webp', $response->getHeaderLine('Content-Type')); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test serving a BMP image. |
| 139 | + */ |
| 140 | + public function testImageVersionedBmp(): void |
| 141 | + { |
| 142 | + $request = $this->createRequest('GET', '/6.0/images/test.bmp'); |
| 143 | + $response = $this->handleRequest($request); |
| 144 | + |
| 145 | + $this->assertResponseStatus(200, $response); |
| 146 | + $this->assertSame('image/bmp', $response->getHeaderLine('Content-Type')); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Test serving an ICO image. |
| 151 | + */ |
| 152 | + public function testImageVersionedIco(): void |
| 153 | + { |
| 154 | + $request = $this->createRequest('GET', '/6.0/images/test.ico'); |
| 155 | + $response = $this->handleRequest($request); |
| 156 | + |
| 157 | + $this->assertResponseStatus(200, $response); |
| 158 | + $this->assertSame('image/x-icon', $response->getHeaderLine('Content-Type')); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Test serving a file with an unknown extension falls back to octet-stream. |
| 163 | + */ |
| 164 | + public function testImageVersionedDefaultMimeType(): void |
| 165 | + { |
| 166 | + $request = $this->createRequest('GET', '/6.0/images/test.bin'); |
| 167 | + $response = $this->handleRequest($request); |
| 168 | + |
| 169 | + $this->assertResponseStatus(200, $response); |
| 170 | + $this->assertSame('application/octet-stream', $response->getHeaderLine('Content-Type')); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Test that an unreadable image file returns a 404 response. |
| 175 | + */ |
| 176 | + public function testImageVersionedNotReadable(): void |
| 177 | + { |
| 178 | + if (function_exists('posix_getuid') && posix_getuid() === 0) { |
| 179 | + $this->markTestSkipped('Cannot test file permissions as root user.'); |
| 180 | + } |
| 181 | + |
| 182 | + $imagePath = __DIR__ . '/../pages/6.0/images/unreadable.jpg'; |
| 183 | + file_put_contents($imagePath, 'data'); |
| 184 | + chmod($imagePath, 0000); |
| 185 | + |
| 186 | + try { |
| 187 | + $request = $this->createRequest('GET', '/6.0/images/unreadable.jpg'); |
| 188 | + $response = $this->handleRequest($request); |
| 189 | + $this->assertResponseStatus(404, $response); |
| 190 | + } finally { |
| 191 | + chmod($imagePath, 0644); |
| 192 | + @unlink($imagePath); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Test that a versioned URL with a trailing slash is redirected (301). |
| 198 | + */ |
| 199 | + public function testTrailingSlashRedirectVersioned(): void |
| 200 | + { |
| 201 | + $request = $this->createRequest('GET', '/6.0/first/'); |
| 202 | + $response = $this->handleRequest($request); |
| 203 | + |
| 204 | + $this->assertResponseStatus(301, $response); |
| 205 | + $this->assertSame('/6.0/first', $response->getHeaderLine('Location')); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Test that a non-versioned URL with a trailing slash is redirected (301). |
| 210 | + */ |
| 211 | + public function testTrailingSlashRedirect(): void |
| 212 | + { |
| 213 | + $request = $this->createRequest('GET', '/first/'); |
| 214 | + $response = $this->handleRequest($request); |
| 215 | + |
| 216 | + $this->assertResponseStatus(301, $response); |
| 217 | + $this->assertSame('/first', $response->getHeaderLine('Location')); |
| 218 | + } |
39 | 219 | } |
0 commit comments