Skip to content

Commit

Permalink
Taxonomy cache fixes (#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Oct 19, 2020
1 parent 64eadfd commit 67332fd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
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'));
}
}

0 comments on commit 67332fd

Please sign in to comment.