-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathBookSortTest.php
More file actions
316 lines (264 loc) · 12.3 KB
/
BookSortTest.php
File metadata and controls
316 lines (264 loc) · 12.3 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
namespace Tests\Sorting;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Sorting\SortRule;
use Tests\TestCase;
class BookSortTest extends TestCase
{
public function test_book_sort_page_shows()
{
$bookToSort = $this->entities->book();
$resp = $this->asAdmin()->get($bookToSort->getUrl());
$this->withHtml($resp)->assertElementExists('a[href="' . $bookToSort->getUrl('/sort') . '"]');
$resp = $this->get($bookToSort->getUrl('/sort'));
$resp->assertStatus(200);
$resp->assertSee($bookToSort->name);
}
public function test_drafts_do_not_show_up()
{
$this->asAdmin();
$pageRepo = app(PageRepo::class);
$book = $this->entities->book();
$draft = $pageRepo->getNewDraftPage($book);
$resp = $this->get($book->getUrl());
$resp->assertSee($draft->name);
$resp = $this->get($book->getUrl('/sort'));
$resp->assertDontSee($draft->name);
}
public function test_book_sort()
{
$oldBook = $this->entities->book();
$chapterToMove = $this->entities->newChapter(['name' => 'chapter to move'], $oldBook);
$newBook = $this->entities->newBook(['name' => 'New sort book']);
$pagesToMove = Page::query()->take(5)->get();
// Create request data
$reqData = [
[
'id' => $chapterToMove->id,
'sort' => 0,
'parentChapter' => false,
'type' => 'chapter',
'book' => $newBook->id,
],
];
foreach ($pagesToMove as $index => $page) {
$reqData[] = [
'id' => $page->id,
'sort' => $index,
'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
'type' => 'page',
'book' => $newBook->id,
];
}
$sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
$sortResp->assertRedirect($newBook->getUrl());
$sortResp->assertStatus(302);
$this->assertDatabaseHasEntityData('chapter', [
'id' => $chapterToMove->id,
'book_id' => $newBook->id,
'priority' => 0,
]);
$this->assertTrue($newBook->chapters()->count() === 1);
$this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
$checkPage = $pagesToMove[1];
$checkResp = $this->get($checkPage->refresh()->getUrl());
$checkResp->assertSee($newBook->name);
}
public function test_book_sort_makes_no_changes_if_new_chapter_does_not_align_with_new_book()
{
$page = $this->entities->pageWithinChapter();
$otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
$sortData = [
'id' => $page->id,
'sort' => 0,
'parentChapter' => $otherChapter->id,
'type' => 'page',
'book' => $page->book_id,
];
$this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
]);
}
public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_chapter()
{
$page = $this->entities->pageWithinChapter();
/** @var Chapter $otherChapter */
$otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
$this->permissions->setEntityPermissions($otherChapter);
$sortData = [
'id' => $page->id,
'sort' => 0,
'parentChapter' => $otherChapter->id,
'type' => 'page',
'book' => $otherChapter->book_id,
];
$this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
]);
}
public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_book()
{
$page = $this->entities->pageWithinChapter();
/** @var Chapter $otherChapter */
$otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
$editor = $this->users->editor();
$this->permissions->setEntityPermissions($otherChapter->book, ['update', 'delete'], [$editor->roles()->first()]);
$sortData = [
'id' => $page->id,
'sort' => 0,
'parentChapter' => $otherChapter->id,
'type' => 'page',
'book' => $otherChapter->book_id,
];
$this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
]);
}
public function test_book_sort_makes_no_changes_if_no_update_or_create_permissions_on_new_chapter()
{
$page = $this->entities->pageWithinChapter();
/** @var Chapter $otherChapter */
$otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
$editor = $this->users->editor();
$this->permissions->setEntityPermissions($otherChapter, ['view', 'delete'], [$editor->roles()->first()]);
$sortData = [
'id' => $page->id,
'sort' => 0,
'parentChapter' => $otherChapter->id,
'type' => 'page',
'book' => $otherChapter->book_id,
];
$this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
]);
}
public function test_book_sort_makes_no_changes_if_no_update_permissions_on_moved_item()
{
$page = $this->entities->pageWithinChapter();
/** @var Chapter $otherChapter */
$otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
$editor = $this->users->editor();
$this->permissions->setEntityPermissions($page, ['view', 'delete'], [$editor->roles()->first()]);
$sortData = [
'id' => $page->id,
'sort' => 0,
'parentChapter' => $otherChapter->id,
'type' => 'page',
'book' => $otherChapter->book_id,
];
$this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
]);
}
public function test_book_sort_makes_no_changes_if_no_delete_permissions_on_moved_item()
{
$page = $this->entities->pageWithinChapter();
/** @var Chapter $otherChapter */
$otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
$editor = $this->users->editor();
$this->permissions->setEntityPermissions($page, ['view', 'update'], [$editor->roles()->first()]);
$sortData = [
'id' => $page->id,
'sort' => 0,
'parentChapter' => $otherChapter->id,
'type' => 'page',
'book' => $otherChapter->book_id,
];
$this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
]);
}
public function test_book_sort_does_not_change_timestamps_on_just_order_changes()
{
$book = $this->entities->bookHasChaptersAndPages();
$chapter = $book->chapters()->first();
Chapter::query()->where('id', '=', $chapter->id)->update([
'priority' => 10001,
'updated_at' => \Carbon\Carbon::now()->subYear(5),
]);
$chapter->refresh();
$oldUpdatedAt = $chapter->updated_at->unix();
$sortData = [
'id' => $chapter->id,
'sort' => 0,
'parentChapter' => false,
'type' => 'chapter',
'book' => $book->id,
];
$this->asEditor()->put($book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
$chapter->refresh();
$this->assertNotEquals(10001, $chapter->priority);
$this->assertEquals($oldUpdatedAt, $chapter->updated_at->unix());
}
public function test_book_sort_item_returns_book_content()
{
$bookToSort = $this->entities->book();
$firstPage = $bookToSort->pages[0];
$firstChapter = $bookToSort->chapters[0];
$resp = $this->asAdmin()->get($bookToSort->getUrl('/sort-item'));
// Ensure book details are returned
$resp->assertSee($bookToSort->name);
$resp->assertSee($firstPage->name);
$resp->assertSee($firstChapter->name);
}
public function test_book_sort_item_shows_auto_sort_status()
{
$sort = SortRule::factory()->create(['name' => 'My sort']);
$book = $this->entities->book();
$resp = $this->asAdmin()->get($book->getUrl('/sort-item'));
$this->withHtml($resp)->assertElementNotExists("span[title='Auto Sort Active: My sort']");
$book->sort_rule_id = $sort->id;
$book->save();
$resp = $this->asAdmin()->get($book->getUrl('/sort-item'));
$this->withHtml($resp)->assertElementExists("span[title='Auto Sort Active: My sort']");
}
public function test_auto_sort_options_shown_on_sort_page()
{
$sort = SortRule::factory()->create();
$book = $this->entities->book();
$resp = $this->asAdmin()->get($book->getUrl('/sort'));
$this->withHtml($resp)->assertElementExists('select[name="auto-sort"] option[value="' . $sort->id . '"]');
}
public function test_auto_sort_option_submit_saves_to_book()
{
$sort = SortRule::factory()->create();
$book = $this->entities->book();
$bookPage = $book->pages()->first();
$bookPage->priority = 10000;
$bookPage->save();
$resp = $this->asAdmin()->put($book->getUrl('/sort'), [
'auto-sort' => $sort->id,
]);
$resp->assertRedirect($book->getUrl());
$book->refresh();
$bookPage->refresh();
$this->assertEquals($sort->id, $book->sort_rule_id);
$this->assertNotEquals(10000, $bookPage->priority);
$resp = $this->get($book->getUrl('/sort'));
$this->withHtml($resp)->assertElementExists('select[name="auto-sort"] option[value="' . $sort->id . '"][selected]');
}
public function test_pages_in_book_show_sorted_by_priority()
{
$book = $this->entities->bookHasChaptersAndPages();
$book->chapters()->forceDelete();
/** @var Page[] $pages */
$pages = $book->pages()->whereNull('chapter_id')->take(2)->get();
$book->pages()->whereNotIn('id', $pages->pluck('id'))->delete();
$resp = $this->asEditor()->get($book->getUrl());
$this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[0]->name);
$this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[1]->name);
$pages[0]->forceFill(['priority' => 10])->save();
$pages[1]->forceFill(['priority' => 5])->save();
$resp = $this->asEditor()->get($book->getUrl());
$this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[1]->name);
$this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[0]->name);
}
}