1 <?php namespace Tests\Api;
3 use BookStack\Entities\Models\Book;
6 class BooksApiTest extends TestCase
10 protected $baseEndpoint = '/api/books';
12 public function test_index_endpoint_returns_expected_book()
14 $this->actingAsApiEditor();
15 $firstBook = Book::query()->orderBy('id', 'asc')->first();
17 $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
18 $resp->assertJson(['data' => [
20 'id' => $firstBook->id,
21 'name' => $firstBook->name,
22 'slug' => $firstBook->slug,
27 public function test_create_endpoint()
29 $this->actingAsApiEditor();
31 'name' => 'My API book',
32 'description' => 'A book created via the API',
35 $resp = $this->postJson($this->baseEndpoint, $details);
36 $resp->assertStatus(200);
37 $newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
38 $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
39 $this->assertActivityExists('book_create', $newItem);
42 public function test_book_name_needed_to_create()
44 $this->actingAsApiEditor();
46 'description' => 'A book created via the API',
49 $resp = $this->postJson($this->baseEndpoint, $details);
50 $resp->assertStatus(422);
53 "message" => "The given data was invalid.",
55 "name" => ["The name field is required."]
62 public function test_read_endpoint()
64 $this->actingAsApiEditor();
65 $book = Book::visible()->first();
67 $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
69 $resp->assertStatus(200);
72 'slug' => $book->slug,
74 'name' => $book->createdBy->name,
77 'name' => $book->createdBy->name,
80 'name' => $book->ownedBy->name
85 public function test_update_endpoint()
87 $this->actingAsApiEditor();
88 $book = Book::visible()->first();
90 'name' => 'My updated API book',
91 'description' => 'A book created via the API',
94 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
97 $resp->assertStatus(200);
98 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
99 $this->assertActivityExists('book_update', $book);
102 public function test_delete_endpoint()
104 $this->actingAsApiEditor();
105 $book = Book::visible()->first();
106 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
108 $resp->assertStatus(204);
109 $this->assertActivityExists('book_delete');
112 public function test_export_html_endpoint()
114 $this->actingAsApiEditor();
115 $book = Book::visible()->first();
117 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
118 $resp->assertStatus(200);
119 $resp->assertSee($book->name);
120 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
123 public function test_export_plain_text_endpoint()
125 $this->actingAsApiEditor();
126 $book = Book::visible()->first();
128 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
129 $resp->assertStatus(200);
130 $resp->assertSee($book->name);
131 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
134 public function test_export_pdf_endpoint()
136 $this->actingAsApiEditor();
137 $book = Book::visible()->first();
139 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
140 $resp->assertStatus(200);
141 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
144 public function test_export_markdown_endpoint()
146 $this->actingAsApiEditor();
147 $book = Book::visible()->has('pages')->has('chapters')->first();
149 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/markdown");
150 $resp->assertStatus(200);
151 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.md"');
152 $resp->assertSee('# ' . $book->name);
153 $resp->assertSee('# ' . $book->pages()->first()->name);
154 $resp->assertSee('# ' . $book->chapters()->first()->name);