-
-
Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathUpdateCollectionTest.php
More file actions
245 lines (204 loc) · 9.01 KB
/
Copy pathUpdateCollectionTest.php
File metadata and controls
245 lines (204 loc) · 9.01 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Tests\Feature\Collections;
use Facades\Statamic\Fields\BlueprintRepository;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Collection;
use Statamic\Facades\User;
use Tests\Fakes\FakeBlueprintRepository;
use Tests\FakesRoles;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class UpdateCollectionTest extends TestCase
{
use FakesRoles;
use PreventSavingStacheItemsToDisk;
#[Test]
public function it_denies_access_if_you_dont_have_permission()
{
$collection = Collection::make('test')->save();
$this
->from('/original')
->actingAs($this->userWithoutPermission())
->update($collection)
->assertRedirect('/original')
->assertSessionHas('error');
}
#[Test]
public function it_updates_a_collection()
{
$this->withoutExceptionHandling();
config(['statamic.amp.enabled' => true]);
$collection = tap(
Collection::make('test')
->title('Original title')
->dated(false)
->template('original-template')
->layout('original-layout')
->defaultPublishState(true)
->sortDirection('asc')
)->save();
$this->assertCount(1, Collection::all());
$this->assertEquals('Original title', $collection->title());
$this->assertFalse($collection->dated());
$this->assertEquals('public', $collection->pastDateBehavior());
$this->assertEquals('public', $collection->futureDateBehavior());
$this->assertEquals('original-template', $collection->template());
$this->assertEquals('original-layout', $collection->layout());
$this->assertTrue($collection->defaultPublishState());
$this->assertEquals('asc', $collection->sortDirection());
$this
->actingAs($this->userWithPermission())
->update($collection, [
'title' => 'Updated title',
'dated' => true,
'past_date_behavior' => 'private',
'future_date_behavior' => 'hidden',
'template' => 'updated-template',
'layout' => 'updated-layout',
'default_publish_state' => false,
'sort_direction' => 'desc',
])
->assertOk();
$this->assertCount(1, Collection::all());
$updated = Collection::all()->first();
$this->assertEquals('Updated title', $updated->title());
$this->assertTrue($updated->dated());
$this->assertEquals('private', $collection->pastDateBehavior());
$this->assertEquals('hidden', $collection->futureDateBehavior());
$this->assertEquals('updated-template', $updated->template());
$this->assertEquals('updated-layout', $updated->layout());
$this->assertFalse($updated->defaultPublishState());
$this->assertEquals('desc', $updated->sortDirection());
// structure
}
#[Test]
public function setting_links_to_true_will_create_a_blueprint_if_it_doesnt_already_exist()
{
BlueprintRepository::swap(new FakeBlueprintRepository(BlueprintRepository::getFacadeRoot()));
$collection = tap(Collection::make('test'))->save();
Blueprint::make('not_link')->setNamespace('collections.test')->save();
$this->assertCount(1, Blueprint::in('collections.test'));
$this
->actingAs($this->userWithPermission())
->update($collection, ['links' => true])
->assertOk();
$this->assertCount(2, Blueprint::in('collections.test'));
}
#[Test]
public function setting_links_to_true_will_do_nothing_if_an_existing_link_blueprint_already_exists()
{
BlueprintRepository::swap(new FakeBlueprintRepository(BlueprintRepository::getFacadeRoot()));
$collection = tap(Collection::make('test'))->save();
Blueprint::make('link')->setNamespace('collections.test')->setContents(['title' => 'Existing'])->save();
$this->assertCount(1, Blueprint::in('collections.test'));
$this
->actingAs($this->userWithPermission())
->update($collection, ['links' => true])
->assertOk();
$this->assertCount(1, Blueprint::in('collections.test'));
$this->assertEquals('Existing', Blueprint::find('collections.test.link')->title());
}
#[Test]
public function setting_links_to_false_will_delete_the_blueprint_if_exists()
{
BlueprintRepository::swap(new FakeBlueprintRepository(BlueprintRepository::getFacadeRoot()));
$collection = tap(Collection::make('test'))->save();
Blueprint::make('link')->setNamespace('collections.test')->save();
$this->assertCount(1, Blueprint::in('collections.test'));
$this
->actingAs($this->userWithPermission())
->update($collection, ['links' => false])
->assertOk();
$this->assertCount(0, Blueprint::in('collections.test'));
}
#[Test]
public function settings_links_to_true_will_also_create_the_default_blueprint_if_none_exist()
{
// this is so that you aren't left in awkward situation where there's only a links blueprint.
BlueprintRepository::swap(new FakeBlueprintRepository(BlueprintRepository::getFacadeRoot()));
$collection = tap(Collection::make('test'))->save();
$this->assertCount(0, Blueprint::in('collections.test'));
$this
->actingAs($this->userWithPermission())
->update($collection, ['links' => true])
->assertOk();
$this->assertCount(2, $blueprints = Blueprint::in('collections.test'));
$this->assertEquals(['test', 'link'], $blueprints->map->handle()->values()->all());
}
#[Test]
public function it_updates_collection_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/'],
]);
$collection = Collection::make('test')->sites(['en', 'fr'])->save();
$this
->actingAs($this->userWithPermission())
->update($collection, [
'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'],
],
'origin_behavior' => 'root',
'propagate' => true,
'structured' => false,
'require_slugs' => true,
'preview_targets' => [],
])
->assertOk();
$updated = Collection::findByHandle('test');
$this->assertEquals(['en', 'de'], $updated->sites()->all());
$this->assertEquals('root', $updated->originBehavior());
$this->assertTrue($updated->propagate());
}
#[Test]
public function it_updates_collection_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/'],
]);
$collection = Collection::make('test')->sites(['en', 'fr'])->save();
$this
->actingAs($this->userWithPermission())
->update($collection, [
'sites' => ['en', 'de'],
'origin_behavior' => 'root',
'propagate' => false,
'structured' => false,
'require_slugs' => true,
'preview_targets' => [],
])
->assertOk();
$this->assertEquals(['en', 'de'], Collection::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 collections']]);
return tap(User::make()->assignRole('test'))->save();
}
private function update($collection, $params = [])
{
$params = array_merge([
'title' => 'Updated title',
'dated' => false,
'past_date_behavior' => 'public',
'future_date_behavior' => 'public',
'template' => 'updated-template',
'layout' => 'updated-layout',
'default_publish_state' => true,
'ampable' => false,
], $params);
return $this->patch(cp_route('collections.update', $collection->handle()), $params);
}
}