3 use Illuminate\Foundation\Testing\DatabaseTransactions;
5 class TestCase extends Illuminate\Foundation\Testing\TestCase
8 use DatabaseTransactions;
11 * The base URL to use while testing the application.
15 protected $baseUrl = 'http://localhost';
19 * Creates the application.
21 * @return \Illuminate\Foundation\Application
23 public function createApplication()
25 $app = require __DIR__.'/../bootstrap/app.php';
27 $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
32 public function asAdmin()
34 if($this->admin === null) {
35 $adminRole = \BookStack\Role::getRole('admin');
36 $this->admin = $adminRole->users->first();
38 return $this->actingAs($this->admin);
42 * Quickly sets an array of settings.
43 * @param $settingsArray
45 protected function setSettings($settingsArray)
47 $settings = app('BookStack\Services\SettingService');
48 foreach ($settingsArray as $key => $value) {
49 $settings->put($key, $value);
54 * Create a group of entities that belong to a specific user.
59 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
61 if ($updaterUser === false) $updaterUser = $creatorUser;
62 $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
63 $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
64 $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
65 $book->chapters()->saveMany([$chapter]);
66 $chapter->pages()->saveMany([$page]);
69 'chapter' => $chapter,
75 * Quick way to create a new user
76 * @param array $attributes
79 protected function getNewUser($attributes = [])
81 $user = factory(\BookStack\User::class)->create($attributes);
82 $role = \BookStack\Role::getRole('editor');
83 $user->attachRole($role);;
88 * Quick way to create a new user without any permissions
89 * @param array $attributes
92 protected function getNewBlankUser($attributes = [])
94 $user = factory(\BookStack\User::class)->create($attributes);
99 * Assert that a given string is seen inside an element.
101 * @param bool|string|null $element
102 * @param integer $position
103 * @param string $text
104 * @param bool $negate
107 protected function seeInNthElement($element, $position, $text, $negate = false)
109 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
111 $rawPattern = preg_quote($text, '/');
113 $escapedPattern = preg_quote(e($text), '/');
115 $content = $this->crawler->filter($element)->eq($position)->html();
117 $pattern = $rawPattern == $escapedPattern
118 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
120 $this->$method("/$pattern/i", $content);
126 * Click the text within the selected element.
127 * @param $parentElement
131 protected function clickInElement($parentElement, $linkText)
133 $elem = $this->crawler->filter($parentElement);
134 $link = $elem->selectLink($linkText);
135 $this->visit($link->link()->getUri());