|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Localized; |
| 4 | + |
| 5 | +use Illuminate\Support\Carbon; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use Statamic\Facades\Entry; |
| 8 | +use Statamic\Facades\Term; |
| 9 | +use Statamic\Facades\YAML; |
| 10 | +use Statamic\SeoPro\Reporting\Report; |
| 11 | +use Statamic\Support\Str; |
| 12 | + |
| 13 | +class ReportTest extends LocalizedTestCase |
| 14 | +{ |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + Entry::all()->filter(fn ($entry) => $entry->hasOrigin())->each->delete(); |
| 20 | + Entry::all()->each->delete(); |
| 21 | + Term::all()->each->delete(); |
| 22 | + |
| 23 | + if ($this->files->exists($path = $this->reportsPath())) { |
| 24 | + $this->files->deleteDirectory($path); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @see https://github.com/statamic/seo-pro/issues/213 |
| 30 | + */ |
| 31 | + #[Test] |
| 32 | + public function it_does_not_flag_duplicate_titles_and_descriptions_across_sites() |
| 33 | + { |
| 34 | + $this->makeArticle('default', 'one', 'Shared Title', 'Shared Description'); |
| 35 | + $this->makeArticle('french', 'two', 'Shared Title', 'Shared Description'); |
| 36 | + |
| 37 | + $this->assertEqualsIgnoringLineEndings(0, $this->getReportResult('UniqueTitleTag')); |
| 38 | + $this->assertEqualsIgnoringLineEndings(0, $this->getReportResult('UniqueMetaDescription')); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @see https://github.com/statamic/seo-pro/issues/213 |
| 43 | + */ |
| 44 | + #[Test] |
| 45 | + public function it_still_flags_duplicate_titles_and_descriptions_within_the_same_site() |
| 46 | + { |
| 47 | + $this->makeArticle('default', 'one', 'Shared Title', 'Shared Description'); |
| 48 | + $this->makeArticle('default', 'two', 'Shared Title', 'Shared Description'); |
| 49 | + $this->makeArticle('french', 'three', 'Shared Title', 'Shared Description'); |
| 50 | + |
| 51 | + $this->assertEqualsIgnoringLineEndings(2, $this->getReportResult('UniqueTitleTag')); |
| 52 | + $this->assertEqualsIgnoringLineEndings(2, $this->getReportResult('UniqueMetaDescription')); |
| 53 | + } |
| 54 | + |
| 55 | + private function makeArticle($site, $slug, $title, $description) |
| 56 | + { |
| 57 | + Entry::make() |
| 58 | + ->collection('articles') |
| 59 | + ->blueprint('articles') |
| 60 | + ->locale($site) |
| 61 | + ->slug($slug) |
| 62 | + ->date('2024-01-01') |
| 63 | + ->set('title', $title) |
| 64 | + ->set('seo', ['title' => $title, 'description' => $description]) |
| 65 | + ->save(); |
| 66 | + |
| 67 | + return $this; |
| 68 | + } |
| 69 | + |
| 70 | + private function reportsPath($path = null) |
| 71 | + { |
| 72 | + if ($path) { |
| 73 | + $path = Str::ensureLeft($path, '/'); |
| 74 | + } |
| 75 | + |
| 76 | + return storage_path('statamic/seopro/reports').$path; |
| 77 | + } |
| 78 | + |
| 79 | + private function getReportResult($key) |
| 80 | + { |
| 81 | + Carbon::setTestNow(now()); |
| 82 | + |
| 83 | + Report::create()->save()->generate(); |
| 84 | + |
| 85 | + return YAML::file($this->reportsPath('1/report.yaml'))->parse()['results'][$key]; |
| 86 | + } |
| 87 | +} |
0 commit comments