]> BookStack Code Mirror - bookstack/blobdiff - tests/Entity/BookTest.php
New translations editor.php (Italian)
[bookstack] / tests / Entity / BookTest.php
index fa63c0bf98c1ef3bbf8865294685b05e45ca3d24..6b3c6aa388534e1d84fce61efdb04568efc8f56d 100644 (file)
@@ -3,13 +3,18 @@
 namespace Tests\Entity;
 
 use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\BookChild;
+use BookStack\Entities\Repos\BookRepo;
 use Tests\TestCase;
+use Tests\Uploads\UsesImages;
 
 class BookTest extends TestCase
 {
+    use UsesImages;
+
     public function test_create()
     {
-        $book = factory(Book::class)->make([
+        $book = Book::factory()->make([
             'name' => 'My First Book',
         ]);
 
@@ -29,7 +34,7 @@ class BookTest extends TestCase
 
     public function test_create_uses_different_slugs_when_name_reused()
     {
-        $book = factory(Book::class)->make([
+        $book = Book::factory()->make([
             'name' => 'My First Book',
         ]);
 
@@ -45,6 +50,33 @@ class BookTest extends TestCase
         $this->assertEquals('my-first-book', $books[1]->slug);
     }
 
+    public function test_create_sets_tags()
+    {
+        // Cheeky initial update to refresh slug
+        $this->asEditor()->post('books', [
+            'name'        => 'My book with tags',
+            'description' => 'A book with tags',
+            'tags'        => [
+                [
+                    'name'  => 'Category',
+                    'value' => 'Donkey Content',
+                ],
+                [
+                    'name'  => 'Level',
+                    'value' => '5',
+                ],
+            ],
+        ]);
+
+        /** @var Book $book */
+        $book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
+        $tags = $book->tags()->get();
+
+        $this->assertEquals(2, $tags->count());
+        $this->assertEquals('Donkey Content', $tags[0]->value);
+        $this->assertEquals('Level', $tags[1]->name);
+    }
+
     public function test_update()
     {
         /** @var Book $book */
@@ -69,6 +101,36 @@ class BookTest extends TestCase
         $resp->assertSee($newDesc);
     }
 
+    public function test_update_sets_tags()
+    {
+        /** @var Book $book */
+        $book = Book::query()->first();
+
+        $this->assertEquals(0, $book->tags()->count());
+
+        // Cheeky initial update to refresh slug
+        $this->asEditor()->put($book->getUrl(), [
+            'name' => $book->name,
+            'tags' => [
+                [
+                    'name'  => 'Category',
+                    'value' => 'Dolphin Content',
+                ],
+                [
+                    'name'  => 'Level',
+                    'value' => '5',
+                ],
+            ],
+        ]);
+
+        $book->refresh();
+        $tags = $book->tags()->get();
+
+        $this->assertEquals(2, $tags->count());
+        $this->assertEquals('Dolphin Content', $tags[0]->value);
+        $this->assertEquals('Level', $tags[1]->name);
+    }
+
     public function test_delete()
     {
         $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
@@ -187,7 +249,7 @@ class BookTest extends TestCase
             'name' => 'информация',
         ]);
 
-        $this->assertEquals('informatsiya', $book->slug);
+        $this->assertEquals('informaciya', $book->slug);
 
         $book = $this->newBook([
             'name' => '¿Qué?',
@@ -204,4 +266,89 @@ class BookTest extends TestCase
 
         $this->assertEquals('parta-partb-partc', $book->slug);
     }
+
+    public function test_show_view_has_copy_button()
+    {
+        /** @var Book $book */
+        $book = Book::query()->first();
+        $resp = $this->asEditor()->get($book->getUrl());
+
+        $resp->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
+    }
+
+    public function test_copy_view()
+    {
+        /** @var Book $book */
+        $book = Book::query()->first();
+        $resp = $this->asEditor()->get($book->getUrl('/copy'));
+
+        $resp->assertOk();
+        $resp->assertSee('Copy Book');
+        $resp->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
+    }
+
+    public function test_copy()
+    {
+        /** @var Book $book */
+        $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
+        $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
+
+        /** @var Book $copy */
+        $copy = Book::query()->where('name', '=', 'My copy book')->first();
+
+        $resp->assertRedirect($copy->getUrl());
+        $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
+    }
+
+    public function test_copy_does_not_copy_non_visible_content()
+    {
+        /** @var Book $book */
+        $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
+
+        // Hide child content
+        /** @var BookChild $page */
+        foreach ($book->getDirectChildren() as $child) {
+            $child->restricted = true;
+            $child->save();
+            $this->regenEntityPermissions($child);
+        }
+
+        $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
+        /** @var Book $copy */
+        $copy = Book::query()->where('name', '=', 'My copy book')->first();
+
+        $this->assertEquals(0, $copy->getDirectChildren()->count());
+    }
+
+    public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
+    {
+        /** @var Book $book */
+        $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
+        $viewer = $this->getViewer();
+        $this->giveUserPermissions($viewer, ['book-create-all']);
+
+        $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
+        /** @var Book $copy */
+        $copy = Book::query()->where('name', '=', 'My copy book')->first();
+
+        $this->assertEquals(0, $copy->pages()->count());
+        $this->assertEquals(0, $copy->chapters()->count());
+    }
+
+    public function test_copy_clones_cover_image_if_existing()
+    {
+        /** @var Book $book */
+        $book = Book::query()->first();
+        $bookRepo = $this->app->make(BookRepo::class);
+        $coverImageFile = $this->getTestImage('cover.png');
+        $bookRepo->updateCoverImage($book, $coverImageFile);
+
+        $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
+
+        /** @var Book $copy */
+        $copy = Book::query()->where('name', '=', 'My copy book')->first();
+
+        $this->assertNotNull($copy->cover);
+        $this->assertNotEquals($book->cover->id, $copy->cover->id);
+    }
 }