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,
82 public function test_update_endpoint()
84 $this->actingAsApiEditor();
85 $book = Book::visible()->first();
87 'name' => 'My updated API book',
88 'description' => 'A book created via the API',
91 $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
94 $resp->assertStatus(200);
95 $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
96 $this->assertActivityExists('book_update', $book);
99 public function test_delete_endpoint()
101 $this->actingAsApiEditor();
102 $book = Book::visible()->first();
103 $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
105 $resp->assertStatus(204);
106 $this->assertActivityExists('book_delete');
109 public function test_export_html_endpoint()
111 $this->actingAsApiEditor();
112 $book = Book::visible()->first();
114 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/html");
115 $resp->assertStatus(200);
116 $resp->assertSee($book->name);
117 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
120 public function test_export_plain_text_endpoint()
122 $this->actingAsApiEditor();
123 $book = Book::visible()->first();
125 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/plaintext");
126 $resp->assertStatus(200);
127 $resp->assertSee($book->name);
128 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
131 public function test_export_pdf_endpoint()
133 $this->actingAsApiEditor();
134 $book = Book::visible()->first();
136 $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
137 $resp->assertStatus(200);
138 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');