/** * a topic can only be restored by * the moderator of the section and destination section * * @return bool */ public function authorize() { $return = false; $currentUserID = \Auth::user()->id; $trashedTopic = \Nexus\Topic::onlyTrashed()->findOrFail($this->topic); $originalSection = \Nexus\Section::withTrashed()->findOrFail($trashedTopic->section_id); $destinationSection = \Nexus\Section::findOrFail($this->destination); if ($destinationSection->moderator->id === $currentUserID && $originalSection->moderator->id === $currentUserID) { $return = true; } else { $return = false; } return $return; }
public function test_deleting_section_soft_deletes_its_subsections() { // given we have a user with a section and that sub section $user = factory(User::class, 1)->create(); // AND we have a section $section = factory(Section::class, 1)->create(['parent_id' => null, 'user_id' => $user->id]); // with subsections factory(Section::class, 6)->create(['parent_id' => $section->id, 'user_id' => $user->id]); $subsectionCount = Section::where('parent_id', $section->id)->count(); // when we delete that section $section->delete(); // then section and subsections are soft deleted $this->assertTrue($section->trashed()); // we have no subsections $this->assertEquals(Section::where('parent_id', $section->id)->count(), 0); // we have the right amount of soft deleted subsections $this->assertEquals(Section::withTrashed()->where('parent_id', $section->id)->count(), $subsectionCount); }
public function test_restoreSectionToSection_restores_a_section() { // given we have a section with topics $number_of_topics = 10; $section = factory(Section::class, 1)->create(); $section_id = $section->id; factory(Topic::class, $number_of_topics)->create(['section_id' => $section_id]); // and we have another section $anotherSection = factory(Section::class, 1)->create(); // and we delete the section $section->delete(); // the section is deleted $this->assertTrue($section->trashed()); // and so are the topics $this->assertEquals(Section::withTrashed()->find($section_id)->topics->count(), 0); $this->assertEquals(Section::withTrashed()->find($section_id)->trashedTopics->count(), $number_of_topics); // when the section is restored to another section \Nexus\Helpers\RestoreHelper::restoreSectionToSection($section, $anotherSection); // then the section is not trashed $this->assertFalse($section->trashed()); // and neither are the topics $this->assertEquals(Section::find($section_id)->topics->count(), $number_of_topics); $this->assertEquals(Section::find($section_id)->trashedTopics->count(), 0); // and its parent is the other section $this->assertEquals($section->parent_id, $anotherSection->id); }