1 <?php namespace Tests\Api;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Chapter;
7 class ChaptersApiTest extends TestCase
11 protected $baseEndpoint = '/api/chapters';
13 public function test_index_endpoint_returns_expected_chapter()
15 $this->actingAsApiEditor();
16 $firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
18 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
19 $resp->assertJson(['data' => [
21 'id' => $firstChapter->id,
22 'name' => $firstChapter->name,
23 'slug' => $firstChapter->slug,
24 'book_id' => $firstChapter->book->id,
25 'priority' => $firstChapter->priority,
30 public function test_create_endpoint()
32 $this->actingAsApiEditor();
33 $book = Book::query()->first();
35 'name' => 'My API chapter',
36 'description' => 'A chapter created via the API',
37 'book_id' => $book->id,
41 'value' => 'tagvalue',
46 $resp = $this->postJson($this->baseEndpoint, $details);
47 $resp->assertStatus(200);
48 $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
49 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
50 $this->assertDatabaseHas('tags', [
51 'entity_id' => $newItem->id,
52 'entity_type' => $newItem->getMorphClass(),
54 'value' => 'tagvalue',
56 $resp->assertJsonMissing(['pages' => []]);
57 $this->assertActivityExists('chapter_create', $newItem);
60 public function test_chapter_name_needed_to_create()
62 $this->actingAsApiEditor();
63 $book = Book::query()->first();
65 'book_id' => $book->id,
66 'description' => 'A chapter created via the API',
69 $resp = $this->postJson($this->baseEndpoint, $details);
70 $resp->assertStatus(422);
71 $resp->assertJson($this->validationResponse([
72 "name" => ["The name field is required."]
76 public function test_chapter_book_id_needed_to_create()
78 $this->actingAsApiEditor();
80 'name' => 'My api chapter',
81 'description' => 'A chapter created via the API',
84 $resp = $this->postJson($this->baseEndpoint, $details);
85 $resp->assertStatus(422);
86 $resp->assertJson($this->validationResponse([
87 "book_id" => ["The book id field is required."]
91 public function test_read_endpoint()
93 $this->actingAsApiEditor();
94 $chapter = Chapter::visible()->first();
95 $page = $chapter->pages()->first();
97 $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
98 $resp->assertStatus(200);
100 'id' => $chapter->id,
101 'slug' => $chapter->slug,
103 'name' => $chapter->createdBy->name,
105 'book_id' => $chapter->book_id,
107 'name' => $chapter->createdBy->name,
110 'name' => $chapter->ownedBy->name
115 'slug' => $page->slug,
116 'name' => $page->name,
120 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
123 public function test_update_endpoint()
125 $this->actingAsApiEditor();
126 $chapter = Chapter::visible()->first();
128 'name' => 'My updated API chapter',
129 'description' => 'A chapter created via the API',
132 'name' => 'freshtag',
133 'value' => 'freshtagval',
138 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
141 $resp->assertStatus(200);
142 $resp->assertJson(array_merge($details, [
143 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id
145 $this->assertActivityExists('chapter_update', $chapter);
148 public function test_delete_endpoint()
150 $this->actingAsApiEditor();
151 $chapter = Chapter::visible()->first();
152 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
154 $resp->assertStatus(204);
155 $this->assertActivityExists('chapter_delete');
158 public function test_export_html_endpoint()
160 $this->actingAsApiEditor();
161 $chapter = Chapter::visible()->first();
163 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
164 $resp->assertStatus(200);
165 $resp->assertSee($chapter->name);
166 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
169 public function test_export_plain_text_endpoint()
171 $this->actingAsApiEditor();
172 $chapter = Chapter::visible()->first();
174 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
175 $resp->assertStatus(200);
176 $resp->assertSee($chapter->name);
177 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
180 public function test_export_pdf_endpoint()
182 $this->actingAsApiEditor();
183 $chapter = Chapter::visible()->first();
185 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
186 $resp->assertStatus(200);
187 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');