-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathEntityPermissionsTest.php
More file actions
715 lines (546 loc) · 25.8 KB
/
EntityPermissionsTest.php
File metadata and controls
715 lines (546 loc) · 25.8 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<?php
namespace Tests\Permissions;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Users\Models\Role;
use BookStack\Users\Models\User;
use Exception;
use Illuminate\Support\Str;
use Tests\TestCase;
class EntityPermissionsTest extends TestCase
{
protected User $user;
protected User $viewer;
protected function setUp(): void
{
parent::setUp();
$this->user = $this->users->editor();
$this->viewer = $this->users->viewer();
}
protected function setRestrictionsForTestRoles(Entity $entity, array $actions = [])
{
$roles = [
$this->user->roles->first(),
$this->viewer->roles->first(),
];
$this->permissions->setEntityPermissions($entity, $actions, $roles);
}
public function test_bookshelf_view_restriction()
{
$shelf = $this->entities->shelf();
$this->actingAs($this->user)
->get($shelf->getUrl())
->assertStatus(200);
$this->setRestrictionsForTestRoles($shelf, []);
$this->followingRedirects()->get($shelf->getUrl())
->assertSee('Shelf not found');
$this->setRestrictionsForTestRoles($shelf, ['view']);
$this->get($shelf->getUrl())
->assertSee($shelf->name);
}
public function test_bookshelf_update_restriction()
{
$shelf = $this->entities->shelf();
$this->actingAs($this->user)
->get($shelf->getUrl('/edit'))
->assertSee('Edit Shelf');
$this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
$resp = $this->get($shelf->getUrl('/edit'))
->assertRedirect('/');
$this->followRedirects($resp)->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
$this->get($shelf->getUrl('/edit'))
->assertOk();
}
public function test_bookshelf_delete_restriction()
{
$shelf = $this->entities->shelf();
$this->actingAs($this->user)
->get($shelf->getUrl('/delete'))
->assertSee('Delete Shelf');
$this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
$this->get($shelf->getUrl('/delete'))->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
$this->get($shelf->getUrl('/delete'))
->assertOk()
->assertSee('Delete Shelf');
}
public function test_book_view_restriction()
{
$book = $this->entities->book();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
$bookUrl = $book->getUrl();
$this->actingAs($this->user)
->get($bookUrl)
->assertOk();
$this->setRestrictionsForTestRoles($book, []);
$this->followingRedirects()->get($bookUrl)
->assertSee('Book not found');
$this->followingRedirects()->get($bookPage->getUrl())
->assertSee('Page not found');
$this->followingRedirects()->get($bookChapter->getUrl())
->assertSee('Chapter not found');
$this->setRestrictionsForTestRoles($book, ['view']);
$this->get($bookUrl)
->assertSee($book->name);
$this->get($bookPage->getUrl())
->assertSee($bookPage->name);
$this->get($bookChapter->getUrl())
->assertSee($bookChapter->name);
}
public function test_book_create_restriction()
{
$book = $this->entities->book();
$bookUrl = $book->getUrl();
$resp = $this->actingAs($this->viewer)->get($bookUrl);
$this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
->assertElementNotContains('.actions', 'New Chapter');
$resp = $this->actingAs($this->user)->get($bookUrl);
$this->withHtml($resp)->assertElementContains('.actions', 'New Page')
->assertElementContains('.actions', 'New Chapter');
$this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']);
$this->get($bookUrl . '/create-chapter')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookUrl . '/create-page')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$resp = $this->get($bookUrl);
$this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
->assertElementNotContains('.actions', 'New Chapter');
$this->setRestrictionsForTestRoles($book, ['view', 'create']);
$resp = $this->post($book->getUrl('/create-chapter'), [
'name' => 'test chapter',
'description' => 'desc',
]);
$resp->assertRedirect($book->getUrl('/chapter/test-chapter'));
$this->get($book->getUrl('/create-page'));
/** @var Page $page */
$page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
$resp = $this->post($page->getUrl(), [
'name' => 'test page',
'html' => 'test content',
]);
$resp->assertRedirect($book->getUrl('/page/test-page'));
$resp = $this->get($bookUrl);
$this->withHtml($resp)->assertElementContains('.actions', 'New Page')
->assertElementContains('.actions', 'New Chapter');
}
public function test_book_update_restriction()
{
$book = $this->entities->book();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
$bookUrl = $book->getUrl();
$this->actingAs($this->user)
->get($bookUrl . '/edit')
->assertSee('Edit Book');
$this->setRestrictionsForTestRoles($book, ['view', 'delete']);
$this->get($bookUrl . '/edit')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookPage->getUrl() . '/edit')->assertRedirect($bookPage->getUrl());
$this->get('/')->assertSee('You do not have permission');
$this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($book, ['view', 'update']);
$this->get($bookUrl . '/edit')->assertOk();
$this->get($bookPage->getUrl() . '/edit')->assertOk();
$this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter');
}
public function test_book_delete_restriction()
{
$book = $this->entities->book();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
$bookUrl = $book->getUrl();
$this->actingAs($this->user)->get($bookUrl . '/delete')
->assertSee('Delete Book');
$this->setRestrictionsForTestRoles($book, ['view', 'update']);
$this->get($bookUrl . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookPage->getUrl() . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($book, ['view', 'delete']);
$this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book');
$this->get($bookPage->getUrl('/delete'))->assertOk()->assertSee('Delete Page');
$this->get($bookChapter->getUrl('/delete'))->assertSee('Delete Chapter');
}
public function test_chapter_view_restriction()
{
$chapter = $this->entities->chapter();
$chapterPage = $chapter->pages->first();
$chapterUrl = $chapter->getUrl();
$this->actingAs($this->user)->get($chapterUrl)->assertOk();
$this->setRestrictionsForTestRoles($chapter, []);
$this->followingRedirects()->get($chapterUrl)->assertSee('Chapter not found');
$this->followingRedirects()->get($chapterPage->getUrl())->assertSee('Page not found');
$this->setRestrictionsForTestRoles($chapter, ['view']);
$this->get($chapterUrl)->assertSee($chapter->name);
$this->get($chapterPage->getUrl())->assertSee($chapterPage->name);
}
public function test_chapter_create_restriction()
{
$chapter = $this->entities->chapter();
$chapterUrl = $chapter->getUrl();
$resp = $this->actingAs($this->user)->get($chapterUrl);
$this->withHtml($resp)->assertElementContains('.actions', 'New Page');
$this->setRestrictionsForTestRoles($chapter, ['view', 'delete', 'update']);
$this->get($chapterUrl . '/create-page')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->withHtml($this->get($chapterUrl))->assertElementNotContains('.actions', 'New Page');
$this->setRestrictionsForTestRoles($chapter, ['view', 'create']);
$this->get($chapter->getUrl('/create-page'));
/** @var Page $page */
$page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
$resp = $this->post($page->getUrl(), [
'name' => 'test page',
'html' => 'test content',
]);
$resp->assertRedirect($chapter->book->getUrl('/page/test-page'));
$this->withHtml($this->get($chapterUrl))->assertElementContains('.actions', 'New Page');
}
public function test_chapter_update_restriction()
{
$chapter = $this->entities->chapter();
$chapterPage = $chapter->pages->first();
$chapterUrl = $chapter->getUrl();
$this->actingAs($this->user)->get($chapterUrl . '/edit')
->assertSee('Edit Chapter');
$this->setRestrictionsForTestRoles($chapter, ['view', 'delete']);
$this->get($chapterUrl . '/edit')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($chapterPage->getUrl() . '/edit')->assertRedirect($chapterPage->getUrl());
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($chapter, ['view', 'update']);
$this->get($chapterUrl . '/edit')->assertOk()->assertSee('Edit Chapter');
$this->get($chapterPage->getUrl() . '/edit')->assertOk();
}
public function test_chapter_delete_restriction()
{
$chapter = $this->entities->chapter();
$chapterPage = $chapter->pages->first();
$chapterUrl = $chapter->getUrl();
$this->actingAs($this->user)
->get($chapterUrl . '/delete')
->assertSee('Delete Chapter');
$this->setRestrictionsForTestRoles($chapter, ['view', 'update']);
$this->get($chapterUrl . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($chapterPage->getUrl() . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($chapter, ['view', 'delete']);
$this->get($chapterUrl . '/delete')->assertOk()->assertSee('Delete Chapter');
$this->get($chapterPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page');
}
public function test_page_view_restriction()
{
$page = $this->entities->page();
$pageUrl = $page->getUrl();
$this->actingAs($this->user)->get($pageUrl)->assertOk();
$this->setRestrictionsForTestRoles($page, ['update', 'delete']);
$this->get($pageUrl)->assertSee('Page not found');
$this->setRestrictionsForTestRoles($page, ['view']);
$this->get($pageUrl)->assertSee($page->name);
}
public function test_page_update_restriction()
{
$page = $this->entities->page();
$pageUrl = $page->getUrl();
$resp = $this->actingAs($this->user)
->get($pageUrl . '/edit');
$this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]');
$this->setRestrictionsForTestRoles($page, ['view', 'delete']);
$this->get($pageUrl . '/edit')->assertRedirect($pageUrl);
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($page, ['view', 'update']);
$resp = $this->get($pageUrl . '/edit')
->assertOk();
$this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]');
}
public function test_page_delete_restriction()
{
$page = $this->entities->page();
$pageUrl = $page->getUrl();
$this->actingAs($this->user)
->get($pageUrl . '/delete')
->assertSee('Delete Page');
$this->setRestrictionsForTestRoles($page, ['view', 'update']);
$this->get($pageUrl . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($page, ['view', 'delete']);
$this->get($pageUrl . '/delete')->assertOk()->assertSee('Delete Page');
}
protected function entityRestrictionFormTest(string $model, string $title, string $permission, string $roleId)
{
/** @var Entity $modelInstance */
$modelInstance = $model::query()->first();
$this->asAdmin()->get($modelInstance->getUrl('/permissions'))
->assertSee($title);
$this->put($modelInstance->getUrl('/permissions'), [
'permissions' => [
$roleId => [
$permission => 'true',
],
],
]);
$this->assertDatabaseHas('entity_permissions', [
'entity_id' => $modelInstance->id,
'entity_type' => $modelInstance->getMorphClass(),
'role_id' => $roleId,
$permission => true,
]);
}
public function test_bookshelf_restriction_form()
{
$this->entityRestrictionFormTest(Bookshelf::class, 'Shelf Permissions', 'view', '2');
}
public function test_book_restriction_form()
{
$this->entityRestrictionFormTest(Book::class, 'Book Permissions', 'view', '2');
}
public function test_chapter_restriction_form()
{
$this->entityRestrictionFormTest(Chapter::class, 'Chapter Permissions', 'update', '2');
}
public function test_page_restriction_form()
{
$this->entityRestrictionFormTest(Page::class, 'Page Permissions', 'delete', '2');
}
public function test_shelf_create_permission_visible_with_notice()
{
$shelf = $this->entities->shelf();
$resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
$html = $this->withHtml($resp);
$html->assertElementExists('input[name$="[create]"]');
$resp->assertSee('Shelf create permissions are only used for copying permissions to child books using the action below.');
}
public function test_restricted_pages_not_visible_in_book_navigation_on_pages()
{
$chapter = $this->entities->chapter();
$page = $chapter->pages->first();
$page2 = $chapter->pages[2];
$this->setRestrictionsForTestRoles($page, []);
$resp = $this->actingAs($this->user)->get($page2->getUrl());
$this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name);
}
public function test_restricted_pages_not_visible_in_book_navigation_on_chapters()
{
$chapter = $this->entities->chapter();
$page = $chapter->pages->first();
$this->setRestrictionsForTestRoles($page, []);
$resp = $this->actingAs($this->user)->get($chapter->getUrl());
$this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name);
}
public function test_restricted_pages_not_visible_on_chapter_pages()
{
$chapter = $this->entities->chapter();
$page = $chapter->pages->first();
$this->setRestrictionsForTestRoles($page, []);
$this->actingAs($this->user)
->get($chapter->getUrl())
->assertDontSee($page->name);
}
public function test_restricted_chapter_pages_not_visible_on_book_page()
{
$chapter = $this->entities->chapter();
$this->actingAs($this->user)
->get($chapter->book->getUrl())
->assertSee($chapter->pages->first()->name);
foreach ($chapter->pages as $page) {
$this->setRestrictionsForTestRoles($page, []);
}
$this->actingAs($this->user)
->get($chapter->book->getUrl())
->assertDontSee($chapter->pages->first()->name);
}
public function test_bookshelf_update_restriction_override()
{
$shelf = $this->entities->shelf();
$this->actingAs($this->viewer)
->get($shelf->getUrl('/edit'))
->assertDontSee('Edit Book');
$this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
$this->get($shelf->getUrl('/edit'))->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
$this->get($shelf->getUrl('/edit'))->assertOk();
}
public function test_bookshelf_delete_restriction_override()
{
$shelf = $this->entities->shelf();
$this->actingAs($this->viewer)
->get($shelf->getUrl('/delete'))
->assertDontSee('Delete Book');
$this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
$this->get($shelf->getUrl('/delete'))->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
$this->get($shelf->getUrl('/delete'))->assertOk()->assertSee('Delete Shelf');
}
public function test_book_create_restriction_override()
{
$book = $this->entities->book();
$bookUrl = $book->getUrl();
$resp = $this->actingAs($this->viewer)->get($bookUrl);
$this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
->assertElementNotContains('.actions', 'New Chapter');
$this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']);
$this->get($bookUrl . '/create-chapter')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookUrl . '/create-page')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$resp = $this->get($bookUrl);
$this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
->assertElementNotContains('.actions', 'New Chapter');
$this->setRestrictionsForTestRoles($book, ['view', 'create']);
$resp = $this->post($book->getUrl('/create-chapter'), [
'name' => 'test chapter',
'description' => 'test desc',
]);
$resp->assertRedirect($book->getUrl('/chapter/test-chapter'));
$this->get($book->getUrl('/create-page'));
/** @var Page $page */
$page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
$resp = $this->post($page->getUrl(), [
'name' => 'test page',
'html' => 'test desc',
]);
$resp->assertRedirect($book->getUrl('/page/test-page'));
$resp = $this->get($bookUrl);
$this->withHtml($resp)->assertElementContains('.actions', 'New Page')
->assertElementContains('.actions', 'New Chapter');
}
public function test_book_update_restriction_override()
{
$book = $this->entities->book();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
$bookUrl = $book->getUrl();
$this->actingAs($this->viewer)->get($bookUrl . '/edit')
->assertDontSee('Edit Book');
$this->setRestrictionsForTestRoles($book, ['view', 'delete']);
$this->get($bookUrl . '/edit')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookPage->getUrl() . '/edit')->assertRedirect($bookPage->getUrl());
$this->get('/')->assertSee('You do not have permission');
$this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($book, ['view', 'update']);
$this->get($bookUrl . '/edit')->assertOk();
$this->get($bookPage->getUrl() . '/edit')->assertOk();
$this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter');
}
public function test_book_delete_restriction_override()
{
$book = $this->entities->book();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
$bookUrl = $book->getUrl();
$this->actingAs($this->viewer)
->get($bookUrl . '/delete')
->assertDontSee('Delete Book');
$this->setRestrictionsForTestRoles($book, ['view', 'update']);
$this->get($bookUrl . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookPage->getUrl() . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
$this->setRestrictionsForTestRoles($book, ['view', 'delete']);
$this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book');
$this->get($bookPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page');
$this->get($bookChapter->getUrl() . '/delete')->assertSee('Delete Chapter');
}
public function test_page_visible_if_has_permissions_when_book_not_visible()
{
$book = $this->entities->book();
$bookChapter = $book->chapters->first();
$bookPage = $bookChapter->pages->first();
foreach ([$book, $bookChapter, $bookPage] as $entity) {
$entity->name = Str::random(24);
$entity->save();
}
$this->setRestrictionsForTestRoles($book, []);
$this->setRestrictionsForTestRoles($bookPage, ['view']);
$this->actingAs($this->viewer);
$resp = $this->get($bookPage->getUrl());
$resp->assertOk();
$resp->assertSee($bookPage->name);
$resp->assertDontSee(substr($book->name, 0, 15));
$resp->assertDontSee(substr($bookChapter->name, 0, 15));
}
public function test_book_sort_view_permission()
{
/** @var Book $firstBook */
$firstBook = Book::query()->first();
/** @var Book $secondBook */
$secondBook = Book::query()->find(2);
$this->setRestrictionsForTestRoles($firstBook, ['view', 'update']);
$this->setRestrictionsForTestRoles($secondBook, ['view']);
// Test sort page visibility
$this->actingAs($this->user)->get($secondBook->getUrl('/sort'))->assertRedirect('/');
$this->get('/')->assertSee('You do not have permission');
// Check sort page on first book
$this->actingAs($this->user)->get($firstBook->getUrl('/sort'));
}
public function test_can_create_page_if_chapter_has_permissions_when_book_not_visible()
{
$book = $this->entities->book();
$this->setRestrictionsForTestRoles($book, []);
$bookChapter = $book->chapters->first();
$this->setRestrictionsForTestRoles($bookChapter, ['view']);
$this->actingAs($this->user)->get($bookChapter->getUrl())
->assertDontSee('New Page');
$this->setRestrictionsForTestRoles($bookChapter, ['view', 'create']);
$this->get($bookChapter->getUrl('/create-page'));
/** @var Page $page */
$page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
$resp = $this->post($page->getUrl(), [
'name' => 'test page',
'html' => 'test content',
]);
$resp->assertRedirect($book->getUrl('/page/test-page'));
}
public function test_access_to_item_prevented_if_inheritance_active_but_permission_prevented_via_role()
{
$user = $this->users->viewer();
$viewerRole = $user->roles->first();
$chapter = $this->entities->chapter();
$book = $chapter->book;
$this->permissions->setEntityPermissions($book, ['update'], [$viewerRole], false);
$this->permissions->setEntityPermissions($chapter, [], [$viewerRole], true);
$this->assertFalse(userCan('chapter-update', $chapter));
}
public function test_access_to_item_allowed_if_inheritance_active_and_permission_prevented_via_role_but_allowed_via_parent()
{
$user = $this->users->viewer();
$viewerRole = $user->roles->first();
$editorRole = Role::getRole('Editor');
$user->attachRole($editorRole);
$chapter = $this->entities->chapter();
$book = $chapter->book;
$this->permissions->setEntityPermissions($book, ['update'], [$editorRole], false);
$this->permissions->setEntityPermissions($chapter, [], [$viewerRole], true);
$this->actingAs($user);
$this->assertTrue(userCan('chapter-update', $chapter));
}
public function test_book_permissions_can_be_generated_without_error_if_child_chapter_is_in_recycle_bin()
{
$book = $this->entities->bookHasChaptersAndPages();
/** @var Chapter $chapter */
$chapter = $book->chapters()->first();
$this->asAdmin()->delete($chapter->getUrl());
$error = null;
try {
$this->permissions->setEntityPermissions($book, ['view'], []);
} catch (Exception $e) {
$error = $e;
}
$this->assertNull($error);
}
}