feat: assign custom 404 pages with the website theme (1.8.0)
CMS quality / php (8.2) (push) Canceled after 0s
CMS quality / php (8.4) (push) Canceled after 0s
CMS quality / browser (push) Canceled after 0s
CMS quality / mysql (push) Canceled after 0s

This commit is contained in:
kantarci committed 2026-09-14 12:18:33 +02:00
1 parent a3c89bb69d
commit 16da844934
17 files changed
+638 -92

No files matched your search

+110
View File
@@ -10,6 +10,7 @@ use App\Content\FontLibrary;
use App\Content\FormRepository;
use App\Content\MediaLibrary;
use App\Content\MenuRepository;
use App\Content\NotFoundPage;
use App\Content\PageRepository;
use App\Content\PartialRepository;
use App\Content\PostLayouts;
@@ -296,6 +297,115 @@ final class CmsTest extends TestCase
self::assertSame(405, $this->request('/', 'POST')->getStatusCode());
}
public function testDefault404UsesTheNormalAreasAndThemeForEveryPublicUrlShape(): void
{
foreach (['/missing', '/Missing/old.html', '/eins/zwei/drei/vier/fuenf', '/__404-vorschau__', '/kategorie/missing'] as $path) {
$response = $this->request($path);
self::assertSame(404, $response->getStatusCode(), $path);
self::assertSame('noindex, follow', $response->headers->get('X-Robots-Tag'));
self::assertFalse($response->headers->has('Location'));
$html = $response->getContent();
self::assertStringContainsString('Seite nicht gefunden', $html);
self::assertStringContainsString('class="site-header', $html);
self::assertStringContainsString('class="site-footer', $html);
self::assertStringContainsString('--k-', $html);
self::assertStringNotContainsString('rel="canonical"', $html);
self::assertStringNotContainsString('application/ld+json', $html);
}
$head = $this->request('/Missing/old.html', 'HEAD');
self::assertSame(404, $head->getStatusCode());
self::assertSame('', $head->getContent());
}
public function testCustom404KeepsDraftPrivateAndSurvivesRenamingAndFallsBackAfterDeletion(): void
{
$document = NotFoundPage::document();
$document['blocks'][0]['children'][1]['props']['text'] = 'Unser individueller Fehlerhinweis';
$id = $this->pages->createPage('fehlerseite', null, 'page', $document);
$this->site->assignNotFoundPage($id, '');
self::assertStringNotContainsString('Unser individueller Fehlerhinweis', $this->request('/unbekannt')->getContent());
$page = $this->pages->findForEditor($id);
$this->pages->publish($id, $page['draft_revision_id'], $page['version']);
$this->pages->saveAreas($id, 'none', 'none', 2);
foreach (['/unbekannt', '/fehlerseite', '/alte.Datei.html'] as $url) {
$response = $this->request($url);
self::assertSame(404, $response->getStatusCode());
self::assertStringContainsString('Unser individueller Fehlerhinweis', $response->getContent());
self::assertStringContainsString('class="site-header', $response->getContent());
self::assertStringContainsString('class="site-footer', $response->getContent());
}
$this->site->save(['homePath' => 'home', 'llmsTxt' => true]);
self::assertSame($id, $this->site->load()['notFoundPageId']);
self::assertStringNotContainsString('/fehlerseite', $this->request('/sitemap.xml')->getContent());
self::assertStringNotContainsString('/fehlerseite', $this->request('/llms.txt')->getContent());
$this->pages->movePage($id, 'fehler-umbenannt', null, 3, $this->redirects);
self::assertStringContainsString('Unser individueller Fehlerhinweis', $this->request('/weiterhin-falsch')->getContent());
$document['blocks'][0]['children'][1]['props']['text'] = 'Noch geheimer Entwurf';
$page = $this->pages->findForEditor($id);
$revision = $this->pages->saveDraft($id, $document, $page['version']);
self::assertStringNotContainsString('Noch geheimer Entwurf', $this->request('/weiterhin-falsch')->getContent());
$page = $this->pages->findForEditor($id);
$this->pages->publish($id, $revision, $page['version']);
self::assertStringContainsString('Noch geheimer Entwurf', $this->request('/weiterhin-falsch')->getContent());
$page = $this->pages->findForEditor($id);
$this->pages->trashPage($id, $page['version']);
self::assertStringContainsString('Seite nicht gefunden', $this->request('/weiterhin-falsch')->getContent());
self::assertStringNotContainsString('Noch geheimer Entwurf', $this->request('/weiterhin-falsch')->getContent());
}
public function test404CreationPreservesExistingPagesAndRejectsStaleAssignments(): void
{
$existing = $this->pages->createPage('404', null, 'page', DemoContent::starter('Vorhandene Seite'));
$service = new NotFoundPage($this->site, $this->pages, $this->db);
$id = $service->create('');
$created = $this->pages->findForEditor($id);
self::assertSame('404-2', $created['path']);
self::assertNull($created['published_revision_id']);
self::assertSame('noindex', $created['robots']);
self::assertSame('Vorhandene Seite', $this->pages->findForEditor($existing)['document']['title']);
$count = $this->db->fetchOne('SELECT COUNT(*) FROM page');
try {
$service->create('');
self::fail('Repeated creation must not create another page.');
} catch (InvalidPage) {
self::assertSame($count, $this->db->fetchOne('SELECT COUNT(*) FROM page'));
}
try {
$this->site->assignNotFoundPage('', '');
self::fail('A stale settings form must not reset a newer assignment.');
} catch (StaleWrite) {
self::assertSame($id, $this->site->load()['notFoundPageId']);
}
foreach (['page-1', 'missing', $this->pages->createPage('beitrag', null, 'post', DemoContent::starter('Beitrag', 'post'))] as $invalid) {
try {
$this->site->assignNotFoundPage($invalid, $id);
self::fail('The home page, posts and missing pages cannot serve as the 404 page.');
} catch (InvalidPage) {
self::assertSame($id, $this->site->load()['notFoundPageId']);
}
}
$this->site->assignNotFoundPage('', $id);
self::assertSame('', $this->site->load()['notFoundPageId']);
}
public function testCustom404DoesNotReplaceRedirectsGoneResourcesOrAdminEndpoints(): void
{
$document = NotFoundPage::document();
$document['title'] = 'CUSTOM404MARKER';
$id = $this->pages->createPage('fehler', null, 'page', $document);
$page = $this->pages->findForEditor($id);
$this->pages->publish($id, $page['draft_revision_id'], $page['version']);
$this->site->assignNotFoundPage($id, '');
$this->redirects->save('umgezogen', 'home', 301);
$this->redirects->save('entfernt', '', 410);
self::assertSame(301, $this->request('/umgezogen')->getStatusCode());
self::assertSame(410, $this->request('/entfernt')->getStatusCode());
self::assertStringNotContainsString('CUSTOM404MARKER', $this->request('/entfernt')->getContent());
foreach (['/assets/missing.js', '/media/missing', '/admin/api/missing', '/missing.json', '/llms.txt'] as $url) {
self::assertStringNotContainsString('CUSTOM404MARKER', $this->request($url)->getContent(), $url);
}
}
public function testHomeAliasRedirectsToCanonical(): void
{
$response = $this->request('/home');