1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-09 10:22:51 +03:00

Added extendable/scalable formatter for webhook data

Creates a new organsied formatting system for webhook data, with
interfaces for extending with custom model formatting rules.
Allows easy usage & extension of the default bookstack formatting
behaviour when customizing webhook events via theme system, and keeps
default data customizations organised.

This also makes the following webhook data changes:
- owned_by/created_by/updated_by user details are loaded for events with
  Entity details. (POTENTIALLY BREAKING CHANGE).
- current_revision details are loaded for page update/create events.

Added testing to cover added model formatting rules.

For #3279 and #3218
This commit is contained in:
Dan Brown
2022-03-26 16:44:34 +00:00
parent 55d61fceb2
commit 3625f12abe
9 changed files with 247 additions and 90 deletions

View File

@@ -0,0 +1,52 @@
<?php
namespace Tests\Actions;
use BookStack\Actions\ActivityType;
use BookStack\Actions\Webhook;
use BookStack\Actions\WebhookFormatter;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use Illuminate\Support\Arr;
use Tests\TestCase;
class WebhookFormatTesting extends TestCase
{
public function test_entity_events_show_related_user_info()
{
$events = [
ActivityType::BOOK_UPDATE => Book::query()->first(),
ActivityType::CHAPTER_CREATE => Chapter::query()->first(),
ActivityType::PAGE_MOVE => Page::query()->first(),
];
foreach ($events as $event => $entity) {
$data = $this->getWebhookData($event, $entity);
$this->assertEquals($entity->createdBy->name, Arr::get($data, 'related_item.created_by.name'));
$this->assertEquals($entity->updatedBy->id, Arr::get($data, 'related_item.updated_by.id'));
$this->assertEquals($entity->ownedBy->slug, Arr::get($data, 'related_item.owned_by.slug'));
}
}
public function test_page_create_and_update_events_show_revision_info()
{
/** @var Page $page */
$page = Page::query()->first();
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
$data = $this->getWebhookData(ActivityType::PAGE_UPDATE, $page);
$this->assertEquals($page->currentRevision->id, Arr::get($data, 'related_item.current_revision.id'));
$this->assertEquals($page->currentRevision->type, Arr::get($data, 'related_item.current_revision.type'));
$this->assertEquals('Update a', Arr::get($data, 'related_item.current_revision.summary'));
}
protected function getWebhookData(string $event, $detail): array
{
$webhook = Webhook::factory()->make();
$user = $this->getEditor();
$formatter = WebhookFormatter::getDefault($event, $webhook, $detail, $user, time());
return $formatter->format();
}
}

View File

@@ -144,13 +144,14 @@ class PageRevisionTest extends TestCase
public function test_revision_deletion()
{
$page = Page::first();
/** @var Page $page */
$page = Page::query()->first();
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
$page = Page::find($page->id);
$page->refresh();
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
$page = Page::find($page->id);
$page->refresh();
$beforeRevisionCount = $page->revisions->count();
// Delete the first revision
@@ -158,18 +159,17 @@ class PageRevisionTest extends TestCase
$resp = $this->asEditor()->delete($revision->getUrl('/delete/'));
$resp->assertRedirect($page->getUrl('/revisions'));
$page = Page::find($page->id);
$page->refresh();
$afterRevisionCount = $page->revisions->count();
$this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));
// Try to delete the latest revision
$beforeRevisionCount = $page->revisions->count();
$currentRevision = $page->getCurrentRevision();
$resp = $this->asEditor()->delete($currentRevision->getUrl('/delete/'));
$resp = $this->asEditor()->delete($page->currentRevision->getUrl('/delete/'));
$resp->assertRedirect($page->getUrl('/revisions'));
$page = Page::find($page->id);
$page->refresh();
$afterRevisionCount = $page->revisions->count();
$this->assertTrue($beforeRevisionCount === $afterRevisionCount);
}