Skip to content

Commit

Permalink
Add tests to ensure trees are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Dec 11, 2023
1 parent ecce5a3 commit 9ac5d66
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
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 9ac5d66

Please sign in to comment.