-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathTermTest.php
More file actions
159 lines (119 loc) · 5.72 KB
/
TermTest.php
File metadata and controls
159 lines (119 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Tests\Terms;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Facade;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Taxonomies\TaxonomyRepository as TaxonomyRepositoryContract;
use Statamic\Eloquent\Entries\Entry;
use Statamic\Eloquent\Taxonomies\Taxonomy;
use Statamic\Eloquent\Taxonomies\TermModel;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Facades\Stache;
use Statamic\Facades\Taxonomy as TaxonomyFacade;
use Statamic\Facades\Term as TermFacade;
use Statamic\Statamic;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
use Tests\TestCase;
class TermTest extends TestCase
{
use PreventsSavingStacheItemsToDisk;
use RefreshDatabase;
#[Test]
public function it_doesnt_create_a_new_model_when_slug_is_changed()
{
Taxonomy::make('test')->title('test')->save();
$term = tap(TermFacade::make('test-term')->taxonomy('test')->data([]))->save();
$this->assertCount(1, TermModel::all());
$this->assertSame('test-term', TermModel::first()->slug);
$term->slug('new-slug');
$term->save();
$this->assertCount(1, TermModel::all());
$this->assertSame('new-slug', TermModel::first()->slug);
}
#[Test]
public function null_values_are_removed_from_data()
{
Taxonomy::make('test')->title('test')->save();
$term = tap(TermFacade::make('test-term')->taxonomy('test')->data(['null_value' => null]))->save();
$this->assertArrayNotHasKey('null_value', $term->model()->data);
}
#[Test]
public function it_saves_updated_at_value_correctly()
{
$this->freezeSecond();
Taxonomy::make('test')->title('test')->save();
tap(TermFacade::make('test-term')->taxonomy('test')->data([]))->save();
/** @var LocalizedTerm $term */
$term = TermFacade::query()->first();
$term->set('foo', 'bar');
$term->save();
$this->assertEquals(now(), $term->updated_at);
$this->assertEquals(now(), TermFacade::query()->first()->updated_at);
}
#[Test]
public function it_gets_entry_count_for_term()
{
Taxonomy::make('test')->title('test')->save();
$term = tap(TermFacade::make('test-term')->taxonomy('test')->data([]))->save();
$collection = Collection::make('blog')->routes('blog/{slug}')->taxonomies(['test'])->save();
(new Entry)->id(1)->collection($collection)->data(['title' => 'Post 1', 'test' => ['test-term']])->slug('alfa')->save();
(new Entry)->id(2)->collection($collection)->data(['title' => 'Post 2', 'test' => ['test-term']])->slug('bravo')->save();
(new Entry)->id(3)->collection($collection)->data(['title' => 'Post 3'])->slug('charlie')->save();
$this->assertEquals(2, TermFacade::entriesCount($term));
}
#[Test]
public function it_build_stache_associations_when_taxonomy_driver_is_not_eloquent()
{
config()->set('statamic.eloquent-driver.taxonomies.driver', 'file');
Facade::clearResolvedInstance(TaxonomyRepositoryContract::class);
Statamic::repository(TaxonomyRepositoryContract::class, \Statamic\Stache\Repositories\TaxonomyRepository::class);
Taxonomy::make('test')->title('test')->save();
TermFacade::make('test-term')->taxonomy('test')->data([])->save();
$taxonomyStore = Stache::stores()->get('terms');
$this->assertCount(0, $taxonomyStore->store('test')->index('associations')->items());
$collection = Collection::make('blog')->routes('blog/{slug}')->taxonomies(['test'])->save();
(new Entry)->id(1)->collection($collection)->data(['title' => 'Post 1', 'test' => ['test-term']])->slug('alfa')->save();
(new Entry)->id(2)->collection($collection)->data(['title' => 'Post 2', 'test' => ['test-term']])->slug('bravo')->save();
(new Entry)->id(3)->collection($collection)->data(['title' => 'Post 3'])->slug('charlie')->save();
$this->assertCount(2, $taxonomyStore->store('test')->index('associations')->items());
}
#[Test]
public function it_applies_taxonomy_wheres_using_pluck_count_and_get()
{
$taxonomy = tap(Taxonomy::make('test')->title('test'))->save();
$term = tap(TermFacade::make('test-term')->taxonomy('test')->data([]))->save();
$this->assertSame(1, $taxonomy->queryTerms()->pluck('slug')->unique()->count());
$this->assertSame(1, $taxonomy->queryTerms()->count());
$this->assertSame($term->slug(), $taxonomy->queryTerms()->get()->pluck('slug')->first());
}
#[Test]
public function it_does_not_cache_null_taxonomy_lookups()
{
$taxonomy = TaxonomyFacade::findByHandle('future');
$this->assertNull($taxonomy);
// Create taxonomy directly in DB, bypassing TaxonomyRepository::save().
$modelClass = app('statamic.eloquent.taxonomies.model');
$modelClass::create([
'handle' => 'future',
'title' => 'Future Taxonomy',
'sites' => ['en'],
'settings' => [],
]);
$taxonomy = TaxonomyFacade::findByHandle('future');
$this->assertNotNull($taxonomy);
$this->assertEquals('future', $taxonomy->handle());
}
#[Test]
public function it_queries_terms_with_taxonomy_available()
{
Taxonomy::make('tags')->title('Tags')->save();
TermFacade::make('test-tag')->taxonomy('tags')->data(['title' => 'Test Tag'])->save();
Blink::flush();
$terms = TermFacade::query()->get();
$this->assertCount(1, $terms);
$this->assertEquals('test-tag', $terms->first()->slug());
$this->assertNotNull($terms->first()->taxonomy());
$this->assertEquals('tags', $terms->first()->taxonomy()->handle());
}
}