Skip to content

Commit 913dbe9

Browse files
Return the SEO Cascade via the REST API (#428)
1 parent 2154be6 commit 913dbe9

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

src/Fieldtypes/SeoProFieldtype.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
use Statamic\Facades\GraphQL;
99
use Statamic\Fields\Fields as BlueprintFields;
1010
use Statamic\Fields\Fieldtype;
11+
use Statamic\SeoPro\Cascade;
1112
use Statamic\SeoPro\Fields as SeoProFields;
13+
use Statamic\SeoPro\GetsSectionDefaults;
14+
use Statamic\SeoPro\SiteDefaults;
15+
use Statamic\Statamic;
1216
use Statamic\Support\Arr;
1317

1418
class SeoProFieldtype extends Fieldtype
1519
{
20+
use GetsSectionDefaults;
21+
1622
protected $selectable = true;
1723
protected $icon = 'seo-search-graph';
1824

@@ -77,6 +83,17 @@ public function extraRules(): array
7783

7884
public function augment($data)
7985
{
86+
if (Statamic::isApiRoute()) {
87+
$content = $this->field()->parent();
88+
89+
return (new Cascade)
90+
->with(SiteDefaults::load()->augmented())
91+
->with($this->getAugmentedSectionDefaults($content))
92+
->with($data)
93+
->withCurrent($content)
94+
->get();
95+
}
96+
8097
if (empty($data) || ! is_iterable($data)) {
8198
return $data;
8299
}

tests/SeoProFieldtypeTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

tests/TestCase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ public static function assertStringNotContainsStringIgnoringLineEndings($needle,
150150
);
151151
}
152152

153+
protected function assertArrayHasKeys(array $keys, array|\ArrayAccess $array): void
154+
{
155+
foreach ($keys as $key) {
156+
$this->assertArrayHasKey($key, $array);
157+
}
158+
}
159+
153160
private function addGqlMacros()
154161
{
155162
TestResponse::macro('assertGqlOk', function () {

0 commit comments

Comments
 (0)