]> BookStack Code Mirror - bookstack/blobdiff - tests/FavouriteTest.php
Guest create page: name field autofocus
[bookstack] / tests / FavouriteTest.php
index a0f11886e5a23760c1e9122f2a76071c7df43df9..456f2213cd464e18643718263bd65c93209f190c 100644 (file)
@@ -1,22 +1,20 @@
 <?php
 
+namespace Tests;
+
 use BookStack\Actions\Favourite;
-use BookStack\Entities\Models\Book;
-use BookStack\Entities\Models\Bookshelf;
-use BookStack\Entities\Models\Chapter;
-use BookStack\Entities\Models\Page;
-use Tests\TestCase;
+use BookStack\Auth\User;
 
 class FavouriteTest extends TestCase
 {
     public function test_page_add_favourite_flow()
     {
-        $page = Page::query()->first();
+        $page = $this->entities->page();
         $editor = $this->getEditor();
 
         $resp = $this->actingAs($editor)->get($page->getUrl());
-        $resp->assertElementContains('button', 'Favourite');
-        $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
+        $this->withHtml($resp)->assertElementContains('button', 'Favourite');
+        $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
 
         $resp = $this->post('/favourites/add', [
             'type' => get_class($page),
@@ -34,7 +32,7 @@ class FavouriteTest extends TestCase
 
     public function test_page_remove_favourite_flow()
     {
-        $page = Page::query()->first();
+        $page = $this->entities->page();
         $editor = $this->getEditor();
         Favourite::query()->forceCreate([
             'user_id'           => $editor->id,
@@ -43,8 +41,8 @@ class FavouriteTest extends TestCase
         ]);
 
         $resp = $this->actingAs($editor)->get($page->getUrl());
-        $resp->assertElementContains('button', 'Unfavourite');
-        $resp->assertElementExists('form[method="POST"][action$="/favourites/remove"]');
+        $this->withHtml($resp)->assertElementContains('button', 'Unfavourite');
+        $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/remove"]');
 
         $resp = $this->post('/favourites/remove', [
             'type' => get_class($page),
@@ -58,26 +56,46 @@ class FavouriteTest extends TestCase
         ]);
     }
 
-    public function test_book_chapter_shelf_pages_contain_favourite_button()
+    public function test_favourite_flow_with_own_permissions()
+    {
+        $book = $this->entities->book();
+        $user = User::factory()->create();
+        $book->owned_by = $user->id;
+        $book->save();
+
+        $this->giveUserPermissions($user, ['book-view-own']);
+
+        $this->actingAs($user)->get($book->getUrl());
+        $resp = $this->post('/favourites/add', [
+            'type' => get_class($book),
+            'id'   => $book->id,
+        ]);
+        $resp->assertRedirect($book->getUrl());
+
+        $this->assertDatabaseHas('favourites', [
+            'user_id'           => $user->id,
+            'favouritable_type' => $book->getMorphClass(),
+            'favouritable_id'   => $book->id,
+        ]);
+    }
+
+    public function test_each_entity_type_shows_favourite_button()
     {
-        $entities = [
-            Bookshelf::query()->first(),
-            Book::query()->first(),
-            Chapter::query()->first(),
-        ];
         $this->actingAs($this->getEditor());
 
-        foreach ($entities as $entity) {
+        foreach ($this->entities->all() as $entity) {
             $resp = $this->get($entity->getUrl());
-            $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
+            $this->withHtml($resp)->assertElementExists('form[method="POST"][action$="/favourites/add"]');
         }
     }
 
     public function test_header_contains_link_to_favourites_page_when_logged_in()
     {
         $this->setSettings(['app-public' => 'true']);
-        $this->get('/')->assertElementNotContains('header', 'My Favourites');
-        $this->actingAs($this->getViewer())->get('/')->assertElementContains('header a', 'My Favourites');
+        $resp = $this->get('/');
+        $this->withHtml($resp)->assertElementNotContains('header', 'My Favourites');
+        $resp = $this->actingAs($this->getViewer())->get('/');
+        $this->withHtml($resp)->assertElementContains('header a', 'My Favourites');
     }
 
     public function test_favourites_shown_on_homepage()
@@ -85,21 +103,19 @@ class FavouriteTest extends TestCase
         $editor = $this->getEditor();
 
         $resp = $this->actingAs($editor)->get('/');
-        $resp->assertElementNotExists('#top-favourites');
+        $this->withHtml($resp)->assertElementNotExists('#top-favourites');
 
-        /** @var Page $page */
-        $page = Page::query()->first();
+        $page = $this->entities->page();
         $page->favourites()->save((new Favourite())->forceFill(['user_id' => $editor->id]));
 
         $resp = $this->get('/');
-        $resp->assertElementExists('#top-favourites');
-        $resp->assertElementContains('#top-favourites', $page->name);
+        $this->withHtml($resp)->assertElementExists('#top-favourites');
+        $this->withHtml($resp)->assertElementContains('#top-favourites', $page->name);
     }
 
     public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
     {
-        /** @var Page $page */
-        $page = Page::query()->first();
+        $page = $this->entities->page();
         $editor = $this->getEditor();
 
         $resp = $this->actingAs($editor)->get('/favourites');