]> BookStack Code Mirror - bookstack/blobdiff - tests/TestCase.php
Added crude example of captcha usage
[bookstack] / tests / TestCase.php
index 94751b0047843369a6a729b06fc7911456b1ead5..939a1a91e8e8adf51a3a131196b8defda0e28fa9 100644 (file)
@@ -1,10 +1,5 @@
 <?php namespace Tests;
 
-use BookStack\Book;
-use BookStack\Chapter;
-use BookStack\Repos\EntityRepo;
-use BookStack\Role;
-use BookStack\Services\SettingService;
 use Illuminate\Foundation\Testing\DatabaseTransactions;
 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
 
@@ -12,9 +7,7 @@ abstract class TestCase extends BaseTestCase
 {
     use CreatesApplication;
     use DatabaseTransactions;
-
-    protected $admin;
-    protected $editor;
+    use SharedTestHelpers;
 
     /**
      * The base URL to use while testing the application.
@@ -23,88 +16,48 @@ abstract class TestCase extends BaseTestCase
     protected $baseUrl = 'http://localhost';
 
     /**
-     * Set the current user context to be an admin.
-     * @return $this
+     * Assert a permission error has occurred.
+     * @param TestResponse $response
+     * @return TestCase
      */
-    public function asAdmin()
+    protected function assertPermissionError(TestResponse $response)
     {
-        return $this->actingAs($this->getAdmin());
-    }
-
-    /**
-     * Get the current admin user.
-     * @return mixed
-     */
-    public function getAdmin() {
-        if($this->admin === null) {
-            $adminRole = Role::getSystemRole('admin');
-            $this->admin = $adminRole->users->first();
-        }
-        return $this->admin;
+        $response->assertRedirect('/');
+        $this->assertSessionHas('error');
+        session()->remove('error');
+        return $this;
     }
 
     /**
-     * Set the current user context to be an editor.
+     * Assert the session contains a specific entry.
+     * @param string $key
      * @return $this
      */
-    public function asEditor()
+    protected function assertSessionHas(string $key)
     {
-        return $this->actingAs($this->getEditor());
-    }
-
-
-    /**
-     * Get a editor user.
-     * @return mixed
-     */
-    public function getEditor() {
-        if($this->editor === null) {
-            $editorRole = Role::getRole('editor');
-            $this->editor = $editorRole->users->first();
-        }
-        return $this->editor;
-    }
-
-    /**
-     * Create and return a new book.
-     * @param array $input
-     * @return Book
-     */
-    public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
-        return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
+        $this->assertTrue(session()->has($key), "Session does not contain a [{$key}] entry");
+        return $this;
     }
 
     /**
-     * Create and return a new test chapter
-     * @param array $input
-     * @param Book $book
-     * @return Chapter
+     * Override of the get method so we can get visibility of custom TestResponse methods.
+     * @param  string  $uri
+     * @param  array  $headers
+     * @return TestResponse
      */
-    public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
-        return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
-    }
-
-    /**
-     * Create and return a new test page
-     * @param array $input
-     * @return Chapter
-     */
-    public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) {
-        $book = Book::first();
-        $entityRepo = $this->app[EntityRepo::class];
-        $draftPage = $entityRepo->getDraftPage($book);
-        return $entityRepo->publishPageDraft($draftPage, $input);
+    public function get($uri, array $headers = [])
+    {
+        return parent::get($uri, $headers);
     }
 
     /**
-     * Quickly sets an array of settings.
-     * @param $settingsArray
+     * Create the test response instance from the given response.
+     *
+     * @param  \Illuminate\Http\Response $response
+     * @return TestResponse
      */
-    protected function setSettings($settingsArray)
+    protected function createTestResponse($response)
     {
-        $settings = app(SettingService::class);
-        foreach ($settingsArray as $key => $value) {
-            $settings->put($key, $value);
-        }
+        return TestResponse::fromBaseResponse($response);
     }
 }
\ No newline at end of file