]> BookStack Code Mirror - bookstack/blob - tests/FavouriteTest.php
Applied StyleCI changes
[bookstack] / tests / FavouriteTest.php
1 <?php
2
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;
8 use Tests\TestCase;
9
10 class FavouriteTest extends TestCase
11 {
12     public function test_page_add_favourite_flow()
13     {
14         $page = Page::query()->first();
15         $editor = $this->getEditor();
16
17         $resp = $this->actingAs($editor)->get($page->getUrl());
18         $resp->assertElementContains('button', 'Favourite');
19         $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
20
21         $resp = $this->post('/favourites/add', [
22             'type' => get_class($page),
23             'id'   => $page->id,
24         ]);
25         $resp->assertRedirect($page->getUrl());
26         $resp->assertSessionHas('success', "\"{$page->name}\" has been added to your favourites");
27
28         $this->assertDatabaseHas('favourites', [
29             'user_id'           => $editor->id,
30             'favouritable_type' => $page->getMorphClass(),
31             'favouritable_id'   => $page->id,
32         ]);
33     }
34
35     public function test_page_remove_favourite_flow()
36     {
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(),
43         ]);
44
45         $resp = $this->actingAs($editor)->get($page->getUrl());
46         $resp->assertElementContains('button', 'Unfavourite');
47         $resp->assertElementExists('form[method="POST"][action$="/favourites/remove"]');
48
49         $resp = $this->post('/favourites/remove', [
50             'type' => get_class($page),
51             'id'   => $page->id,
52         ]);
53         $resp->assertRedirect($page->getUrl());
54         $resp->assertSessionHas('success', "\"{$page->name}\" has been removed from your favourites");
55
56         $this->assertDatabaseMissing('favourites', [
57             'user_id' => $editor->id,
58         ]);
59     }
60
61     public function test_book_chapter_shelf_pages_contain_favourite_button()
62     {
63         $entities = [
64             Bookshelf::query()->first(),
65             Book::query()->first(),
66             Chapter::query()->first(),
67         ];
68         $this->actingAs($this->getEditor());
69
70         foreach ($entities as $entity) {
71             $resp = $this->get($entity->getUrl());
72             $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
73         }
74     }
75
76     public function test_header_contains_link_to_favourites_page_when_logged_in()
77     {
78         $this->setSettings(['app-public' => 'true']);
79         $this->get('/')->assertElementNotContains('header', 'My Favourites');
80         $this->actingAs($this->getViewer())->get('/')->assertElementContains('header a', 'My Favourites');
81     }
82
83     public function test_favourites_shown_on_homepage()
84     {
85         $editor = $this->getEditor();
86
87         $resp = $this->actingAs($editor)->get('/');
88         $resp->assertElementNotExists('#top-favourites');
89
90         /** @var Page $page */
91         $page = Page::query()->first();
92         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
93
94         $resp = $this->get('/');
95         $resp->assertElementExists('#top-favourites');
96         $resp->assertElementContains('#top-favourites', $page->name);
97     }
98
99     public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
100     {
101         /** @var Page $page */
102         $page = Page::query()->first();
103         $editor = $this->getEditor();
104
105         $resp = $this->actingAs($editor)->get('/favourites');
106         $resp->assertDontSee($page->name);
107
108         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
109
110         $resp = $this->get('/favourites');
111         $resp->assertSee($page->name);
112
113         $resp = $this->get('/favourites?page=2');
114         $resp->assertDontSee($page->name);
115     }
116 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.