]> BookStack Code Mirror - bookstack/blob - tests/User/UserProfileTest.php
Merge pull request #2820 from BookStackApp/analysis-6470L9
[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_guest_profile_shows_limited_form()
89     {
90         $this->asAdmin()
91             ->visit('/settings/users')
92             ->click('Guest')
93             ->dontSeeElement('#password');
94     }
95
96     public function test_guest_profile_cannot_be_deleted()
97     {
98         $guestUser = User::getDefault();
99         $this->asAdmin()->visit('/settings/users/' . $guestUser->id . '/delete')
100             ->see('Delete User')->see('Guest')
101             ->press('Confirm')
102             ->seePageIs('/settings/users/' . $guestUser->id)
103             ->see('cannot delete the guest user');
104     }
105
106     public function test_books_view_is_list()
107     {
108         $editor = $this->getEditor();
109         setting()->putUser($editor, 'books_view_type', 'list');
110
111         $this->actingAs($editor)
112             ->visit('/books')
113             ->pageNotHasElement('.featured-image-container')
114             ->pageHasElement('.content-wrap .entity-list-item');
115     }
116
117     public function test_books_view_is_grid()
118     {
119         $editor = $this->getEditor();
120         setting()->putUser($editor, 'books_view_type', 'grid');
121
122         $this->actingAs($editor)
123             ->visit('/books')
124             ->pageHasElement('.featured-image-container');
125     }
126
127     public function test_shelf_view_type_change()
128     {
129         $editor = $this->getEditor();
130         $shelf = Bookshelf::query()->first();
131         setting()->putUser($editor, 'bookshelf_view_type', 'list');
132
133         $this->actingAs($editor)->visit($shelf->getUrl())
134             ->pageNotHasElement('.featured-image-container')
135             ->pageHasElement('.content-wrap .entity-list-item')
136             ->see('Grid View');
137
138         $req = $this->patch("/settings/users/{$editor->id}/switch-shelf-view", ['view_type' => 'grid']);
139         $req->assertRedirectedTo($shelf->getUrl());
140
141         $this->actingAs($editor)->visit($shelf->getUrl())
142             ->pageHasElement('.featured-image-container')
143             ->pageNotHasElement('.content-wrap .entity-list-item')
144             ->see('List View');
145     }
146 }