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;
12 class FavouriteTest extends TestCase
14 public function test_page_add_favourite_flow()
16 $page = Page::query()->first();
17 $editor = $this->getEditor();
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"]');
23 $resp = $this->post('/favourites/add', [
24 'type' => get_class($page),
27 $resp->assertRedirect($page->getUrl());
28 $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites");
30 $this->assertDatabaseHas('favourites', [
31 'user_id' => $editor->id,
32 'favouritable_type' => $page->getMorphClass(),
33 'favouritable_id' => $page->id,
37 public function test_page_remove_favourite_flow()
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(),
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"]');
51 $resp = $this->post('/favourites/remove', [
52 'type' => get_class($page),
55 $resp->assertRedirect($page->getUrl());
56 $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites");
58 $this->assertDatabaseMissing('favourites', [
59 'user_id' => $editor->id,
63 public function test_favourite_flow_with_own_permissions()
65 /** @var Book $book */
66 $book = Book::query()->first();
67 $user = User::factory()->create();
68 $book->owned_by = $user->id;
71 $this->giveUserPermissions($user, ['book-view-own']);
73 $this->actingAs($user)->get($book->getUrl());
74 $resp = $this->post('/favourites/add', [
75 'type' => get_class($book),
78 $resp->assertRedirect($book->getUrl());
80 $this->assertDatabaseHas('favourites', [
81 'user_id' => $user->id,
82 'favouritable_type' => $book->getMorphClass(),
83 'favouritable_id' => $book->id,
87 public function test_book_chapter_shelf_pages_contain_favourite_button()
90 Bookshelf::query()->first(),
91 Book::query()->first(),
92 Chapter::query()->first(),
94 $this->actingAs($this->getEditor());
96 foreach ($entities as $entity) {
97 $resp = $this->get($entity->getUrl());
98 $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
102 public function test_header_contains_link_to_favourites_page_when_logged_in()
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');
111 public function test_favourites_shown_on_homepage()
113 $editor = $this->getEditor();
115 $resp = $this->actingAs($editor)->get('/');
116 $this->withHtml($resp)->assertElementNotExists('#top-favourites');
118 /** @var Page $page */
119 $page = Page::query()->first();
120 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
122 $resp = $this->get('/');
123 $this->withHtml($resp)->assertElementExists('#top-favourites');
124 $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
127 public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
129 /** @var Page $page */
130 $page = Page::query()->first();
131 $editor = $this->getEditor();
133 $resp = $this->actingAs($editor)->get('/favourites');
134 $resp->assertDontSee($page->name);
136 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
138 $resp = $this->get('/favourites');
139 $resp->assertSee($page->name);
141 $resp = $this->get('/favourites?page=2');
142 $resp->assertDontSee($page->name);