3 use BookStack\Actions\Favourite;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Models\Chapter;
7 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 $resp->assertElementContains('button', 'Favourite');
19 $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 $resp->assertElementContains('button', 'Unfavourite');
47 $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_book_chapter_shelf_pages_contain_favourite_button()
64 Bookshelf::query()->first(),
65 Book::query()->first(),
66 Chapter::query()->first(),
68 $this->actingAs($this->getEditor());
70 foreach ($entities as $entity) {
71 $resp = $this->get($entity->getUrl());
72 $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
76 public function test_header_contains_link_to_favourites_page_when_logged_in()
78 $this->setSettings(['app-public' => 'true']);
79 $this->get('/')->assertElementNotContains('header', 'My Favourites');
80 $this->actingAs($this->getViewer())->get('/')->assertElementContains('header a', 'My Favourites');
83 public function test_favourites_shown_on_homepage()
85 $editor = $this->getEditor();
87 $resp = $this->actingAs($editor)->get('/');
88 $resp->assertElementNotExists('#top-favourites');
90 /** @var Page $page */
91 $page = Page::query()->first();
92 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
94 $resp = $this->get('/');
95 $resp->assertElementExists('#top-favourites');
96 $resp->assertElementContains('#top-favourites', $page->name);
99 public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
101 /** @var Page $page */
102 $page = Page::query()->first();
103 $editor = $this->getEditor();
105 $resp = $this->actingAs($editor)->get('/favourites');
106 $resp->assertDontSee($page->name);
108 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
110 $resp = $this->get('/favourites');
111 $resp->assertSee($page->name);
113 $resp = $this->get('/favourites?page=2');
114 $resp->assertDontSee($page->name);