Write tests for revisionable trait

This commit is contained in:
Samuel Georges 2015-09-19 05:36:58 +10:00
parent d9090b3210
commit 3f7aa9f9f7
8 changed files with 259 additions and 37 deletions

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DbSystemRevisions extends Migration
{
public function up()
{
Schema::create('system_revisions', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable()->index();
$table->string('field')->nullable()->index();
$table->string('cast')->nullable();
$table->text('old_value')->nullable();
$table->text('new_value')->nullable();
$table->string('revisionable_type');
$table->integer('revisionable_id');
$table->timestamps();
$table->index(['revisionable_id', 'revisionable_type']);
});
}
public function down()
{
Schema::dropIfExists('system_revisions');
}
}

View File

@ -0,0 +1,17 @@
<?php namespace System\Models;
use October\Rain\Database\Models\Revision as RevisionBase;
/**
* Revision history model
*
* @package october\system
* @author Alexey Bobkov, Samuel Georges
*/
class Revision extends RevisionBase
{
/**
* @var string The database table used by the model.
*/
public $table = 'system_revisions';
}

View File

@ -2,9 +2,6 @@
use Model;
/**
* Author Model
*/
class Author extends Model
{

View File

@ -2,9 +2,6 @@
use Model;
/**
* Post Model
*/
class Post extends Model
{
@ -30,4 +27,73 @@ class Post extends Model
'author' => 'Database\Tester\Models\Author',
];
}
class SluggablePost extends Post
{
use \October\Rain\Database\Traits\Sluggable;
/**
* @var array Guarded fields
*/
protected $guarded = [];
/**
* @var array List of attributes to automatically generate unique URL names (slugs) for.
*/
protected $slugs = [
'slug' => 'title'
];
}
class RevisionablePost extends Post
{
use \October\Rain\Database\Traits\Revisionable;
use \October\Rain\Database\Traits\SoftDeleting;
/**
* @var array Guarded fields
*/
protected $guarded = [];
/**
* @var array Dates
*/
protected $dates = ['published_at', 'deleted_at'];
/**
* @var array Monitor these attributes for changes.
*/
protected $revisionable = [
'title',
'slug',
'description',
'is_published',
'published_at',
'deleted_at'
];
/**
* @var int Maximum number of revision records to keep.
*/
public $revisionableLimit = 8;
/**
* @var array Relations
*/
public $morphMany = [
'revision_history' => ['System\Models\Revision', 'name' => 'revisionable']
];
/**
* The user who made the revision.
*/
public function getRevisionableUser()
{
return 7;
}
}

View File

@ -1,30 +0,0 @@
<?php namespace Database\Tester\Models;
use Model;
/**
* Post Model
*/
class SluggablePost extends Model
{
use \October\Rain\Database\Traits\Sluggable;
/**
* @var string The database table used by the model.
*/
public $table = 'database_tester_posts';
/**
* @var array Guarded fields
*/
protected $guarded = [];
/**
* @var array List of attributes to automatically generate unique URL names (slugs) for.
*/
protected $slugs = [
'slug' => 'title'
];
}

View File

@ -18,6 +18,7 @@ class CreatePostsTable extends Migration
$table->boolean('is_published')->default(false);
$table->timestamp('published_at')->nullable();
$table->integer('author_id')->unsigned()->index()->nullable();
$table->softDeletes();
$table->timestamps();
});
}

View File

@ -0,0 +1,141 @@
<?php
use Carbon\Carbon;
use Database\Tester\Models\RevisionablePost;
class RevisionableModelTest extends PluginTestCase
{
public function setUp()
{
parent::setUp();
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Post.php';
$this->runPluginRefreshCommand('Database.Tester');
}
public function testUpdateNothing()
{
$post = RevisionablePost::create(['title' => 'Hello World!']);
$this->assertEquals('Hello World!', $post->title);
$this->assertEquals(0, $post->revision_history()->count());
$post->revisionsEnabled = false;
$post->title = 'Helloooooooooooo!';
$post->save();
$this->assertEquals(0, $post->revision_history()->count());
}
public function testUpdateSingleField()
{
$post = RevisionablePost::create(['title' => 'Hello World!']);
$this->assertEquals('Hello World!', $post->title);
$this->assertEquals(0, $post->revision_history()->count());
$post->title = 'Helloooooooooooo!';
$post->save();
$this->assertEquals(1, $post->revision_history()->count());
$item = $post->revision_history()->first();
$this->assertEquals('title', $item->field);
$this->assertEquals('Hello World!', $item->old_value);
$this->assertEquals('Helloooooooooooo!', $item->new_value);
$this->assertEquals(7, $item->user_id);
}
public function testUpdateMultipleFields()
{
$post = RevisionablePost::create([
'title' => 'Hello World!',
'slug' => 'hello-world',
'description' => 'Good day, Commander',
'is_published' => true,
'published_at' => new DateTime
]);
$this->assertEquals(0, $post->revision_history()->count());
$post->title = "Gday mate";
$post->slug = "gday-mate";
$post->description = 'Wazzaaaaaaaaaaaaap';
$post->is_published = false;
$post->published_at = Carbon::now()->addDays(1);
$post->save();
$history = $post->revision_history;
$this->assertEquals(5, $history->count());
$this->assertEquals([
'title',
'slug',
'description',
'is_published',
'published_at'
], $history->lists('field'));
}
public function testExceedingRevisionLimit()
{
$post = RevisionablePost::create([
'title' => 'Hello World!',
'slug' => 'hello-world',
'description' => 'Good day, Commander',
'is_published' => true,
'published_at' => new DateTime
]);
$this->assertEquals(0, $post->revision_history()->count());
$post->title = "Gday mate";
$post->slug = "gday-mate";
$post->description = 'Wazzaaaaaaaaaaaaap';
$post->is_published = false;
$post->published_at = Carbon::now()->addDays(1);
$post->save();
$post->title = 'The Boss';
$post->slug = 'the-boss';
$post->description = 'Paid the cost to be the boss';
$post->is_published = true;
$post->published_at = Carbon::now()->addDays(10);
$post->save();
// Count 10 changes above, limit is 8
$this->assertEquals(8, $post->revision_history()->count());
}
public function testSoftDeletes()
{
$post = RevisionablePost::create(['title' => 'Hello World!']);
$this->assertEquals('Hello World!', $post->title);
$this->assertEquals(0, $post->revision_history()->count());
$post->title = 'Helloooooooooooo!';
$post->save();
$this->assertEquals(1, $post->revision_history()->count());
$post->delete();
$this->assertEquals(2, $post->revision_history()->count());
}
public function testRevisionDateCast()
{
$post = RevisionablePost::create([
'title' => 'Hello World!',
'published_at' => Carbon::now()
]);
$this->assertEquals(0, $post->revision_history()->count());
$post->published_at = Carbon::now()->addDays(1);
$post->save();
$this->assertEquals(1, $post->revision_history()->count());
$item = $post->revision_history()->first();
$this->assertEquals('published_at', $item->field);
$this->assertEquals('date', $item->cast);
$this->assertInstanceOf('Carbon\Carbon', $item->old_value);
$this->assertInstanceOf('Carbon\Carbon', $item->new_value);
}
}

View File

@ -8,7 +8,7 @@ class SluggableModelTest extends PluginTestCase
{
parent::setUp();
include_once base_path().'/tests/fixtures/plugins/database/tester/models/SluggablePost.php';
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Post.php';
$this->runPluginRefreshCommand('Database.Tester');
}