]> BookStack Code Mirror - bookstack/blob - tests/FavouriteTest.php
Merge branch 'lang_de' into development
[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         $this->withHtml($resp)->assertElementContains('button', 'Favourite');
19         $this->withHtml($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         $this->withHtml($resp)->assertElementContains('button', 'Unfavourite');
47         $this->withHtml($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             $this->withHtml($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         $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');
83     }
84
85     public function test_favourites_shown_on_homepage()
86     {
87         $editor = $this->getEditor();
88
89         $resp = $this->actingAs($editor)->get('/');
90         $this->withHtml($resp)->assertElementNotExists('#top-favourites');
91
92         /** @var Page $page */
93         $page = Page::query()->first();
94         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
95
96         $resp = $this->get('/');
97         $this->withHtml($resp)->assertElementExists('#top-favourites');
98         $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
99     }
100
101     public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
102     {
103         /** @var Page $page */
104         $page = Page::query()->first();
105         $editor = $this->getEditor();
106
107         $resp = $this->actingAs($editor)->get('/favourites');
108         $resp->assertDontSee($page->name);
109
110         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
111
112         $resp = $this->get('/favourites');
113         $resp->assertSee($page->name);
114
115         $resp = $this->get('/favourites?page=2');
116         $resp->assertDontSee($page->name);
117     }
118 }