]> BookStack Code Mirror - bookstack/blob - tests/Api/ChaptersApiTest.php
Apply fixes from StyleCI
[bookstack] / tests / Api / ChaptersApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use Tests\TestCase;
8
9 class ChaptersApiTest extends TestCase
10 {
11     use TestsApi;
12
13     protected $baseEndpoint = '/api/chapters';
14
15     public function test_index_endpoint_returns_expected_chapter()
16     {
17         $this->actingAsApiEditor();
18         $firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
19
20         $resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
21         $resp->assertJson(['data' => [
22             [
23                 'id'       => $firstChapter->id,
24                 'name'     => $firstChapter->name,
25                 'slug'     => $firstChapter->slug,
26                 'book_id'  => $firstChapter->book->id,
27                 'priority' => $firstChapter->priority,
28             ],
29         ]]);
30     }
31
32     public function test_create_endpoint()
33     {
34         $this->actingAsApiEditor();
35         $book = Book::query()->first();
36         $details = [
37             'name'        => 'My API chapter',
38             'description' => 'A chapter created via the API',
39             'book_id'     => $book->id,
40             'tags'        => [
41                 [
42                     'name'  => 'tagname',
43                     'value' => 'tagvalue',
44                 ],
45             ],
46         ];
47
48         $resp = $this->postJson($this->baseEndpoint, $details);
49         $resp->assertStatus(200);
50         $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
51         $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
52         $this->assertDatabaseHas('tags', [
53             'entity_id'   => $newItem->id,
54             'entity_type' => $newItem->getMorphClass(),
55             'name'        => 'tagname',
56             'value'       => 'tagvalue',
57         ]);
58         $resp->assertJsonMissing(['pages' => []]);
59         $this->assertActivityExists('chapter_create', $newItem);
60     }
61
62     public function test_chapter_name_needed_to_create()
63     {
64         $this->actingAsApiEditor();
65         $book = Book::query()->first();
66         $details = [
67             'book_id'     => $book->id,
68             'description' => 'A chapter created via the API',
69         ];
70
71         $resp = $this->postJson($this->baseEndpoint, $details);
72         $resp->assertStatus(422);
73         $resp->assertJson($this->validationResponse([
74             'name' => ['The name field is required.'],
75         ]));
76     }
77
78     public function test_chapter_book_id_needed_to_create()
79     {
80         $this->actingAsApiEditor();
81         $details = [
82             'name'        => 'My api chapter',
83             'description' => 'A chapter created via the API',
84         ];
85
86         $resp = $this->postJson($this->baseEndpoint, $details);
87         $resp->assertStatus(422);
88         $resp->assertJson($this->validationResponse([
89             'book_id' => ['The book id field is required.'],
90         ]));
91     }
92
93     public function test_read_endpoint()
94     {
95         $this->actingAsApiEditor();
96         $chapter = Chapter::visible()->first();
97         $page = $chapter->pages()->first();
98
99         $resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
100         $resp->assertStatus(200);
101         $resp->assertJson([
102             'id'         => $chapter->id,
103             'slug'       => $chapter->slug,
104             'created_by' => [
105                 'name' => $chapter->createdBy->name,
106             ],
107             'book_id'    => $chapter->book_id,
108             'updated_by' => [
109                 'name' => $chapter->createdBy->name,
110             ],
111             'owned_by' => [
112                 'name' => $chapter->ownedBy->name,
113             ],
114             'pages' => [
115                 [
116                     'id'   => $page->id,
117                     'slug' => $page->slug,
118                     'name' => $page->name,
119                 ],
120             ],
121         ]);
122         $resp->assertJsonCount($chapter->pages()->count(), 'pages');
123     }
124
125     public function test_update_endpoint()
126     {
127         $this->actingAsApiEditor();
128         $chapter = Chapter::visible()->first();
129         $details = [
130             'name'        => 'My updated API chapter',
131             'description' => 'A chapter created via the API',
132             'tags'        => [
133                 [
134                     'name'  => 'freshtag',
135                     'value' => 'freshtagval',
136                 ],
137             ],
138         ];
139
140         $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
141         $chapter->refresh();
142
143         $resp->assertStatus(200);
144         $resp->assertJson(array_merge($details, [
145             'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
146         ]));
147         $this->assertActivityExists('chapter_update', $chapter);
148     }
149
150     public function test_delete_endpoint()
151     {
152         $this->actingAsApiEditor();
153         $chapter = Chapter::visible()->first();
154         $resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
155
156         $resp->assertStatus(204);
157         $this->assertActivityExists('chapter_delete');
158     }
159
160     public function test_export_html_endpoint()
161     {
162         $this->actingAsApiEditor();
163         $chapter = Chapter::visible()->first();
164
165         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/html");
166         $resp->assertStatus(200);
167         $resp->assertSee($chapter->name);
168         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
169     }
170
171     public function test_export_plain_text_endpoint()
172     {
173         $this->actingAsApiEditor();
174         $chapter = Chapter::visible()->first();
175
176         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/plaintext");
177         $resp->assertStatus(200);
178         $resp->assertSee($chapter->name);
179         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
180     }
181
182     public function test_export_pdf_endpoint()
183     {
184         $this->actingAsApiEditor();
185         $chapter = Chapter::visible()->first();
186
187         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/pdf");
188         $resp->assertStatus(200);
189         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
190     }
191
192     public function test_export_markdown_endpoint()
193     {
194         $this->actingAsApiEditor();
195         $chapter = Chapter::visible()->has('pages')->first();
196
197         $resp = $this->get($this->baseEndpoint . "/{$chapter->id}/export/markdown");
198         $resp->assertStatus(200);
199         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.md"');
200         $resp->assertSee('# ' . $chapter->name);
201         $resp->assertSee('# ' . $chapter->pages()->first()->name);
202     }
203 }