]> BookStack Code Mirror - bookstack/blobdiff - tests/SharedTestHelpers.php
Added crude example of captcha usage
[bookstack] / tests / SharedTestHelpers.php
index 581dac5f1e954014f67c809e57dd4d0e7d42e87f..1d87e942aaf2167e10dbaed48f0a20f887511b94 100644 (file)
@@ -1,14 +1,17 @@
 <?php namespace Tests;
 
-use BookStack\Book;
-use BookStack\Bookshelf;
-use BookStack\Chapter;
-use BookStack\Entity;
-use BookStack\Repos\EntityRepo;
-use BookStack\Repos\PermissionsRepo;
-use BookStack\Role;
-use BookStack\Services\PermissionService;
-use BookStack\Services\SettingService;
+use BookStack\Entities\Book;
+use BookStack\Entities\Bookshelf;
+use BookStack\Entities\Chapter;
+use BookStack\Entities\Entity;
+use BookStack\Entities\Page;
+use BookStack\Entities\Repos\EntityRepo;
+use BookStack\Auth\Permissions\PermissionsRepo;
+use BookStack\Auth\Role;
+use BookStack\Auth\Permissions\PermissionService;
+use BookStack\Entities\Repos\PageRepo;
+use BookStack\Settings\SettingService;
+use BookStack\Uploads\HttpFetcher;
 
 trait SharedTestHelpers
 {
@@ -66,7 +69,7 @@ trait SharedTestHelpers
      */
     protected function getViewer($attributes = [])
     {
-        $user = \BookStack\Role::getRole('viewer')->users()->first();
+        $user = \BookStack\Auth\Role::getRole('viewer')->users()->first();
         if (!empty($attributes)) $user->forceFill($attributes)->save();
         return $user;
     }
@@ -77,17 +80,17 @@ trait SharedTestHelpers
      */
     protected function regenEntityPermissions(Entity $entity)
     {
-        $this->app[PermissionService::class]->buildJointPermissionsForEntity($entity);
+        app(PermissionService::class)->buildJointPermissionsForEntity($entity);
         $entity->load('jointPermissions');
     }
 
     /**
      * Create and return a new bookshelf.
      * @param array $input
-     * @return Bookshelf
+     * @return \BookStack\Entities\Bookshelf
      */
     public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) {
-        return $this->app[EntityRepo::class]->createFromInput('bookshelf', $input, false);
+        return app(EntityRepo::class)->createFromInput('bookshelf', $input, false);
     }
 
     /**
@@ -96,29 +99,29 @@ trait SharedTestHelpers
      * @return Book
      */
     public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) {
-        return $this->app[EntityRepo::class]->createFromInput('book', $input, false);
+        return app(EntityRepo::class)->createFromInput('book', $input, false);
     }
 
     /**
      * Create and return a new test chapter
      * @param array $input
      * @param Book $book
-     * @return Chapter
+     * @return \BookStack\Entities\Chapter
      */
     public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) {
-        return $this->app[EntityRepo::class]->createFromInput('chapter', $input, $book);
+        return app(EntityRepo::class)->createFromInput('chapter', $input, $book);
     }
 
     /**
      * Create and return a new test page
      * @param array $input
-     * @return Chapter
+     * @return Page
      */
     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);
+        $pageRepo = app(PageRepo::class);
+        $draftPage = $pageRepo->getDraftPage($book);
+        return $pageRepo->publishPageDraft($draftPage, $input);
     }
 
     /**
@@ -163,10 +166,10 @@ trait SharedTestHelpers
 
     /**
      * Give the given user some permissions.
-     * @param \BookStack\User $user
+     * @param \BookStack\Auth\User $user
      * @param array $permissions
      */
-    protected function giveUserPermissions(\BookStack\User $user, $permissions = [])
+    protected function giveUserPermissions(\BookStack\Auth\User $user, $permissions = [])
     {
         $newRole = $this->createNewRole($permissions);
         $user->attachRole($newRole);
@@ -187,4 +190,18 @@ trait SharedTestHelpers
         return $permissionRepo->saveNewRole($roleData);
     }
 
+    /**
+     * Mock the HttpFetcher service and return the given data on fetch.
+     * @param $returnData
+     * @param int $times
+     */
+    protected function mockHttpFetch($returnData, int $times = 1)
+    {
+        $mockHttp = \Mockery::mock(HttpFetcher::class);
+        $this->app[HttpFetcher::class] = $mockHttp;
+        $mockHttp->shouldReceive('fetch')
+            ->times($times)
+            ->andReturn($returnData);
+    }
+
 }
\ No newline at end of file