3 use Illuminate\Foundation\Testing\DatabaseTransactions;
4 use Symfony\Component\DomCrawler\Crawler;
6 class TestCase extends Illuminate\Foundation\Testing\TestCase
9 use DatabaseTransactions;
12 * The base URL to use while testing the application.
16 protected $baseUrl = 'http://localhost';
18 // Local user instances
23 * Creates the application.
25 * @return \Illuminate\Foundation\Application
27 public function createApplication()
29 $app = require __DIR__.'/../bootstrap/app.php';
31 $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
37 * Set the current user context to be an admin.
40 public function asAdmin()
42 if($this->admin === null) {
43 $adminRole = \BookStack\Role::getRole('admin');
44 $this->admin = $adminRole->users->first();
46 return $this->actingAs($this->admin);
50 * Set the current editor context to be an editor.
53 public function asEditor()
55 if($this->editor === null) {
56 $this->editor = $this->getEditor();
58 return $this->actingAs($this->editor);
62 * Quickly sets an array of settings.
63 * @param $settingsArray
65 protected function setSettings($settingsArray)
67 $settings = app('BookStack\Services\SettingService');
68 foreach ($settingsArray as $key => $value) {
69 $settings->put($key, $value);
74 * Create a group of entities that belong to a specific user.
79 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
81 if ($updaterUser === false) $updaterUser = $creatorUser;
82 $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
83 $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
84 $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
85 $book->chapters()->saveMany([$chapter]);
86 $chapter->pages()->saveMany([$page]);
87 $restrictionService = $this->app[\BookStack\Services\PermissionService::class];
88 $restrictionService->buildJointPermissionsForEntity($book);
91 'chapter' => $chapter,
97 * Quick way to create a new user
98 * @param array $attributes
101 protected function getEditor($attributes = [])
103 $user = factory(\BookStack\User::class)->create($attributes);
104 $role = \BookStack\Role::getRole('editor');
105 $user->attachRole($role);;
110 * Quick way to create a new user without any permissions
111 * @param array $attributes
114 protected function getNewBlankUser($attributes = [])
116 $user = factory(\BookStack\User::class)->create($attributes);
121 * Assert that a given string is seen inside an element.
123 * @param bool|string|null $element
124 * @param integer $position
125 * @param string $text
126 * @param bool $negate
129 protected function seeInNthElement($element, $position, $text, $negate = false)
131 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
133 $rawPattern = preg_quote($text, '/');
135 $escapedPattern = preg_quote(e($text), '/');
137 $content = $this->crawler->filter($element)->eq($position)->html();
139 $pattern = $rawPattern == $escapedPattern
140 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
142 $this->$method("/$pattern/i", $content);
148 * Assert that the current page matches a given URI.
153 protected function seePageUrlIs($uri)
156 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
163 * Do a forced visit that does not error out on exception.
165 * @param array $parameters
166 * @param array $cookies
167 * @param array $files
170 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
173 $uri = $this->prepareUrlForRequest($uri);
174 $this->call($method, $uri, $parameters, $cookies, $files);
175 $this->clearInputs()->followRedirects();
176 $this->currentUri = $this->app->make('request')->fullUrl();
177 $this->crawler = new Crawler($this->response->getContent(), $uri);
182 * Click the text within the selected element.
183 * @param $parentElement
187 protected function clickInElement($parentElement, $linkText)
189 $elem = $this->crawler->filter($parentElement);
190 $link = $elem->selectLink($linkText);
191 $this->visit($link->link()->getUri());
196 * Check if the page contains the given element.
197 * @param string $selector
200 protected function pageHasElement($selector)
202 $elements = $this->crawler->filter($selector);
203 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
208 * Check if the page contains the given element.
209 * @param string $selector
212 protected function pageNotHasElement($selector)
214 $elements = $this->crawler->filter($selector);
215 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);