]> BookStack Code Mirror - bookstack/commitdiff
Added clear activity/revision commands. Cleaned commands.
authorDan Brown <redacted>
Sun, 26 Feb 2017 09:14:18 +0000 (09:14 +0000)
committerDan Brown <redacted>
Sun, 26 Feb 2017 09:16:24 +0000 (09:16 +0000)
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 [new file with mode: 0644]
app/Console/Commands/ClearRevisions.php [new file with mode: 0644]
app/Console/Commands/ClearViews.php [moved from app/Console/Commands/ResetViews.php with 74% similarity]
app/Console/Commands/Inspire.php [deleted file]
app/Console/Commands/RegeneratePermissions.php
app/Console/Kernel.php
tests/CommandsTest.php [new file with mode: 0644]

diff --git a/app/Console/Commands/ClearActivity.php b/app/Console/Commands/ClearActivity.php
new file mode 100644 (file)
index 0000000..66babd9
--- /dev/null
@@ -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.php
new file mode 100644 (file)
index 0000000..f0c8a5e
--- /dev/null
@@ -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');
+    }
+}
similarity index 74%
rename from app/Console/Commands/ResetViews.php
rename to app/Console/Commands/ClearViews.php
index 3a3903ff89c197fe1e63888db4c846381651b8f7..678c64d330179893880f707db7cd0f8a705bf297 100644 (file)
@@ -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 (file)
index 4b115cf..0000000
+++ /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);
-    }
-}
index 60d5f4e455ac8b4bc64cffd25effc585e261a4e4..966ee4a820841429148a19c3724003492909742b 100644 (file)
@@ -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');
     }
 }
index b725c9e217543f82f7bb3c46c80ac62e1e143517..0112e72caa9c825a95e11d8eb167ad88533537d3 100644 (file)
@@ -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 (file)
index 0000000..5df82ee
--- /dev/null
@@ -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]);
+    }
+}