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';
20 * Creates the application.
22 * @return \Illuminate\Foundation\Application
24 public function createApplication()
26 $app = require __DIR__.'/../bootstrap/app.php';
28 $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
33 public function asAdmin()
35 if($this->admin === null) {
36 $adminRole = \BookStack\Role::getRole('admin');
37 $this->admin = $adminRole->users->first();
39 return $this->actingAs($this->admin);
43 * Quickly sets an array of settings.
44 * @param $settingsArray
46 protected function setSettings($settingsArray)
48 $settings = app('BookStack\Services\SettingService');
49 foreach ($settingsArray as $key => $value) {
50 $settings->put($key, $value);
55 * Create a group of entities that belong to a specific user.
60 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
62 if ($updaterUser === false) $updaterUser = $creatorUser;
63 $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
64 $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
65 $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
66 $book->chapters()->saveMany([$chapter]);
67 $chapter->pages()->saveMany([$page]);
70 'chapter' => $chapter,
76 * Quick way to create a new user
77 * @param array $attributes
80 protected function getNewUser($attributes = [])
82 $user = factory(\BookStack\User::class)->create($attributes);
83 $role = \BookStack\Role::getRole('editor');
84 $user->attachRole($role);;
89 * Quick way to create a new user without any permissions
90 * @param array $attributes
93 protected function getNewBlankUser($attributes = [])
95 $user = factory(\BookStack\User::class)->create($attributes);
100 * Assert that a given string is seen inside an element.
102 * @param bool|string|null $element
103 * @param integer $position
104 * @param string $text
105 * @param bool $negate
108 protected function seeInNthElement($element, $position, $text, $negate = false)
110 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
112 $rawPattern = preg_quote($text, '/');
114 $escapedPattern = preg_quote(e($text), '/');
116 $content = $this->crawler->filter($element)->eq($position)->html();
118 $pattern = $rawPattern == $escapedPattern
119 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
121 $this->$method("/$pattern/i", $content);
127 * Assert that the current page matches a given URI.
132 protected function seePageUrlIs($uri)
135 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
142 * Do a forced visit that does not error out on exception.
144 * @param array $parameters
145 * @param array $cookies
146 * @param array $files
149 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
152 $uri = $this->prepareUrlForRequest($uri);
153 $this->call($method, $uri, $parameters, $cookies, $files);
154 $this->clearInputs()->followRedirects();
155 $this->currentUri = $this->app->make('request')->fullUrl();
156 $this->crawler = new Crawler($this->response->getContent(), $uri);
161 * Click the text within the selected element.
162 * @param $parentElement
166 protected function clickInElement($parentElement, $linkText)
168 $elem = $this->crawler->filter($parentElement);
169 $link = $elem->selectLink($linkText);
170 $this->visit($link->link()->getUri());
175 * Check if the page contains the given element.
176 * @param string $selector
179 protected function pageHasElement($selector)
181 $elements = $this->crawler->filter($selector);
182 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
187 * Check if the page contains the given element.
188 * @param string $selector
191 protected function pageNotHasElement($selector)
193 $elements = $this->crawler->filter($selector);
194 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);