5 use BookStack\Entities\Models\Book;
8 class BooksApiTest extends TestCase
12 protected $baseEndpoint = '/api/books';
14 public function test_index_endpoint_returns_expected_book()
16 $this->actingAsApiEditor();
17 $firstBook = Book::query()->orderBy('id', 'asc')->first();
19 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
20 $resp->assertJson(['data' => [
22 'id' => $firstBook->id,
23 'name' => $firstBook->name,
24 'slug' => $firstBook->slug,
29 public function test_create_endpoint()
31 $this->actingAsApiEditor();
33 'name' => 'My API book',
34 'description' => 'A book created via the API',
37 $resp = $this->postJson($this->baseEndpoint, $details);
38 $resp->assertStatus(200);
39 $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
40 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
41 $this->assertActivityExists('book_create', $newItem);
44 public function test_book_name_needed_to_create()
46 $this->actingAsApiEditor();
48 'description' => 'A book created via the API',
51 $resp = $this->postJson($this->baseEndpoint, $details);
52 $resp->assertStatus(422);
55 'message' => 'The given data was invalid.',
57 'name' => ['The name field is required.'],
64 public function test_read_endpoint()
66 $this->actingAsApiEditor();
67 $book = Book::visible()->first();
69 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
71 $resp->assertStatus(200);
74 'slug' => $book->slug,
76 'name' => $book->createdBy->name,
79 'name' => $book->createdBy->name,
82 'name' => $book->ownedBy->name,
87 public function test_update_endpoint()
89 $this->actingAsApiEditor();
90 $book = Book::visible()->first();
92 'name' => 'My updated API book',
93 'description' => 'A book created via the API',
96 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
99 $resp->assertStatus(200);
100 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
101 $this->assertActivityExists('book_update', $book);
104 public function test_delete_endpoint()
106 $this->actingAsApiEditor();
107 $book = Book::visible()->first();
108 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
110 $resp->assertStatus(204);
111 $this->assertActivityExists('book_delete');
114 public function test_export_html_endpoint()
116 $this->actingAsApiEditor();
117 $book = Book::visible()->first();
119 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
120 $resp->assertStatus(200);
121 $resp->assertSee($book->name);
122 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
125 public function test_export_plain_text_endpoint()
127 $this->actingAsApiEditor();
128 $book = Book::visible()->first();
130 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
131 $resp->assertStatus(200);
132 $resp->assertSee($book->name);
133 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
136 public function test_export_pdf_endpoint()
138 $this->actingAsApiEditor();
139 $book = Book::visible()->first();
141 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
142 $resp->assertStatus(200);
143 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
146 public function test_export_markdown_endpoint()
148 $this->actingAsApiEditor();
149 $book = Book::visible()->has('pages')->has('chapters')->first();
151 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
152 $resp->assertStatus(200);
153 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
154 $resp->assertSee('# ' . $book->name);
155 $resp->assertSee('# ' . $book->pages()->first()->name);
156 $resp->assertSee('# ' . $book->chapters()->first()->name);
159 public function test_cant_export_when_not_have_permission()
161 $types = ['html', 'plaintext', 'pdf', 'markdown'];
162 $this->actingAsApiEditor();
163 $this->removePermissionFromUser($this->getEditor(), 'content-export');
165 $book = Book::visible()->first();
166 foreach ($types as $type) {
167 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/{$type}");
168 $this->assertPermissionError($resp);