5 use BookStack\Activity\Models\Favourite;
6 use BookStack\Users\Models\User;
8 class FavouriteTest extends TestCase
10 public function test_page_add_favourite_flow()
12 $page = $this->entities->page();
13 $editor = $this->users->editor();
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"]');
19 $resp = $this->post('/favourites/add', [
20 'type' => $page->getMorphClass(),
23 $resp->assertRedirect($page->getUrl());
24 $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites");
26 $this->assertDatabaseHas('favourites', [
27 'user_id' => $editor->id,
28 'favouritable_type' => $page->getMorphClass(),
29 'favouritable_id' => $page->id,
33 public function test_page_remove_favourite_flow()
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(),
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"]');
47 $resp = $this->post('/favourites/remove', [
48 'type' => $page->getMorphClass(),
51 $resp->assertRedirect($page->getUrl());
52 $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites");
54 $this->assertDatabaseMissing('favourites', [
55 'user_id' => $editor->id,
59 public function test_add_and_remove_redirect_to_entity_without_history()
61 $page = $this->entities->page();
63 $resp = $this->asEditor()->post('/favourites/add', [
64 'type' => $page->getMorphClass(),
67 $resp->assertRedirect($page->getUrl());
69 $resp = $this->asEditor()->post('/favourites/remove', [
70 'type' => $page->getMorphClass(),
73 $resp->assertRedirect($page->getUrl());
76 public function test_favourite_flow_with_own_permissions()
78 $book = $this->entities->book();
79 $user = User::factory()->create();
80 $book->owned_by = $user->id;
83 $this->permissions->grantUserRolePermissions($user, ['book-view-own']);
85 $this->actingAs($user)->get($book->getUrl());
86 $resp = $this->post('/favourites/add', [
87 'type' => $book->getMorphClass(),
90 $resp->assertRedirect($book->getUrl());
92 $this->assertDatabaseHas('favourites', [
93 'user_id' => $user->id,
94 'favouritable_type' => $book->getMorphClass(),
95 'favouritable_id' => $book->id,
99 public function test_each_entity_type_shows_favourite_button()
101 $this->actingAs($this->users->editor());
103 foreach ($this->entities->all() as $entity) {
104 $resp = $this->get($entity->getUrl());
105 $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
109 public function test_header_contains_link_to_favourites_page_when_logged_in()
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');
118 public function test_favourites_shown_on_homepage()
120 $editor = $this->users->editor();
122 $resp = $this->actingAs($editor)->get('/');
123 $this->withHtml($resp)->assertElementNotExists('#top-favourites');
125 $page = $this->entities->page();
126 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
128 $resp = $this->get('/');
129 $this->withHtml($resp)->assertElementExists('#top-favourites');
130 $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
133 public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
135 $page = $this->entities->page();
136 $editor = $this->users->editor();
138 $resp = $this->actingAs($editor)->get('/favourites');
139 $resp->assertDontSee($page->name);
141 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
143 $resp = $this->get('/favourites');
144 $resp->assertSee($page->name);
146 $resp = $this->get('/favourites?page=2');
147 $resp->assertDontSee($page->name);