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
13 public function test_page_add_favourite_flow()
15 $page = Page::query()->first();
16 $editor = $this->getEditor();
18 $resp = $this->actingAs($editor)->get($page->getUrl());
19 $resp->assertElementContains('button', 'Favourite');
20 $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
22 $resp = $this->post('/favourites/add', [
23 'type' => get_class($page),
26 $resp->assertRedirect($page->getUrl());
27 $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites");
29 $this->assertDatabaseHas('favourites', [
30 'user_id' => $editor->id,
31 'favouritable_type' => $page->getMorphClass(),
32 'favouritable_id' => $page->id,
36 public function test_page_remove_favourite_flow()
38 $page = Page::query()->first();
39 $editor = $this->getEditor();
40 Favourite::query()->forceCreate([
41 'user_id' => $editor->id,
42 'favouritable_id' => $page->id,
43 'favouritable_type' => $page->getMorphClass(),
46 $resp = $this->actingAs($editor)->get($page->getUrl());
47 $resp->assertElementContains('button', 'Unfavourite');
48 $resp->assertElementExists('form[method="POST"][action$="/favourites/remove"]');
50 $resp = $this->post('/favourites/remove', [
51 'type' => get_class($page),
54 $resp->assertRedirect($page->getUrl());
55 $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites");
57 $this->assertDatabaseMissing('favourites', [
58 'user_id' => $editor->id,
62 public function test_book_chapter_shelf_pages_contain_favourite_button()
65 Bookshelf::query()->first(),
66 Book::query()->first(),
67 Chapter::query()->first(),
69 $this->actingAs($this->getEditor());
71 foreach ($entities as $entity) {
72 $resp = $this->get($entity->getUrl());
73 $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
77 public function test_header_contains_link_to_favourites_page_when_logged_in()
79 $this->setSettings(['app-public' => 'true']);
80 $this->get('/')->assertElementNotContains('header', 'My Favourites');
81 $this->actingAs($this->getViewer())->get('/')->assertElementContains('header a', 'My Favourites');
84 public function test_favourites_shown_on_homepage()
86 $editor = $this->getEditor();
88 $resp = $this->actingAs($editor)->get('/');
89 $resp->assertElementNotExists('#top-favourites');
91 /** @var Page $page */
92 $page = Page::query()->first();
93 $page->favourites()->save((new Favourite)->forceFill(['user_id' => $editor->id]));
95 $resp = $this->get('/');
96 $resp->assertElementExists('#top-favourites');
97 $resp->assertElementContains('#top-favourites', $page->name);
100 public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
102 /** @var Page $page */
103 $page = Page::query()->first();
104 $editor = $this->getEditor();
106 $resp = $this->actingAs($editor)->get('/favourites');
107 $resp->assertDontSee($page->name);
109 $page->favourites()->save((new Favourite)->forceFill(['user_id' => $editor->id]));
111 $resp = $this->get('/favourites');
112 $resp->assertSee($page->name);
114 $resp = $this->get('/favourites?page=2');
115 $resp->assertDontSee($page->name);