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