]> BookStack Code Mirror - bookstack/blobdiff - tests/Entity/BookShelfTest.php
Update settings.php
[bookstack] / tests / Entity / BookShelfTest.php
index 5c7673847140eb71d284e14d94d92921f6b706c6..abee4d34a6e15e19b0f96c72f69be8f3c182e5d3 100644 (file)
@@ -1,14 +1,18 @@
-<?php namespace Tests;
+<?php namespace Tests\Entity;
 
-use BookStack\Auth\Role;
 use BookStack\Auth\User;
 use BookStack\Entities\Book;
 use BookStack\Entities\Bookshelf;
+use BookStack\Uploads\Image;
 use Illuminate\Support\Str;
+use Tests\TestCase;
+use Tests\Uploads\UsesImages;
 
 class BookShelfTest extends TestCase
 {
 
+    use UsesImages;
+
     public function test_shelves_shows_in_header_if_have_view_permissions()
     {
         $viewer = $this->getViewer();
@@ -83,6 +87,26 @@ class BookShelfTest extends TestCase
         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
     }
 
+    public function test_shelves_create_sets_cover_image()
+    {
+        $shelfInfo = [
+            'name' => 'My test book' . Str::random(4),
+            'description' => 'Test book description ' . Str::random(10)
+        ];
+
+        $imageFile = $this->getTestImage('shelf-test.png');
+        $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
+        $resp->assertRedirect();
+
+        $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
+        $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
+        $this->assertDatabaseHas('bookshelves', [
+            'id' => $shelf->id,
+            'image_id' => $lastImage->id,
+        ]);
+        $this->assertEquals($lastImage->id, $shelf->cover->id);
+    }
+
     public function test_shelf_view()
     {
         $shelf = Bookshelf::first();
@@ -240,4 +264,32 @@ class BookShelfTest extends TestCase
         $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
     }
 
+    public function test_bookshelves_show_on_book()
+    {
+        // Create shelf
+        $shelfInfo = [
+            'name' => 'My test shelf' . Str::random(4),
+            'description' => 'Test shelf description ' . Str::random(10)
+        ];
+
+        $this->asEditor()->post('/shelves', $shelfInfo);
+        $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
+
+        // Create book and add to shelf
+        $this->asEditor()->post($shelf->getUrl('/create-book'), [
+            'name' => 'Test book name',
+            'description' => 'Book in shelf description'
+        ]);
+
+        $newBook = Book::query()->orderBy('id', 'desc')->first();
+
+        $resp = $this->asEditor()->get($newBook->getUrl());
+        $resp->assertElementContains('.tri-layout-left-contents', $shelfInfo['name']);
+
+        // Remove shelf
+        $this->delete($shelf->getUrl());
+
+        $resp = $this->asEditor()->get($newBook->getUrl());
+        $resp->assertDontSee($shelfInfo['name']);
+    }
 }