]> BookStack Code Mirror - bookstack/blobdiff - tests/TestCase.php
Finished migration of last angular code
[bookstack] / tests / TestCase.php
index 3f7d846f7b02c57cacc59718e733ce7569d91456..81bd93ec4de41fdea805824cf22980a5ea785531 100644 (file)
@@ -1,40 +1,93 @@
-<?php
+<?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;
 
-class TestCase extends Illuminate\Foundation\Testing\TestCase
+abstract class TestCase extends BaseTestCase
 {
-
+    use CreatesApplication;
     use DatabaseTransactions;
 
+    protected $admin;
+    protected $editor;
+
     /**
-     * The base URL to use while testing the application.
-     *
-     * @var string
+     * Set the current user context to be an admin.
+     * @return $this
      */
-    protected $baseUrl = 'http://localhost';
-    private $admin;
+    public function asAdmin()
+    {
+        return $this->actingAs($this->getAdmin());
+    }
 
     /**
-     * Creates the application.
-     *
-     * @return \Illuminate\Foundation\Application
+     * Get the current admin user.
+     * @return mixed
      */
-    public function createApplication()
+    public function getAdmin() {
+        if($this->admin === null) {
+            $adminRole = Role::getSystemRole('admin');
+            $this->admin = $adminRole->users->first();
+        }
+        return $this->admin;
+    }
+
+    /**
+     * Set the current user context to be an editor.
+     * @return $this
+     */
+    public function asEditor()
     {
-        $app = require __DIR__.'/../bootstrap/app.php';
+        return $this->actingAs($this->getEditor());
+    }
 
-        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
 
-        return $app;
+    /**
+     * 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;
     }
 
-    public function asAdmin()
-    {
-        if($this->admin === null) {
-            $this->admin = \BookStack\User::find(1);
-        }
-        return $this->actingAs($this->admin);
+    /**
+     * 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);
+    }
+
+    /**
+     * Create and return a new test chapter
+     * @param array $input
+     * @param Book $book
+     * @return Chapter
+     */
+    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);
     }
 
     /**
@@ -43,9 +96,9 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
      */
     protected function setSettings($settingsArray)
     {
-        $settings = app('BookStack\Services\SettingService');
+        $settings = app(SettingService::class);
         foreach ($settingsArray as $key => $value) {
             $settings->put($key, $value);
         }
     }
-}
+}
\ No newline at end of file