Skip to content

Commit 2a7a81e

Browse files
committed
Input WYSIWYG: Updated API testing, fixed description set issue
Fixed issue where an existing description_html field would not be updated via 'description' input.
1 parent 00ae04e commit 2a7a81e

File tree

4 files changed

+148
-12
lines changed

4 files changed

+148
-12
lines changed

app/Entities/Repos/BaseRepo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ protected function updateDescription(Entity $entity, array $input): void
116116
$entity->description = html_entity_decode(strip_tags($input['description_html']));
117117
} else if (isset($input['description'])) {
118118
$entity->description = $input['description'];
119+
$entity->description_html = '';
119120
$entity->description_html = $entity->descriptionHtml();
120121
}
121122
}

tests/Api/BooksApiTest.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,44 @@ public function test_create_endpoint()
3333
$this->actingAsApiEditor();
3434
$templatePage = $this->entities->templatePage();
3535
$details = [
36-
'name' => 'My API book',
37-
'description' => 'A book created via the API',
36+
'name' => 'My API book',
37+
'description' => 'A book created via the API',
3838
'default_template_id' => $templatePage->id,
3939
];
4040

4141
$resp = $this->postJson($this->baseEndpoint, $details);
4242
$resp->assertStatus(200);
4343

4444
$newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
45-
$resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
45+
$resp->assertJson(array_merge($details, [
46+
'id' => $newItem->id,
47+
'slug' => $newItem->slug,
48+
'description_html' => '<p>A book created via the API</p>',
49+
]));
4650
$this->assertActivityExists('book_create', $newItem);
4751
}
4852

53+
public function test_create_endpoint_with_html()
54+
{
55+
$this->actingAsApiEditor();
56+
$details = [
57+
'name' => 'My API book',
58+
'description_html' => '<p>A book <em>created</em> <strong>via</strong> the API</p>',
59+
];
60+
61+
$resp = $this->postJson($this->baseEndpoint, $details);
62+
$resp->assertStatus(200);
63+
64+
$newItem = Book::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
65+
$expectedDetails = array_merge($details, [
66+
'id' => $newItem->id,
67+
'description' => 'A book created via the API',
68+
]);
69+
70+
$resp->assertJson($expectedDetails);
71+
$this->assertDatabaseHas('books', $expectedDetails);
72+
}
73+
4974
public function test_book_name_needed_to_create()
5075
{
5176
$this->actingAsApiEditor();
@@ -61,7 +86,7 @@ public function test_book_name_needed_to_create()
6186
'validation' => [
6287
'name' => ['The name field is required.'],
6388
],
64-
'code' => 422,
89+
'code' => 422,
6590
],
6691
]);
6792
}
@@ -128,18 +153,37 @@ public function test_update_endpoint()
128153
$templatePage = $this->entities->templatePage();
129154
$details = [
130155
'name' => 'My updated API book',
131-
'description' => 'A book created via the API',
156+
'description' => 'A book updated via the API',
132157
'default_template_id' => $templatePage->id,
133158
];
134159

135160
$resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
136161
$book->refresh();
137162

138163
$resp->assertStatus(200);
139-
$resp->assertJson(array_merge($details, ['id' => $book->id, 'slug' => $book->slug]));
164+
$resp->assertJson(array_merge($details, [
165+
'id' => $book->id,
166+
'slug' => $book->slug,
167+
'description_html' => '<p>A book updated via the API</p>',
168+
]));
140169
$this->assertActivityExists('book_update', $book);
141170
}
142171

172+
public function test_update_endpoint_with_html()
173+
{
174+
$this->actingAsApiEditor();
175+
$book = $this->entities->book();
176+
$details = [
177+
'name' => 'My updated API book',
178+
'description_html' => '<p>A book <strong>updated</strong> via the API</p>',
179+
];
180+
181+
$resp = $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
182+
$resp->assertStatus(200);
183+
184+
$this->assertDatabaseHas('books', array_merge($details, ['id' => $book->id, 'description' => 'A book updated via the API']));
185+
}
186+
143187
public function test_update_increments_updated_date_if_only_tags_are_sent()
144188
{
145189
$this->actingAsApiEditor();

tests/Api/ChaptersApiTest.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public function test_create_endpoint()
5151
$resp = $this->postJson($this->baseEndpoint, $details);
5252
$resp->assertStatus(200);
5353
$newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
54-
$resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
54+
$resp->assertJson(array_merge($details, [
55+
'id' => $newItem->id,
56+
'slug' => $newItem->slug,
57+
'description_html' => '<p>A chapter created via the API</p>',
58+
]));
5559
$this->assertDatabaseHas('tags', [
5660
'entity_id' => $newItem->id,
5761
'entity_type' => $newItem->getMorphClass(),
@@ -62,6 +66,28 @@ public function test_create_endpoint()
6266
$this->assertActivityExists('chapter_create', $newItem);
6367
}
6468

69+
public function test_create_endpoint_with_html()
70+
{
71+
$this->actingAsApiEditor();
72+
$book = $this->entities->book();
73+
$details = [
74+
'name' => 'My API chapter',
75+
'description_html' => '<p>A chapter <strong>created</strong> via the API</p>',
76+
'book_id' => $book->id,
77+
];
78+
79+
$resp = $this->postJson($this->baseEndpoint, $details);
80+
$resp->assertStatus(200);
81+
$newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
82+
83+
$expectedDetails = array_merge($details, [
84+
'id' => $newItem->id,
85+
'description' => 'A chapter created via the API',
86+
]);
87+
$resp->assertJson($expectedDetails);
88+
$this->assertDatabaseHas('chapters', $expectedDetails);
89+
}
90+
6591
public function test_chapter_name_needed_to_create()
6692
{
6793
$this->actingAsApiEditor();
@@ -131,7 +157,7 @@ public function test_update_endpoint()
131157
$chapter = $this->entities->chapter();
132158
$details = [
133159
'name' => 'My updated API chapter',
134-
'description' => 'A chapter created via the API',
160+
'description' => 'A chapter updated via the API',
135161
'tags' => [
136162
[
137163
'name' => 'freshtag',
@@ -146,11 +172,31 @@ public function test_update_endpoint()
146172

147173
$resp->assertStatus(200);
148174
$resp->assertJson(array_merge($details, [
149-
'id' => $chapter->id, 'slug' => $chapter->slug, 'book_id' => $chapter->book_id,
175+
'id' => $chapter->id,
176+
'slug' => $chapter->slug,
177+
'book_id' => $chapter->book_id,
178+
'description_html' => '<p>A chapter updated via the API</p>',
150179
]));
151180
$this->assertActivityExists('chapter_update', $chapter);
152181
}
153182

183+
public function test_update_endpoint_with_html()
184+
{
185+
$this->actingAsApiEditor();
186+
$chapter = $this->entities->chapter();
187+
$details = [
188+
'name' => 'My updated API chapter',
189+
'description_html' => '<p>A chapter <em>updated</em> via the API</p>',
190+
];
191+
192+
$resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
193+
$resp->assertStatus(200);
194+
195+
$this->assertDatabaseHas('chapters', array_merge($details, [
196+
'id' => $chapter->id, 'description' => 'A chapter updated via the API'
197+
]));
198+
}
199+
154200
public function test_update_increments_updated_date_if_only_tags_are_sent()
155201
{
156202
$this->actingAsApiEditor();

tests/Api/ShelvesApiTest.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public function test_create_endpoint()
4242
$resp = $this->postJson($this->baseEndpoint, array_merge($details, ['books' => [$books[0]->id, $books[1]->id]]));
4343
$resp->assertStatus(200);
4444
$newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
45-
$resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug]));
45+
$resp->assertJson(array_merge($details, [
46+
'id' => $newItem->id,
47+
'slug' => $newItem->slug,
48+
'description_html' => '<p>A shelf created via the API</p>',
49+
]));
4650
$this->assertActivityExists('bookshelf_create', $newItem);
4751
foreach ($books as $index => $book) {
4852
$this->assertDatabaseHas('bookshelves_books', [
@@ -53,6 +57,28 @@ public function test_create_endpoint()
5357
}
5458
}
5559

60+
public function test_create_endpoint_with_html()
61+
{
62+
$this->actingAsApiEditor();
63+
64+
$details = [
65+
'name' => 'My API shelf',
66+
'description_html' => '<p>A <strong>shelf</strong> created via the API</p>',
67+
];
68+
69+
$resp = $this->postJson($this->baseEndpoint, $details);
70+
$resp->assertStatus(200);
71+
$newItem = Bookshelf::query()->orderByDesc('id')->where('name', '=', $details['name'])->first();
72+
73+
$expectedDetails = array_merge($details, [
74+
'id' => $newItem->id,
75+
'description' => 'A shelf created via the API',
76+
]);
77+
78+
$resp->assertJson($expectedDetails);
79+
$this->assertDatabaseHas('bookshelves', $expectedDetails);
80+
}
81+
5682
public function test_shelf_name_needed_to_create()
5783
{
5884
$this->actingAsApiEditor();
@@ -102,17 +128,36 @@ public function test_update_endpoint()
102128
$shelf = Bookshelf::visible()->first();
103129
$details = [
104130
'name' => 'My updated API shelf',
105-
'description' => 'A shelf created via the API',
131+
'description' => 'A shelf updated via the API',
106132
];
107133

108134
$resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
109135
$shelf->refresh();
110136

111137
$resp->assertStatus(200);
112-
$resp->assertJson(array_merge($details, ['id' => $shelf->id, 'slug' => $shelf->slug]));
138+
$resp->assertJson(array_merge($details, [
139+
'id' => $shelf->id,
140+
'slug' => $shelf->slug,
141+
'description_html' => '<p>A shelf updated via the API</p>',
142+
]));
113143
$this->assertActivityExists('bookshelf_update', $shelf);
114144
}
115145

146+
public function test_update_endpoint_with_html()
147+
{
148+
$this->actingAsApiEditor();
149+
$shelf = Bookshelf::visible()->first();
150+
$details = [
151+
'name' => 'My updated API shelf',
152+
'description_html' => '<p>A shelf <em>updated</em> via the API</p>',
153+
];
154+
155+
$resp = $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
156+
$resp->assertStatus(200);
157+
158+
$this->assertDatabaseHas('bookshelves', array_merge($details, ['id' => $shelf->id, 'description' => 'A shelf updated via the API']));
159+
}
160+
116161
public function test_update_increments_updated_date_if_only_tags_are_sent()
117162
{
118163
$this->actingAsApiEditor();

0 commit comments

Comments
 (0)