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.
This commit is contained in:
Dan Brown 2017-02-26 09:14:18 +00:00
parent 22077d4181
commit 0abed1afe5
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
7 changed files with 209 additions and 41 deletions

View File

@ -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');
}
}

View File

@ -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');
}
}

View 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');
}
}

View File

@ -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);
}
}

View 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');
}
}

View 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();
//
}
}

102
tests/CommandsTest.php Normal file
View File

@ -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]);
}
}