|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bolt\Tests\Utils; |
| 6 | + |
| 7 | +use Bolt\Canonical; |
| 8 | +use Bolt\Configuration\Config; |
| 9 | +use Bolt\Configuration\Content\ContentType; |
| 10 | +use Bolt\Entity\Content; |
| 11 | +use Bolt\Twig\LocaleExtension; |
| 12 | +use Bolt\Utils\ContentHelper; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 16 | + |
| 17 | +/** |
| 18 | + * Canonical route/params resolution, with focus on the singleton handling: |
| 19 | + * a singleton is served at its slugless listing URL (ListingController forwards |
| 20 | + * a singleton listing to the record), so its canonical must point there |
| 21 | + * (`/{singularSlug}`) and not at the record-detail route (`/{singularSlug}/{slug}`). |
| 22 | + */ |
| 23 | +class ContentHelperTest extends TestCase |
| 24 | +{ |
| 25 | + public function testSingletonCanonicalUsesSluglessListingRoute(): void |
| 26 | + { |
| 27 | + $content = $this->createContent( |
| 28 | + singleton: true, |
| 29 | + singularSlug: 'contact', |
| 30 | + slug: 'contact-info', |
| 31 | + recordSlug: 'contact-2' |
| 32 | + ); |
| 33 | + |
| 34 | + $canonical = $this->createMock(Canonical::class); |
| 35 | + $canonical->expects($this->once()) |
| 36 | + ->method('setPath') |
| 37 | + ->with('listing_locale', [ |
| 38 | + 'contentTypeSlug' => 'contact', |
| 39 | + '_locale' => 'en', |
| 40 | + ]); |
| 41 | + |
| 42 | + $this->createContentHelper($canonical)->setCanonicalPath($content, 'en'); |
| 43 | + } |
| 44 | + |
| 45 | + public function testNonSingletonCanonicalUsesRecordRouteWithSlug(): void |
| 46 | + { |
| 47 | + $content = $this->createContent( |
| 48 | + singleton: false, |
| 49 | + singularSlug: 'entry', |
| 50 | + slug: 'entries', |
| 51 | + recordSlug: 'my-post', |
| 52 | + recordRoute: 'record' |
| 53 | + ); |
| 54 | + |
| 55 | + $canonical = $this->createMock(Canonical::class); |
| 56 | + $canonical->expects($this->once()) |
| 57 | + ->method('setPath') |
| 58 | + ->with('record', [ |
| 59 | + 'contentTypeSlug' => 'entry', |
| 60 | + 'slugOrId' => 'my-post', |
| 61 | + '_locale' => 'en', |
| 62 | + ]); |
| 63 | + |
| 64 | + $this->createContentHelper($canonical)->setCanonicalPath($content, 'en'); |
| 65 | + } |
| 66 | + |
| 67 | + public function testHomepageCanonicalIsNotAffectedBySingletonHandling(): void |
| 68 | + { |
| 69 | + // The homepage is itself a singleton, but must keep resolving to the |
| 70 | + // homepage route, not the listing route. |
| 71 | + $content = $this->createContent( |
| 72 | + singleton: true, |
| 73 | + singularSlug: 'homepage', |
| 74 | + slug: 'homepage', |
| 75 | + recordSlug: 'homepage' |
| 76 | + ); |
| 77 | + |
| 78 | + $canonical = $this->createMock(Canonical::class); |
| 79 | + $canonical->expects($this->once()) |
| 80 | + ->method('setPath') |
| 81 | + ->with('homepage_locale', [ |
| 82 | + '_locale' => 'en', |
| 83 | + ]); |
| 84 | + |
| 85 | + $this->createContentHelper($canonical)->setCanonicalPath($content, 'en'); |
| 86 | + } |
| 87 | + |
| 88 | + public function testNonContentIsIgnored(): void |
| 89 | + { |
| 90 | + $canonical = $this->createMock(Canonical::class); |
| 91 | + $canonical->expects($this->never()) |
| 92 | + ->method('setPath'); |
| 93 | + |
| 94 | + $this->createContentHelper($canonical)->setCanonicalPath(null, 'en'); |
| 95 | + } |
| 96 | + |
| 97 | + private function createContent( |
| 98 | + bool $singleton, |
| 99 | + string $singularSlug, |
| 100 | + string $slug, |
| 101 | + string $recordSlug, |
| 102 | + ?string $recordRoute = null |
| 103 | + ): Content { |
| 104 | + $definition = $this->createMock(ContentType::class); |
| 105 | + $definition->method('get')->willReturnCallback( |
| 106 | + static fn (string $key) => match ($key) { |
| 107 | + 'singleton' => $singleton, |
| 108 | + 'record_route' => $recordRoute, |
| 109 | + default => null, |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + $content = $this->createMock(Content::class); |
| 114 | + $content->method('getDefinition')->willReturn($definition); |
| 115 | + $content->method('getContentTypeSingularSlug')->willReturn($singularSlug); |
| 116 | + $content->method('getContentTypeSlug')->willReturn($slug); |
| 117 | + $content->method('getSlug')->willReturn($recordSlug); |
| 118 | + $content->method('getId')->willReturn(1); |
| 119 | + |
| 120 | + return $content; |
| 121 | + } |
| 122 | + |
| 123 | + private function createContentHelper(Canonical $canonical): ContentHelper |
| 124 | + { |
| 125 | + $config = $this->createMock(Config::class); |
| 126 | + // Only `general/homepage` is consulted (via isHomepage()); a content type |
| 127 | + // is the homepage when its slug matches this value. |
| 128 | + $config->method('get')->willReturnCallback( |
| 129 | + static fn (string $path) => $path === 'general/homepage' ? 'homepage' : null |
| 130 | + ); |
| 131 | + |
| 132 | + $requestStack = $this->createMock(RequestStack::class); |
| 133 | + $requestStack->method('getCurrentRequest')->willReturn(new Request()); |
| 134 | + |
| 135 | + return new ContentHelper( |
| 136 | + $canonical, |
| 137 | + $requestStack, |
| 138 | + $config, |
| 139 | + $this->createMock(LocaleExtension::class) |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
0 commit comments