Skip to content

Commit d200806

Browse files
afonicryanmitchell
andauthored
Remove ensure associations (#434)
* Remove ensureAssociations from TermRepository * Add test to ensure count is correct * Tidy up * Revert "Remove ensureAssociations from TermRepository" This reverts commit 9049a4e. * Change approach * when taxonomies not terms * Add test to ensure associations generate --------- Co-authored-by: Ryan Mitchell <[email protected]>
1 parent 2c85ac6 commit d200806

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Taxonomies/TermRepository.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,13 @@ public static function bindings(): array
122122
TermContract::class => Term::class,
123123
];
124124
}
125+
126+
protected function ensureAssociations()
127+
{
128+
if (config('statamic.eloquent-driver.taxonomies.driver', 'file') === 'eloquent') {
129+
return;
130+
}
131+
132+
parent::ensureAssociations();
133+
}
125134
}

tests/Terms/TermTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
namespace Tests\Terms;
44

55
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Illuminate\Support\Facades\Facade;
67
use PHPUnit\Framework\Attributes\Test;
8+
use Statamic\Contracts\Taxonomies\TaxonomyRepository as TaxonomyRepositoryContract;
9+
use Statamic\Eloquent\Entries\Entry;
710
use Statamic\Eloquent\Taxonomies\Taxonomy;
811
use Statamic\Eloquent\Taxonomies\TermModel;
12+
use Statamic\Facades\Collection;
13+
use Statamic\Facades\Stache;
914
use Statamic\Facades\Term as TermFacade;
15+
use Statamic\Statamic;
16+
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
1017
use Tests\TestCase;
1118

1219
class TermTest extends TestCase
1320
{
21+
use PreventsSavingStacheItemsToDisk;
1422
use RefreshDatabase;
1523

1624
#[Test]
@@ -57,4 +65,42 @@ public function it_saves_updated_at_value_correctly()
5765
$this->assertEquals(now(), $term->updated_at);
5866
$this->assertEquals(now(), TermFacade::query()->first()->updated_at);
5967
}
68+
69+
#[Test]
70+
public function it_gets_entry_count_for_term()
71+
{
72+
Taxonomy::make('test')->title('test')->save();
73+
74+
$term = tap(TermFacade::make('test-term')->taxonomy('test')->data([]))->save();
75+
76+
$collection = Collection::make('blog')->routes('blog/{slug}')->taxonomies(['test'])->save();
77+
78+
(new Entry)->id(1)->collection($collection)->data(['title' => 'Post 1', 'test' => ['test-term']])->slug('alfa')->save();
79+
(new Entry)->id(2)->collection($collection)->data(['title' => 'Post 2', 'test' => ['test-term']])->slug('bravo')->save();
80+
(new Entry)->id(3)->collection($collection)->data(['title' => 'Post 3'])->slug('charlie')->save();
81+
82+
$this->assertEquals(2, TermFacade::entriesCount($term));
83+
}
84+
85+
#[Test]
86+
public function it_build_stache_associations_when_taxonomy_driver_is_not_eloquent()
87+
{
88+
Facade::clearResolvedInstance(TaxonomyRepositoryContract::class);
89+
Statamic::repository(TaxonomyRepositoryContract::class, \Statamic\Stache\Repositories\TaxonomyRepository::class);
90+
91+
Taxonomy::make('test')->title('test')->save();
92+
93+
TermFacade::make('test-term')->taxonomy('test')->data([])->save();
94+
95+
$taxonomyStore = Stache::stores()->get('terms');
96+
$this->assertCount(0, $taxonomyStore->store('test')->index('associations')->items());
97+
98+
$collection = Collection::make('blog')->routes('blog/{slug}')->taxonomies(['test'])->save();
99+
100+
(new Entry)->id(1)->collection($collection)->data(['title' => 'Post 1', 'test' => ['test-term']])->slug('alfa')->save();
101+
(new Entry)->id(2)->collection($collection)->data(['title' => 'Post 2', 'test' => ['test-term']])->slug('bravo')->save();
102+
(new Entry)->id(3)->collection($collection)->data(['title' => 'Post 3'])->slug('charlie')->save();
103+
104+
$this->assertCount(2, $taxonomyStore->store('test')->index('associations')->items());
105+
}
60106
}

0 commit comments

Comments
 (0)