|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Request; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use Statamic\Facades\Collection; |
| 8 | +use Statamic\Facades\Entry; |
| 9 | +use Statamic\Fields\Field; |
| 10 | +use Statamic\Fields\Value; |
| 11 | +use Statamic\SeoPro\Fieldtypes\SeoProFieldtype; |
| 12 | + |
| 13 | +class SeoProFieldtypeTest extends TestCase |
| 14 | +{ |
| 15 | + #[Test] |
| 16 | + public function it_augments() |
| 17 | + { |
| 18 | + $field = (new SeoProFieldtype)->setField(new Field('test', ['type' => 'seo_pro'])); |
| 19 | + |
| 20 | + $augment = $field->augment([ |
| 21 | + 'title' => 'Foo', |
| 22 | + 'description' => 'Bar', |
| 23 | + ]); |
| 24 | + |
| 25 | + $this->assertCount(2, $augment); |
| 26 | + |
| 27 | + $this->assertInstanceOf(Value::class, $augment['title']); |
| 28 | + $this->assertEquals('Foo', $augment['title']->value()); |
| 29 | + |
| 30 | + $this->assertInstanceOf(Value::class, $augment['description']); |
| 31 | + $this->assertEquals('Bar', $augment['description']->value()); |
| 32 | + } |
| 33 | + |
| 34 | + #[Test] |
| 35 | + public function it_augments_for_api_routes() |
| 36 | + { |
| 37 | + config()->set('statamic.editions.pro', true); |
| 38 | + config()->set('statamic.api.enabled', true); |
| 39 | + config()->set('statamic.api.resources.collections', true); |
| 40 | + |
| 41 | + // Changing our config values above won't register the API routes. To avoid |
| 42 | + // undefined route errors, we'll register what we need here. |
| 43 | + $this->app['router']->get('/api/collection/{collection}/entries/{handle}', fn () => null)->name('statamic.api.collections.entries.show'); |
| 44 | + $this->app['router']->getRoutes()->refreshNameLookups(); |
| 45 | + |
| 46 | + $this->app->instance('request', Request::create('/api/collections/pages/entries')); |
| 47 | + |
| 48 | + $collection = tap(Collection::make('pages'))->save(); |
| 49 | + $entry = tap(Entry::make()->collection($collection)->data(['title' => 'Foo', 'description' => 'Bar']))->save(); |
| 50 | + |
| 51 | + $field = (new SeoProFieldtype)->setField((new Field('test', ['type' => 'seo_pro']))->setParent($entry)); |
| 52 | + |
| 53 | + $augment = $field->augment([ |
| 54 | + 'title' => 'Foo', |
| 55 | + 'description' => 'Bar', |
| 56 | + ]); |
| 57 | + |
| 58 | + $this->assertArrayHasKeys([ |
| 59 | + 'title', |
| 60 | + 'description', |
| 61 | + 'site_name', |
| 62 | + 'site_name_position', |
| 63 | + 'site_name_separator', |
| 64 | + 'canonical_url', |
| 65 | + 'priority', |
| 66 | + 'change_frequency', |
| 67 | + 'compiled_title', |
| 68 | + 'og_title', |
| 69 | + 'prev_url', |
| 70 | + 'next_url', |
| 71 | + 'home_url', |
| 72 | + 'humans_txt', |
| 73 | + 'site', |
| 74 | + 'alternate_locales', |
| 75 | + 'current_hreflang', |
| 76 | + 'last_modified', |
| 77 | + 'twitter_card', |
| 78 | + 'twitter_title', |
| 79 | + 'twitter_description', |
| 80 | + ], $augment); |
| 81 | + |
| 82 | + $this->assertEquals('Foo', (string) $augment['title']); |
| 83 | + $this->assertEquals('Bar', (string) $augment['description']); |
| 84 | + $this->assertEquals('Site Name', (string) $augment['site_name']); // Site default |
| 85 | + } |
| 86 | +} |
0 commit comments