]> BookStack Code Mirror - bookstack/blob - tests/CommandsTest.php
Merge branch 'feature_change_view_in_shelves_show' of git://github.com/philjak/BookSt...
[bookstack] / tests / CommandsTest.php
1 <?php namespace Tests;
2
3 use BookStack\Auth\Permissions\JointPermission;
4 use BookStack\Entities\Bookshelf;
5 use BookStack\Entities\Page;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Repos\PageRepo;
8 use Symfony\Component\Console\Exception\RuntimeException;
9
10 class CommandsTest extends TestCase
11 {
12
13     public function test_clear_views_command()
14     {
15         $this->asEditor();
16         $page = Page::first();
17
18         $this->get($page->getUrl());
19
20         $this->assertDatabaseHas('views', [
21             'user_id' => $this->getEditor()->id,
22             'viewable_id' => $page->id,
23             'views' => 1
24         ]);
25
26         $exitCode = \Artisan::call('bookstack:clear-views');
27         $this->assertTrue($exitCode === 0, 'Command executed successfully');
28
29         $this->assertDatabaseMissing('views', [
30             'user_id' => $this->getEditor()->id
31         ]);
32     }
33
34     public function test_clear_activity_command()
35     {
36         $this->asEditor();
37         $page = Page::first();
38         \Activity::add($page, 'page_update', $page->book->id);
39
40         $this->assertDatabaseHas('activities', [
41             'key' => 'page_update',
42             'entity_id' => $page->id,
43             'user_id' => $this->getEditor()->id
44         ]);
45
46         $exitCode = \Artisan::call('bookstack:clear-activity');
47         $this->assertTrue($exitCode === 0, 'Command executed successfully');
48
49
50         $this->assertDatabaseMissing('activities', [
51             'key' => 'page_update'
52         ]);
53     }
54
55     public function test_clear_revisions_command()
56     {
57         $this->asEditor();
58         $pageRepo = app(PageRepo::class);
59         $page = Page::first();
60         $pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
61         $pageRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '<p>new content in draft</p>', 'summary' => 'page revision testing']);
62
63         $this->assertDatabaseHas('page_revisions', [
64             'page_id' => $page->id,
65             'type' => 'version'
66         ]);
67         $this->assertDatabaseHas('page_revisions', [
68             'page_id' => $page->id,
69             'type' => 'update_draft'
70         ]);
71
72         $exitCode = \Artisan::call('bookstack:clear-revisions');
73         $this->assertTrue($exitCode === 0, 'Command executed successfully');
74
75         $this->assertDatabaseMissing('page_revisions', [
76             'page_id' => $page->id,
77             'type' => 'version'
78         ]);
79         $this->assertDatabaseHas('page_revisions', [
80             'page_id' => $page->id,
81             'type' => 'update_draft'
82         ]);
83
84         $exitCode = \Artisan::call('bookstack:clear-revisions', ['--all' => true]);
85         $this->assertTrue($exitCode === 0, 'Command executed successfully');
86
87         $this->assertDatabaseMissing('page_revisions', [
88             'page_id' => $page->id,
89             'type' => 'update_draft'
90         ]);
91     }
92
93     public function test_regen_permissions_command()
94     {
95         JointPermission::query()->truncate();
96         $page = Page::first();
97
98         $this->assertDatabaseMissing('joint_permissions', ['entity_id' => $page->id]);
99
100         $exitCode = \Artisan::call('bookstack:regenerate-permissions');
101         $this->assertTrue($exitCode === 0, 'Command executed successfully');
102
103         $this->assertDatabaseHas('joint_permissions', ['entity_id' => $page->id]);
104     }
105
106     public function test_add_admin_command()
107     {
108         $exitCode = \Artisan::call('bookstack:create-admin', [
109             '--email' => '[email protected]',
110             '--name' => 'Admin Test',
111             '--password' => 'testing-4',
112         ]);
113         $this->assertTrue($exitCode === 0, 'Command executed successfully');
114
115         $this->assertDatabaseHas('users', [
116             'email' => '[email protected]',
117             'name' => 'Admin Test'
118         ]);
119
120         $this->assertTrue(User::where('email', '=', '[email protected]')->first()->hasSystemRole('admin'), 'User has admin role as expected');
121         $this->assertTrue(\Auth::attempt(['email' => '[email protected]', 'password' => 'testing-4']), 'Password stored as expected');
122     }
123
124     public function test_copy_shelf_permissions_command_shows_error_when_no_required_option_given()
125     {
126         $this->artisan('bookstack:copy-shelf-permissions')
127             ->expectsOutput('Either a --slug or --all option must be provided.')
128             ->assertExitCode(0);
129     }
130
131     public function test_copy_shelf_permissions_command_using_slug()
132     {
133         $shelf = Bookshelf::first();
134         $child = $shelf->books()->first();
135         $editorRole = $this->getEditor()->roles()->first();
136         $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
137         $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
138
139         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
140         $this->artisan('bookstack:copy-shelf-permissions', [
141             '--slug' => $shelf->slug,
142         ]);
143         $child = $shelf->books()->first();
144
145         $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
146         $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
147         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
148         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
149     }
150
151     public function test_copy_shelf_permissions_command_using_all()
152     {
153         $shelf = Bookshelf::query()->first();
154         Bookshelf::query()->where('id', '!=', $shelf->id)->delete();
155         $child = $shelf->books()->first();
156         $editorRole = $this->getEditor()->roles()->first();
157         $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
158         $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
159
160         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
161         $this->artisan('bookstack:copy-shelf-permissions --all')
162             ->expectsQuestion('Permission settings for all shelves will be cascaded. Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. Are you sure you want to proceed?', 'y');
163         $child = $shelf->books()->first();
164
165         $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
166         $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
167         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
168         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
169     }
170
171     public function test_update_url_command_updates_page_content()
172     {
173         $page = Page::query()->first();
174         $page->html = '<a href="https://example.com/donkeys"></a>';
175         $page->save();
176
177         $this->artisan('bookstack:update-url https://example.com https://cats.example.com')
178             ->expectsQuestion("This will search for \"https://example.com\" in your database and replace it with  \"https://cats.example.com\".\nAre you sure you want to proceed?", 'y')
179             ->expectsQuestion("This operation could cause issues if used incorrectly. Have you made a backup of your existing database?", 'y');
180
181         $this->assertDatabaseHas('pages', [
182             'id' => $page->id,
183             'html' => '<a href="https://cats.example.com/donkeys"></a>'
184         ]);
185     }
186
187     public function test_update_url_command_requires_valid_url()
188     {
189         $badUrlMessage = "The given urls are expected to be full urls starting with http:// or https://";
190         $this->artisan('bookstack:update-url //example.com https://cats.example.com')->expectsOutput($badUrlMessage);
191         $this->artisan('bookstack:update-url https://example.com htts://cats.example.com')->expectsOutput($badUrlMessage);
192         $this->artisan('bookstack:update-url example.com https://cats.example.com')->expectsOutput($badUrlMessage);
193
194         $this->expectException(RuntimeException::class);
195         $this->artisan('bookstack:update-url https://cats.example.com');
196     }
197 }