From 631304abc2c515b8b9cd231d70cb6e123b11742b Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 13 Jan 2016 20:11:39 +1100 Subject: [PATCH] Tests for model delete event cascading --- .../database/tester/assets/images/avatar.png | Bin 0 -> 1505 bytes .../plugins/database/tester/models/Author.php | 13 ++- .../plugins/database/tester/models/Post.php | 2 +- .../plugins/database/tester/models/User.php | 57 ++++++++++ .../tester/updates/create_authors_table.php | 2 + .../tester/updates/create_users_table.php | 27 +++++ .../database/tester/updates/version.yaml | 1 + .../plugins/database/AttachManyModelTest.php | 53 ++++++++++ .../plugins/database/AttachOneModelTest.php | 65 ++++++++++++ .../plugins/database/SoftDeleteModelTest.php | 98 ++++++++++++++++++ 10 files changed, 314 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/plugins/database/tester/assets/images/avatar.png create mode 100644 tests/fixtures/plugins/database/tester/models/User.php create mode 100644 tests/fixtures/plugins/database/tester/updates/create_users_table.php create mode 100644 tests/unit/plugins/database/AttachManyModelTest.php create mode 100644 tests/unit/plugins/database/AttachOneModelTest.php create mode 100644 tests/unit/plugins/database/SoftDeleteModelTest.php diff --git a/tests/fixtures/plugins/database/tester/assets/images/avatar.png b/tests/fixtures/plugins/database/tester/assets/images/avatar.png new file mode 100644 index 0000000000000000000000000000000000000000..99109125a21cdefe7b04ae2f5d4d1f429a43956b GIT binary patch literal 1505 zcmeAS@N?(olHy`uVBq!ia0vp^79h;Q1SHj;tnma=k|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*9U+n3Xd_B1$5BeXNr6bM+EIYV;~{3xK*A7;Nk-3KEmEQ%e+* zQqwc@Y?a>c-mj#PnPRIHZt82`Ti~3Uk?B!Ylp0*+7m{3+ootz+WN)WnQ(*-(AUCxn zQK2F?C$HG5!d3}vt`(3C64qBz04piUwpD^SD#ABF!8yMuRl!uxR5#hc$WX!DQqR!T z#M01EN5ROz&{*HlK;Otx*U-?)#N5izOaTg%fVLH-q*(>I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%!YY?y^>|E(EssLDS4NK2{C`hK;|z VI$V;}-UBLhJzf1=);T3K0RYZHOMd_W literal 0 HcmV?d00001 diff --git a/tests/fixtures/plugins/database/tester/models/Author.php b/tests/fixtures/plugins/database/tester/models/Author.php index e1b757d55..3f1c6d2a2 100644 --- a/tests/fixtures/plugins/database/tester/models/Author.php +++ b/tests/fixtures/plugins/database/tester/models/Author.php @@ -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; } \ No newline at end of file diff --git a/tests/fixtures/plugins/database/tester/models/Post.php b/tests/fixtures/plugins/database/tester/models/Post.php index 3dc11afa5..f3bde4026 100644 --- a/tests/fixtures/plugins/database/tester/models/Post.php +++ b/tests/fixtures/plugins/database/tester/models/Post.php @@ -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 diff --git a/tests/fixtures/plugins/database/tester/models/User.php b/tests/fixtures/plugins/database/tester/models/User.php new file mode 100644 index 000000000..b22916357 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/models/User.php @@ -0,0 +1,57 @@ + '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; +} \ No newline at end of file diff --git a/tests/fixtures/plugins/database/tester/updates/create_authors_table.php b/tests/fixtures/plugins/database/tester/updates/create_authors_table.php index ccc15e383..bbed9c7b7 100644 --- a/tests/fixtures/plugins/database/tester/updates/create_authors_table.php +++ b/tests/fixtures/plugins/database/tester/updates/create_authors_table.php @@ -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(); }); } diff --git a/tests/fixtures/plugins/database/tester/updates/create_users_table.php b/tests/fixtures/plugins/database/tester/updates/create_users_table.php new file mode 100644 index 000000000..1025138c7 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/updates/create_users_table.php @@ -0,0 +1,27 @@ +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'); + } + +} diff --git a/tests/fixtures/plugins/database/tester/updates/version.yaml b/tests/fixtures/plugins/database/tester/updates/version.yaml index 3897c3a8c..29c6b2ee8 100644 --- a/tests/fixtures/plugins/database/tester/updates/version.yaml +++ b/tests/fixtures/plugins/database/tester/updates/version.yaml @@ -6,3 +6,4 @@ - create_phones_table.php - create_categories_table.php - create_roles_table.php + - create_users_table.php diff --git a/tests/unit/plugins/database/AttachManyModelTest.php b/tests/unit/plugins/database/AttachManyModelTest.php new file mode 100644 index 000000000..79c03c443 --- /dev/null +++ b/tests/unit/plugins/database/AttachManyModelTest.php @@ -0,0 +1,53 @@ +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)); + } +} diff --git a/tests/unit/plugins/database/AttachOneModelTest.php b/tests/unit/plugins/database/AttachOneModelTest.php new file mode 100644 index 000000000..a72b44f75 --- /dev/null +++ b/tests/unit/plugins/database/AttachOneModelTest.php @@ -0,0 +1,65 @@ +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)); + } +} diff --git a/tests/unit/plugins/database/SoftDeleteModelTest.php b/tests/unit/plugins/database/SoftDeleteModelTest.php new file mode 100644 index 000000000..4051204cd --- /dev/null +++ b/tests/unit/plugins/database/SoftDeleteModelTest.php @@ -0,0 +1,98 @@ +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)); + } + +} \ No newline at end of file