Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'mount' variable to Entries - Take #2 #3046

Merged
merged 6 commits into from Dec 23, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Entries/AugmentedEntry.php
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Entries;

use Statamic\Data\AbstractAugmented;
use Statamic\Facades\Collection;

class AugmentedEntry extends AbstractAugmented
{
Expand Down Expand Up @@ -32,6 +33,7 @@ private function commonKeys()
'order',
'is_entry',
'collection',
'mount',
'last_modified',
'updated_at',
'updated_by',
Expand Down Expand Up @@ -62,4 +64,9 @@ protected function parent()
{
return $this->data->parent();
}

protected function mount()
{
return $this->data->value('mount') ?? Collection::findByMount($this->data);
}
}
24 changes: 24 additions & 0 deletions tests/Data/Entries/AugmentedEntryTest.php
Expand Up @@ -76,6 +76,8 @@ public function it_gets_values()
->setSupplement('three', 'the "three" value supplemented on the entry')
->setSupplement('four', 'the "four" value supplemented on the entry and in the blueprint');

$mount = tap(Collection::make('mountable')->mount($entry->id()))->save();

$augmented = new AugmentedEntry($entry);

$expectations = [
Expand All @@ -93,6 +95,7 @@ public function it_gets_values()
'order' => ['type' => 'null', 'value' => null], // todo: test for when this is an int
'is_entry' => ['type' => 'bool', 'value' => true],
'collection' => ['type' => CollectionContract::class, 'value' => $collection],
'mount' => ['type' => CollectionContract::class, 'value' => $mount],
'last_modified' => ['type' => Carbon::class, 'value' => '2017-02-03 14:10'],
'updated_at' => ['type' => Carbon::class, 'value' => '2017-02-03 14:10'],
'updated_by' => ['type' => UserContract::class, 'value' => 'test-user'],
Expand All @@ -109,4 +112,25 @@ public function it_gets_values()

$this->assertAugmentedCorrectly($expectations, $augmented);
}

/** @test */
public function it_gets_the_mount_from_the_value_first_if_it_exists()
{
$mount = tap(Collection::make('a'))->save();

$entry = EntryFactory::id('entry-id')
->collection('test')
->slug('entry-slug')
->create();

$augmented = new AugmentedEntry($entry);

$this->assertNull($augmented->get('mount'));

$mount->mount($entry->id())->save();
$this->assertEquals($mount, $augmented->get('mount'));

$entry->set('mount', 'b');
$this->assertEquals('b', $augmented->get('mount'));
}
}