5 use BookStack\Actions\Favourite;
6 use BookStack\Auth\User;
8 class FavouriteTest extends TestCase
10 public function test_page_add_favourite_flow()
12 $page = $this->entities->page();
13 $editor = $this->getEditor();
15 $resp = $this->actingAs($editor)->get($page->getUrl());
16 $this->withHtml($resp)->assertElementContains('button', 'Favourite');
17 $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
19 $resp = $this->post('/favourites/add', [
20 'type' => get_class($page),
23 $resp->assertRedirect($page->getUrl());
24 $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites");
26 $this->assertDatabaseHas('favourites', [
27 'user_id' => $editor->id,
28 'favouritable_type' => $page->getMorphClass(),
29 'favouritable_id' => $page->id,
33 public function test_page_remove_favourite_flow()
35 $page = $this->entities->page();
36 $editor = $this->getEditor();
37 Favourite::query()->forceCreate([
38 'user_id' => $editor->id,
39 'favouritable_id' => $page->id,
40 'favouritable_type' => $page->getMorphClass(),
43 $resp = $this->actingAs($editor)->get($page->getUrl());
44 $this->withHtml($resp)->assertElementContains('button', 'Unfavourite');
45 $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/remove"]');
47 $resp = $this->post('/favourites/remove', [
48 'type' => get_class($page),
51 $resp->assertRedirect($page->getUrl());
52 $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites");
54 $this->assertDatabaseMissing('favourites', [
55 'user_id' => $editor->id,
59 public function test_favourite_flow_with_own_permissions()
61 $book = $this->entities->book();
62 $user = User::factory()->create();
63 $book->owned_by = $user->id;
66 $this->giveUserPermissions($user, ['book-view-own']);
68 $this->actingAs($user)->get($book->getUrl());
69 $resp = $this->post('/favourites/add', [
70 'type' => get_class($book),
73 $resp->assertRedirect($book->getUrl());
75 $this->assertDatabaseHas('favourites', [
76 'user_id' => $user->id,
77 'favouritable_type' => $book->getMorphClass(),
78 'favouritable_id' => $book->id,
82 public function test_each_entity_type_shows_favourite_button()
84 $this->actingAs($this->getEditor());
86 foreach ($this->entities->all() as $entity) {
87 $resp = $this->get($entity->getUrl());
88 $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
92 public function test_header_contains_link_to_favourites_page_when_logged_in()
94 $this->setSettings(['app-public' => 'true']);
95 $resp = $this->get('/');
96 $this->withHtml($resp)->assertElementNotContains('header', 'My Favourites');
97 $resp = $this->actingAs($this->getViewer())->get('/');
98 $this->withHtml($resp)->assertElementContains('header a', 'My Favourites');
101 public function test_favourites_shown_on_homepage()
103 $editor = $this->getEditor();
105 $resp = $this->actingAs($editor)->get('/');
106 $this->withHtml($resp)->assertElementNotExists('#top-favourites');
108 $page = $this->entities->page();
109 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
111 $resp = $this->get('/');
112 $this->withHtml($resp)->assertElementExists('#top-favourites');
113 $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
116 public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
118 $page = $this->entities->page();
119 $editor = $this->getEditor();
121 $resp = $this->actingAs($editor)->get('/favourites');
122 $resp->assertDontSee($page->name);
124 $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
126 $resp = $this->get('/favourites');
127 $resp->assertSee($page->name);
129 $resp = $this->get('/favourites?page=2');
130 $resp->assertDontSee($page->name);