Added clear activity/revision commands. Cleaned commands.
Added testing to cover each command. Removed example laravel inspire command. Standardised command names to be behind 'bookstack' naming. In reference to #320.
Showing
6 changed files
with
181 additions
and
13 deletions
| ... | @@ -2,24 +2,37 @@ | ... | @@ -2,24 +2,37 @@ |
| 2 | 2 | ||
| 3 | namespace BookStack\Console\Commands; | 3 | namespace BookStack\Console\Commands; |
| 4 | 4 | ||
| 5 | +use BookStack\Activity; | ||
| 5 | use Illuminate\Console\Command; | 6 | use Illuminate\Console\Command; |
| 6 | -use Illuminate\Foundation\Inspiring; | ||
| 7 | 7 | ||
| 8 | -class Inspire extends Command | 8 | +class ClearActivity extends Command |
| 9 | { | 9 | { |
| 10 | /** | 10 | /** |
| 11 | * The name and signature of the console command. | 11 | * The name and signature of the console command. |
| 12 | * | 12 | * |
| 13 | * @var string | 13 | * @var string |
| 14 | */ | 14 | */ |
| 15 | - protected $signature = 'inspire'; | 15 | + protected $signature = 'bookstack:clear-activity'; |
| 16 | 16 | ||
| 17 | /** | 17 | /** |
| 18 | * The console command description. | 18 | * The console command description. |
| 19 | * | 19 | * |
| 20 | * @var string | 20 | * @var string |
| 21 | */ | 21 | */ |
| 22 | - protected $description = 'Display an inspiring quote'; | 22 | + protected $description = 'Clear user activity from the system'; |
| 23 | + | ||
| 24 | + protected $activity; | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * Create a new command instance. | ||
| 28 | + * | ||
| 29 | + * @param Activity $activity | ||
| 30 | + */ | ||
| 31 | + public function __construct(Activity $activity) | ||
| 32 | + { | ||
| 33 | + $this->activity = $activity; | ||
| 34 | + parent::__construct(); | ||
| 35 | + } | ||
| 23 | 36 | ||
| 24 | /** | 37 | /** |
| 25 | * Execute the console command. | 38 | * Execute the console command. |
| ... | @@ -28,6 +41,7 @@ class Inspire extends Command | ... | @@ -28,6 +41,7 @@ class Inspire extends Command |
| 28 | */ | 41 | */ |
| 29 | public function handle() | 42 | public function handle() |
| 30 | { | 43 | { |
| 31 | - $this->comment(PHP_EOL.Inspiring::quote().PHP_EOL); | 44 | + $this->activity->newQuery()->truncate(); |
| 45 | + $this->comment('System activity cleared'); | ||
| 32 | } | 46 | } |
| 33 | } | 47 | } | ... | ... |
app/Console/Commands/ClearRevisions.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace BookStack\Console\Commands; | ||
| 4 | + | ||
| 5 | +use BookStack\PageRevision; | ||
| 6 | +use Illuminate\Console\Command; | ||
| 7 | + | ||
| 8 | +class ClearRevisions extends Command | ||
| 9 | +{ | ||
| 10 | + /** | ||
| 11 | + * The name and signature of the console command. | ||
| 12 | + * | ||
| 13 | + * @var string | ||
| 14 | + */ | ||
| 15 | + protected $signature = 'bookstack:clear-revisions | ||
| 16 | + {--a|all : Include active update drafts in deletion} | ||
| 17 | + '; | ||
| 18 | + | ||
| 19 | + /** | ||
| 20 | + * The console command description. | ||
| 21 | + * | ||
| 22 | + * @var string | ||
| 23 | + */ | ||
| 24 | + protected $description = 'Clear page revisions'; | ||
| 25 | + | ||
| 26 | + protected $pageRevision; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * Create a new command instance. | ||
| 30 | + * | ||
| 31 | + * @param PageRevision $pageRevision | ||
| 32 | + */ | ||
| 33 | + public function __construct(PageRevision $pageRevision) | ||
| 34 | + { | ||
| 35 | + $this->pageRevision = $pageRevision; | ||
| 36 | + parent::__construct(); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * Execute the console command. | ||
| 41 | + * | ||
| 42 | + * @return mixed | ||
| 43 | + */ | ||
| 44 | + public function handle() | ||
| 45 | + { | ||
| 46 | + $deleteTypes = $this->option('all') ? ['version', 'update_draft'] : ['version']; | ||
| 47 | + $this->pageRevision->newQuery()->whereIn('type', $deleteTypes)->delete(); | ||
| 48 | + $this->comment('Revisions deleted'); | ||
| 49 | + } | ||
| 50 | +} |
| ... | @@ -4,21 +4,21 @@ namespace BookStack\Console\Commands; | ... | @@ -4,21 +4,21 @@ namespace BookStack\Console\Commands; |
| 4 | 4 | ||
| 5 | use Illuminate\Console\Command; | 5 | use Illuminate\Console\Command; |
| 6 | 6 | ||
| 7 | -class ResetViews extends Command | 7 | +class ClearViews extends Command |
| 8 | { | 8 | { |
| 9 | /** | 9 | /** |
| 10 | * The name and signature of the console command. | 10 | * The name and signature of the console command. |
| 11 | * | 11 | * |
| 12 | * @var string | 12 | * @var string |
| 13 | */ | 13 | */ |
| 14 | - protected $signature = 'views:reset'; | 14 | + protected $signature = 'bookstack:clear-views'; |
| 15 | 15 | ||
| 16 | /** | 16 | /** |
| 17 | * The console command description. | 17 | * The console command description. |
| 18 | * | 18 | * |
| 19 | * @var string | 19 | * @var string |
| 20 | */ | 20 | */ |
| 21 | - protected $description = 'Reset all view-counts for all entities.'; | 21 | + protected $description = 'Clear all view-counts for all entities.'; |
| 22 | 22 | ||
| 23 | /** | 23 | /** |
| 24 | * Create a new command instance. | 24 | * Create a new command instance. |
| ... | @@ -37,5 +37,6 @@ class ResetViews extends Command | ... | @@ -37,5 +37,6 @@ class ResetViews extends Command |
| 37 | public function handle() | 37 | public function handle() |
| 38 | { | 38 | { |
| 39 | \Views::resetAll(); | 39 | \Views::resetAll(); |
| 40 | + $this->comment('Views cleared'); | ||
| 40 | } | 41 | } |
| 41 | } | 42 | } | ... | ... |
| ... | @@ -12,7 +12,7 @@ class RegeneratePermissions extends Command | ... | @@ -12,7 +12,7 @@ class RegeneratePermissions extends Command |
| 12 | * | 12 | * |
| 13 | * @var string | 13 | * @var string |
| 14 | */ | 14 | */ |
| 15 | - protected $signature = 'permissions:regen'; | 15 | + protected $signature = 'bookstack:regenerate-permissions'; |
| 16 | 16 | ||
| 17 | /** | 17 | /** |
| 18 | * The console command description. | 18 | * The console command description. |
| ... | @@ -47,5 +47,6 @@ class RegeneratePermissions extends Command | ... | @@ -47,5 +47,6 @@ class RegeneratePermissions extends Command |
| 47 | public function handle() | 47 | public function handle() |
| 48 | { | 48 | { |
| 49 | $this->permissionService->buildJointPermissions(); | 49 | $this->permissionService->buildJointPermissions(); |
| 50 | + $this->comment('Permissions regenerated'); | ||
| 50 | } | 51 | } |
| 51 | } | 52 | } | ... | ... |
| ... | @@ -13,8 +13,9 @@ class Kernel extends ConsoleKernel | ... | @@ -13,8 +13,9 @@ class Kernel extends ConsoleKernel |
| 13 | * @var array | 13 | * @var array |
| 14 | */ | 14 | */ |
| 15 | protected $commands = [ | 15 | protected $commands = [ |
| 16 | - \BookStack\Console\Commands\Inspire::class, | 16 | + \BookStack\Console\Commands\ClearViews::class, |
| 17 | - \BookStack\Console\Commands\ResetViews::class, | 17 | + \BookStack\Console\Commands\ClearActivity::class, |
| 18 | + \BookStack\Console\Commands\ClearRevisions::class, | ||
| 18 | \BookStack\Console\Commands\RegeneratePermissions::class, | 19 | \BookStack\Console\Commands\RegeneratePermissions::class, |
| 19 | ]; | 20 | ]; |
| 20 | 21 | ||
| ... | @@ -26,7 +27,6 @@ class Kernel extends ConsoleKernel | ... | @@ -26,7 +27,6 @@ class Kernel extends ConsoleKernel |
| 26 | */ | 27 | */ |
| 27 | protected function schedule(Schedule $schedule) | 28 | protected function schedule(Schedule $schedule) |
| 28 | { | 29 | { |
| 29 | - $schedule->command('inspire') | 30 | + // |
| 30 | - ->hourly(); | ||
| 31 | } | 31 | } |
| 32 | } | 32 | } | ... | ... |
tests/CommandsTest.php
0 → 100644
| 1 | +<?php namespace Tests; | ||
| 2 | + | ||
| 3 | +use BookStack\JointPermission; | ||
| 4 | +use BookStack\Page; | ||
| 5 | +use BookStack\Repos\EntityRepo; | ||
| 6 | + | ||
| 7 | +class CommandsTest extends TestCase | ||
| 8 | +{ | ||
| 9 | + | ||
| 10 | + public function test_clear_views_command() | ||
| 11 | + { | ||
| 12 | + $this->asEditor(); | ||
| 13 | + $page = Page::first(); | ||
| 14 | + | ||
| 15 | + $this->get($page->getUrl()); | ||
| 16 | + | ||
| 17 | + $this->assertDatabaseHas('views', [ | ||
| 18 | + 'user_id' => $this->getEditor()->id, | ||
| 19 | + 'viewable_id' => $page->id, | ||
| 20 | + 'views' => 1 | ||
| 21 | + ]); | ||
| 22 | + | ||
| 23 | + $exitCode = \Artisan::call('bookstack:clear-views'); | ||
| 24 | + $this->assertTrue($exitCode === 0, 'Command executed successfully'); | ||
| 25 | + | ||
| 26 | + $this->assertDatabaseMissing('views', [ | ||
| 27 | + 'user_id' => $this->getEditor()->id | ||
| 28 | + ]); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public function test_clear_activity_command() | ||
| 32 | + { | ||
| 33 | + $this->asEditor(); | ||
| 34 | + $page = Page::first(); | ||
| 35 | + \Activity::add($page, 'page_update', $page->book->id); | ||
| 36 | + | ||
| 37 | + $this->assertDatabaseHas('activities', [ | ||
| 38 | + 'key' => 'page_update', | ||
| 39 | + 'entity_id' => $page->id, | ||
| 40 | + 'user_id' => $this->getEditor()->id | ||
| 41 | + ]); | ||
| 42 | + | ||
| 43 | + $exitCode = \Artisan::call('bookstack:clear-activity'); | ||
| 44 | + $this->assertTrue($exitCode === 0, 'Command executed successfully'); | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + $this->assertDatabaseMissing('activities', [ | ||
| 48 | + 'key' => 'page_update' | ||
| 49 | + ]); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public function test_clear_revisions_command() | ||
| 53 | + { | ||
| 54 | + $this->asEditor(); | ||
| 55 | + $entityRepo = $this->app[EntityRepo::class]; | ||
| 56 | + $page = Page::first(); | ||
| 57 | + $entityRepo->updatePage($page, $page->book_id, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']); | ||
| 58 | + $entityRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '<p>new content in draft</p>', 'summary' => 'page revision testing']); | ||
| 59 | + | ||
| 60 | + $this->assertDatabaseHas('page_revisions', [ | ||
| 61 | + 'page_id' => $page->id, | ||
| 62 | + 'type' => 'version' | ||
| 63 | + ]); | ||
| 64 | + $this->assertDatabaseHas('page_revisions', [ | ||
| 65 | + 'page_id' => $page->id, | ||
| 66 | + 'type' => 'update_draft' | ||
| 67 | + ]); | ||
| 68 | + | ||
| 69 | + $exitCode = \Artisan::call('bookstack:clear-revisions'); | ||
| 70 | + $this->assertTrue($exitCode === 0, 'Command executed successfully'); | ||
| 71 | + | ||
| 72 | + $this->assertDatabaseMissing('page_revisions', [ | ||
| 73 | + 'page_id' => $page->id, | ||
| 74 | + 'type' => 'version' | ||
| 75 | + ]); | ||
| 76 | + $this->assertDatabaseHas('page_revisions', [ | ||
| 77 | + 'page_id' => $page->id, | ||
| 78 | + 'type' => 'update_draft' | ||
| 79 | + ]); | ||
| 80 | + | ||
| 81 | + $exitCode = \Artisan::call('bookstack:clear-revisions', ['--all' => true]); | ||
| 82 | + $this->assertTrue($exitCode === 0, 'Command executed successfully'); | ||
| 83 | + | ||
| 84 | + $this->assertDatabaseMissing('page_revisions', [ | ||
| 85 | + 'page_id' => $page->id, | ||
| 86 | + 'type' => 'update_draft' | ||
| 87 | + ]); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + public function test_regen_permissions_command() | ||
| 91 | + { | ||
| 92 | + JointPermission::query()->truncate(); | ||
| 93 | + $page = Page::first(); | ||
| 94 | + | ||
| 95 | + $this->assertDatabaseMissing('joint_permissions', ['entity_id' => $page->id]); | ||
| 96 | + | ||
| 97 | + $exitCode = \Artisan::call('bookstack:regenerate-permissions'); | ||
| 98 | + $this->assertTrue($exitCode === 0, 'Command executed successfully'); | ||
| 99 | + | ||
| 100 | + $this->assertDatabaseHas('joint_permissions', ['entity_id' => $page->id]); | ||
| 101 | + } | ||
| 102 | +} |
-
Please register or sign in to post a comment