]> BookStack Code Mirror - bookstack/blob - tests/Api/BooksApiTest.php
446ba28117a765a7663b6c95def30782939d9329
[bookstack] / tests / Api / BooksApiTest.php
1 <?php namespace Tests\Api;
2
3 use BookStack\Entities\Models\Book;
4 use Tests\TestCase;
5
6 class BooksApiTest extends TestCase
7 {
8     use TestsApi;
9
10     protected $baseEndpoint = '/api/books';
11
12     public function test_index_endpoint_returns_expected_book()
13     {
14         $this->actingAsApiEditor();
15         $firstBook = Book::query()->orderBy('id', 'asc')->first();
16
17         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
18         $resp->assertJson(['data' => [
19             [
20                 'id' => $firstBook->id,
21                 'name' => $firstBook->name,
22                 'slug' => $firstBook->slug,
23             ]
24         ]]);
25     }
26
27     public function test_create_endpoint()
28     {
29         $this->actingAsApiEditor();
30         $details = [
31             'name' => 'My API book',
32             'description' => 'A book created via the API',
33         ];
34
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);
40     }
41
42     public function test_book_name_needed_to_create()
43     {
44         $this->actingAsApiEditor();
45         $details = [
46             'description' => 'A book created via the API',
47         ];
48
49         $resp = $this->postJson($this->baseEndpoint, $details);
50         $resp->assertStatus(422);
51         $resp->assertJson([
52             "error" => [
53                 "message" => "The given data was invalid.",
54                 "validation" => [
55                     "name" => ["The name field is required."]
56                 ],
57                 "code" => 422,
58             ],
59         ]);
60     }
61
62     public function test_read_endpoint()
63     {
64         $this->actingAsApiEditor();
65         $book = Book::visible()->first();
66
67         $resp = $this->getJson($this->baseEndpoint . "/{$book->id}");
68
69         $resp->assertStatus(200);
70         $resp->assertJson([
71             'id' => $book->id,
72             'slug' => $book->slug,
73             'created_by' => [
74                 'name' => $book->createdBy->name,
75             ],
76             'updated_by' => [
77                 'name' => $book->createdBy->name,
78             ],
79             'owned_by' => [
80                 'name' => $book->ownedBy->name
81             ],
82         ]);
83     }
84
85     public function test_update_endpoint()
86     {
87         $this->actingAsApiEditor();
88         $book = Book::visible()->first();
89         $details = [
90             'name' => 'My updated API book',
91             'description' => 'A book created via the API',
92         ];
93
94         $resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
95         $book->refresh();
96
97         $resp->assertStatus(200);
98         $resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
99         $this->assertActivityExists('book_update', $book);
100     }
101
102     public function test_delete_endpoint()
103     {
104         $this->actingAsApiEditor();
105         $book = Book::visible()->first();
106         $resp = $this->deleteJson($this->baseEndpoint . "/{$book->id}");
107
108         $resp->assertStatus(204);
109         $this->assertActivityExists('book_delete');
110     }
111
112     public function test_export_html_endpoint()
113     {
114         $this->actingAsApiEditor();
115         $book = Book::visible()->first();
116
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"');
121     }
122
123     public function test_export_plain_text_endpoint()
124     {
125         $this->actingAsApiEditor();
126         $book = Book::visible()->first();
127
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"');
132     }
133
134     public function test_export_pdf_endpoint()
135     {
136         $this->actingAsApiEditor();
137         $book = Book::visible()->first();
138
139         $resp = $this->get($this->baseEndpoint . "/{$book->id}/export/pdf");
140         $resp->assertStatus(200);
141         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
142     }
143
144     public function test_export_markdown_endpoint()
145     {
146         $this->actingAsApiEditor();
147         $book = Book::visible()->has('pages')->has('chapters')->first();
148
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);
155     }
156 }