Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Stache/Traverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public function traverse($store)

public function filter($filter)
{
$this->filter = $filter;
$clone = clone $this;

return $this;
$clone->filter = $filter;

return $clone;
}
}
45 changes: 45 additions & 0 deletions tests/Stache/TraverserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Path;
use Statamic\Facades\Stache as StacheFacade;
use Statamic\Stache\Stache;
use Statamic\Stache\Stores\BasicStore;
use Statamic\Stache\Stores\CollectionsStore;
use Statamic\Stache\Stores\EntriesStore;
use Statamic\Stache\Traverser;
use Symfony\Component\Finder\SplFileInfo;
use Tests\TestCase;
Expand Down Expand Up @@ -124,4 +127,46 @@ public function makeItemFromFile($path, $contents)
$dir.'/three.txt' => 1234567893,
], $files->all());
}

#[Test]
public function filtering_does_not_change_the_filter_of_the_original_instance()
{
touch($this->tempDir.'/one.txt');
touch($this->tempDir.'/two.yaml');

$store = Mockery::mock();
$store->shouldReceive('directory')->andReturn($this->tempDir);

$txt = $this->traverser->filter(fn ($file) => $file->getExtension() === 'txt');
$yaml = $this->traverser->filter(fn ($file) => $file->getExtension() === 'yaml');

$dir = Path::tidy($this->tempDir);
$this->assertSame([$dir.'/one.txt'], $txt->traverse($store)->keys()->all());
$this->assertSame([$dir.'/two.yaml'], $yaml->traverse($store)->keys()->all());
$this->assertCount(2, $this->traverser->traverse($store));
}

#[Test]
public function a_traversal_started_by_a_filter_does_not_replace_the_running_traversals_filter()
{
$this->setSites([
'en' => ['url' => '/', 'locale' => 'en_US'],
'fr' => ['url' => '/fr/', 'locale' => 'fr_FR'],
]);

mkdir($this->tempDir.'/blog/en', 0777, true);
file_put_contents($this->tempDir.'/blog.yaml', "sites:\n - en\n - fr\n");

foreach (['alpha', 'bravo', 'charlie'] as $slug) {
file_put_contents($this->tempDir."/blog/en/{$slug}.md", "---\nid: {$slug}\ntitle: {$slug}\n---\n");
}

// On multisite, the entries store's filter resolves the collection, and on
// a cold cache that builds the collections store from disk while the
// entries traversal is still looping over its files.
StacheFacade::registerStore((new CollectionsStore)->directory($this->tempDir));
StacheFacade::registerStore((new EntriesStore)->directory($this->tempDir));

$this->assertCount(3, StacheFacade::store('entries')->store('blog')->paths());
}
}
Loading