Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Data/DataReferenceUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,23 @@ public function updateReferences($originalValue, $newValue)
*/
protected function getTopLevelFields()
{
return $this->item->blueprint()->fields()->all();
return $this->fieldsWithItemAsParent($this->item->blueprint()->fields());
}

/**
* While updating references, the parent of every field is the item being updated.
*
* Blueprints hand back a Fields instance that's cached globally by handle, and nested fields
* are constructed without a parent at all, so neither can be relied upon to have the right
* one. The fields are cloned rather than mutated in place, otherwise the item would leak
* into every other consumer of that blueprint for the rest of the request.
*
* @param \Statamic\Fields\Fields $fields
* @return \Illuminate\Support\Collection
*/
private function fieldsWithItemAsParent($fields)
{
return $fields->all()->map(fn ($field) => (clone $field)->setParent($this->item));
}

/**
Expand Down Expand Up @@ -120,7 +136,7 @@ protected function fieldsWithReferenceUpdates($fields)
*/
public function processNestedFields($fields, $dottedPrefix): void
{
$this->recursivelyUpdateFields($fields->all(), $dottedPrefix);
$this->recursivelyUpdateFields($this->fieldsWithItemAsParent($fields), $dottedPrefix);
}

/**
Expand Down
120 changes: 120 additions & 0 deletions tests/Data/DataReferenceUpdaterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Tests\Data;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Assets\AssetReferenceUpdater;
use Statamic\Facades;
use Statamic\Fields\Fieldtype;
use Statamic\Fieldtypes\UpdatesReferences;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class DataReferenceUpdaterTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

public function setUp(): void
{
parent::setUp();

ParentRecorderFieldtype::register();
ParentRecorderFieldtype::$parents = [];

tap(Facades\Collection::make('articles'))->save();
}

#[Test]
public function it_gives_top_level_fields_the_item_being_updated_as_their_parent()
{
$this->setInBlueprints('collections/articles', [
'fields' => [
['handle' => 'hero', 'field' => ['type' => 'parent_recorder']],
],
]);

$one = tap(Facades\Entry::make()->collection('articles')->slug('one')->data(['hero' => 'hoff.jpg']))->save();
$two = tap(Facades\Entry::make()->collection('articles')->slug('two')->data(['hero' => 'hoff.jpg']))->save();

$this->updateReferences($one);
$this->updateReferences($two);

$this->assertCount(2, ParentRecorderFieldtype::$parents);
$this->assertSame($one, ParentRecorderFieldtype::$parents[0]);
$this->assertSame($two, ParentRecorderFieldtype::$parents[1]);
}

#[Test]
public function it_gives_nested_fields_the_item_being_updated_as_their_parent()
{
$this->setInBlueprints('collections/articles', [
'fields' => [
[
'handle' => 'grid',
'field' => [
'type' => 'grid',
'fields' => [
['handle' => 'hero', 'field' => ['type' => 'parent_recorder']],
],
],
],
],
]);

$entry = tap(Facades\Entry::make()->collection('articles')->slug('one')->data([
'grid' => [['hero' => 'hoff.jpg']],
]))->save();

$this->updateReferences($entry);

$this->assertCount(1, ParentRecorderFieldtype::$parents);
$this->assertSame($entry, ParentRecorderFieldtype::$parents[0]);
}

#[Test]
public function it_doesnt_leave_the_item_on_the_blueprints_shared_fields()
{
$this->setInBlueprints('collections/articles', [
'fields' => [
['handle' => 'hero', 'field' => ['type' => 'parent_recorder']],
],
]);

$entry = tap(Facades\Entry::make()->collection('articles')->slug('one')->data(['hero' => 'hoff.jpg']))->save();

$blueprint = Facades\Collection::find('articles')->entryBlueprint();
$blueprint->setParent(null);

$this->updateReferences($entry);

$this->assertNull($blueprint->fields()->get('hero')->parent());
}

private function setInBlueprints($namespace, $blueprintContents)
{
$blueprint = tap(Facades\Blueprint::make('set-in-blueprints')->setContents($blueprintContents))->save();

Facades\Blueprint::shouldReceive('in')->with($namespace)->andReturn(collect([$blueprint]));
}

private function updateReferences($item)
{
AssetReferenceUpdater::item($item)
->filterByContainer('test_container')
->updateReferences('hoff.jpg', 'norris.jpg');
}
}

class ParentRecorderFieldtype extends Fieldtype
{
use UpdatesReferences;

public static $parents = [];

public function replaceAssetReferences($data, ?string $newValue, string $oldValue, string $container)
{
static::$parents[] = $this->field->parent();

return $data;
}
}
Loading