File size: 4,001 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
<?php

declare(strict_types=1);

namespace Mautic\CampaignBundle\Tests\Controller;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use PHPUnit\Framework\Assert;
use Symfony\Component\DomCrawler\Crawler;

final class VisitedPageConditionControllerFunctionalTest extends MauticMysqlTestCase
{
    /**
     * @dataProvider fieldAndValueProvider
     *
     * @param array<mixed,mixed> $pageUrl
     * @param array<mixed,mixed> $startDate
     * @param array<mixed,mixed> $endDate
     * @param array<mixed,mixed> $accumulativeTime
     * @param array<mixed,mixed> $page
     */
    public function testCreatePageHitConditionForm(
        array $pageUrl,
        array $startDate,
        array $endDate,
        array $accumulativeTime,
        array $page
    ): void {
        // Fetch the campaign condition form.
        $uri = 's/campaigns/events/new?type=lead.pageHit&eventType=condition&campaignId=3&anchor=leadsource&anchorEventType=source&_=1682493324393&mauticUserLastActive=897&mauticLastNotificationId=';
        $this->client->request('GET', $uri, [], [], $this->createAjaxHeaders());
        $response = $this->client->getResponse();
        Assert::assertTrue($response->isOk(), $response->getContent());

        // Get the form HTML element out of the response, fill it in and submit.
        $responseData = json_decode($response->getContent(), true);
        $crawler      = new Crawler($responseData['newContent'], $this->client->getInternalRequest()->getUri());
        $form         = $crawler->filterXPath('//form[@name="campaignevent"]')->form();
        $form->setValues(
            [
                'campaignevent[anchor]'                                  => 'leadsource',
                'campaignevent[properties]['.$pageUrl[0].']'             => $pageUrl[1],
                'campaignevent[properties]['.$startDate[0].']'           => $startDate[1],
                'campaignevent[properties]['.$endDate[0].']'             => $endDate[1],
                'campaignevent[properties]['.$accumulativeTime[0].']'    => $accumulativeTime[1],
                'campaignevent[properties]['.$page[0].']'                => $page[1] ?? '',
                'campaignevent[type]'                                    => 'lead.pageHit',
                'campaignevent[eventType]'                               => 'condition',
                'campaignevent[anchorEventType]'                         => 'source',
                'campaignevent[campaignId]'                              => '3',
            ]
        );

        $this->client->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), [], $this->createAjaxHeaders());
        $response = $this->client->getResponse();
        Assert::assertTrue($response->isOk(), $response->getContent());
        $responseData = json_decode($response->getContent(), true);
        Assert::assertSame(1, $responseData['success'], print_r(json_decode($response->getContent(), true), true));
    }

    /**
     * @return array<mixed,mixed>
     */
    public static function fieldAndValueProvider(): array
    {
        return [
            [
                'page_url'           => ['page_url', 'https://example.com'],
                'startDate'          => ['startDate', (new \DateTime())->format('Y-m-d H:i:s')],
                'endDate'            => ['endDate', (new \DateTime())->modify('+ 5 days')->format('Y-m-d H:i:s')],
                'accumulative_time'  => ['accumulative_time', 5],
                'page'               => ['page', null],
            ],
            [
                'page_url'           => ['page_url', 'https://example.com'],
                'startDate'          => ['startDate', (new \DateTime())->format('Y-m-d H:i:s')],
                'endDate'            => ['endDate', (new \DateTime())->modify('+ 10 days')->format('Y-m-d H:i:s')],
                'accumulative_time'  => ['accumulative_time', null],
                'page'               => ['page', ''],
            ],
        ];
    }
}