Skip to content

Commit

Permalink
Ensure asset container contents folder check finishes with / (#356)
Browse files Browse the repository at this point in the history
* Ensure folder check finishes with /

* Test coverage

* Better test name
  • Loading branch information
ryanmitchell authored Sep 26, 2024
1 parent c58b1cf commit f0c69a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Assets/AssetContainerContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function filteredDirectoriesIn($folder, $recursive)

return $this->directories()
->filter(function ($dir) use ($folder) {
if ($folder && ! Str::startsWith($dir['path'], $folder)) {
if ($folder && ! Str::startsWith($dir['path'], Str::finish($folder, '/'))) {
return false;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Assets/AssetContainerContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,18 @@ public function it_creates_parent_folders_where_they_dont_exist()

$this->assertCount(3, $container->contents()->filteredDirectoriesIn('', true));
}

#[Test]
public function it_doesnt_nest_folders_that_start_with_the_same_name()
{
$container = tap(AssetContainer::make('test')->disk('test'))->save();
$container->makeAsset('one/file.txt')->upload(UploadedFile::fake()->create('one.txt'));
$container->makeAsset('one-two/file.txt')->upload(UploadedFile::fake()->create('one.txt'));
$container->makeAsset('one/two/file.txt')->upload(UploadedFile::fake()->create('one.txt'));

$filtered = $container->contents()->filteredDirectoriesIn('one/', true);

$this->assertCount(1, $filtered);
$this->assertSame($filtered->keys()->all(), ['one/two']);
}
}

0 comments on commit f0c69a3

Please sign in to comment.