BookStack/tests/Api/ChaptersApiTest.php

233 lines
7.8 KiB
PHP
Raw Normal View History

2021-06-26 17:23:15 +02:00
<?php
namespace Tests\Api;
2020-05-23 01:28:41 +02:00
use BookStack\Entities\Models\Chapter;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
2020-05-23 01:28:41 +02:00
use Tests\TestCase;
class ChaptersApiTest extends TestCase
{
use TestsApi;
protected string $baseEndpoint = '/api/chapters';
2020-05-23 01:28:41 +02:00
public function test_index_endpoint_returns_expected_chapter()
{
$this->actingAsApiEditor();
$firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
$resp->assertJson(['data' => [
[
2021-06-26 17:23:15 +02:00
'id' => $firstChapter->id,
'name' => $firstChapter->name,
'slug' => $firstChapter->slug,
'book_id' => $firstChapter->book->id,
2020-05-23 01:28:41 +02:00
'priority' => $firstChapter->priority,
2021-06-26 17:23:15 +02:00
],
2020-05-23 01:28:41 +02:00
]]);
}
public function test_create_endpoint()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
2020-05-23 01:28:41 +02:00
$details = [
2021-06-26 17:23:15 +02:00
'name' => 'My API chapter',
2020-05-23 01:28:41 +02:00
'description' => 'A chapter created via the API',
2021-06-26 17:23:15 +02:00
'book_id' => $book->id,
'tags' => [
2020-05-23 01:28:41 +02:00
[
2021-06-26 17:23:15 +02:00
'name' => 'tagname',
2020-05-23 01:28:41 +02:00
'value' => 'tagvalue',
2021-06-26 17:23:15 +02:00
],
],
2020-05-23 01:28:41 +02:00
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(200);
$newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
$resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
$this->assertDatabaseHas('tags', [
2021-06-26 17:23:15 +02:00
'entity_id' => $newItem->id,
2020-05-23 01:28:41 +02:00
'entity_type' => $newItem->getMorphClass(),
2021-06-26 17:23:15 +02:00
'name' => 'tagname',
'value' => 'tagvalue',
2020-05-23 01:28:41 +02:00
]);
$resp->assertJsonMissing(['pages' => []]);
$this->assertActivityExists('chapter_create', $newItem);
}
public function test_chapter_name_needed_to_create()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
2020-05-23 01:28:41 +02:00
$details = [
2021-06-26 17:23:15 +02:00
'book_id' => $book->id,
2020-05-23 01:28:41 +02:00
'description' => 'A chapter created via the API',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse([
2021-06-26 17:23:15 +02:00
'name' => ['The name field is required.'],
2020-05-23 01:28:41 +02:00
]));
}
public function test_chapter_book_id_needed_to_create()
{
$this->actingAsApiEditor();
$details = [
2021-06-26 17:23:15 +02:00
'name' => 'My api chapter',
2020-05-23 01:28:41 +02:00
'description' => 'A chapter created via the API',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse([
2021-06-26 17:23:15 +02:00
'book_id' => ['The book id field is required.'],
2020-05-23 01:28:41 +02:00
]));
}
public function test_read_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
2020-05-23 01:28:41 +02:00
$page = $chapter->pages()->first();
$resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
$resp->assertStatus(200);
$resp->assertJson([
2021-06-26 17:23:15 +02:00
'id' => $chapter->id,
'slug' => $chapter->slug,
2020-05-23 01:28:41 +02:00
'created_by' => [
'name' => $chapter->createdBy->name,
],
2021-06-26 17:23:15 +02:00
'book_id' => $chapter->book_id,
2020-05-23 01:28:41 +02:00
'updated_by' => [
'name' => $chapter->createdBy->name,
],
'owned_by' => [
2021-06-26 17:23:15 +02:00
'name' => $chapter->ownedBy->name,
],
2020-05-23 01:28:41 +02:00
'pages' => [
[
2021-06-26 17:23:15 +02:00
'id' => $page->id,
2020-05-23 01:28:41 +02:00
'slug' => $page->slug,
'name' => $page->name,
2021-06-26 17:23:15 +02:00
],
2020-05-23 01:28:41 +02:00
],
]);
$resp->assertJsonCount($chapter->pages()->count(), 'pages');
}
public function test_update_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
2020-05-23 01:28:41 +02:00
$details = [
2021-06-26 17:23:15 +02:00
'name' => 'My updated API chapter',
2020-05-23 01:28:41 +02:00
'description' => 'A chapter created via the API',
2021-06-26 17:23:15 +02:00
'tags' => [
2020-05-23 01:28:41 +02:00
[
2021-06-26 17:23:15 +02:00
'name' => 'freshtag',
2020-05-23 01:28:41 +02:00
'value' => 'freshtagval',
2021-06-26 17:23:15 +02:00
],
2020-05-23 01:28:41 +02:00
],
];
$resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
$chapter->refresh();
$resp->assertStatus(200);
$resp->assertJson(array_merge($details, [
2021-06-26 17:23:15 +02:00
'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
2020-05-23 01:28:41 +02:00
]));
$this->assertActivityExists('chapter_update', $chapter);
}
public function test_update_increments_updated_date_if_only_tags_are_sent()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
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());
}
2020-05-23 01:28:41 +02:00
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
2020-05-23 01:28:41 +02:00
$resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
$resp->assertStatus(204);
$this->assertActivityExists('chapter_delete');
}
public function test_export_html_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
2020-05-23 01:28:41 +02:00
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
$resp->assertStatus(200);
$resp->assertSee($chapter->name);
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
}
public function test_export_plain_text_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
2020-05-23 01:28:41 +02:00
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
$resp->assertStatus(200);
$resp->assertSee($chapter->name);
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
}
public function test_export_pdf_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
2020-05-23 01:28:41 +02:00
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
$resp->assertStatus(200);
$resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
}
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->permissions->removeUserRolePermissions($this->users->editor(), ['content-export']);
$chapter = Chapter::visible()->has('pages')->first();
foreach ($types as $type) {
$resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/{$type}");
$this->assertPermissionError($resp);
}
}
2021-06-26 17:23:15 +02:00
}