From 0abed1afe5006173c9a16e369a28b693c6716423 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 26 Feb 2017 09:14:18 +0000 Subject: [PATCH] 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. --- app/Console/Commands/ClearActivity.php | 47 ++++++++ app/Console/Commands/ClearRevisions.php | 50 +++++++++ .../{ResetViews.php => ClearViews.php} | 7 +- app/Console/Commands/Inspire.php | 33 ------ .../Commands/RegeneratePermissions.php | 3 +- app/Console/Kernel.php | 8 +- tests/CommandsTest.php | 102 ++++++++++++++++++ 7 files changed, 209 insertions(+), 41 deletions(-) create mode 100644 app/Console/Commands/ClearActivity.php create mode 100644 app/Console/Commands/ClearRevisions.php rename app/Console/Commands/{ResetViews.php => ClearViews.php} (74%) delete mode 100644 app/Console/Commands/Inspire.php create mode 100644 tests/CommandsTest.php diff --git a/app/Console/Commands/ClearActivity.php b/app/Console/Commands/ClearActivity.php new file mode 100644 index 000000000..66babd9a9 --- /dev/null +++ b/app/Console/Commands/ClearActivity.php @@ -0,0 +1,47 @@ +activity = $activity; + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $this->activity->newQuery()->truncate(); + $this->comment('System activity cleared'); + } +} diff --git a/app/Console/Commands/ClearRevisions.php b/app/Console/Commands/ClearRevisions.php new file mode 100644 index 000000000..f0c8a5e85 --- /dev/null +++ b/app/Console/Commands/ClearRevisions.php @@ -0,0 +1,50 @@ +pageRevision = $pageRevision; + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $deleteTypes = $this->option('all') ? ['version', 'update_draft'] : ['version']; + $this->pageRevision->newQuery()->whereIn('type', $deleteTypes)->delete(); + $this->comment('Revisions deleted'); + } +} diff --git a/app/Console/Commands/ResetViews.php b/app/Console/Commands/ClearViews.php similarity index 74% rename from app/Console/Commands/ResetViews.php rename to app/Console/Commands/ClearViews.php index 3a3903ff8..678c64d33 100644 --- a/app/Console/Commands/ResetViews.php +++ b/app/Console/Commands/ClearViews.php @@ -4,21 +4,21 @@ namespace BookStack\Console\Commands; use Illuminate\Console\Command; -class ResetViews extends Command +class ClearViews extends Command { /** * The name and signature of the console command. * * @var string */ - protected $signature = 'views:reset'; + protected $signature = 'bookstack:clear-views'; /** * The console command description. * * @var string */ - protected $description = 'Reset all view-counts for all entities.'; + protected $description = 'Clear all view-counts for all entities.'; /** * Create a new command instance. @@ -37,5 +37,6 @@ class ResetViews extends Command public function handle() { \Views::resetAll(); + $this->comment('Views cleared'); } } diff --git a/app/Console/Commands/Inspire.php b/app/Console/Commands/Inspire.php deleted file mode 100644 index 4b115cfb9..000000000 --- a/app/Console/Commands/Inspire.php +++ /dev/null @@ -1,33 +0,0 @@ -comment(PHP_EOL.Inspiring::quote().PHP_EOL); - } -} diff --git a/app/Console/Commands/RegeneratePermissions.php b/app/Console/Commands/RegeneratePermissions.php index 60d5f4e45..966ee4a82 100644 --- a/app/Console/Commands/RegeneratePermissions.php +++ b/app/Console/Commands/RegeneratePermissions.php @@ -12,7 +12,7 @@ class RegeneratePermissions extends Command * * @var string */ - protected $signature = 'permissions:regen'; + protected $signature = 'bookstack:regenerate-permissions'; /** * The console command description. @@ -47,5 +47,6 @@ class RegeneratePermissions extends Command public function handle() { $this->permissionService->buildJointPermissions(); + $this->comment('Permissions regenerated'); } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index b725c9e21..0112e72ca 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -13,8 +13,9 @@ class Kernel extends ConsoleKernel * @var array */ protected $commands = [ - \BookStack\Console\Commands\Inspire::class, - \BookStack\Console\Commands\ResetViews::class, + \BookStack\Console\Commands\ClearViews::class, + \BookStack\Console\Commands\ClearActivity::class, + \BookStack\Console\Commands\ClearRevisions::class, \BookStack\Console\Commands\RegeneratePermissions::class, ]; @@ -26,7 +27,6 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - $schedule->command('inspire') - ->hourly(); + // } } diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php new file mode 100644 index 000000000..5df82ee51 --- /dev/null +++ b/tests/CommandsTest.php @@ -0,0 +1,102 @@ +asEditor(); + $page = Page::first(); + + $this->get($page->getUrl()); + + $this->assertDatabaseHas('views', [ + 'user_id' => $this->getEditor()->id, + 'viewable_id' => $page->id, + 'views' => 1 + ]); + + $exitCode = \Artisan::call('bookstack:clear-views'); + $this->assertTrue($exitCode === 0, 'Command executed successfully'); + + $this->assertDatabaseMissing('views', [ + 'user_id' => $this->getEditor()->id + ]); + } + + public function test_clear_activity_command() + { + $this->asEditor(); + $page = Page::first(); + \Activity::add($page, 'page_update', $page->book->id); + + $this->assertDatabaseHas('activities', [ + 'key' => 'page_update', + 'entity_id' => $page->id, + 'user_id' => $this->getEditor()->id + ]); + + $exitCode = \Artisan::call('bookstack:clear-activity'); + $this->assertTrue($exitCode === 0, 'Command executed successfully'); + + + $this->assertDatabaseMissing('activities', [ + 'key' => 'page_update' + ]); + } + + public function test_clear_revisions_command() + { + $this->asEditor(); + $entityRepo = $this->app[EntityRepo::class]; + $page = Page::first(); + $entityRepo->updatePage($page, $page->book_id, ['name' => 'updated page', 'html' => '

new content

', 'summary' => 'page revision testing']); + $entityRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '

new content in draft

', 'summary' => 'page revision testing']); + + $this->assertDatabaseHas('page_revisions', [ + 'page_id' => $page->id, + 'type' => 'version' + ]); + $this->assertDatabaseHas('page_revisions', [ + 'page_id' => $page->id, + 'type' => 'update_draft' + ]); + + $exitCode = \Artisan::call('bookstack:clear-revisions'); + $this->assertTrue($exitCode === 0, 'Command executed successfully'); + + $this->assertDatabaseMissing('page_revisions', [ + 'page_id' => $page->id, + 'type' => 'version' + ]); + $this->assertDatabaseHas('page_revisions', [ + 'page_id' => $page->id, + 'type' => 'update_draft' + ]); + + $exitCode = \Artisan::call('bookstack:clear-revisions', ['--all' => true]); + $this->assertTrue($exitCode === 0, 'Command executed successfully'); + + $this->assertDatabaseMissing('page_revisions', [ + 'page_id' => $page->id, + 'type' => 'update_draft' + ]); + } + + public function test_regen_permissions_command() + { + JointPermission::query()->truncate(); + $page = Page::first(); + + $this->assertDatabaseMissing('joint_permissions', ['entity_id' => $page->id]); + + $exitCode = \Artisan::call('bookstack:regenerate-permissions'); + $this->assertTrue($exitCode === 0, 'Command executed successfully'); + + $this->assertDatabaseHas('joint_permissions', ['entity_id' => $page->id]); + } +}