]> BookStack Code Mirror - bookstack/blobdiff - tests/Api/ChaptersApiTest.php
Fixed failing test after drawio default url change
[bookstack] / tests / Api / ChaptersApiTest.php
index c7368eaee1835209d64df5ce90642f864bf3131b..6f00f9eade1c8596c8719be837f38eed0f0bcac0 100644 (file)
@@ -1,14 +1,18 @@
-<?php namespace Tests\Api;
+<?php
+
+namespace Tests\Api;
 
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Chapter;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
 use Tests\TestCase;
 
 class ChaptersApiTest extends TestCase
 {
     use TestsApi;
 
-    protected $baseEndpoint = '/api/chapters';
+    protected string $baseEndpoint = '/api/chapters';
 
     public function test_index_endpoint_returns_expected_chapter()
     {
@@ -18,12 +22,12 @@ class ChaptersApiTest extends TestCase
         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
         $resp->assertJson(['data' => [
             [
-                'id' => $firstChapter->id,
-                'name' => $firstChapter->name,
-                'slug' => $firstChapter->slug,
-                'book_id' => $firstChapter->book->id,
+                'id'       => $firstChapter->id,
+                'name'     => $firstChapter->name,
+                'slug'     => $firstChapter->slug,
+                'book_id'  => $firstChapter->book->id,
                 'priority' => $firstChapter->priority,
-            ]
+            ],
         ]]);
     }
 
@@ -32,15 +36,15 @@ class ChaptersApiTest extends TestCase
         $this->actingAsApiEditor();
         $book = Book::query()->first();
         $details = [
-            'name' => 'My API chapter',
+            'name'        => 'My API chapter',
             'description' => 'A chapter created via the API',
-            'book_id' => $book->id,
-            'tags' => [
+            'book_id'     => $book->id,
+            'tags'        => [
                 [
-                    'name' => 'tagname',
+                    'name'  => 'tagname',
                     'value' => 'tagvalue',
-                ]
-            ]
+                ],
+            ],
         ];
 
         $resp = $this->postJson($this->baseEndpoint, $details);
@@ -48,10 +52,10 @@ class ChaptersApiTest extends TestCase
         $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
         $this->assertDatabaseHas('tags', [
-            'entity_id' => $newItem->id,
+            'entity_id'   => $newItem->id,
             'entity_type' => $newItem->getMorphClass(),
-            'name' => 'tagname',
-            'value' => 'tagvalue',
+            'name'        => 'tagname',
+            'value'       => 'tagvalue',
         ]);
         $resp->assertJsonMissing(['pages' => []]);
         $this->assertActivityExists('chapter_create', $newItem);
@@ -62,14 +66,14 @@ class ChaptersApiTest extends TestCase
         $this->actingAsApiEditor();
         $book = Book::query()->first();
         $details = [
-            'book_id' => $book->id,
+            'book_id'     => $book->id,
             'description' => 'A chapter created via the API',
         ];
 
         $resp = $this->postJson($this->baseEndpoint, $details);
         $resp->assertStatus(422);
         $resp->assertJson($this->validationResponse([
-            "name" => ["The name field is required."]
+            'name' => ['The name field is required.'],
         ]));
     }
 
@@ -77,14 +81,14 @@ class ChaptersApiTest extends TestCase
     {
         $this->actingAsApiEditor();
         $details = [
-            'name' => 'My api chapter',
+            'name'        => 'My api chapter',
             'description' => 'A chapter created via the API',
         ];
 
         $resp = $this->postJson($this->baseEndpoint, $details);
         $resp->assertStatus(422);
         $resp->assertJson($this->validationResponse([
-            "book_id" => ["The book id field is required."]
+            'book_id' => ['The book id field is required.'],
         ]));
     }
 
@@ -97,24 +101,24 @@ class ChaptersApiTest extends TestCase
         $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
         $resp->assertStatus(200);
         $resp->assertJson([
-            'id' => $chapter->id,
-            'slug' => $chapter->slug,
+            'id'         => $chapter->id,
+            'slug'       => $chapter->slug,
             'created_by' => [
                 'name' => $chapter->createdBy->name,
             ],
-            'book_id' => $chapter->book_id,
+            'book_id'    => $chapter->book_id,
             'updated_by' => [
                 'name' => $chapter->createdBy->name,
             ],
             'owned_by' => [
-                'name' => $chapter->ownedBy->name
+                'name' => $chapter->ownedBy->name,
             ],
             'pages' => [
                 [
-                    'id' => $page->id,
+                    'id'   => $page->id,
                     'slug' => $page->slug,
                     'name' => $page->name,
-                ]
+                ],
             ],
         ]);
         $resp->assertJsonCount($chapter->pages()->count(), 'pages');
@@ -125,13 +129,13 @@ class ChaptersApiTest extends TestCase
         $this->actingAsApiEditor();
         $chapter = Chapter::visible()->first();
         $details = [
-            'name' => 'My updated API chapter',
+            'name'        => 'My updated API chapter',
             'description' => 'A chapter created via the API',
-            'tags' => [
+            'tags'        => [
                 [
-                    'name' => 'freshtag',
+                    'name'  => 'freshtag',
                     'value' => 'freshtagval',
-                ]
+                ],
             ],
         ];
 
@@ -140,11 +144,26 @@ class ChaptersApiTest extends TestCase
 
         $resp->assertStatus(200);
         $resp->assertJson(array_merge($details, [
-            'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id
+            'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
         ]));
         $this->assertActivityExists('chapter_update', $chapter);
     }
 
+    public function test_update_increments_updated_date_if_only_tags_are_sent()
+    {
+        $this->actingAsApiEditor();
+        $chapter = Chapter::visible()->first();
+        DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+        $details = [
+            'tags' => [['name' => 'Category', 'value' => 'Testing']]
+        ];
+
+        $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
+        $chapter->refresh();
+        $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
+    }
+
     public function test_delete_endpoint()
     {
         $this->actingAsApiEditor();
@@ -186,4 +205,29 @@ class ChaptersApiTest extends TestCase
         $resp->assertStatus(200);
         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
     }
-}
\ No newline at end of file
+
+    public function test_export_markdown_endpoint()
+    {
+        $this->actingAsApiEditor();
+        $chapter = Chapter::visible()->has('pages')->first();
+
+        $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
+        $resp->assertStatus(200);
+        $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
+        $resp->assertSee('# ' . $chapter->name);
+        $resp->assertSee('# ' . $chapter->pages()->first()->name);
+    }
+
+    public function test_cant_export_when_not_have_permission()
+    {
+        $types = ['html', 'plaintext', 'pdf', 'markdown'];
+        $this->actingAsApiEditor();
+        $this->removePermissionFromUser($this->getEditor(), 'content-export');
+
+        $chapter = Chapter::visible()->has('pages')->first();
+        foreach ($types as $type) {
+            $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
+            $this->assertPermissionError($resp);
+        }
+    }
+}