NOW MANAGED ON CODEBERG
Added clear activity/revision commands. Cleaned commands.
Dan Brown0abed1afe500
diff --git a/app/Console/Commands/ClearActivity.php b/app/Console/Commands/ClearActivity.phpnew file mode 100644index 000000000..66babd9a9--- /dev/null+++ b/app/Console/Commands/ClearActivity.php@@ -0,0 +1,47 @@+<?php++namespace BookStack\Console\Commands;++use BookStack\Activity;+use Illuminate\Console\Command;++class ClearActivity extends Command+{+ /**+ * The name and signature of the console command.+ *+ * @var string+ */+ protected $signature = 'bookstack:clear-activity';++ /**+ * The console command description.+ *+ * @var string+ */+ protected $description = 'Clear user activity from the system';++ protected $activity;++ /**+ * Create a new command instance.+ *+ * @param Activity $activity+ */+ public function __construct(Activity $activity)+ {+ $this->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.phpnew file mode 100644index 000000000..f0c8a5e85--- /dev/null+++ b/app/Console/Commands/ClearRevisions.php@@ -0,0 +1,50 @@+<?php++namespace BookStack\Console\Commands;++use BookStack\PageRevision;+use Illuminate\Console\Command;++class ClearRevisions extends Command+{+ /**+ * The name and signature of the console command.+ *+ * @var string+ */+ protected $signature = 'bookstack:clear-revisions+ {--a|all : Include active update drafts in deletion}+ ';++ /**+ * The console command description.+ *+ * @var string+ */+ protected $description = 'Clear page revisions';++ protected $pageRevision;++ /**+ * Create a new command instance.+ *+ * @param PageRevision $pageRevision+ */+ public function __construct(PageRevision $pageRevision)+ {+ $this->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.phpsimilarity index 74%rename from app/Console/Commands/ResetViews.phprename to app/Console/Commands/ClearViews.phpindex 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 Commandpublic function handle(){\Views::resetAll();+ $this->comment('Views cleared');}}diff --git a/app/Console/Commands/Inspire.php b/app/Console/Commands/Inspire.phpdeleted file mode 100644index 4b115cfb9..000000000--- a/app/Console/Commands/Inspire.php+++ /dev/null@@ -1,33 +0,0 @@-<?php--namespace BookStack\Console\Commands;--use Illuminate\Console\Command;-use Illuminate\Foundation\Inspiring;--class Inspire extends Command-{- /**- * The name and signature of the console command.- *- * @var string- */- protected $signature = 'inspire';-- /**- * The console command description.- *- * @var string- */- protected $description = 'Display an inspiring quote';-- /**- * Execute the console command.- *- * @return mixed- */- public function handle()- {- $this->comment(PHP_EOL.Inspiring::quote().PHP_EOL);- }-}diff --git a/app/Console/Commands/RegeneratePermissions.php b/app/Console/Commands/RegeneratePermissions.phpindex 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 Commandpublic function handle(){$this->permissionService->buildJointPermissions();+ $this->comment('Permissions regenerated');}}diff --git a/app/Console/Kernel.php b/app/Console/Kernel.phpindex 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.phpnew file mode 100644index 000000000..5df82ee51--- /dev/null+++ b/tests/CommandsTest.php@@ -0,0 +1,102 @@+<?php namespace Tests;++use BookStack\JointPermission;+use BookStack\Page;+use BookStack\Repos\EntityRepo;++class CommandsTest extends TestCase+{++ public function test_clear_views_command()+ {+ $this->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' => '<p>new content</p>', 'summary' => 'page revision testing']);+ $entityRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '<p>new content in draft</p>', '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]);+ }+}