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 000000000..99109125a Binary files /dev/null and b/tests/fixtures/plugins/database/tester/assets/images/avatar.png differ 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