X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/473261be35ab50e6c9bc5914c899a34cd6cccf57..refs/pull/598/head:/tests/TestCase.php diff --git a/tests/TestCase.php b/tests/TestCase.php index ce4e93b12..81bd93ec4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,127 +1,104 @@ -make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); - - return $app; + return $this->actingAs($this->getAdmin()); } - public function asAdmin() - { + /** + * Get the current admin user. + * @return mixed + */ + public function getAdmin() { if($this->admin === null) { - $adminRole = \BookStack\Role::getRole('admin'); + $adminRole = Role::getSystemRole('admin'); $this->admin = $adminRole->users->first(); } - return $this->actingAs($this->admin); + return $this->admin; } /** - * Quickly sets an array of settings. - * @param $settingsArray + * Set the current user context to be an editor. + * @return $this */ - protected function setSettings($settingsArray) + public function asEditor() { - $settings = app('BookStack\Services\SettingService'); - foreach ($settingsArray as $key => $value) { - $settings->put($key, $value); - } + return $this->actingAs($this->getEditor()); } + /** - * Create a group of entities that belong to a specific user. - * @param $creatorUser - * @param $updaterUser - * @return array + * Get a editor user. + * @return mixed */ - protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false) - { - if ($updaterUser === false) $updaterUser = $creatorUser; - $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); - $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); - $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]); - $book->chapters()->saveMany([$chapter]); - $chapter->pages()->saveMany([$page]); - return [ - 'book' => $book, - 'chapter' => $chapter, - 'page' => $page - ]; + public function getEditor() { + if($this->editor === null) { + $editorRole = Role::getRole('editor'); + $this->editor = $editorRole->users->first(); + } + return $this->editor; } /** - * Quick way to create a new user - * @param array $attributes - * @return mixed + * Create and return a new book. + * @param array $input + * @return Book */ - protected function getNewUser($attributes = []) - { - $user = factory(\BookStack\User::class)->create($attributes); - $userRepo = app('BookStack\Repos\UserRepo'); - $userRepo->attachDefaultRole($user); - return $user; + public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) { + return $this->app[EntityRepo::class]->createFromInput('book', $input, false); } /** - * Assert that a given string is seen inside an element. - * - * @param bool|string|null $element - * @param integer $position - * @param string $text - * @param bool $negate - * @return $this + * Create and return a new test chapter + * @param array $input + * @param Book $book + * @return Chapter */ - protected function seeInNthElement($element, $position, $text, $negate = false) - { - $method = $negate ? 'assertNotRegExp' : 'assertRegExp'; - - $rawPattern = preg_quote($text, '/'); - - $escapedPattern = preg_quote(e($text), '/'); - - $content = $this->crawler->filter($element)->eq($position)->html(); - - $pattern = $rawPattern == $escapedPattern - ? $rawPattern : "({$rawPattern}|{$escapedPattern})"; - - $this->$method("/$pattern/i", $content); + public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) { + return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book); + } - return $this; + /** + * Create and return a new test page + * @param array $input + * @return Chapter + */ + public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) { + $book = Book::first(); + $entityRepo = $this->app[EntityRepo::class]; + $draftPage = $entityRepo->getDraftPage($book); + return $entityRepo->publishPageDraft($draftPage, $input); } /** - * Click the text within the selected element. - * @param $parentElement - * @param $linkText - * @return $this + * Quickly sets an array of settings. + * @param $settingsArray */ - protected function clickInElement($parentElement, $linkText) + protected function setSettings($settingsArray) { - $elem = $this->crawler->filter($parentElement); - $link = $elem->selectLink($linkText); - $this->visit($link->link()->getUri()); - return $this; + $settings = app(SettingService::class); + foreach ($settingsArray as $key => $value) { + $settings->put($key, $value); + } } -} +} \ No newline at end of file