-
-
Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathUpdateTaxonomyTest.php
More file actions
150 lines (124 loc) · 5.09 KB
/
Copy pathUpdateTaxonomyTest.php
File metadata and controls
150 lines (124 loc) · 5.09 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
<?php
namespace Tests\Feature\Taxonomies;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Collection;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\User;
use Tests\FakesRoles;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class UpdateTaxonomyTest extends TestCase
{
use FakesRoles;
use PreventSavingStacheItemsToDisk;
#[Test]
public function it_denies_access_if_you_dont_have_permission()
{
$taxonomy = tap(Taxonomy::make('test'))->save();
$this
->from('/original')
->actingAs($this->userWithoutPermission())
->update($taxonomy)
->assertRedirect('/original')
->assertSessionHas('error');
}
#[Test]
public function it_updates_a_taxonomy()
{
$taxonomy = tap(
Taxonomy::make('test')
->title('Original title')
)->save();
$this->assertCount(1, Taxonomy::all());
$this->assertEquals('Original title', $taxonomy->title());
$this
->actingAs($this->userWithPermission())
->update($taxonomy, [
'title' => 'Updated title',
])
->assertOk();
$this->assertCount(1, Taxonomy::all());
$this->assertEquals('Updated title', $taxonomy->title());
}
#[Test]
public function it_associates_taxonomies_with_collections()
{
$taxonomy = tap(Taxonomy::make('test'))->save();
$collectionOne = tap(Collection::make('one')->taxonomies(['test']))->save();
$collectionTwo = tap(Collection::make('two')->taxonomies(['test']))->save();
$collectionThree = tap(Collection::make('three'))->save();
$this->assertEquals(['one', 'two'], $taxonomy->collections()->map->handle()->all());
$this->assertTrue($collectionOne->taxonomies()->contains($taxonomy));
$this->assertTrue($collectionTwo->taxonomies()->contains($taxonomy));
$this->assertFalse($collectionThree->taxonomies()->contains($taxonomy));
$this
->actingAs($this->userWithPermission())
->update($taxonomy, [
'collections' => ['one', 'three'],
])
->assertOk();
$this->assertEquals(['one', 'three'], $taxonomy->collections()->map->handle()->all());
$this->assertTrue($collectionOne->taxonomies()->contains($taxonomy));
$this->assertFalse($collectionTwo->taxonomies()->contains($taxonomy));
$this->assertTrue($collectionThree->taxonomies()->contains($taxonomy));
}
#[Test]
public function it_updates_taxonomy_sites_from_enabled_rows_and_ignores_origins()
{
$this->setSites([
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'],
'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'],
'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://de.test.com/'],
]);
$taxonomy = tap(Taxonomy::make('test')->sites(['en', 'fr']))->save();
$this
->actingAs($this->userWithPermission())
->update($taxonomy, [
'sites' => [
['name' => 'English', 'handle' => 'en', 'enabled' => true, 'origin' => null],
['name' => 'French', 'handle' => 'fr', 'enabled' => false, 'origin' => 'en'],
['name' => 'German', 'handle' => 'de', 'enabled' => true, 'origin' => 'en'],
],
'preview_targets' => [],
'collections' => [],
])
->assertOk();
$this->assertEquals(['en', 'de'], Taxonomy::findByHandle('test')->sites()->all());
}
#[Test]
public function it_updates_taxonomy_sites_from_legacy_handle_array()
{
$this->setSites([
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'],
'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'],
'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://de.test.com/'],
]);
$taxonomy = tap(Taxonomy::make('test')->sites(['en', 'fr']))->save();
$this
->actingAs($this->userWithPermission())
->update($taxonomy, [
'sites' => ['en', 'de'],
'preview_targets' => [],
'collections' => [],
])
->assertOk();
$this->assertEquals(['en', 'de'], Taxonomy::findByHandle('test')->sites()->all());
}
private function userWithoutPermission()
{
$this->setTestRoles(['test' => ['access cp']]);
return tap(User::make()->assignRole('test'))->save();
}
private function userWithPermission()
{
$this->setTestRoles(['test' => ['access cp', 'configure taxonomies']]);
return tap(User::make()->assignRole('test'))->save();
}
private function update($taxonomy, $params = [])
{
$params = array_merge([
'title' => 'Updated title',
], $params);
return $this->patch(cp_route('taxonomies.update', $taxonomy->handle()), $params);
}
}