diff --git a/src/Stache/Indexes/Terms/Associations.php b/src/Stache/Indexes/Terms/Associations.php index bb7e185780..a1569d609b 100644 --- a/src/Stache/Indexes/Terms/Associations.php +++ b/src/Stache/Indexes/Terms/Associations.php @@ -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) { // diff --git a/src/Stache/Stores/TaxonomyTermsStore.php b/src/Stache/Stores/TaxonomyTermsStore.php index 21cac1a0f5..01de86a667 100644 --- a/src/Stache/Stores/TaxonomyTermsStore.php +++ b/src/Stache/Stores/TaxonomyTermsStore.php @@ -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([ @@ -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) @@ -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); diff --git a/tests/Stache/Stores/EntriesStoreTest.php b/tests/Stache/Stores/EntriesStoreTest.php index 1a9fd66795..8e3f3722f5 100644 --- a/tests/Stache/Stores/EntriesStoreTest.php +++ b/tests/Stache/Stores/EntriesStoreTest.php @@ -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 */ diff --git a/tests/Stache/Stores/TermsStoreTest.php b/tests/Stache/Stores/TermsStoreTest.php new file mode 100644 index 0000000000..9961d42dd2 --- /dev/null +++ b/tests/Stache/Stores/TermsStoreTest.php @@ -0,0 +1,42 @@ +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')); + } +}