]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Update PageRepo.php
[bookstack] / tests / TestCase.php
1 <?php
2
3 use Illuminate\Foundation\Testing\DatabaseTransactions;
4
5 class TestCase extends Illuminate\Foundation\Testing\TestCase
6 {
7
8     use DatabaseTransactions;
9
10     /**
11      * The base URL to use while testing the application.
12      *
13      * @var string
14      */
15     protected $baseUrl = 'http://localhost';
16     private $admin;
17
18     /**
19      * Creates the application.
20      *
21      * @return \Illuminate\Foundation\Application
22      */
23     public function createApplication()
24     {
25         $app = require __DIR__.'/../bootstrap/app.php';
26
27         $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
28
29         return $app;
30     }
31
32     public function asAdmin()
33     {
34         if($this->admin === null) {
35             $this->admin = \BookStack\User::find(1);
36         }
37         return $this->actingAs($this->admin);
38     }
39
40     /**
41      * Quickly sets an array of settings.
42      * @param $settingsArray
43      */
44     protected function setSettings($settingsArray)
45     {
46         $settings = app('BookStack\Services\SettingService');
47         foreach ($settingsArray as $key => $value) {
48             $settings->put($key, $value);
49         }
50     }
51
52     /**
53      * Create a group of entities that belong to a specific user.
54      * @param $creatorUser
55      * @param $updaterUser
56      * @return array
57      */
58     protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false)
59     {
60         if ($updaterUser === false) $updaterUser = $creatorUser;
61         $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
62         $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]);
63         $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]);
64         $book->chapters()->saveMany([$chapter]);
65         $chapter->pages()->saveMany([$page]);
66         return [
67             'book' => $book,
68             'chapter' => $chapter,
69             'page' => $page
70         ];
71     }
72
73     /**
74      * Quick way to create a new user
75      * @param array $attributes
76      * @return mixed
77      */
78     protected function getNewUser($attributes = [])
79     {
80         $user = factory(\BookStack\User::class)->create($attributes);
81         $userRepo = app('BookStack\Repos\UserRepo');
82         $userRepo->attachDefaultRole($user);
83         return $user;
84     }
85
86     /**
87      * Assert that a given string is seen inside an element.
88      *
89      * @param  bool|string|null $element
90      * @param  integer          $position
91      * @param  string           $text
92      * @param  bool             $negate
93      * @return $this
94      */
95     protected function seeInNthElement($element, $position, $text, $negate = false)
96     {
97         $method = $negate ? 'assertNotRegExp' : 'assertRegExp';
98
99         $rawPattern = preg_quote($text, '/');
100
101         $escapedPattern = preg_quote(e($text), '/');
102
103         $content = $this->crawler->filter($element)->eq($position)->html();
104
105         $pattern = $rawPattern == $escapedPattern
106             ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
107
108         $this->$method("/$pattern/i", $content);
109
110         return $this;
111     }
112
113     /**
114      * Click the text within the selected element.
115      * @param $parentElement
116      * @param $linkText
117      * @return $this
118      */
119     protected function clickInElement($parentElement, $linkText)
120     {
121         $elem = $this->crawler->filter($parentElement);
122         $link = $elem->selectLink($linkText);
123         $this->visit($link->link()->getUri());
124         return $this;
125     }
126 }