-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathSortRuleTest.php
More file actions
271 lines (225 loc) · 9.38 KB
/
SortRuleTest.php
File metadata and controls
271 lines (225 loc) · 9.38 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
namespace Tests\Sorting;
use BookStack\Activity\ActivityType;
use BookStack\Entities\Models\Book;
use BookStack\Sorting\SortRule;
use BookStack\Sorting\SortRuleOperation;
use Tests\Api\TestsApi;
use Tests\TestCase;
class SortRuleTest extends TestCase
{
use TestsApi;
public function test_manage_settings_permission_required()
{
$rule = SortRule::factory()->create();
$user = $this->users->viewer();
$this->actingAs($user);
$actions = [
['GET', '/settings/sorting'],
['POST', '/settings/sorting/rules'],
['GET', "/settings/sorting/rules/{$rule->id}"],
['PUT', "/settings/sorting/rules/{$rule->id}"],
['DELETE', "/settings/sorting/rules/{$rule->id}"],
];
foreach ($actions as [$method, $path]) {
$resp = $this->call($method, $path);
$this->assertPermissionError($resp);
}
$this->permissions->grantUserRolePermissions($user, ['settings-manage']);
foreach ($actions as [$method, $path]) {
$resp = $this->call($method, $path);
$this->assertNotPermissionError($resp);
}
}
public function test_create_flow()
{
$resp = $this->asAdmin()->get('/settings/sorting');
$this->withHtml($resp)->assertLinkExists(url('/settings/sorting/rules/new'));
$resp = $this->get('/settings/sorting/rules/new');
$this->withHtml($resp)->assertElementExists('form[action$="/settings/sorting/rules"] input[name="name"]');
$resp->assertSeeText('Name - Alphabetical (Asc)');
$details = ['name' => 'My new sort', 'sequence' => 'name_asc'];
$resp = $this->post('/settings/sorting/rules', $details);
$resp->assertRedirect('/settings/sorting');
$this->assertActivityExists(ActivityType::SORT_RULE_CREATE);
$this->assertDatabaseHas('sort_rules', $details);
}
public function test_listing_in_settings()
{
$rule = SortRule::factory()->create(['name' => 'My super sort rule', 'sequence' => 'name_asc']);
$books = Book::query()->limit(5)->get();
foreach ($books as $book) {
$book->sort_rule_id = $rule->id;
$book->save();
}
$resp = $this->asAdmin()->get('/settings/sorting');
$resp->assertSeeText('My super sort rule');
$resp->assertSeeText('Name - Alphabetical (Asc)');
$this->withHtml($resp)->assertElementContains('.item-list-row [title="Assigned to 5 Books"]', '5');
}
public function test_update_flow()
{
$rule = SortRule::factory()->create(['name' => 'My sort rule to update', 'sequence' => 'name_asc']);
$resp = $this->asAdmin()->get("/settings/sorting/rules/{$rule->id}");
$respHtml = $this->withHtml($resp);
$respHtml->assertElementContains('.configured-option-list', 'Name - Alphabetical (Asc)');
$respHtml->assertElementNotContains('.available-option-list', 'Name - Alphabetical (Asc)');
$updateData = ['name' => 'My updated sort', 'sequence' => 'name_desc,chapters_last'];
$resp = $this->put("/settings/sorting/rules/{$rule->id}", $updateData);
$resp->assertRedirect('/settings/sorting');
$this->assertActivityExists(ActivityType::SORT_RULE_UPDATE);
$this->assertDatabaseHas('sort_rules', $updateData);
}
public function test_update_triggers_resort_on_assigned_books()
{
$book = $this->entities->bookHasChaptersAndPages();
$chapter = $book->chapters()->first();
$rule = SortRule::factory()->create(['name' => 'My sort rule to update', 'sequence' => 'name_asc']);
$book->sort_rule_id = $rule->id;
$book->save();
$chapter->priority = 10000;
$chapter->save();
$resp = $this->asAdmin()->put("/settings/sorting/rules/{$rule->id}", ['name' => $rule->name, 'sequence' => 'chapters_last']);
$resp->assertRedirect('/settings/sorting');
$chapter->refresh();
$this->assertNotEquals(10000, $chapter->priority);
}
public function test_delete_flow()
{
$rule = SortRule::factory()->create();
$resp = $this->asAdmin()->get("/settings/sorting/rules/{$rule->id}");
$resp->assertSeeText('Delete Sort Rule');
$resp = $this->delete("settings/sorting/rules/{$rule->id}");
$resp->assertRedirect('/settings/sorting');
$this->assertActivityExists(ActivityType::SORT_RULE_DELETE);
$this->assertDatabaseMissing('sort_rules', ['id' => $rule->id]);
}
public function test_delete_requires_confirmation_if_books_assigned()
{
$rule = SortRule::factory()->create();
$books = Book::query()->limit(5)->get();
foreach ($books as $book) {
$book->sort_rule_id = $rule->id;
$book->save();
}
$resp = $this->asAdmin()->get("/settings/sorting/rules/{$rule->id}");
$resp->assertSeeText('Delete Sort Rule');
$resp = $this->delete("settings/sorting/rules/{$rule->id}");
$resp->assertRedirect("/settings/sorting/rules/{$rule->id}#delete");
$resp = $this->followRedirects($resp);
$resp->assertSeeText('This sort rule is currently used on 5 book(s). Are you sure you want to delete this?');
$this->assertDatabaseHas('sort_rules', ['id' => $rule->id]);
$resp = $this->delete("settings/sorting/rules/{$rule->id}", ['confirm' => 'true']);
$resp->assertRedirect('/settings/sorting');
$this->assertDatabaseMissing('sort_rules', ['id' => $rule->id]);
$this->assertDatabaseMissing('entity_container_data', ['sort_rule_id' => $rule->id]);
}
public function test_page_create_triggers_book_sort()
{
$book = $this->entities->bookHasChaptersAndPages();
$rule = SortRule::factory()->create(['sequence' => 'name_asc,chapters_first']);
$book->sort_rule_id = $rule->id;
$book->save();
$resp = $this->actingAsApiEditor()->post("/api/pages", [
'book_id' => $book->id,
'name' => '1111 page',
'markdown' => 'Hi'
]);
$resp->assertOk();
$this->assertDatabaseHasEntityData('page', [
'book_id' => $book->id,
'name' => '1111 page',
'priority' => $book->chapters()->count() + 1,
]);
}
public function test_auto_book_sort_does_not_touch_timestamps()
{
$book = $this->entities->bookHasChaptersAndPages();
$rule = SortRule::factory()->create(['sequence' => 'name_asc,chapters_first']);
$book->sort_rule_id = $rule->id;
$book->save();
$page = $book->pages()->first();
$chapter = $book->chapters()->first();
$resp = $this->actingAsApiEditor()->put("/api/pages/{$page->id}", [
'name' => '1111 page',
]);
$resp->assertOk();
$oldTime = $chapter->updated_at->unix();
$oldPriority = $chapter->priority;
$chapter->refresh();
$this->assertEquals($oldTime, $chapter->updated_at->unix());
$this->assertNotEquals($oldPriority, $chapter->priority);
}
public function test_name_alphabetical_ordering()
{
$book = Book::factory()->create();
$rule = SortRule::factory()->create(['sequence' => 'name_asc']);
$book->sort_rule_id = $rule->id;
$book->save();
$this->permissions->regenerateForEntity($book);
$namesToAdd = [
"Beans",
"bread",
"Éclaire",
"egg",
"É😀ire",
"É🫠ire",
"Milk",
"pizza",
"Tomato",
];
$reverseNamesToAdd = array_reverse($namesToAdd);
foreach ($reverseNamesToAdd as $name) {
$this->actingAsApiEditor()->post("/api/pages", [
'book_id' => $book->id,
'name' => $name,
'markdown' => 'Hello'
]);
}
foreach ($namesToAdd as $index => $name) {
$this->assertDatabaseHasEntityData('page', [
'book_id' => $book->id,
'name' => $name,
'priority' => $index + 1,
]);
}
}
public function test_name_numeric_ordering()
{
$book = Book::factory()->create();
$rule = SortRule::factory()->create(['sequence' => 'name_numeric_asc']);
$book->sort_rule_id = $rule->id;
$book->save();
$this->permissions->regenerateForEntity($book);
$namesToAdd = [
"1 - Pizza",
"2.0 - Tomato",
"2.5 - Beans",
"10 - Bread",
"20 - Milk",
];
$reverseNamesToAdd = array_reverse($namesToAdd);
foreach ($reverseNamesToAdd as $name) {
$this->actingAsApiEditor()->post("/api/pages", [
'book_id' => $book->id,
'name' => $name,
'markdown' => 'Hello'
]);
}
foreach ($namesToAdd as $index => $name) {
$this->assertDatabaseHasEntityData('page', [
'book_id' => $book->id,
'name' => $name,
'priority' => $index + 1,
]);
}
}
public function test_each_sort_rule_operation_has_a_comparison_function()
{
$operations = SortRuleOperation::cases();
foreach ($operations as $operation) {
$comparisonFunc = $operation->getSortFunction();
$this->assertIsCallable($comparisonFunc);
}
}
}