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,
112 'slug' => $page->slug,
113 'name' => $page->name,
117 $resp->assertJsonCount($chapter->pages()->count(), 'pages');
120 public function test_update_endpoint()
122 $this->actingAsApiEditor();
123 $chapter = Chapter::visible()->first();
125 'name' => 'My updated API chapter',
126 'description' => 'A chapter created via the API',
129 'name' => 'freshtag',
130 'value' => 'freshtagval',
135 $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
138 $resp->assertStatus(200);
139 $resp->assertJson(array_merge($details, [
140 'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id
142 $this->assertActivityExists('chapter_update', $chapter);
145 public function test_delete_endpoint()
147 $this->actingAsApiEditor();
148 $chapter = Chapter::visible()->first();
149 $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
151 $resp->assertStatus(204);
152 $this->assertActivityExists('chapter_delete');
155 public function test_export_html_endpoint()
157 $this->actingAsApiEditor();
158 $chapter = Chapter::visible()->first();
160 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
161 $resp->assertStatus(200);
162 $resp->assertSee($chapter->name);
163 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
166 public function test_export_plain_text_endpoint()
168 $this->actingAsApiEditor();
169 $chapter = Chapter::visible()->first();
171 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
172 $resp->assertStatus(200);
173 $resp->assertSee($chapter->name);
174 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
177 public function test_export_pdf_endpoint()
179 $this->actingAsApiEditor();
180 $chapter = Chapter::visible()->first();
182 $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
183 $resp->assertStatus(200);
184 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');