Skip to content

Commit

Permalink
Fix 404 page title meta output (#268)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Leite <[email protected]>
  • Loading branch information
o1y and jesseleite authored May 17, 2023
1 parent e32a227 commit 3b7176d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
"require": {
"statamic/cms": "^4.0"
"statamic/cms": "^4.1.3"
},
"require-dev": {
"orchestra/testbench": "^7.0 || ^8.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Cascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function get()
return $this->getForSitemap();
}

if (array_get($this->current, 'response_code') === 404) {
if (array_get($this->data, 'response_code') === 404) {
$this->current['title'] = '404 Page Not Found';
}

Expand Down
14 changes: 14 additions & 0 deletions tests/CascadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,18 @@ public function it_generates_seo_cascade_without_exception_when_no_home_entry_ex

$this->assertArraySubset($expected, $data);
}

/** @test */
public function it_generates_404_title_with_404_in_response_code_in_context()
{
$data = (new Cascade)
->with(SiteDefaults::load()->all())
->with([
'response_code' => 404,
])
->get();

$this->assertEquals('404 Page Not Found', $data['title']);
$this->assertEquals('404 Page Not Found | Site Name', $data['compiled_title']);
}
}
2 changes: 2 additions & 0 deletions tests/Fixtures/views/antlers/errors/404.antlers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>antlers</h1>
<h2>404!</h2>
4 changes: 4 additions & 0 deletions tests/Fixtures/views/blade-components/errors/404.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<x-layout>
<h1>blade-components</h1>
<h2>404!</h2>
</x-layout>
3 changes: 3 additions & 0 deletions tests/Fixtures/views/blade/errors/404.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@extends('layout')
<h1>blade</h1>
<h2>404!</h2>
19 changes: 19 additions & 0 deletions tests/MetaTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,25 @@ public function it_generates_search_engine_verification_codes($viewType)
$response->assertSee('<meta name="msvalidate.01" content="bing123" />', false);
}

/**
* @test
*
* @dataProvider viewScenarioProvider
*/
public function it_generates_proper_404_page_title($viewType)
{
$this->prepareViews($viewType);

$expected = <<<'EOT'
<title>404 Page Not Found | Site Name</title>
EOT;

$content = $this->get('/non-existent-page')->content();
$this->assertStringContainsString("<h1>{$viewType}</h1>", $content);
$this->assertStringContainsString('<h2>404!</h2>', $content);
$this->assertStringContainsString($this->normalizeMultilineString($expected), $content);
}

protected function setCustomGlidePresetDimensions($app)
{
$app->config->set('statamic.seo-pro.assets', [
Expand Down

0 comments on commit 3b7176d

Please sign in to comment.