Skip to content

Commit

Permalink
[4.x] Delete collection tree files when deleting collections (#9183)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Dec 11, 2023
1 parent f104d0b commit 0c73ede
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Entries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ public function delete()
$entry->delete();
});

if ($this->hasStructure()) {
$this->structure()->trees()->each->delete();
}

Facades\Collection::delete($this);

CollectionDeleted::dispatch($this);
Expand Down
2 changes: 1 addition & 1 deletion src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function delete()
if (optional($parent)->isRoot()) {
$parent = null;
}
$this->page()->pages()->all()->each(function ($child) use ($tree, $parent) {
$this->page()?->pages()->all()->each(function ($child) use ($tree, $parent) {
$tree->move($child->id(), optional($parent)->id());
});
$tree->remove($this);
Expand Down
2 changes: 1 addition & 1 deletion src/Structures/StructureRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function find($id): ?Structure
public function findByHandle($handle): ?Structure
{
if (Str::startsWith($handle, 'collection::')) {
return Collection::find(Str::after($handle, 'collection::'))->structure();
return Collection::find(Str::after($handle, 'collection::'))?->structure();
}

return Nav::find($handle);
Expand Down
53 changes: 53 additions & 0 deletions tests/Feature/Collections/DeleteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Facades\Structure;
use Statamic\Facades\User;
use Tests\FakesRoles;
use Tests\PreventSavingStacheItemsToDisk;
Expand Down Expand Up @@ -77,4 +78,56 @@ public function it_deletes_the_collection_with_localized_entries()

$this->assertCount(0, Collection::all());
}

/** @test */
public function it_deletes_tree_files()
{
$this->setTestRoles(['test' => ['access cp', 'configure collections']]);
$user = tap(User::make()->assignRole('test'))->save();

$collection = tap(Collection::make('test')->structureContents(['root' => true]))->save();

Entry::make()->id('a')->slug('a')->collection('test')->data(['title' => 'A'])->save();
Entry::make()->id('b')->slug('b')->collection('test')->data(['title' => 'B'])->save();
Entry::make()->id('c')->slug('c')->collection('test')->data(['title' => 'C'])->save();

$collection->structure()->in('en')->tree([['entry' => 'a'], ['entry' => 'b'], ['entry' => 'c']]);

$this
->actingAs($user)
->delete(cp_route('collections.destroy', $collection->handle()))
->assertOk();

$this->assertCount(0, Collection::all());
$this->assertNull(Structure::find('collection::test'));
}

/** @test */
public function it_deletes_tree_files_in_a_multisite()
{
Site::setConfig(['sites' => [
'en' => ['url' => '/', 'locale' => 'en_US'],
'fr' => ['url' => '/fr', 'locale' => 'fr_FR'],
]]);

$this->setTestRoles(['test' => ['access cp', 'configure collections']]);
$user = tap(User::make()->assignRole('test'))->save();

$collection = tap(Collection::make('test')->sites(['en', 'fr'])->structureContents(['root' => true]))->save();

Entry::make()->id('a')->slug('a')->locale('en')->collection('test')->data(['title' => 'A'])->save();
Entry::make()->id('b')->slug('b')->locale('en')->collection('test')->data(['title' => 'B'])->save();
Entry::make()->id('c')->slug('c')->locale('fr')->collection('test')->data(['title' => 'C'])->save();

$collection->structure()->in('en')->tree([['entry' => 'a'], ['entry' => 'b']]);
$collection->structure()->in('fr')->tree([['entry' => 'c']]);

$this
->actingAs($user)
->delete(cp_route('collections.destroy', $collection->handle()))
->assertOk();

$this->assertCount(0, Collection::all());
$this->assertNull(Structure::find('collection::test'));
}
}

0 comments on commit 0c73ede

Please sign in to comment.