]> BookStack Code Mirror - bookstack/blob - tests/FavouriteTest.php
Fixed failed permission checks due to non-loaded fields
[bookstack] / tests / FavouriteTest.php
1 <?php namespace Tests;
2
3 use BookStack\Actions\Favourite;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Page;
9
10 class FavouriteTest extends TestCase
11 {
12     public function test_page_add_favourite_flow()
13     {
14         $page = Page::query()->first();
15         $editor = $this->getEditor();
16
17         $resp = $this->actingAs($editor)->get($page->getUrl());
18         $this->withHtml($resp)->assertElementContains('button', 'Favourite');
19         $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
20
21         $resp = $this->post('/favourites/add', [
22             'type' => get_class($page),
23             'id'   => $page->id,
24         ]);
25         $resp->assertRedirect($page->getUrl());
26         $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites");
27
28         $this->assertDatabaseHas('favourites', [
29             'user_id'           => $editor->id,
30             'favouritable_type' => $page->getMorphClass(),
31             'favouritable_id'   => $page->id,
32         ]);
33     }
34
35     public function test_page_remove_favourite_flow()
36     {
37         $page = Page::query()->first();
38         $editor = $this->getEditor();
39         Favourite::query()->forceCreate([
40             'user_id'           => $editor->id,
41             'favouritable_id'   => $page->id,
42             'favouritable_type' => $page->getMorphClass(),
43         ]);
44
45         $resp = $this->actingAs($editor)->get($page->getUrl());
46         $this->withHtml($resp)->assertElementContains('button', 'Unfavourite');
47         $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/remove"]');
48
49         $resp = $this->post('/favourites/remove', [
50             'type' => get_class($page),
51             'id'   => $page->id,
52         ]);
53         $resp->assertRedirect($page->getUrl());
54         $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites");
55
56         $this->assertDatabaseMissing('favourites', [
57             'user_id' => $editor->id,
58         ]);
59     }
60
61     public function test_favourite_flow_with_own_permissions()
62     {
63         /** @var Book $book */
64         $book = Book::query()->first();
65         $user = User::factory()->create();
66         $book->owned_by = $user->id;
67         $book->save();
68
69         $this->giveUserPermissions($user, ['book-view-own']);
70
71         $this->actingAs($user)->get($book->getUrl());
72         $resp = $this->post('/favourites/add', [
73             'type' => get_class($book),
74             'id'   => $book->id,
75         ]);
76         $resp->assertRedirect($book->getUrl());
77
78         $this->assertDatabaseHas('favourites', [
79             'user_id'           => $user->id,
80             'favouritable_type' => $book->getMorphClass(),
81             'favouritable_id'   => $book->id,
82         ]);
83     }
84
85     public function test_book_chapter_shelf_pages_contain_favourite_button()
86     {
87         $entities = [
88             Bookshelf::query()->first(),
89             Book::query()->first(),
90             Chapter::query()->first(),
91         ];
92         $this->actingAs($this->getEditor());
93
94         foreach ($entities as $entity) {
95             $resp = $this->get($entity->getUrl());
96             $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
97         }
98     }
99
100     public function test_header_contains_link_to_favourites_page_when_logged_in()
101     {
102         $this->setSettings(['app-public' => 'true']);
103         $resp = $this->get('/');
104         $this->withHtml($resp)->assertElementNotContains('header', 'My Favourites');
105         $resp = $this->actingAs($this->getViewer())->get('/');
106         $this->withHtml($resp)->assertElementContains('header a', 'My Favourites');
107     }
108
109     public function test_favourites_shown_on_homepage()
110     {
111         $editor = $this->getEditor();
112
113         $resp = $this->actingAs($editor)->get('/');
114         $this->withHtml($resp)->assertElementNotExists('#top-favourites');
115
116         /** @var Page $page */
117         $page = Page::query()->first();
118         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
119
120         $resp = $this->get('/');
121         $this->withHtml($resp)->assertElementExists('#top-favourites');
122         $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
123     }
124
125     public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
126     {
127         /** @var Page $page */
128         $page = Page::query()->first();
129         $editor = $this->getEditor();
130
131         $resp = $this->actingAs($editor)->get('/favourites');
132         $resp->assertDontSee($page->name);
133
134         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
135
136         $resp = $this->get('/favourites');
137         $resp->assertSee($page->name);
138
139         $resp = $this->get('/favourites?page=2');
140         $resp->assertDontSee($page->name);
141     }
142 }