]> BookStack Code Mirror - bookstack/blob - tests/CommandsTest.php
Merge branch 'master' of git://github.com/Binternet/BookStack into Binternet-master
[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
9 class CommandsTest extends TestCase
10 {
11
12     public function test_clear_views_command()
13     {
14         $this->asEditor();
15         $page = Page::first();
16
17         $this->get($page->getUrl());
18
19         $this->assertDatabaseHas('views', [
20             'user_id' => $this->getEditor()->id,
21             'viewable_id' => $page->id,
22             'views' => 1
23         ]);
24
25         $exitCode = \Artisan::call('bookstack:clear-views');
26         $this->assertTrue($exitCode === 0, 'Command executed successfully');
27
28         $this->assertDatabaseMissing('views', [
29             'user_id' => $this->getEditor()->id
30         ]);
31     }
32
33     public function test_clear_activity_command()
34     {
35         $this->asEditor();
36         $page = Page::first();
37         \Activity::add($page, 'page_update', $page->book->id);
38
39         $this->assertDatabaseHas('activities', [
40             'key' => 'page_update',
41             'entity_id' => $page->id,
42             'user_id' => $this->getEditor()->id
43         ]);
44
45         $exitCode = \Artisan::call('bookstack:clear-activity');
46         $this->assertTrue($exitCode === 0, 'Command executed successfully');
47
48
49         $this->assertDatabaseMissing('activities', [
50             'key' => 'page_update'
51         ]);
52     }
53
54     public function test_clear_revisions_command()
55     {
56         $this->asEditor();
57         $pageRepo = app(PageRepo::class);
58         $page = Page::first();
59         $pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
60         $pageRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '<p>new content in draft</p>', 'summary' => 'page revision testing']);
61
62         $this->assertDatabaseHas('page_revisions', [
63             'page_id' => $page->id,
64             'type' => 'version'
65         ]);
66         $this->assertDatabaseHas('page_revisions', [
67             'page_id' => $page->id,
68             'type' => 'update_draft'
69         ]);
70
71         $exitCode = \Artisan::call('bookstack:clear-revisions');
72         $this->assertTrue($exitCode === 0, 'Command executed successfully');
73
74         $this->assertDatabaseMissing('page_revisions', [
75             'page_id' => $page->id,
76             'type' => 'version'
77         ]);
78         $this->assertDatabaseHas('page_revisions', [
79             'page_id' => $page->id,
80             'type' => 'update_draft'
81         ]);
82
83         $exitCode = \Artisan::call('bookstack:clear-revisions', ['--all' => true]);
84         $this->assertTrue($exitCode === 0, 'Command executed successfully');
85
86         $this->assertDatabaseMissing('page_revisions', [
87             'page_id' => $page->id,
88             'type' => 'update_draft'
89         ]);
90     }
91
92     public function test_regen_permissions_command()
93     {
94         JointPermission::query()->truncate();
95         $page = Page::first();
96
97         $this->assertDatabaseMissing('joint_permissions', ['entity_id' => $page->id]);
98
99         $exitCode = \Artisan::call('bookstack:regenerate-permissions');
100         $this->assertTrue($exitCode === 0, 'Command executed successfully');
101
102         $this->assertDatabaseHas('joint_permissions', ['entity_id' => $page->id]);
103     }
104
105     public function test_add_admin_command()
106     {
107         $exitCode = \Artisan::call('bookstack:create-admin', [
108             '--email' => '[email protected]',
109             '--name' => 'Admin Test',
110             '--password' => 'testing-4',
111         ]);
112         $this->assertTrue($exitCode === 0, 'Command executed successfully');
113
114         $this->assertDatabaseHas('users', [
115             'email' => '[email protected]',
116             'name' => 'Admin Test'
117         ]);
118
119         $this->assertTrue(User::where('email', '=', '[email protected]')->first()->hasSystemRole('admin'), 'User has admin role as expected');
120         $this->assertTrue(\Auth::attempt(['email' => '[email protected]', 'password' => 'testing-4']), 'Password stored as expected');
121     }
122
123     public function test_copy_shelf_permissions_command_shows_error_when_no_required_option_given()
124     {
125         $this->artisan('bookstack:copy-shelf-permissions')
126             ->expectsOutput('Either a --slug or --all option must be provided.')
127             ->assertExitCode(0);
128     }
129
130     public function test_copy_shelf_permissions_command_using_slug()
131     {
132         $shelf = Bookshelf::first();
133         $child = $shelf->books()->first();
134         $editorRole = $this->getEditor()->roles()->first();
135         $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
136         $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
137
138         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
139         $this->artisan('bookstack:copy-shelf-permissions', [
140             '--slug' => $shelf->slug,
141         ]);
142         $child = $shelf->books()->first();
143
144         $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
145         $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
146         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
147         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
148     }
149
150     public function test_copy_shelf_permissions_command_using_all()
151     {
152         $shelf = Bookshelf::query()->first();
153         Bookshelf::query()->where('id', '!=', $shelf->id)->delete();
154         $child = $shelf->books()->first();
155         $editorRole = $this->getEditor()->roles()->first();
156         $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
157         $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
158
159         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
160         $this->artisan('bookstack:copy-shelf-permissions --all')
161             ->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');
162         $child = $shelf->books()->first();
163
164         $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
165         $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
166         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
167         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
168     }
169 }