Added testing to cover each command.
Removed example laravel inspire command.
Standardised command names to be behind 'bookstack' naming.
In reference to #320.
--- /dev/null
+<?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');
+ }
+}
--- /dev/null
+<?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');
+ }
+}
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.
public function handle()
{
\Views::resetAll();
+ $this->comment('Views cleared');
}
}
+++ /dev/null
-<?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);
- }
-}
*
* @var string
*/
- protected $signature = 'permissions:regen';
+ protected $signature = 'bookstack:regenerate-permissions';
/**
* The console command description.
public function handle()
{
$this->permissionService->buildJointPermissions();
+ $this->comment('Permissions regenerated');
}
}
* @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,
];
*/
protected function schedule(Schedule $schedule)
{
- $schedule->command('inspire')
- ->hourly();
+ //
}
}
--- /dev/null
+<?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]);
+ }
+}