public function test_deleting_topic_soft_deletes_its_posts() { // GIVEN we have a topic with post $topic = factory(Topic::class, 1)->create(); factory(Post::class, 20)->create(['topic_id' => $topic->id]); // we have 1 topic with 20 posts $this->assertEquals(Topic::all()->count(), 1); $this->assertEquals(Post::where('topic_id', $topic->id)->count(), 20); // WHEN we archive the topic $topic->delete(); // THEN we have no topics and no posts $this->assertEquals(Topic::all()->count(), 0); $this->assertEquals(Post::all()->count(), 0); // BUT we have 1 trashed topic and 20 trashed posts $this->assertEquals(Topic::withTrashed()->count(), 1); $this->assertEquals(Post::withTrashed()->where('topic_id', $topic->id)->count(), 20); }
public function test_deleting_section_soft_deletes_its_topics() { // GIVEN we have a user $user = factory(User::class, 1)->create(); // AND we have a section $section = factory(Section::class, 1)->create(['parent_id' => null, 'user_id' => $user->id]); // AND that section has topics factory(Topic::class, 10)->create(['section_id' => $section->id]); $topicsInSectionCount = $section->topics->count(); $topicCount = Topic::all()->count(); // WHEN we delete that section $section->delete(); // THEN the total number of topics is reduced by the number of topics // belonging to the original section $topicCountAfterDeletion = Topic::all()->count(); $this->assertEquals($topicCount - $topicsInSectionCount, $topicCountAfterDeletion); // AND the count of topics for that section is now zero $this->assertEquals(Topic::where('section_id', $section->id)->count(), 0); // BUT that section has soft deleted topics with match the orignal count $this->assertEquals(Topic::withTrashed()->where('section_id', $section->id)->count(), $topicsInSectionCount); }