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