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