5 use BookStack\Repos\EntityRepo;
7 use BookStack\Services\SettingService;
8 use Illuminate\Foundation\Testing\DatabaseTransactions;
9 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
11 abstract class TestCase extends BaseTestCase
13 use CreatesApplication;
14 use DatabaseTransactions;
20 * The base URL to use while testing the application.
23 protected $baseUrl = 'http://localhost';
26 * Set the current user context to be an admin.
29 public function asAdmin()
31 return $this->actingAs($this->getAdmin());
35 * Get the current admin user.
38 public function getAdmin() {
39 if($this->admin === null) {
40 $adminRole = Role::getSystemRole('admin');
41 $this->admin = $adminRole->users->first();
47 * Set the current user context to be an editor.
50 public function asEditor()
52 return $this->actingAs($this->getEditor());
60 public function getEditor() {
61 if($this->editor === null) {
62 $editorRole = Role::getRole('editor');
63 $this->editor = $editorRole->users->first();
69 * Get an instance of a user with 'viewer' permissions
73 protected function getViewer($attributes = [])
75 $user = \BookStack\Role::getRole('viewer')->users()->first();
76 if (!empty($attributes)) $user->forceFill($attributes)->save();
81 * Create and return a new book.
85 public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
86 return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
90 * Create and return a new test chapter
95 public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
96 return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
100 * Create and return a new test page
101 * @param array $input
104 public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
105 $book = Book::first();
106 $entityRepo = $this->app[EntityRepo::class];
107 $draftPage = $entityRepo->getDraftPage($book);
108 return $entityRepo->publishPageDraft($draftPage, $input);
112 * Quickly sets an array of settings.
113 * @param $settingsArray
115 protected function setSettings($settingsArray)
117 $settings = app(SettingService::class);
118 foreach ($settingsArray as $key => $value) {
119 $settings->put($key, $value);