]> BookStack Code Mirror - bookstack/blob - tests/Actions/WebhookFormatTesting.php
Added extendable/scalable formatter for webhook data
[bookstack] / tests / Actions / WebhookFormatTesting.php
1 <?php
2
3 namespace Tests\Actions;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\Webhook;
7 use BookStack\Actions\WebhookFormatter;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Chapter;
10 use BookStack\Entities\Models\Page;
11 use Illuminate\Support\Arr;
12 use Tests\TestCase;
13
14 class WebhookFormatTesting extends TestCase
15 {
16     public function test_entity_events_show_related_user_info()
17     {
18         $events = [
19             ActivityType::BOOK_UPDATE => Book::query()->first(),
20             ActivityType::CHAPTER_CREATE => Chapter::query()->first(),
21             ActivityType::PAGE_MOVE => Page::query()->first(),
22         ];
23
24         foreach ($events as $event => $entity) {
25             $data = $this->getWebhookData($event, $entity);
26
27             $this->assertEquals($entity->createdBy->name, Arr::get($data, 'related_item.created_by.name'));
28             $this->assertEquals($entity->updatedBy->id, Arr::get($data, 'related_item.updated_by.id'));
29             $this->assertEquals($entity->ownedBy->slug, Arr::get($data, 'related_item.owned_by.slug'));
30         }
31     }
32
33     public function test_page_create_and_update_events_show_revision_info()
34     {
35         /** @var Page $page */
36         $page = Page::query()->first();
37         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
38
39         $data = $this->getWebhookData(ActivityType::PAGE_UPDATE, $page);
40         $this->assertEquals($page->currentRevision->id, Arr::get($data, 'related_item.current_revision.id'));
41         $this->assertEquals($page->currentRevision->type, Arr::get($data, 'related_item.current_revision.type'));
42         $this->assertEquals('Update a', Arr::get($data, 'related_item.current_revision.summary'));
43     }
44
45     protected function getWebhookData(string $event, $detail): array
46     {
47         $webhook = Webhook::factory()->make();
48         $user = $this->getEditor();
49         $formatter = WebhookFormatter::getDefault($event, $webhook, $detail, $user, time());
50         return $formatter->format();
51     }
52 }