File size: 3,525 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

namespace Mautic\WebhookBundle\Tests\Functional\Model;

use Doctrine\Common\Collections\Criteria;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\WebhookBundle\Entity\Event;
use Mautic\WebhookBundle\Entity\Webhook;
use Mautic\WebhookBundle\Entity\WebhookQueue;
use Mautic\WebhookBundle\Model\WebhookModel;
use PHPUnit\Framework\Assert;

final class WebhookModelTest extends MauticMysqlTestCase
{
    protected $useCleanupRollback = false;

    protected function setUp(): void
    {
        parent::setUp();

        // Cleanup from previous tests
        $this->connection->executeStatement('DELETE FROM '.MAUTIC_TABLE_PREFIX.'webhook_queue');
        $this->connection->executeStatement('ALTER TABLE '.MAUTIC_TABLE_PREFIX.'webhook_queue AUTO_INCREMENT = 1');
    }

    public function testEventsOrderByDirAsc(): void
    {
        $webhookModel = $this->getWebhookModel(Criteria::ASC);
        $webhook      = $this->createWebhookAndQueue();
        $queueArray   = $webhookModel->getWebhookQueues($webhook);

        // Order should be 1 to 10
        $counter = 1;

        foreach ($queueArray as $queuedEvent) {
            Assert::assertSame($counter, $queuedEvent->getId());

            $payload = json_decode($queuedEvent->getPayload(), true);
            Assert::assertSame($counter, $payload['spoof']);

            ++$counter;
        }

        Assert::assertSame(11, $counter);
    }

    public function testEventsOrderByDirDesc(): void
    {
        $webhookModel = $this->getWebhookModel(Criteria::DESC);
        $webhook      = $this->createWebhookAndQueue();
        $queueArray   = $webhookModel->getWebhookQueues($webhook);

        // Order should be 10 to 1
        $counter = 10;
        foreach ($queueArray as $queuedEvent) {
            Assert::assertSame($counter, $queuedEvent->getId());

            $payload = json_decode($queuedEvent->getPayload(), true);
            Assert::assertSame($counter, $payload['spoof']);

            --$counter;
        }

        Assert::assertSame(0, $counter);
    }

    private function createWebhookAndQueue(): Webhook
    {
        $webhook = new Webhook();

        $webhook->setName('Test Webhook');
        $webhook->setWebhookUrl('https://localhost');
        $webhook->setSecret('abc13');
        $this->em->persist($webhook);
        $this->em->flush();

        $event = new Event();
        $event->setWebhook($webhook);
        $event->setEventType('mautic.email_on_send');
        $this->em->persist($event);
        $this->em->flush();

        $counter = 1;
        while ($counter <= 10) {
            $this->createWebhookQueue($webhook, $event, ['spoof' => $counter]);

            ++$counter;
        }

        return $webhook;
    }

    /**
     * @param mixed[] $payload
     */
    private function createWebhookQueue(Webhook $webhook, Event $event, array $payload): void
    {
        $queue = new WebhookQueue();
        $queue->setDateAdded(new \DateTime());
        $queue->setEvent($event);
        $queue->setWebhook($webhook);
        $queue->setPayload(json_encode($payload));
        $this->em->persist($queue);
        $this->em->flush();
    }

    private function getWebhookModel(string $direction): WebhookModel
    {
        $webhookParams = [
            'queue_mode'         => WebhookModel::COMMAND_PROCESS,
            'events_orderby_dir' => $direction,
        ];

        $this->setUpSymfony($webhookParams);

        return static::getContainer()->get('mautic.webhook.model.webhook');
    }
}