Skip to content
Draft
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
17 changes: 17 additions & 0 deletions src/Entries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ public function updateEntryUris($ids = null)
{
Facades\Entry::updateUris($this, $ids);

$this->collectionsMountedOnEntries($ids)->each->updateEntryUris();

return $this;
}

Expand Down Expand Up @@ -979,6 +981,21 @@ public function getQueryableValue(string $field)
return null;
}

private function collectionsMountedOnEntries($ids)
{
$mounted = Facades\Collection::all()
->filter(fn ($collection) => $collection->mount()?->collectionHandle() === $this->handle());

if ($mounted->isEmpty() || collect($ids)->isEmpty()) {
return $mounted;
}

// Compare roots, since the ids may be localizations of the mounted entry.
$rootIds = collect($ids)->map(fn ($id) => Entry::find($id)?->root()->id())->filter()->all();

return $mounted->filter(fn ($collection) => in_array($collection->mount()->root()->id(), $rootIds));
}

private function queryableMethods(): array
{
return [
Expand Down
89 changes: 89 additions & 0 deletions tests/Feature/Entries/MountingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,93 @@ public function updating_a_mounted_page_will_not_update_the_uris_when_slug_is_cl

$mount->save();
}

#[Test]
public function updating_the_parent_of_a_mounted_page_will_update_the_uris_for_each_entry_in_that_collection()
{
config(['cache.default' => 'file']); // Doesn't work when they're arrays since the object is stored in memory.
Cache::clear();

$this->makeStructuredPages();
Collection::make('blog')->routes('{mount}/{slug}')->mount('blog')->save();
$one = EntryFactory::collection('blog')->slug('one')->create();

$this->assertEquals($one->id(), Entry::findByUri('/about/blog/one')->id());

Entry::find('about')->slug('info')->save();

$this->assertNull(Entry::findByUri('/about/blog/one'));
$this->assertEquals($one->id(), Entry::findByUri('/info/blog/one')->id());
}

#[Test]
public function moving_a_mounted_page_in_the_tree_will_update_the_uris_for_each_entry_in_that_collection()
{
config(['cache.default' => 'file']); // Doesn't work when they're arrays since the object is stored in memory.
Cache::clear();

$pages = $this->makeStructuredPages();
Collection::make('blog')->routes('{mount}/{slug}')->mount('blog')->save();
$one = EntryFactory::collection('blog')->slug('one')->create();

$this->assertEquals($one->id(), Entry::findByUri('/about/blog/one')->id());

$pages->structure()->in('en')->tree([
['entry' => 'about'],
['entry' => 'contact', 'children' => [['entry' => 'blog']]],
])->save();

$this->assertNull(Entry::findByUri('/about/blog/one'));
$this->assertEquals($one->id(), Entry::findByUri('/contact/blog/one')->id());
}

#[Test]
public function moving_a_localized_mounted_page_in_the_tree_will_update_the_uris_for_each_entry_in_that_collection()
{
config(['cache.default' => 'file']); // Doesn't work when they're arrays since the object is stored in memory.
Cache::clear();

$this->setSites([
'en' => ['url' => '/', 'locale' => 'en'],
'fr' => ['url' => '/fr/', 'locale' => 'fr'],
]);

$pages = $this->makeStructuredPages(['en', 'fr']);
collect(['about', 'contact', 'blog'])->each(
fn ($id) => Entry::find($id)->makeLocalization('fr')->id("{$id}-fr")->save()
);
$pages->structure()->in('fr')->tree([
['entry' => 'about-fr', 'children' => [['entry' => 'blog-fr']]],
['entry' => 'contact-fr'],
])->save();

Collection::make('blog')->sites(['en', 'fr'])->routes('{mount}/{slug}')->mount('blog')->save();
$one = EntryFactory::collection('blog')->locale('fr')->slug('one')->create();

$this->assertEquals($one->id(), Entry::findByUri('/about/blog/one', 'fr')->id());

$pages->structure()->in('fr')->tree([
['entry' => 'about-fr'],
['entry' => 'contact-fr', 'children' => [['entry' => 'blog-fr']]],
])->save();

$this->assertNull(Entry::findByUri('/about/blog/one', 'fr'));
$this->assertEquals($one->id(), Entry::findByUri('/contact/blog/one', 'fr')->id());
}

private function makeStructuredPages(array $sites = ['en'])
{
$pages = tap(Collection::make('pages')->sites($sites)->routes('{parent_uri}/{slug}')->structureContents(['root' => false]))->save();

EntryFactory::id('about')->collection('pages')->slug('about')->create();
EntryFactory::id('contact')->collection('pages')->slug('contact')->create();
EntryFactory::id('blog')->collection('pages')->slug('blog')->create();

$pages->structure()->in('en')->tree([
['entry' => 'about', 'children' => [['entry' => 'blog']]],
['entry' => 'contact'],
])->save();

return $pages;
}
}
Loading