Skip to content

Commit 4a87181

Browse files
committed
API: Added changelog support to page create/update API
Also changed the UI based forms to submit as changelog instead of summary to align with more sensible name used on the API. Added testing to cover. For #6162
1 parent f1fe98d commit 4a87181

10 files changed

Lines changed: 66 additions & 26 deletions

File tree

app/Entities/Controllers/PageApiController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class PageApiController extends ApiController
2727
'markdown' => ['required_without:html', 'string'],
2828
'tags' => ['array'],
2929
'priority' => ['integer'],
30+
'changelog' => ['string', 'min:1', 'max:180'],
3031
],
3132
'update' => [
3233
'book_id' => ['integer'],
@@ -36,6 +37,7 @@ class PageApiController extends ApiController
3637
'markdown' => ['string'],
3738
'tags' => ['array'],
3839
'priority' => ['integer'],
40+
'changelog' => ['string', 'min:1', 'max:180'],
3941
],
4042
];
4143

@@ -75,7 +77,7 @@ public function list(): JsonResponse
7577
*/
7678
public function create(Request $request): JsonResponse
7779
{
78-
$this->validate($request, $this->rules['create']);
80+
$validated = $this->validate($request, $this->rules['create']);
7981

8082
if ($request->has('chapter_id')) {
8183
$parent = $this->entityQueries->chapters->findVisibleByIdOrFail(intval($request->input('chapter_id')));
@@ -85,7 +87,7 @@ public function create(Request $request): JsonResponse
8587
$this->checkOwnablePermission(Permission::PageCreate, $parent);
8688

8789
$draft = $this->pageRepo->getNewDraftPage($parent);
88-
$this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
90+
$this->pageRepo->publishDraft($draft, $validated);
8991

9092
return response()->json($draft->forJsonDisplay());
9193
}

app/Entities/Repos/PageRepo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function publishDraft(Page $draft, array $input): Page
9191
$draft = $this->baseRepo->update($draft, $input);
9292
$draft->rebuildPermissions();
9393

94-
$summary = trim($input['summary'] ?? '') ?: trans('entities.pages_initial_revision');
94+
$summary = trim($input['changelog'] ?? '') ?: trans('entities.pages_initial_revision');
9595
$this->revisionRepo->storeNewForPage($draft, $summary);
9696
$draft->refresh();
9797

@@ -134,7 +134,7 @@ public function update(Page $page, array $input): Page
134134
$this->revisionRepo->deleteDraftsForCurrentUser($page);
135135

136136
// Save a revision after updating
137-
$summary = trim($input['summary'] ?? '');
137+
$summary = trim($input['changelog'] ?? '');
138138
$htmlChanged = isset($input['html']) && $input['html'] !== $oldHtml;
139139
$nameChanged = isset($input['name']) && $input['name'] !== $oldName;
140140
$markdownChanged = isset($input['markdown']) && $input['markdown'] !== $oldMarkdown;

resources/views/pages/parts/editor-toolbar.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class="dropdown-container">
108108
<p class="text-muted pb-s">{{ trans('entities.pages_edit_enter_changelog_desc') }}</p>
109109
<textarea
110110
refs="page-editor@changelogInput"
111-
name="summary"
111+
name="changelog"
112112
id="summary-input"
113113
rows="2"
114114
maxlength="180"

tests/Activity/WebhookFormatTesting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function test_entity_events_show_related_user_info()
3030
public function test_page_create_and_update_events_show_revision_info()
3131
{
3232
$page = $this->entities->page();
33-
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
33+
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'changelog' => 'Update a']);
3434

3535
$data = $this->getWebhookData(ActivityType::PAGE_UPDATE, $page);
3636
$this->assertEquals($page->currentRevision->id, Arr::get($data, 'related_item.current_revision.id'));

tests/Api/PagesApiTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@ public function test_markdown_can_be_provided_for_create()
128128
$this->assertStringContainsString('href="https://example.com"', $respHtml);
129129
}
130130

131+
public function test_create_can_accept_a_changelog_message()
132+
{
133+
$this->actingAsApiEditor();
134+
$book = $this->entities->book();
135+
$details = [
136+
'name' => 'My updated API page',
137+
'html' => '<p>A page updated via the API</p>',
138+
'changelog' => 'This is a create changelog message via the API',
139+
'book_id' => $book->id,
140+
];
141+
142+
$resp = $this->postJson($this->baseEndpoint, $details);
143+
$resp->assertOk();
144+
145+
$pageId = $resp->json('id');
146+
$page = Page::query()->findOrFail($pageId);
147+
148+
$latestRevision = $page->revisions()->orderBy('id', 'desc')->first();
149+
$this->assertEquals('This is a create changelog message via the API', $latestRevision->summary);
150+
}
151+
131152
public function test_read_endpoint()
132153
{
133154
$this->actingAsApiEditor();
@@ -338,6 +359,23 @@ public function test_update_increments_updated_date_if_only_tags_are_sent()
338359
$this->assertGreaterThan(Carbon::now()->subDay()->unix(), $page->updated_at->unix());
339360
}
340361

362+
public function test_update_can_accept_a_changelog_message()
363+
{
364+
$this->actingAsApiEditor();
365+
$page = $this->entities->page();
366+
$details = [
367+
'name' => 'My updated API page',
368+
'html' => '<p>A page updated via the API</p>',
369+
'changelog' => 'This is a changelog message via the API',
370+
];
371+
372+
$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
373+
$resp->assertOk();
374+
375+
$latestRevision = $page->revisions()->orderBy('id', 'desc')->first();
376+
$this->assertEquals('This is a changelog message via the API', $latestRevision->summary);
377+
}
378+
341379
public function test_delete_endpoint()
342380
{
343381
$this->actingAsApiEditor();

tests/Commands/ClearRevisionsCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public function test_clear_revisions_command()
1414
$this->asEditor();
1515
$pageRepo = app(PageRepo::class);
1616
$page = Page::first();
17-
$pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
18-
$pageRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '<p>new content in draft</p>', 'summary' => 'page revision testing']);
17+
$pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'changelog' => 'page revision testing']);
18+
$pageRepo->updatePageDraft($page, ['name' => 'updated page', 'html' => '<p>new content in draft</p>', 'changelog' => 'page revision testing']);
1919

2020
$this->assertDatabaseHas('page_revisions', [
2121
'page_id' => $page->id,

tests/Entity/PageContentTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function test_saving_page_with_includes()
4848
$includeTag = '{{@' . $secondPage->id . '}}';
4949
$page->html = '<p>' . $includeTag . '</p>';
5050

51-
$resp = $this->put($page->getUrl(), ['name' => $page->name, 'html' => $page->html, 'summary' => '']);
51+
$resp = $this->put($page->getUrl(), ['name' => $page->name, 'html' => $page->html, 'changelog' => '']);
5252

5353
$resp->assertStatus(302);
5454

@@ -127,7 +127,7 @@ public function test_duplicate_ids_fixed_on_page_save()
127127
$pageSave = $this->put($page->getUrl(), [
128128
'name' => $page->name,
129129
'html' => $content,
130-
'summary' => '',
130+
'changelog' => '',
131131
]);
132132
$pageSave->assertRedirect();
133133

@@ -144,7 +144,7 @@ public function test_anchors_referencing_non_bkmrk_ids_rewritten_after_save()
144144
$this->put($page->getUrl(), [
145145
'name' => $page->name,
146146
'html' => $content,
147-
'summary' => '',
147+
'changelog' => '',
148148
]);
149149

150150
$updatedPage = Page::query()->where('id', '=', $page->id)->first();
@@ -253,7 +253,7 @@ public function test_page_markdown_table_rendering()
253253
| Paragraph | Text |';
254254
$this->put($page->getUrl(), [
255255
'name' => $page->name, 'markdown' => $content,
256-
'html' => '', 'summary' => '',
256+
'html' => '', 'changelog' => '',
257257
]);
258258

259259
$page->refresh();
@@ -272,7 +272,7 @@ public function test_page_markdown_task_list_rendering()
272272
- [x] Item b';
273273
$this->put($page->getUrl(), [
274274
'name' => $page->name, 'markdown' => $content,
275-
'html' => '', 'summary' => '',
275+
'html' => '', 'changelog' => '',
276276
]);
277277

278278
$page->refresh();
@@ -292,7 +292,7 @@ public function test_page_markdown_strikethrough_rendering()
292292
$content = '~~some crossed out text~~';
293293
$this->put($page->getUrl(), [
294294
'name' => $page->name, 'markdown' => $content,
295-
'html' => '', 'summary' => '',
295+
'html' => '', 'changelog' => '',
296296
]);
297297

298298
$page->refresh();
@@ -311,7 +311,7 @@ public function test_page_markdown_single_html_comment_saving()
311311
$content = '<!-- Test Comment -->';
312312
$this->put($page->getUrl(), [
313313
'name' => $page->name, 'markdown' => $content,
314-
'html' => '', 'summary' => '',
314+
'html' => '', 'changelog' => '',
315315
])->assertRedirect();
316316

317317
$page->refresh();
@@ -328,7 +328,7 @@ public function test_base64_images_get_extracted_from_page_content()
328328
$page = $this->entities->page();
329329

330330
$this->put($page->getUrl(), [
331-
'name' => $page->name, 'summary' => '',
331+
'name' => $page->name, 'changelog' => '',
332332
'html' => '<p>test<img src="data:image/jpeg;base64,' . $this->base64Jpeg . '"/></p>',
333333
]);
334334

@@ -352,7 +352,7 @@ public function test_base64_images_get_extracted_when_containing_whitespace()
352352
$base64PngWithWhitespace = "iVBORw0KGg\noAAAANSUhE\tUgAAAAEAAAA BCA YAAAAfFcSJAAA\n\t ACklEQVR4nGMAAQAABQAB";
353353
$base64PngWithoutWhitespace = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQAB';
354354
$this->put($page->getUrl(), [
355-
'name' => $page->name, 'summary' => '',
355+
'name' => $page->name, 'changelog' => '',
356356
'html' => '<p>test<img src="data:image/png;base64,' . $base64PngWithWhitespace . '"/></p>',
357357
]);
358358

@@ -381,7 +381,7 @@ public function test_base64_images_within_html_blanked_if_not_supported_extensio
381381
$page = $this->entities->page();
382382

383383
$this->put($page->getUrl(), [
384-
'name' => $page->name, 'summary' => '',
384+
'name' => $page->name, 'changelog' => '',
385385
'html' => '<p>test<img src="data:image/' . $extension . ';base64,' . $this->base64Jpeg . '"/></p>',
386386
]);
387387

@@ -425,7 +425,7 @@ public function test_base64_images_get_extracted_from_markdown_page_content()
425425
$page = $this->entities->page();
426426

427427
$this->put($page->getUrl(), [
428-
'name' => $page->name, 'summary' => '',
428+
'name' => $page->name, 'changelog' => '',
429429
'markdown' => 'test ![test](data:image/jpeg;base64,' . $this->base64Jpeg . ')',
430430
]);
431431

@@ -456,7 +456,7 @@ public function test_markdown_base64_extract_not_limited_by_pcre_limits()
456456
$base64Content = base64_encode($content);
457457

458458
$this->put($page->getUrl(), [
459-
'name' => $page->name, 'summary' => '',
459+
'name' => $page->name, 'changelog' => '',
460460
'markdown' => 'test ![test](data:image/jpeg;base64,' . $base64Content . ') ![test](data:image/jpeg;base64,' . $base64Content . ')',
461461
]);
462462

@@ -479,7 +479,7 @@ public function test_base64_images_within_markdown_blanked_if_not_supported_exte
479479
$page = $this->entities->page();
480480

481481
$this->asEditor()->put($page->getUrl(), [
482-
'name' => $page->name, 'summary' => '',
482+
'name' => $page->name, 'changelog' => '',
483483
'markdown' => 'test ![test](data:image/jiff;base64,' . $this->base64Jpeg . ')',
484484
]);
485485

tests/Entity/PageRevisionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function test_page_revision_restore_sets_new_revision_with_summary()
119119
{
120120
$this->asEditor();
121121
$page = $this->entities->page();
122-
$this->createRevisions($page, 1, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'summary' => 'My first update']);
122+
$this->createRevisions($page, 1, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'changelog' => 'My first update']);
123123
$this->createRevisions($page, 1, ['html' => '<p>new content</p>']);
124124
$page->refresh();
125125

@@ -327,7 +327,7 @@ protected function createRevisions(Page $page, int $times, array $attrs = [])
327327
$user = user();
328328

329329
for ($i = 0; $i < $times; $i++) {
330-
$data = ['name' => 'Page update' . $i, 'summary' => 'Update entry' . $i];
330+
$data = ['name' => 'Page update' . $i, 'changelog' => 'Update entry' . $i];
331331
if (!isset($attrs['markdown'])) {
332332
$data['html'] = '<p>My update page</p>';
333333
}

tests/Entity/PageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function test_page_creation_allows_summary_to_be_set()
9898
$details = [
9999
'html' => '<h1>a title</h1>',
100100
'name' => 'My page with summary',
101-
'summary' => 'Here is my changelog message for a new page!',
101+
'changelog' => 'Here is my changelog message for a new page!',
102102
];
103103
$resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
104104
$resp->assertRedirect();

tests/Uploads/ImageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ public function test_deleted_unused_images()
819819
$pageRepo->update($page, [
820820
'name' => $page->name,
821821
'html' => $page->html . "<img src=\"{$image->url}\">",
822-
'summary' => '',
822+
'changelog' => '',
823823
]);
824824

825825
// Ensure no images are reported as deletable
@@ -831,7 +831,7 @@ public function test_deleted_unused_images()
831831
$pageRepo->update($page, [
832832
'name' => $page->name,
833833
'html' => '<p>Hello</p>',
834-
'summary' => '',
834+
'changelog' => '',
835835
]);
836836

837837
// Ensure revision images are picked up okay

0 commit comments

Comments
 (0)