|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Data; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\Test; |
| 6 | +use Statamic\Assets\AssetReferenceUpdater; |
| 7 | +use Statamic\Facades; |
| 8 | +use Statamic\Fields\Fieldtype; |
| 9 | +use Statamic\Fieldtypes\UpdatesReferences; |
| 10 | +use Tests\PreventSavingStacheItemsToDisk; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class DataReferenceUpdaterTest extends TestCase |
| 14 | +{ |
| 15 | + use PreventSavingStacheItemsToDisk; |
| 16 | + |
| 17 | + public function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + ParentRecorderFieldtype::register(); |
| 22 | + ParentRecorderFieldtype::$parents = []; |
| 23 | + |
| 24 | + tap(Facades\Collection::make('articles'))->save(); |
| 25 | + } |
| 26 | + |
| 27 | + #[Test] |
| 28 | + public function it_gives_top_level_fields_the_item_being_updated_as_their_parent() |
| 29 | + { |
| 30 | + $this->setInBlueprints('collections/articles', [ |
| 31 | + 'fields' => [ |
| 32 | + ['handle' => 'hero', 'field' => ['type' => 'parent_recorder']], |
| 33 | + ], |
| 34 | + ]); |
| 35 | + |
| 36 | + $one = tap(Facades\Entry::make()->collection('articles')->slug('one')->data(['hero' => 'hoff.jpg']))->save(); |
| 37 | + $two = tap(Facades\Entry::make()->collection('articles')->slug('two')->data(['hero' => 'hoff.jpg']))->save(); |
| 38 | + |
| 39 | + $this->updateReferences($one); |
| 40 | + $this->updateReferences($two); |
| 41 | + |
| 42 | + $this->assertCount(2, ParentRecorderFieldtype::$parents); |
| 43 | + $this->assertSame($one, ParentRecorderFieldtype::$parents[0]); |
| 44 | + $this->assertSame($two, ParentRecorderFieldtype::$parents[1]); |
| 45 | + } |
| 46 | + |
| 47 | + #[Test] |
| 48 | + public function it_gives_nested_fields_the_item_being_updated_as_their_parent() |
| 49 | + { |
| 50 | + $this->setInBlueprints('collections/articles', [ |
| 51 | + 'fields' => [ |
| 52 | + [ |
| 53 | + 'handle' => 'grid', |
| 54 | + 'field' => [ |
| 55 | + 'type' => 'grid', |
| 56 | + 'fields' => [ |
| 57 | + ['handle' => 'hero', 'field' => ['type' => 'parent_recorder']], |
| 58 | + ], |
| 59 | + ], |
| 60 | + ], |
| 61 | + ], |
| 62 | + ]); |
| 63 | + |
| 64 | + $entry = tap(Facades\Entry::make()->collection('articles')->slug('one')->data([ |
| 65 | + 'grid' => [['hero' => 'hoff.jpg']], |
| 66 | + ]))->save(); |
| 67 | + |
| 68 | + $this->updateReferences($entry); |
| 69 | + |
| 70 | + $this->assertCount(1, ParentRecorderFieldtype::$parents); |
| 71 | + $this->assertSame($entry, ParentRecorderFieldtype::$parents[0]); |
| 72 | + } |
| 73 | + |
| 74 | + #[Test] |
| 75 | + public function it_doesnt_leave_the_item_on_the_blueprints_shared_fields() |
| 76 | + { |
| 77 | + $this->setInBlueprints('collections/articles', [ |
| 78 | + 'fields' => [ |
| 79 | + ['handle' => 'hero', 'field' => ['type' => 'parent_recorder']], |
| 80 | + ], |
| 81 | + ]); |
| 82 | + |
| 83 | + $entry = tap(Facades\Entry::make()->collection('articles')->slug('one')->data(['hero' => 'hoff.jpg']))->save(); |
| 84 | + |
| 85 | + $blueprint = Facades\Collection::find('articles')->entryBlueprint(); |
| 86 | + $blueprint->setParent(null); |
| 87 | + |
| 88 | + $this->updateReferences($entry); |
| 89 | + |
| 90 | + $this->assertNull($blueprint->fields()->get('hero')->parent()); |
| 91 | + } |
| 92 | + |
| 93 | + private function setInBlueprints($namespace, $blueprintContents) |
| 94 | + { |
| 95 | + $blueprint = tap(Facades\Blueprint::make('set-in-blueprints')->setContents($blueprintContents))->save(); |
| 96 | + |
| 97 | + Facades\Blueprint::shouldReceive('in')->with($namespace)->andReturn(collect([$blueprint])); |
| 98 | + } |
| 99 | + |
| 100 | + private function updateReferences($item) |
| 101 | + { |
| 102 | + AssetReferenceUpdater::item($item) |
| 103 | + ->filterByContainer('test_container') |
| 104 | + ->updateReferences('hoff.jpg', 'norris.jpg'); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class ParentRecorderFieldtype extends Fieldtype |
| 109 | +{ |
| 110 | + use UpdatesReferences; |
| 111 | + |
| 112 | + public static $parents = []; |
| 113 | + |
| 114 | + public function replaceAssetReferences($data, ?string $newValue, string $oldValue, string $container) |
| 115 | + { |
| 116 | + static::$parents[] = $this->field->parent(); |
| 117 | + |
| 118 | + return $data; |
| 119 | + } |
| 120 | +} |
0 commit comments