]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
#47 - Fixes the issues with the test case.
[bookstack] / tests / TestCase.php
1 <?php namespace Tests;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Role;
7 use Illuminate\Foundation\Testing\DatabaseTransactions;
8 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
9
10 abstract class TestCase extends BaseTestCase
11 {
12     use CreatesApplication;
13     use DatabaseTransactions;
14
15     protected $admin;
16     protected $editor;
17
18     /**
19      * Set the current user context to be an admin.
20      * @return $this
21      */
22     public function asAdmin()
23     {
24         return $this->actingAs($this->getAdmin());
25     }
26
27     /**
28      * Get the current admin user.
29      * @return mixed
30      */
31     public function getAdmin() {
32         if($this->admin === null) {
33             $adminRole = Role::getSystemRole('admin');
34             $this->admin = $adminRole->users->first();
35         }
36         return $this->admin;
37     }
38
39     /**
40      * Set the current user context to be an editor.
41      * @return $this
42      */
43     public function asEditor()
44     {
45         return $this->actingAs($this->getEditor());
46     }
47
48
49     /**
50      * Get a editor user.
51      * @return mixed
52      */
53     public function getEditor() {
54         if($this->editor === null) {
55             $editorRole = Role::getRole('editor');
56             $this->editor = $editorRole->users->first();
57         }
58         return $this->editor;
59     }
60
61     /**
62      * Create and return a new book.
63      * @param array $input
64      * @return Book
65      */
66     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
67         return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
68     }
69
70     /**
71      * Create and return a new test chapter
72      * @param array $input
73      * @param Book $book
74      * @return Chapter
75      */
76     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
77         return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
78     }
79
80     /**
81      * Create and return a new test page
82      * @param array $input
83      * @return Chapter
84      */
85     public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
86         $book = Book::first();
87         $entityRepo = $this->app[EntityRepo::class];
88         $draftPage = $entityRepo->getDraftPage($book);
89         return $entityRepo->publishPageDraft($draftPage, $input);
90     }
91 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.