3 use BookStack\Entities\Entity;
4 use BookStack\Auth\Role;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Settings\SettingService;
7 use Illuminate\Contracts\Console\Kernel;
8 use Illuminate\Foundation\Testing\DatabaseTransactions;
9 use Laravel\BrowserKitTesting\TestCase;
10 use Symfony\Component\DomCrawler\Crawler;
12 abstract class BrowserKitTest extends TestCase
15 use DatabaseTransactions;
16 use SharedTestHelpers;
19 * The base URL to use while testing the application.
22 protected $baseUrl = 'http://localhost';
24 public function tearDown()
31 * Creates the application.
33 * @return \Illuminate\Foundation\Application
35 public function createApplication()
37 $app = require __DIR__.'/../bootstrap/app.php';
39 $app->make(Kernel::class)->bootstrap();
46 * Get a user that's not a system user such as the guest user.
48 public function getNormalUser()
50 return \BookStack\Auth\User::where('system_name', '=', null)->get()->last();
54 * Quickly sets an array of settings.
55 * @param $settingsArray
57 protected function setSettings($settingsArray)
59 $settings = app(SettingService::class);
60 foreach ($settingsArray as $key => $value) {
61 $settings->put($key, $value);
66 * Create a group of entities that belong to a specific user.
71 protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
73 if ($updaterUser === false) $updaterUser = $creatorUser;
74 $book = factory(\BookStack\Entities\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
75 $chapter = factory(\BookStack\Entities\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
76 $page = factory(\BookStack\Entities\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id, 'chapter_id' => $chapter->id]);
77 $restrictionService = $this->app[PermissionService::class];
78 $restrictionService->buildJointPermissionsForEntity($book);
81 'chapter' => $chapter,
87 * Helper for updating entity permissions.
88 * @param Entity $entity
90 protected function updateEntityPermissions(Entity $entity)
92 $restrictionService = $this->app[PermissionService::class];
93 $restrictionService->buildJointPermissionsForEntity($entity);
98 * Quick way to create a new user without any permissions
99 * @param array $attributes
102 protected function getNewBlankUser($attributes = [])
104 $user = factory(\BookStack\Auth\User::class)->create($attributes);
109 * Assert that a given string is seen inside an element.
111 * @param bool|string|null $element
112 * @param integer $position
113 * @param string $text
114 * @param bool $negate
117 protected function seeInNthElement($element, $position, $text, $negate = false)
119 $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
121 $rawPattern = preg_quote($text, '/');
123 $escapedPattern = preg_quote(e($text), '/');
125 $content = $this->crawler->filter($element)->eq($position)->html();
127 $pattern = $rawPattern == $escapedPattern
128 ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
130 $this->$method("/$pattern/i", $content);
136 * Assert that the current page matches a given URI.
141 protected function seePageUrlIs($uri)
144 $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
151 * Do a forced visit that does not error out on exception.
153 * @param array $parameters
154 * @param array $cookies
155 * @param array $files
158 protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
161 $uri = $this->prepareUrlForRequest($uri);
162 $this->call($method, $uri, $parameters, $cookies, $files);
163 $this->clearInputs()->followRedirects();
164 $this->currentUri = $this->app->make('request')->fullUrl();
165 $this->crawler = new Crawler($this->response->getContent(), $uri);
170 * Click the text within the selected element.
171 * @param $parentElement
175 protected function clickInElement($parentElement, $linkText)
177 $elem = $this->crawler->filter($parentElement);
178 $link = $elem->selectLink($linkText);
179 $this->visit($link->link()->getUri());
184 * Check if the page contains the given element.
185 * @param string $selector
187 protected function pageHasElement($selector)
189 $elements = $this->crawler->filter($selector);
190 $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
195 * Check if the page contains the given element.
196 * @param string $selector
198 protected function pageNotHasElement($selector)
200 $elements = $this->crawler->filter($selector);
201 $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);