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 $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_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 $this->withHtml($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 $resp = $this->get('/');
80 $this->withHtml($resp)->assertElementNotContains('header', 'My Favourites');
81 $resp = $this->actingAs($this->getViewer())->get('/');
82 $this->withHtml($resp)->assertElementContains('header a', 'My Favourites');
85 public function test_favourites_shown_on_homepage()
87 $editor = $this->getEditor();
89 $resp = $this->actingAs($editor)->get('/');
90 $this->withHtml($resp)->assertElementNotExists('#top-favourites');
92 /** @var Page $page */
93 $page = Page::query()->first();
94 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
96 $resp = $this->get('/');
97 $this->withHtml($resp)->assertElementExists('#top-favourites');
98 $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
101 public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
103 /** @var Page $page */
104 $page = Page::query()->first();
105 $editor = $this->getEditor();
107 $resp = $this->actingAs($editor)->get('/favourites');
108 $resp->assertDontSee($page->name);
110 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
112 $resp = $this->get('/favourites');
113 $resp->assertSee($page->name);
115 $resp = $this->get('/favourites?page=2');
116 $resp->assertDontSee($page->name);