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