diff --git a/src/Illuminate/Contracts/Database/Eloquent/Builder.php b/src/Illuminate/Contracts/Database/Eloquent/Builder.php new file mode 100644 index 000000000000..6fdf405c9954 --- /dev/null +++ b/src/Illuminate/Contracts/Database/Eloquent/Builder.php @@ -0,0 +1,14 @@ +increments('id'); $table->integer('user_id'); $table->string('title'); + $table->integer('priority')->default(0); $table->timestamps(); $table->softDeletes(); }); @@ -491,6 +492,109 @@ public function testHasManyRelationshipCanBeSoftDeleted() $this->assertCount(1, $abigail->posts()->withTrashed()->get()); } + public function testRelationToSqlAppliesSoftDelete() + { + $this->createUsers(); + + $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first(); + + $this->assertSame( + 'select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null and "posts"."deleted_at" is null', + $abigail->posts()->toSql() + ); + } + + public function testRelationExistsAndDoesntExistHonorsSoftDelete() + { + $this->createUsers(); + $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first(); + + // 'exists' should return true before soft delete + $abigail->posts()->create(['title' => 'First Title']); + $this->assertTrue($abigail->posts()->exists()); + $this->assertFalse($abigail->posts()->doesntExist()); + + // 'exists' should return false after soft delete + $abigail->posts()->first()->delete(); + $this->assertFalse($abigail->posts()->exists()); + $this->assertTrue($abigail->posts()->doesntExist()); + + // 'exists' should return true after restore + $abigail->posts()->withTrashed()->restore(); + $this->assertTrue($abigail->posts()->exists()); + $this->assertFalse($abigail->posts()->doesntExist()); + + // 'exists' should return false after a force delete + $abigail->posts()->first()->forceDelete(); + $this->assertFalse($abigail->posts()->exists()); + $this->assertTrue($abigail->posts()->doesntExist()); + } + + public function testRelationCountHonorsSoftDelete() + { + $this->createUsers(); + $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first(); + + // check count before soft delete + $abigail->posts()->create(['title' => 'First Title']); + $abigail->posts()->create(['title' => 'Second Title']); + $this->assertEquals(2, $abigail->posts()->count()); + + // check count after soft delete + $abigail->posts()->where('title', 'Second Title')->delete(); + $this->assertEquals(1, $abigail->posts()->count()); + + // check count after restore + $abigail->posts()->withTrashed()->restore(); + $this->assertEquals(2, $abigail->posts()->count()); + + // check count after a force delete + $abigail->posts()->where('title', 'Second Title')->forceDelete(); + $this->assertEquals(1, $abigail->posts()->count()); + } + + public function testRelationAggregatesHonorsSoftDelete() + { + $this->createUsers(); + $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first(); + + // check aggregates before soft delete + $abigail->posts()->create(['title' => 'First Title', 'priority' => 2]); + $abigail->posts()->create(['title' => 'Second Title', 'priority' => 4]); + $abigail->posts()->create(['title' => 'Third Title', 'priority' => 6]); + $this->assertEquals(2, $abigail->posts()->min('priority')); + $this->assertEquals(6, $abigail->posts()->max('priority')); + $this->assertEquals(12, $abigail->posts()->sum('priority')); + $this->assertEquals(4, $abigail->posts()->avg('priority')); + + // check aggregates after soft delete + $abigail->posts()->where('title', 'First Title')->delete(); + $this->assertEquals(4, $abigail->posts()->min('priority')); + $this->assertEquals(6, $abigail->posts()->max('priority')); + $this->assertEquals(10, $abigail->posts()->sum('priority')); + $this->assertEquals(5, $abigail->posts()->avg('priority')); + + // check aggregates after restore + $abigail->posts()->withTrashed()->restore(); + $this->assertEquals(2, $abigail->posts()->min('priority')); + $this->assertEquals(6, $abigail->posts()->max('priority')); + $this->assertEquals(12, $abigail->posts()->sum('priority')); + $this->assertEquals(4, $abigail->posts()->avg('priority')); + + // check aggregates after a force delete + $abigail->posts()->where('title', 'Third Title')->forceDelete(); + $this->assertEquals(2, $abigail->posts()->min('priority')); + $this->assertEquals(4, $abigail->posts()->max('priority')); + $this->assertEquals(6, $abigail->posts()->sum('priority')); + $this->assertEquals(3, $abigail->posts()->avg('priority')); + } + + public function testSoftDeleteIsAppliedToNewQuery() + { + $query = (new SoftDeletesTestUser)->newQuery(); + $this->assertSame('select * from "users" where "users"."deleted_at" is null', $query->toSql()); + } + public function testSecondLevelRelationshipCanBeSoftDeleted() { $this->createUsers();