-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathChaptersApiTest.php
More file actions
272 lines (239 loc) · 9.64 KB
/
Copy pathChaptersApiTest.php
File metadata and controls
272 lines (239 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace Tests\Api;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class ChaptersApiTest extends TestCase
{
use TestsApi;
protected string $baseEndpoint = '/api/chapters';
public function test_index_endpoint_returns_expected_chapter()
{
$this->actingAsApiEditor();
$firstChapter = Chapter::query()->orderBy('id', 'asc')->first();
$resp = $this->getJson($this->baseEndpoint . '?count=1&sort=+id');
$resp->assertJson(['data' => [
[
'id' => $firstChapter->id,
'name' => $firstChapter->name,
'slug' => $firstChapter->slug,
'book_id' => $firstChapter->book->id,
'priority' => $firstChapter->priority,
'book_slug' => $firstChapter->book->slug,
'owned_by' => $firstChapter->owned_by,
'created_by' => $firstChapter->created_by,
'updated_by' => $firstChapter->updated_by,
],
]]);
}
public function test_create_endpoint()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$templatePage = $this->entities->templatePage();
$details = [
'name' => 'My API chapter',
'description' => 'A chapter created via the API',
'book_id' => $book->id,
'tags' => [
[
'name' => 'tagname',
'value' => 'tagvalue',
],
],
'priority' => 15,
'default_template_id' => $templatePage->id,
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(200);
$newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
$resp->assertJson(array_merge($details, [
'id' => $newItem->id,
'slug' => $newItem->slug,
'description_html' => '<p>A chapter created via the API</p>',
]));
$this->assertDatabaseHas('tags', [
'entity_id' => $newItem->id,
'entity_type' => $newItem->getMorphClass(),
'name' => 'tagname',
'value' => 'tagvalue',
]);
$resp->assertJsonMissing(['pages' => []]);
$this->assertActivityExists('chapter_create', $newItem);
}
public function test_create_endpoint_with_html()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$details = [
'name' => 'My API chapter',
'description_html' => '<p>A chapter <strong>created</strong> via the API</p>',
'book_id' => $book->id,
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(200);
$newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
$expectedDetails = array_merge($details, [
'id' => $newItem->id,
'description' => 'A chapter created via the API',
]);
$resp->assertJson($expectedDetails);
$this->assertDatabaseHasEntityData('chapter', $expectedDetails);
}
public function test_chapter_name_needed_to_create()
{
$this->actingAsApiEditor();
$book = $this->entities->book();
$details = [
'book_id' => $book->id,
'description' => 'A chapter created via the API',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse([
'name' => ['The name field is required.'],
]));
}
public function test_chapter_book_id_needed_to_create()
{
$this->actingAsApiEditor();
$details = [
'name' => 'My api chapter',
'description' => 'A chapter created via the API',
];
$resp = $this->postJson($this->baseEndpoint, $details);
$resp->assertStatus(422);
$resp->assertJson($this->validationResponse([
'book_id' => ['The book id field is required.'],
]));
}
public function test_read_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
$page = $chapter->pages()->first();
$resp = $this->getJson($this->baseEndpoint . "/{$chapter->id}");
$resp->assertStatus(200);
$resp->assertJson([
'id' => $chapter->id,
'slug' => $chapter->slug,
'book_slug' => $chapter->book->slug,
'created_by' => [
'name' => $chapter->createdBy->name,
],
'book_id' => $chapter->book_id,
'updated_by' => [
'name' => $chapter->createdBy->name,
],
'owned_by' => [
'name' => $chapter->ownedBy->name,
],
'pages' => [
[
'id' => $page->id,
'slug' => $page->slug,
'name' => $page->name,
'owned_by' => $page->owned_by,
'created_by' => $page->created_by,
'updated_by' => $page->updated_by,
'book_id' => $page->book->id,
'chapter_id' => $chapter->id,
'priority' => $page->priority,
'book_slug' => $chapter->book->slug,
'draft' => $page->draft,
'template' => $page->template,
'editor' => $page->editor,
],
],
'default_template_id' => null,
]);
$resp->assertJsonMissingPath('book');
$resp->assertJsonCount($chapter->pages()->count(), 'pages');
}
public function test_update_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
$templatePage = $this->entities->templatePage();
$details = [
'name' => 'My updated API chapter',
'description' => 'A chapter updated via the API',
'tags' => [
[
'name' => 'freshtag',
'value' => 'freshtagval',
],
],
'priority' => 15,
'default_template_id' => $templatePage->id,
];
$resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
$chapter->refresh();
$resp->assertStatus(200);
$resp->assertJson(array_merge($details, [
'id' => $chapter->id,
'slug' => $chapter->slug,
'book_id' => $chapter->book_id,
'description_html' => '<p>A chapter updated via the API</p>',
]));
$this->assertActivityExists('chapter_update', $chapter);
}
public function test_update_endpoint_with_html()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
$details = [
'name' => 'My updated API chapter',
'description_html' => '<p>A chapter <em>updated</em> via the API</p>',
];
$resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
$resp->assertStatus(200);
$this->assertDatabaseHasEntityData('chapter', array_merge($details, [
'id' => $chapter->id, 'description' => 'A chapter updated via the API'
]));
}
public function test_update_increments_updated_date_if_only_tags_are_sent()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
$chapter->newQuery()->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
$details = [
'tags' => [['name' => 'Category', 'value' => 'Testing']],
];
$this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
$chapter->refresh();
$this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
}
public function test_update_with_book_id_moves_chapter()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapterHasPages();
$page = $chapter->pages()->first();
$newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
$resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
$resp->assertOk();
$chapter->refresh();
$this->assertDatabaseHasEntityData('chapter', ['id' => $chapter->id, 'book_id' => $newBook->id]);
$this->assertDatabaseHasEntityData('page', ['id' => $page->id, 'book_id' => $newBook->id, 'chapter_id' => $chapter->id]);
}
public function test_update_with_new_book_id_requires_delete_permission()
{
$editor = $this->users->editor();
$this->permissions->removeUserRolePermissions($editor, ['chapter-delete-all', 'chapter-delete-own']);
$this->actingAsForApi($editor);
$chapter = $this->entities->chapterHasPages();
$newBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
$resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", ['book_id' => $newBook->id]);
$this->assertPermissionError($resp);
}
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
$chapter = $this->entities->chapter();
$resp = $this->deleteJson($this->baseEndpoint . "/{$chapter->id}");
$resp->assertStatus(204);
$this->assertActivityExists('chapter_delete');
}
}