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;
10 class FavouriteTest extends TestCase
12 public function test_page_add_favourite_flow()
14 $page = Page::query()->first();
15 $editor = $this->getEditor();
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"]');
21 $resp = $this->post('/favourites/add', [
22 'type' => get_class($page),
25 $resp->assertRedirect($page->getUrl());
26 $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites");
28 $this->assertDatabaseHas('favourites', [
29 'user_id' => $editor->id,
30 'favouritable_type' => $page->getMorphClass(),
31 'favouritable_id' => $page->id,
35 public function test_page_remove_favourite_flow()
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(),
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"]');
49 $resp = $this->post('/favourites/remove', [
50 'type' => get_class($page),
53 $resp->assertRedirect($page->getUrl());
54 $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites");
56 $this->assertDatabaseMissing('favourites', [
57 'user_id' => $editor->id,
61 public function test_favourite_flow_with_own_permissions()
63 /** @var Book $book */
64 $book = Book::query()->first();
65 $user = User::factory()->create();
66 $book->owned_by = $user->id;
69 $this->giveUserPermissions($user, ['book-view-own']);
71 $this->actingAs($user)->get($book->getUrl());
72 $resp = $this->post('/favourites/add', [
73 'type' => get_class($book),
76 $resp->assertRedirect($book->getUrl());
78 $this->assertDatabaseHas('favourites', [
79 'user_id' => $user->id,
80 'favouritable_type' => $book->getMorphClass(),
81 'favouritable_id' => $book->id,
85 public function test_book_chapter_shelf_pages_contain_favourite_button()
88 Bookshelf::query()->first(),
89 Book::query()->first(),
90 Chapter::query()->first(),
92 $this->actingAs($this->getEditor());
94 foreach ($entities as $entity) {
95 $resp = $this->get($entity->getUrl());
96 $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
100 public function test_header_contains_link_to_favourites_page_when_logged_in()
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');
109 public function test_favourites_shown_on_homepage()
111 $editor = $this->getEditor();
113 $resp = $this->actingAs($editor)->get('/');
114 $this->withHtml($resp)->assertElementNotExists('#top-favourites');
116 /** @var Page $page */
117 $page = Page::query()->first();
118 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
120 $resp = $this->get('/');
121 $this->withHtml($resp)->assertElementExists('#top-favourites');
122 $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
125 public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
127 /** @var Page $page */
128 $page = Page::query()->first();
129 $editor = $this->getEditor();
131 $resp = $this->actingAs($editor)->get('/favourites');
132 $resp->assertDontSee($page->name);
134 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
136 $resp = $this->get('/favourites');
137 $resp->assertSee($page->name);
139 $resp = $this->get('/favourites?page=2');
140 $resp->assertDontSee($page->name);