]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Added basic system tests for markdown editor, Added extra test helpers
[bookstack] / tests / TestCase.php
1 <?php
2
3 use Illuminate\Foundation\Testing\DatabaseTransactions;
4 use Symfony\Component\DomCrawler\Crawler;
5
6 class TestCase extends Illuminate\Foundation\Testing\TestCase
7 {
8
9     use DatabaseTransactions;
10
11     /**
12      * The base URL to use while testing the application.
13      *
14      * @var string
15      */
16     protected $baseUrl = 'http://localhost';
17     private $admin;
18
19     /**
20      * Creates the application.
21      *
22      * @return \Illuminate\Foundation\Application
23      */
24     public function createApplication()
25     {
26         $app = require __DIR__.'/../bootstrap/app.php';
27
28         $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
29
30         return $app;
31     }
32
33     public function asAdmin()
34     {
35         if($this->admin === null) {
36             $adminRole = \BookStack\Role::getRole('admin');
37             $this->admin = $adminRole->users->first();
38         }
39         return $this->actingAs($this->admin);
40     }
41
42     /**
43      * Quickly sets an array of settings.
44      * @param $settingsArray
45      */
46     protected function setSettings($settingsArray)
47     {
48         $settings = app('BookStack\Services\SettingService');
49         foreach ($settingsArray as $key => $value) {
50             $settings->put($key, $value);
51         }
52     }
53
54     /**
55      * Create a group of entities that belong to a specific user.
56      * @param $creatorUser
57      * @param $updaterUser
58      * @return array
59      */
60     protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
61     {
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]);
68         return [
69             'book' => $book,
70             'chapter' => $chapter,
71             'page' => $page
72         ];
73     }
74
75     /**
76      * Quick way to create a new user
77      * @param array $attributes
78      * @return mixed
79      */
80     protected function getNewUser($attributes = [])
81     {
82         $user = factory(\BookStack\User::class)->create($attributes);
83         $role = \BookStack\Role::getRole('editor');
84         $user->attachRole($role);;
85         return $user;
86     }
87
88     /**
89      * Quick way to create a new user without any permissions
90      * @param array $attributes
91      * @return mixed
92      */
93     protected function getNewBlankUser($attributes = [])
94     {
95         $user = factory(\BookStack\User::class)->create($attributes);
96         return $user;
97     }
98
99     /**
100      * Assert that a given string is seen inside an element.
101      *
102      * @param  bool|string|null $element
103      * @param  integer          $position
104      * @param  string           $text
105      * @param  bool             $negate
106      * @return $this
107      */
108     protected function seeInNthElement($element, $position, $text, $negate = false)
109     {
110         $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
111
112         $rawPattern = preg_quote($text, '/');
113
114         $escapedPattern = preg_quote(e($text), '/');
115
116         $content = $this->crawler->filter($element)->eq($position)->html();
117
118         $pattern = $rawPattern == $escapedPattern
119             ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
120
121         $this->$method("/$pattern/i", $content);
122
123         return $this;
124     }
125
126     /**
127      * Assert that the current page matches a given URI.
128      *
129      * @param  string  $uri
130      * @return $this
131      */
132     protected function seePageUrlIs($uri)
133     {
134         $this->assertEquals(
135             $uri, $this->currentUri, "Did not land on expected page [{$uri}].\n"
136         );
137
138         return $this;
139     }
140
141     /**
142      * Do a forced visit that does not error out on exception.
143      * @param string $uri
144      * @param array $parameters
145      * @param array $cookies
146      * @param array $files
147      * @return $this
148      */
149     protected function forceVisit($uri, $parameters = [], $cookies = [], $files = [])
150     {
151         $method = 'GET';
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);
157         return $this;
158     }
159
160     /**
161      * Click the text within the selected element.
162      * @param $parentElement
163      * @param $linkText
164      * @return $this
165      */
166     protected function clickInElement($parentElement, $linkText)
167     {
168         $elem = $this->crawler->filter($parentElement);
169         $link = $elem->selectLink($linkText);
170         $this->visit($link->link()->getUri());
171         return $this;
172     }
173
174     /**
175      * Check if the page contains the given element.
176      * @param  string  $selector
177      * @return bool
178      */
179     protected function pageHasElement($selector)
180     {
181         $elements = $this->crawler->filter($selector);
182         $this->assertTrue(count($elements) > 0, "The page does not contain an element matching " . $selector);
183         return $this;
184     }
185
186     /**
187      * Check if the page contains the given element.
188      * @param  string  $selector
189      * @return bool
190      */
191     protected function pageNotHasElement($selector)
192     {
193         $elements = $this->crawler->filter($selector);
194         $this->assertFalse(count($elements) > 0, "The page contains " . count($elements) . " elements matching " . $selector);
195         return $this;
196     }
197 }