3 use BookStack\Actions\Comment;
4 use BookStack\Actions\CommentRepo;
5 use BookStack\Auth\Permissions\JointPermission;
6 use BookStack\Entities\Bookshelf;
7 use BookStack\Entities\Page;
8 use BookStack\Auth\User;
9 use BookStack\Entities\Repos\PageRepo;
10 use Symfony\Component\Console\Exception\RuntimeException;
12 class CommandsTest extends TestCase
15 public function test_clear_views_command()
18 $page = Page::first();
20 $this->get($page->getUrl());
22 $this->assertDatabaseHas('views', [
23 'user_id' => $this->getEditor()->id,
24 'viewable_id' => $page->id,
28 $exitCode = \Artisan::call('bookstack:clear-views');
29 $this->assertTrue($exitCode === 0, 'Command executed successfully');
31 $this->assertDatabaseMissing('views', [
32 'user_id' => $this->getEditor()->id
36 public function test_clear_activity_command()
39 $page = Page::first();
40 \Activity::add($page, 'page_update', $page->book->id);
42 $this->assertDatabaseHas('activities', [
43 'key' => 'page_update',
44 'entity_id' => $page->id,
45 'user_id' => $this->getEditor()->id
48 $exitCode = \Artisan::call('bookstack:clear-activity');
49 $this->assertTrue($exitCode === 0, 'Command executed successfully');
52 $this->assertDatabaseMissing('activities', [
53 'key' => 'page_update'
57 public function test_clear_revisions_command()
60 $pageRepo = app(PageRepo::class);
61 $page = Page::first();
62 $pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
63 $pageRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '<p>new content in draft</p>', 'summary' => 'page revision testing']);
65 $this->assertDatabaseHas('page_revisions', [
66 'page_id' => $page->id,
69 $this->assertDatabaseHas('page_revisions', [
70 'page_id' => $page->id,
71 'type' => 'update_draft'
74 $exitCode = \Artisan::call('bookstack:clear-revisions');
75 $this->assertTrue($exitCode === 0, 'Command executed successfully');
77 $this->assertDatabaseMissing('page_revisions', [
78 'page_id' => $page->id,
81 $this->assertDatabaseHas('page_revisions', [
82 'page_id' => $page->id,
83 'type' => 'update_draft'
86 $exitCode = \Artisan::call('bookstack:clear-revisions', ['--all' => true]);
87 $this->assertTrue($exitCode === 0, 'Command executed successfully');
89 $this->assertDatabaseMissing('page_revisions', [
90 'page_id' => $page->id,
91 'type' => 'update_draft'
95 public function test_regen_permissions_command()
97 JointPermission::query()->truncate();
98 $page = Page::first();
100 $this->assertDatabaseMissing('joint_permissions', ['entity_id' => $page->id]);
102 $exitCode = \Artisan::call('bookstack:regenerate-permissions');
103 $this->assertTrue($exitCode === 0, 'Command executed successfully');
105 $this->assertDatabaseHas('joint_permissions', ['entity_id' => $page->id]);
108 public function test_add_admin_command()
110 $exitCode = \Artisan::call('bookstack:create-admin', [
111 '--email' => 'admintest@example.com',
112 '--name' => 'Admin Test',
113 '--password' => 'testing-4',
115 $this->assertTrue($exitCode === 0, 'Command executed successfully');
117 $this->assertDatabaseHas('users', [
118 'email' => 'admintest@example.com',
119 'name' => 'Admin Test'
122 $this->assertTrue(User::where('email', '=', 'admintest@example.com')->first()->hasSystemRole('admin'), 'User has admin role as expected');
123 $this->assertTrue(\Auth::attempt(['email' => 'admintest@example.com', 'password' => 'testing-4']), 'Password stored as expected');
126 public function test_copy_shelf_permissions_command_shows_error_when_no_required_option_given()
128 $this->artisan('bookstack:copy-shelf-permissions')
129 ->expectsOutput('Either a --slug or --all option must be provided.')
133 public function test_copy_shelf_permissions_command_using_slug()
135 $shelf = Bookshelf::first();
136 $child = $shelf->books()->first();
137 $editorRole = $this->getEditor()->roles()->first();
138 $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
139 $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
141 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
142 $this->artisan('bookstack:copy-shelf-permissions', [
143 '--slug' => $shelf->slug,
145 $child = $shelf->books()->first();
147 $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
148 $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
149 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
150 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
153 public function test_copy_shelf_permissions_command_using_all()
155 $shelf = Bookshelf::query()->first();
156 Bookshelf::query()->where('id', '!=', $shelf->id)->delete();
157 $child = $shelf->books()->first();
158 $editorRole = $this->getEditor()->roles()->first();
159 $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
160 $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
162 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
163 $this->artisan('bookstack:copy-shelf-permissions --all')
164 ->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');
165 $child = $shelf->books()->first();
167 $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
168 $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
169 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
170 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
173 public function test_update_url_command_updates_page_content()
175 $page = Page::query()->first();
176 $page->html = '<a href="https://example.com/donkeys"></a>';
179 $this->artisan('bookstack:update-url https://example.com https://cats.example.com')
180 ->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')
181 ->expectsQuestion("This operation could cause issues if used incorrectly. Have you made a backup of your existing database?", 'y');
183 $this->assertDatabaseHas('pages', [
185 'html' => '<a href="https://cats.example.com/donkeys"></a>'
189 public function test_update_url_command_requires_valid_url()
191 $badUrlMessage = "The given urls are expected to be full urls starting with http:// or https://";
192 $this->artisan('bookstack:update-url //example.com https://cats.example.com')->expectsOutput($badUrlMessage);
193 $this->artisan('bookstack:update-url https://example.com htts://cats.example.com')->expectsOutput($badUrlMessage);
194 $this->artisan('bookstack:update-url example.com https://cats.example.com')->expectsOutput($badUrlMessage);
196 $this->expectException(RuntimeException::class);
197 $this->artisan('bookstack:update-url https://cats.example.com');
200 public function test_regenerate_comment_content_command()
202 Comment::query()->forceCreate([
203 'html' => 'some_old_content',
204 'text' => 'some_fresh_content',
207 $this->assertDatabaseHas('comments', [
208 'html' => 'some_old_content',
211 $exitCode = \Artisan::call('bookstack:regenerate-comment-content');
212 $this->assertTrue($exitCode === 0, 'Command executed successfully');
214 $this->assertDatabaseMissing('comments', [
215 'html' => 'some_old_content',
217 $this->assertDatabaseHas('comments', [
218 'html' => "<p>some_fresh_content</p>\n",