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