Skip to content

Commit

Permalink
[5.x] Add failing test for issue caused by PR #9585 (#10061)
Browse files Browse the repository at this point in the history
Co-authored-by: John Koster <john@stillat.com>
  • Loading branch information
aerni and JohnathonKoster authored May 20, 2024
1 parent ab16009 commit abf641a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Fields/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public function ensureFieldInTab($handle, $config, $tab, $prepend = false)

$this->ensuredFields[$handle] = compact('handle', 'tab', 'prepend', 'config');

$this->resetFieldsCache();
$this->resetBlueprintCache()->resetFieldsCache();

return $this;
}
Expand Down Expand Up @@ -573,7 +573,7 @@ public function removeTab($handle)

Arr::pull($this->contents['tabs'], $handle);

return $this->resetFieldsCache();
return $this->resetBlueprintCache()->resetFieldsCache();
}

public function removeFieldFromTab($handle, $tab)
Expand All @@ -591,7 +591,7 @@ public function removeFieldFromTab($handle, $tab)
// Pull it out.
Arr::pull($this->contents['tabs'][$tab]['sections'][$sectionIndex]['fields'], $fieldKey);

return $this->resetFieldsCache();
return $this->resetBlueprintCache()->resetFieldsCache();
}

private function getTabFields($tab)
Expand Down Expand Up @@ -621,7 +621,7 @@ protected function ensureFieldInTabHasConfig($handle, $tab, $config)
// Merge in new field config.
$this->contents['tabs'][$tab]['sections'][$sectionKey]['fields'][$fieldKey]['field'] = array_merge($existingConfig, $config);

return $this->resetFieldsCache();
return $this->resetBlueprintCache()->resetFieldsCache();
}

public function validateUniqueHandles()
Expand All @@ -635,6 +635,13 @@ public function validateUniqueHandles()
}
}

protected function resetBlueprintCache()
{
$this->lastBlueprintHandle = null;

return $this;
}

protected function resetFieldsCache()
{
if ($this->parent) {
Expand Down
24 changes: 24 additions & 0 deletions tests/Fields/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Statamic\Contracts\Query\QueryableValue;
use Statamic\CP\Column;
use Statamic\CP\Columns;
use Statamic\Entries\Entry;
use Statamic\Events\BlueprintCreated;
use Statamic\Events\BlueprintCreating;
use Statamic\Events\BlueprintDeleted;
Expand Down Expand Up @@ -783,6 +784,29 @@ public function it_ensures_a_field_exists_in_a_specific_tab()
$this->assertEquals(['type' => 'textarea'], $blueprint->fields()->get('new')->config());
}

/** @test */
public function it_can_add_fields_multiple_times()
{
$blueprint = (new Blueprint)
->setNamespace('collections/collection_one')
->setHandle('blueprint_one');

$entry = (new Entry)
->collection('collection_one')
->blueprint($blueprint);

$blueprint->setParent($entry);

$blueprint->ensureFieldsInTab(['field_one' => ['type' => 'text']], 'tab_one');

$this->assertTrue($blueprint->hasField('field_one'));

$blueprint->ensureField('field_two', ['type' => 'textarea']);

$this->assertTrue($blueprint->hasField('field_two'));

}

/** @test */
public function it_ensures_a_field_has_config()
{
Expand Down

1 comment on commit abf641a

@jasonvarga
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I shoulda renamed the PR before merging.

Please sign in to comment.