Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taxonomy cache fixes #2686

Merged
merged 5 commits into from
Oct 19, 2020
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
7 changes: 7 additions & 0 deletions src/Stache/Indexes/Terms/Associations.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public function getItems()
})->all();
}

public function forgetEntry($id)
{
$this->items = $this->items()->reject(function ($association) use ($id) {
return $association['entry'] === $id;
})->all();
}

public function updateItem($item)
{
//
Expand Down
21 changes: 9 additions & 12 deletions src/Stache/Stores/TaxonomyTermsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public function sync($entry, $terms)
return [Str::slug($value) => $value];
});

$indexes = $this->resolveIndexes()->except('associations');
$associations = $this->index('associations');
$titles = $this->index('title');
$uris = $this->index('uri');

$associations->forgetEntry($entry->id());

foreach ($terms as $slug => $value) {
$associations->push([
Expand All @@ -113,17 +114,13 @@ public function sync($entry, $terms)
'entry' => $entry->id(),
'site' => $entry->locale(),
]);

$key = $entry->locale().'::'.$slug;

$titles->put($key, $value);

$uris->put($key, $this->makeTerm($taxonomy, $slug)->uri());
}

$associations->cache();
$titles->cache();
$uris->cache();

foreach ($terms as $slug => $value) {
$term = $this->makeTerm($taxonomy, $slug);
$indexes->each->updateItem($term);
}
}

protected function makeTerm($taxonomy, $slug)
Expand Down Expand Up @@ -212,7 +209,7 @@ public function save($term)

$this->forgetItem($key);

$this->setPath($key, $item->path());
$this->setPath($key, $item->locale().'::'.$item->path());

$this->resolveIndexes()->each->updateItem($item);

Expand Down
2 changes: 2 additions & 0 deletions tests/Stache/Stores/EntriesStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public function it_saves_to_disk()
$this->assertFileEqualsString($path = $this->directory.'/blog/2017-07-04.test.md', $entry->fileContents());
@unlink($path);
$this->assertFileNotExists($path);

$this->assertEquals($path, $this->parent->store('blog')->paths()->get('123'));
}

/** @test */
Expand Down
42 changes: 42 additions & 0 deletions tests/Stache/Stores/TermsStoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Tests\Stache\Stores;

use Statamic\Facades;
use Statamic\Facades\Stache;
use Statamic\Stache\Stores\TermsStore;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class TermsStoreTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

public function setUp(): void
{
parent::setUp();

$this->parent = (new TermsStore)->directory(
$this->directory = __DIR__.'/../__fixtures__/content/taxonomies'
);

Stache::registerStore($this->parent);

Stache::store('taxonomies')->directory($this->directory);
}

/** @test */
public function it_saves_to_disk()
{
$term = Facades\Term::make('test')->taxonomy('tags');
$term->in('en')->set('title', 'Test');

$this->parent->store('tags')->save($term);

$this->assertFileEqualsString($path = $this->directory.'/tags/test.yaml', $term->fileContents());
@unlink($path);
$this->assertFileNotExists($path);

$this->assertEquals('en::'.$path, $this->parent->store('tags')->paths()->get('en::test'));
}
}