]> BookStack Code Mirror - bookstack/blob - tests/User/UserProfileTest.php
Merge branch 'v21.05.x'
[bookstack] / tests / User / UserProfileTest.php
1 <?php
2
3 namespace Tests\User;
4
5 use Activity;
6 use BookStack\Actions\ActivityType;
7 use BookStack\Auth\User;
8 use BookStack\Entities\Models\Bookshelf;
9 use Tests\BrowserKitTest;
10
11 class UserProfileTest extends BrowserKitTest
12 {
13     protected $user;
14
15     public function setUp(): void
16     {
17         parent::setUp();
18         $this->user = User::all()->last();
19     }
20
21     public function test_profile_page_shows_name()
22     {
23         $this->asAdmin()
24             ->visit('/user/' . $this->user->slug)
25             ->see($this->user->name);
26     }
27
28     public function test_profile_page_shows_recent_entities()
29     {
30         $content = $this->createEntityChainBelongingToUser($this->user, $this->user);
31
32         $this->asAdmin()
33             ->visit('/user/' . $this->user->slug)
34             // Check the recently created page is shown
35             ->see($content['page']->name)
36             // Check the recently created chapter is shown
37             ->see($content['chapter']->name)
38             // Check the recently created book is shown
39             ->see($content['book']->name);
40     }
41
42     public function test_profile_page_shows_created_content_counts()
43     {
44         $newUser = $this->getNewBlankUser();
45
46         $this->asAdmin()->visit('/user/' . $newUser->slug)
47             ->see($newUser->name)
48             ->seeInElement('#content-counts', '0 Books')
49             ->seeInElement('#content-counts', '0 Chapters')
50             ->seeInElement('#content-counts', '0 Pages');
51
52         $this->createEntityChainBelongingToUser($newUser, $newUser);
53
54         $this->asAdmin()->visit('/user/' . $newUser->slug)
55             ->see($newUser->name)
56             ->seeInElement('#content-counts', '1 Book')
57             ->seeInElement('#content-counts', '1 Chapter')
58             ->seeInElement('#content-counts', '1 Page');
59     }
60
61     public function test_profile_page_shows_recent_activity()
62     {
63         $newUser = $this->getNewBlankUser();
64         $this->actingAs($newUser);
65         $entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
66         Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
67         Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
68
69         $this->asAdmin()->visit('/user/' . $newUser->slug)
70             ->seeInElement('#recent-user-activity', 'updated book')
71             ->seeInElement('#recent-user-activity', 'created page')
72             ->seeInElement('#recent-user-activity', $entities['page']->name);
73     }
74
75     public function test_clicking_user_name_in_activity_leads_to_profile_page()
76     {
77         $newUser = $this->getNewBlankUser();
78         $this->actingAs($newUser);
79         $entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
80         Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
81         Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
82
83         $this->asAdmin()->visit('/')->clickInElement('#recent-activity', $newUser->name)
84             ->seePageIs('/user/' . $newUser->slug)
85             ->see($newUser->name);
86     }
87
88     public function test_profile_has_search_links_in_created_entity_lists()
89     {
90         $user = $this->getEditor();
91         $resp = $this->actingAs($this->getAdmin())->visit('/user/' . $user->slug);
92
93         $expectedLinks = [
94             '/search?term=%7Bcreated_by%3A' . $user->slug . '%7D+%7Btype%3Apage%7D',
95             '/search?term=%7Bcreated_by%3A' . $user->slug . '%7D+%7Btype%3Achapter%7D',
96             '/search?term=%7Bcreated_by%3A' . $user->slug . '%7D+%7Btype%3Abook%7D',
97             '/search?term=%7Bcreated_by%3A' . $user->slug . '%7D+%7Btype%3Abookshelf%7D',
98         ];
99
100         foreach ($expectedLinks as $link) {
101             $resp->seeInElement('[href$="' . $link . '"]', 'View All');
102         }
103     }
104
105     public function test_guest_profile_shows_limited_form()
106     {
107         $this->asAdmin()
108             ->visit('/settings/users')
109             ->click('Guest')
110             ->dontSeeElement('#password');
111     }
112
113     public function test_guest_profile_cannot_be_deleted()
114     {
115         $guestUser = User::getDefault();
116         $this->asAdmin()->visit('/settings/users/' . $guestUser->id . '/delete')
117             ->see('Delete User')->see('Guest')
118             ->press('Confirm')
119             ->seePageIs('/settings/users/' . $guestUser->id)
120             ->see('cannot delete the guest user');
121     }
122
123     public function test_books_view_is_list()
124     {
125         $editor = $this->getEditor();
126         setting()->putUser($editor, 'books_view_type', 'list');
127
128         $this->actingAs($editor)
129             ->visit('/books')
130             ->pageNotHasElement('.featured-image-container')
131             ->pageHasElement('.content-wrap .entity-list-item');
132     }
133
134     public function test_books_view_is_grid()
135     {
136         $editor = $this->getEditor();
137         setting()->putUser($editor, 'books_view_type', 'grid');
138
139         $this->actingAs($editor)
140             ->visit('/books')
141             ->pageHasElement('.featured-image-container');
142     }
143
144     public function test_shelf_view_type_change()
145     {
146         $editor = $this->getEditor();
147         $shelf = Bookshelf::query()->first();
148         setting()->putUser($editor, 'bookshelf_view_type', 'list');
149
150         $this->actingAs($editor)->visit($shelf->getUrl())
151             ->pageNotHasElement('.featured-image-container')
152             ->pageHasElement('.content-wrap .entity-list-item')
153             ->see('Grid View');
154
155         $req = $this->patch("/settings/users/{$editor->id}/switch-shelf-view", ['view_type' => 'grid']);
156         $req->assertRedirectedTo($shelf->getUrl());
157
158         $this->actingAs($editor)->visit($shelf->getUrl())
159             ->pageHasElement('.featured-image-container')
160             ->pageNotHasElement('.content-wrap .entity-list-item')
161             ->see('List View');
162     }
163 }