public function test_restoreTopicToSection_restores_a_topic_and_views() { // GIVEN I have a topic with posts in a section $section = factory(Section::class, 1)->create(); $topic = factory(Topic::class, 1)->create(['section_id' => $section->id]); factory(Post::class, 20)->create(['topic_id' => $topic->id]); $topic_id = $topic->id; // and a user reads that topic $user = factory(User::class, 1)->create(); \Nexus\Helpers\ViewHelper::updateReadProgress($user, $topic); $viewsCount = $topic->views->count(); $this->assertNotEquals($viewsCount, 0); // and then that topic is deleted $topic->delete(); // check topic deleted $this->assertTrue($topic->trashed()); // and that we have no views - need to use trashed here $this->assertEquals(Topic::withTrashed()->find($topic_id)->views->count(), 0); // WHEN the topic is restored to the section \Nexus\Helpers\RestoreHelper::restoreTopicToSection($topic, $section); // check posts and views are restored $this->assertEquals($topic->views->count(), $viewsCount); // THEN the topic is restored $this->assertFalse($topic->trashed()); }
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); }