]> BookStack Code Mirror - bookstack/blob - tests/FavouriteTest.php
ZIP Exports: Changed the instance id mechanism
[bookstack] / tests / FavouriteTest.php
1 <?php
2
3 namespace Tests;
4
5 use BookStack\Activity\Models\Favourite;
6 use BookStack\Users\Models\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->users->editor();
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"] input[name="type"][value="page"]');
18
19         $resp = $this->post('/favourites/add', [
20             'type' => $page->getMorphClass(),
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->users->editor();
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' => $page->getMorphClass(),
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_add_and_remove_redirect_to_entity_without_history()
60     {
61         $page = $this->entities->page();
62
63         $resp = $this->asEditor()->post('/favourites/add', [
64             'type' => $page->getMorphClass(),
65             'id'   => $page->id,
66         ]);
67         $resp->assertRedirect($page->getUrl());
68
69         $resp = $this->asEditor()->post('/favourites/remove', [
70             'type' => $page->getMorphClass(),
71             'id'   => $page->id,
72         ]);
73         $resp->assertRedirect($page->getUrl());
74     }
75
76     public function test_favourite_flow_with_own_permissions()
77     {
78         $book = $this->entities->book();
79         $user = User::factory()->create();
80         $book->owned_by = $user->id;
81         $book->save();
82
83         $this->permissions->grantUserRolePermissions($user, ['book-view-own']);
84
85         $this->actingAs($user)->get($book->getUrl());
86         $resp = $this->post('/favourites/add', [
87             'type' => $book->getMorphClass(),
88             'id'   => $book->id,
89         ]);
90         $resp->assertRedirect($book->getUrl());
91
92         $this->assertDatabaseHas('favourites', [
93             'user_id'           => $user->id,
94             'favouritable_type' => $book->getMorphClass(),
95             'favouritable_id'   => $book->id,
96         ]);
97     }
98
99     public function test_each_entity_type_shows_favourite_button()
100     {
101         $this->actingAs($this->users->editor());
102
103         foreach ($this->entities->all() as $entity) {
104             $resp = $this->get($entity->getUrl());
105             $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
106         }
107     }
108
109     public function test_header_contains_link_to_favourites_page_when_logged_in()
110     {
111         $this->setSettings(['app-public' => 'true']);
112         $resp = $this->get('/');
113         $this->withHtml($resp)->assertElementNotContains('header', 'My Favourites');
114         $resp = $this->actingAs($this->users->viewer())->get('/');
115         $this->withHtml($resp)->assertElementContains('header a', 'My Favourites');
116     }
117
118     public function test_favourites_shown_on_homepage()
119     {
120         $editor = $this->users->editor();
121
122         $resp = $this->actingAs($editor)->get('/');
123         $this->withHtml($resp)->assertElementNotExists('#top-favourites');
124
125         $page = $this->entities->page();
126         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
127
128         $resp = $this->get('/');
129         $this->withHtml($resp)->assertElementExists('#top-favourites');
130         $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
131     }
132
133     public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
134     {
135         $page = $this->entities->page();
136         $editor = $this->users->editor();
137
138         $resp = $this->actingAs($editor)->get('/favourites');
139         $resp->assertDontSee($page->name);
140
141         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
142
143         $resp = $this->get('/favourites');
144         $resp->assertSee($page->name);
145
146         $resp = $this->get('/favourites?page=2');
147         $resp->assertDontSee($page->name);
148     }
149 }