Skip to content

Commit df48578

Browse files
edalzelljesseleite
andauthored
Ensure only published content is shown in alternate locale meta (#257)
Co-authored-by: Jesse Leite <jesseleite@gmail.com> Closes #243
1 parent 561a43e commit df48578

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/Cascade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ protected function alternateLocales()
291291
->filter(function ($locale) {
292292
return $this->model->in($locale);
293293
})
294+
->filter(function ($locale) {
295+
return $this->model->in($locale)->status() === 'published';
296+
})
294297
->reject(function ($locale) {
295298
return collect(config('statamic.seo-pro.alternate_locales.excluded_sites'))->contains($locale);
296299
})

tests/Localized/MetaTagTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Localized;
44

55
use Statamic\Facades\Config;
6+
use Statamic\Facades\Entry;
67
use Tests\TestCase;
78
use Tests\ViewScenarios;
89

@@ -141,4 +142,97 @@ public function it_doesnt_generate_multisite_meta_for_excluded_sites($viewType)
141142
$this->assertStringNotContainsString('content="fr_FR"', $content);
142143
$this->assertStringNotContainsString('hreflang="fr"', $content);
143144
}
145+
146+
/**
147+
* @test
148+
* @dataProvider viewScenarioProvider
149+
*/
150+
public function it_doesnt_generate_multisite_meta_for_unpublished_content($viewType)
151+
{
152+
$this->prepareViews($viewType);
153+
154+
Entry::find('62136fa2-9e5c-4c38-a894-a2753f02f5ff')->in('french')->unpublish()->save();
155+
156+
$expectedOgLocaleMeta = <<<'EOT'
157+
<meta property="og:locale" content="en_US" />
158+
<meta property="og:locale:alternate" content="it_IT" />
159+
EOT;
160+
161+
$expectedAlternateHreflangMeta = <<<'EOT'
162+
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
163+
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
164+
EOT;
165+
166+
$content = $this->get('/about')->content();
167+
168+
$this->assertStringContainsString("<h1>{$viewType}</h1>", $content);
169+
$this->assertStringContainsString($expectedOgLocaleMeta, $content);
170+
$this->assertStringContainsString($expectedAlternateHreflangMeta, $content);
171+
}
172+
173+
/**
174+
* @test
175+
* @dataProvider viewScenarioProvider
176+
*/
177+
public function it_doesnt_generate_multisite_meta_for_scheduled_content($viewType)
178+
{
179+
$this->prepareViews($viewType);
180+
181+
$entry = Entry::find('62136fa2-9e5c-4c38-a894-a2753f02f5ff');
182+
183+
$collection = $entry->collection()->dated(true)->futureDateBehavior('private')->save();
184+
185+
$entry->in('default')->date(now()->subDays(5))->save();
186+
$entry->in('french')->date(now()->addDays(3))->save(); // This entry is scheduled, should not show
187+
$entry->in('italian')->date(now()->subDays(5))->save();
188+
189+
$expectedOgLocaleMeta = <<<'EOT'
190+
<meta property="og:locale" content="en_US" />
191+
<meta property="og:locale:alternate" content="it_IT" />
192+
EOT;
193+
194+
$expectedAlternateHreflangMeta = <<<'EOT'
195+
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
196+
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
197+
EOT;
198+
199+
$content = $this->get('/about')->content();
200+
201+
$this->assertStringContainsString("<h1>{$viewType}</h1>", $content);
202+
$this->assertStringContainsString($expectedOgLocaleMeta, $content);
203+
$this->assertStringContainsString($expectedAlternateHreflangMeta, $content);
204+
}
205+
206+
/**
207+
* @test
208+
* @dataProvider viewScenarioProvider
209+
*/
210+
public function it_doesnt_generate_multisite_meta_for_expired_content($viewType)
211+
{
212+
$this->prepareViews($viewType);
213+
214+
$entry = Entry::find('62136fa2-9e5c-4c38-a894-a2753f02f5ff');
215+
216+
$collection = $entry->collection()->dated(true)->pastDateBehavior('private')->save();
217+
218+
$entry->in('default')->date(now()->addDays(5))->save();
219+
$entry->in('french')->date(now()->subDays(3))->save(); // This entry is expired, should not show
220+
$entry->in('italian')->date(now()->addDays(5))->save();
221+
222+
$expectedOgLocaleMeta = <<<'EOT'
223+
<meta property="og:locale" content="en_US" />
224+
<meta property="og:locale:alternate" content="it_IT" />
225+
EOT;
226+
227+
$expectedAlternateHreflangMeta = <<<'EOT'
228+
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
229+
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
230+
EOT;
231+
232+
$content = $this->get('/about')->content();
233+
234+
$this->assertStringContainsString("<h1>{$viewType}</h1>", $content);
235+
$this->assertStringContainsString($expectedOgLocaleMeta, $content);
236+
$this->assertStringContainsString($expectedAlternateHreflangMeta, $content);
237+
}
144238
}

0 commit comments

Comments
 (0)