Tests for model delete event cascading
This commit is contained in:
parent
a3b338671d
commit
631304abc2
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
|
|
@ -18,13 +18,15 @@ class Author extends Model
|
|||
/**
|
||||
* @var array Relations
|
||||
*/
|
||||
public $belongsTo = [
|
||||
'user' => ['Database\Tester\Models\User', 'delete' => true],
|
||||
'user_soft' => ['Database\Tester\Models\SoftDeleteUser', 'key' => 'user_id', 'softDelete' => true],
|
||||
];
|
||||
|
||||
public $hasMany = [
|
||||
'posts' => 'Database\Tester\Models\Post',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array Relations
|
||||
*/
|
||||
public $hasOne = [
|
||||
'phone' => 'Database\Tester\Models\Phone',
|
||||
];
|
||||
|
|
@ -33,4 +35,9 @@ class Author extends Model
|
|||
'roles' => ['Database\Tester\Models\Role', 'table' => 'database_tester_authors_roles']
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
class SoftDeleteAuthor extends Author
|
||||
{
|
||||
use \October\Rain\Database\Traits\SoftDelete;
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ class RevisionablePost extends Post
|
|||
{
|
||||
|
||||
use \October\Rain\Database\Traits\Revisionable;
|
||||
use \October\Rain\Database\Traits\SoftDeleting;
|
||||
use \October\Rain\Database\Traits\SoftDelete;
|
||||
|
||||
/**
|
||||
* @var array Guarded fields
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
<?php namespace Database\Tester\Models;
|
||||
|
||||
use Model;
|
||||
|
||||
class User extends Model
|
||||
{
|
||||
/**
|
||||
* @var string The database table used by the model.
|
||||
*/
|
||||
public $table = 'database_tester_users';
|
||||
|
||||
/**
|
||||
* @var array Guarded fields
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* @var array Relations
|
||||
*/
|
||||
public $attachOne = [
|
||||
'avatar' => 'System\Models\File'
|
||||
];
|
||||
|
||||
public $attachMany = [
|
||||
'photos' => 'System\Models\File'
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
class SoftDeleteUser extends User
|
||||
{
|
||||
use \October\Rain\Database\Traits\SoftDelete;
|
||||
}
|
||||
|
||||
class UserWithAuthor extends User
|
||||
{
|
||||
public $hasOne = [
|
||||
'author' => ['Database\Tester\Models\Author', 'key' => 'user_id', 'delete' => true],
|
||||
];
|
||||
}
|
||||
|
||||
class UserWithSoftAuthor extends User
|
||||
{
|
||||
public $hasOne = [
|
||||
'author' => ['Database\Tester\Models\SoftDeleteAuthor', 'key' => 'user_id', 'softDelete' => true],
|
||||
];
|
||||
}
|
||||
|
||||
class UserWithAuthorAndSoftDelete extends UserWithAuthor
|
||||
{
|
||||
use \October\Rain\Database\Traits\SoftDelete;
|
||||
}
|
||||
|
||||
class UserWithSoftAuthorAndSoftDelete extends UserWithSoftAuthor
|
||||
{
|
||||
use \October\Rain\Database\Traits\SoftDelete;
|
||||
}
|
||||
|
|
@ -12,8 +12,10 @@ class CreateAuthorsTable extends Migration
|
|||
{
|
||||
$table->engine = 'InnoDB';
|
||||
$table->increments('id');
|
||||
$table->integer('user_id')->unsigned()->index()->nullable();
|
||||
$table->string('name')->nullable();
|
||||
$table->string('email')->nullable();
|
||||
$table->softDeletes();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php namespace Database\Tester\Updates;
|
||||
|
||||
use Schema;
|
||||
use October\Rain\Database\Updates\Migration;
|
||||
|
||||
class CreateUsersTable extends Migration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
Schema::create('database_tester_users', function($table)
|
||||
{
|
||||
$table->engine = 'InnoDB';
|
||||
$table->increments('id');
|
||||
$table->string('name')->nullable();
|
||||
$table->string('email')->nullable();
|
||||
$table->softDeletes();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('database_tester_users');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -6,3 +6,4 @@
|
|||
- create_phones_table.php
|
||||
- create_categories_table.php
|
||||
- create_roles_table.php
|
||||
- create_users_table.php
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
use System\Models\File as FileModel;
|
||||
use Database\Tester\Models\User;
|
||||
|
||||
class AttachManyModelTest extends PluginTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/User.php';
|
||||
|
||||
$this->runPluginRefreshCommand('Database.Tester');
|
||||
}
|
||||
|
||||
public function testDeleteFlagDestroyRelationship()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = User::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
Model::reguard();
|
||||
|
||||
$this->assertEmpty($user->photos);
|
||||
$user->photos()->create(['data' => base_path().'/tests/fixtures/plugins/database/tester/assets/images/avatar.png']);
|
||||
$user->reloadRelations();
|
||||
$this->assertNotEmpty($user->photos);
|
||||
|
||||
$photo = $user->photos->first();
|
||||
$photoId = $photo->id;
|
||||
|
||||
$user->photos()->remove($photo);
|
||||
$this->assertNull(FileModel::find($photoId));
|
||||
}
|
||||
|
||||
public function testDeleteFlagDeleteModel()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = User::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
Model::reguard();
|
||||
|
||||
$this->assertEmpty($user->photos);
|
||||
$user->photos()->create(['data' => base_path().'/tests/fixtures/plugins/database/tester/assets/images/avatar.png']);
|
||||
$user->reloadRelations();
|
||||
$this->assertNotEmpty($user->photos);
|
||||
|
||||
$photo = $user->photos->first();
|
||||
$this->assertNotNull($photo);
|
||||
$photoId = $photo->id;
|
||||
|
||||
$user->delete();
|
||||
$this->assertNull(FileModel::find($photoId));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
use System\Models\File as FileModel;
|
||||
use Database\Tester\Models\User;
|
||||
use Database\Tester\Models\SoftDeleteUser;
|
||||
|
||||
class AttachOneModelTest extends PluginTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/User.php';
|
||||
|
||||
$this->runPluginRefreshCommand('Database.Tester');
|
||||
}
|
||||
|
||||
public function testDeleteFlagDestroyRelationship()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = User::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
Model::reguard();
|
||||
|
||||
$this->assertNull($user->avatar);
|
||||
$user->avatar()->create(['data' => base_path().'/tests/fixtures/plugins/database/tester/assets/images/avatar.png']);
|
||||
$user->reloadRelations();
|
||||
$this->assertNotNull($user->avatar);
|
||||
|
||||
$avatar = $user->avatar;
|
||||
$avatarId = $avatar->id;
|
||||
|
||||
$user->avatar()->remove($avatar);
|
||||
$this->assertNull(FileModel::find($avatarId));
|
||||
}
|
||||
|
||||
public function testDeleteFlagDeleteModel()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = User::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
Model::reguard();
|
||||
|
||||
$this->assertNull($user->avatar);
|
||||
$user->avatar()->create(['data' => base_path().'/tests/fixtures/plugins/database/tester/assets/images/avatar.png']);
|
||||
$user->reloadRelations();
|
||||
$this->assertNotNull($user->avatar);
|
||||
|
||||
$avatarId = $user->avatar->id;
|
||||
$user->delete();
|
||||
$this->assertNull(FileModel::find($avatarId));
|
||||
}
|
||||
|
||||
public function testDeleteFlagSoftDeleteModel()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = SoftDeleteUser::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
Model::reguard();
|
||||
|
||||
$user->avatar()->create(['data' => base_path().'/tests/fixtures/plugins/database/tester/assets/images/avatar.png']);
|
||||
$this->assertNotNull($user->avatar);
|
||||
|
||||
$avatarId = $user->avatar->id;
|
||||
$user->delete();
|
||||
$this->assertNotNull(FileModel::find($avatarId));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
use Database\Tester\Models\User;
|
||||
use Database\Tester\Models\Author;
|
||||
use Database\Tester\Models\UserWithAuthor;
|
||||
use Database\Tester\Models\SoftDeleteAuthor;
|
||||
use Database\Tester\Models\UserWithSoftAuthor;
|
||||
use Database\Tester\Models\UserWithAuthorAndSoftDelete;
|
||||
use Database\Tester\Models\UserWithSoftAuthorAndSoftDelete;
|
||||
|
||||
class SoftDeleteModelTest extends PluginTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/User.php';
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Author.php';
|
||||
|
||||
$this->runPluginRefreshCommand('Database.Tester');
|
||||
}
|
||||
|
||||
public function testDeleteOptionOnHardModel()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = UserWithAuthor::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
$author = Author::create(['name' => 'Louie', 'email' => 'louie@email.tld', 'user_id' => $user->id]);
|
||||
Model::reguard();
|
||||
|
||||
$authorId = $author->id;
|
||||
$user->delete(); // Hard
|
||||
$this->assertNull(Author::find($authorId));
|
||||
}
|
||||
|
||||
public function testSoftDeleteOptionOnHardModel()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = UserWithSoftAuthor::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
$author = Author::create(['name' => 'Louie', 'email' => 'louie@email.tld', 'user_id' => $user->id]);
|
||||
Model::reguard();
|
||||
|
||||
$authorId = $author->id;
|
||||
$user->delete(); // Hard
|
||||
$this->assertNotNull(Author::find($authorId)); // Do nothing
|
||||
}
|
||||
|
||||
public function testSoftDeleteOptionOnSoftModel()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = UserWithSoftAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
$author = SoftDeleteAuthor::create(['name' => 'Louie', 'email' => 'louie@email.tld', 'user_id' => $user->id]);
|
||||
Model::reguard();
|
||||
|
||||
$authorId = $author->id;
|
||||
$user->delete(); // Soft
|
||||
$this->assertNull(SoftDeleteAuthor::find($authorId));
|
||||
$this->assertNotNull(SoftDeleteAuthor::withTrashed()->find($authorId));
|
||||
}
|
||||
|
||||
public function testDeleteOptionOnSoftModel()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = UserWithAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
$author = Author::create(['name' => 'Louie', 'email' => 'louie@email.tld', 'user_id' => $user->id]);
|
||||
Model::reguard();
|
||||
|
||||
$authorId = $author->id;
|
||||
$user->delete(); // Soft
|
||||
$this->assertNotNull(Author::find($authorId)); // Do nothing
|
||||
|
||||
$userId = $user->id;
|
||||
$user = UserWithAuthorAndSoftDelete::withTrashed()->find($userId);
|
||||
$user->restore();
|
||||
|
||||
$user->forceDelete(); // Hard
|
||||
$this->assertNull(Author::find($authorId));
|
||||
}
|
||||
|
||||
public function testRestoreSoftDeleteRelation()
|
||||
{
|
||||
Model::unguard();
|
||||
$user = UserWithSoftAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
$author = SoftDeleteAuthor::create(['name' => 'Louie', 'email' => 'louie@email.tld', 'user_id' => $user->id]);
|
||||
Model::reguard();
|
||||
|
||||
$authorId = $author->id;
|
||||
$user->delete(); // Soft
|
||||
$this->assertNull(SoftDeleteAuthor::find($authorId));
|
||||
$this->assertNotNull(SoftDeleteAuthor::withTrashed()->find($authorId));
|
||||
|
||||
$userId = $user->id;
|
||||
$user = UserWithSoftAuthorAndSoftDelete::withTrashed()->find($userId);
|
||||
$user->restore();
|
||||
|
||||
$this->assertNotNull(SoftDeleteAuthor::find($authorId));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue