2022-03-26 12:44:34 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Actions;
|
|
|
|
|
|
|
|
use BookStack\Actions\ActivityType;
|
|
|
|
use BookStack\Actions\Webhook;
|
|
|
|
use BookStack\Actions\WebhookFormatter;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class WebhookFormatTesting extends TestCase
|
|
|
|
{
|
|
|
|
public function test_entity_events_show_related_user_info()
|
|
|
|
{
|
|
|
|
$events = [
|
2022-09-29 17:11:16 -04:00
|
|
|
ActivityType::BOOK_UPDATE => $this->entities->book(),
|
|
|
|
ActivityType::CHAPTER_CREATE => $this->entities->chapter(),
|
|
|
|
ActivityType::PAGE_MOVE => $this->entities->page(),
|
2022-03-26 12:44:34 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2022-03-26 12:44:34 -04:00
|
|
|
$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();
|
2023-01-21 06:08:34 -05:00
|
|
|
$user = $this->users->editor();
|
2022-03-26 12:44:34 -04:00
|
|
|
$formatter = WebhookFormatter::getDefault($event, $webhook, $detail, $user, time());
|
2022-03-26 16:38:03 -04:00
|
|
|
|
2022-03-26 12:44:34 -04:00
|
|
|
return $formatter->format();
|
|
|
|
}
|
2022-03-26 16:38:03 -04:00
|
|
|
}
|