File size: 6,954 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Controller;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\CoreBundle\Tests\Traits\ControllerTrait;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Entity\ListLead;
use Symfony\Component\HttpFoundation\Response;

class ListControllerTest extends MauticMysqlTestCase
{
    use ControllerTrait;

    /**
     * Index action should return status code 200.
     */
    public function testIndexAction(): void
    {
        $list = $this->createList();

        $this->em->persist($list);
        $this->em->flush();
        $this->em->clear();

        $urlAlias   = 'segments';
        $routeAlias = 'leadlist';
        $column     = 'dateModified';
        $column2    = 'name';
        $tableAlias = 'l.';

        $this->getControllerColumnTests($urlAlias, $routeAlias, $column, $tableAlias, $column2);
    }

    /**
     * Check if list contains correct values.
     */
    public function testViewList(): void
    {
        $list = $this->createList();
        $list->setDateAdded(new \DateTime('2020-02-07 20:29:02'));
        $list->setDateModified(new \DateTime('2020-03-21 20:29:02'));
        $list->setCreatedByUser('Test User');

        $this->em->persist($list);
        $this->em->flush();
        $this->em->clear();

        $this->client->request('GET', '/s/segments');
        $clientResponse = $this->client->getResponse();
        $this->assertSame(200, $clientResponse->getStatusCode(), 'Return code must be 200.');
        $this->assertStringContainsString('February 7, 2020', $clientResponse->getContent());
        $this->assertStringContainsString('March 21, 2020', $clientResponse->getContent());
        $this->assertStringContainsString('Test User', $clientResponse->getContent());
    }

    /**
     * Filtering should return status code 200.
     */
    public function testIndexActionWhenFiltering(): void
    {
        $this->client->request('GET', '/s/segments?search=has%3Aresults&tmpl=list');
        $clientResponse = $this->client->getResponse();
        $this->assertSame(200, $clientResponse->getStatusCode(), 'Return code must be 200.');
    }

    public function testSegmentView(): void
    {
        $contacts = $this->createContacts();
        $segment  = $this->addContactsToSegment($contacts, 'MySeg');
        $this->client->request('GET', sprintf('/s/segments/view/%d', $segment->getId()));
        $response = $this->client->getResponse();
        self::assertSame(Response::HTTP_OK, $response->getStatusCode());
        self::assertStringContainsString('MySeg', $response->getContent());
        // Make sure that contact grid is not loaded synchronously
        self::assertStringNotContainsString('Kane', $response->getContent());
        self::assertStringNotContainsString('Jacques', $response->getContent());
        // Make sure the data-target-url is not an absolute URL
        self::assertStringContainsString(sprintf('data-target-url="/s/segment/view/%s/contact/1"', $segment->getId()), $response->getContent());
    }

    public function testSegmentContactGrid(): void
    {
        $pageId   = 1;
        $contacts = $this->createContacts();
        $segment  = $this->addContactsToSegment($contacts, 'MySeg');
        $this->client->request('GET', sprintf('/s/segment/view/%d/contact/%d', $segment->getId(), $pageId));
        $response = $this->client->getResponse();
        self::assertSame(Response::HTTP_OK, $response->getStatusCode());
        self::assertStringContainsString('Kane', $response->getContent());
        self::assertStringContainsString('Jacques', $response->getContent());
    }

    private function createList(string $suffix = 'A'): LeadList
    {
        $list = new LeadList();
        $list->setName("Segment $suffix");
        $list->setPublicName("Segment $suffix");
        $list->setAlias("segment-$suffix");
        $list->setDateAdded(new \DateTime('2020-02-07 20:29:02'));
        $list->setDateModified(new \DateTime('2020-03-21 20:29:02'));
        $list->setCreatedByUser('Test User');

        return $list;
    }

    /**
     * @return Lead[]
     */
    private function createContacts(): array
    {
        $contact1 = new Lead();
        $contact1->setFirstname('Kane');
        $contact1->setLastname('Williamson');
        $contact1->setEmail('[email protected]');

        $contact2 = new Lead();
        $contact2->setFirstname('Jacques');
        $contact2->setLastname('Kallis');
        $contact2->setEmail('[email protected]');

        $this->em->persist($contact1);
        $this->em->persist($contact2);
        $this->em->flush();

        return [$contact1, $contact2];
    }

    /**
     * @param Lead[] $contacts
     */
    private function addContactsToSegment(array $contacts, string $segmentName): LeadList
    {
        $filters = [
            [
                'glue'       => 'and',
                'field'      => 'company',
                'object'     => 'lead',
                'type'       => 'text',
                'operator'   => 'contains',
                'properties' => [
                    'filter' => 'Acquia',
                ],
                'filter'  => 'Acquia',
                'display' => null,
            ],
        ];

        $segment = new LeadList();
        $segment->setName($segmentName);
        $segment->setPublicName($segmentName);
        $segment->setAlias(strtolower($segmentName));
        $segment->isPublished(true);
        $segment->setDateAdded(new \DateTime());
        $segment->setFilters($filters);
        $segment->setIsGlobal(true);
        $segment->setIsPreferenceCenter(false);
        $this->em->persist($segment);

        foreach ($contacts as $contact) {
            $segmentContacts = new ListLead();
            $segmentContacts->setList($segment);
            $segmentContacts->setLead($contact);
            $segmentContacts->setDateAdded(new \DateTime());
            $segmentContacts->setManuallyAdded(false);
            $segmentContacts->setManuallyRemoved(false);
            $this->em->persist($segmentContacts);
        }

        $this->em->flush();

        return $segment;
    }

    public function testCloneSegmentPage(): void
    {
        $list = $this->createList('clone');
        $list->setDateAdded(new \DateTime('2020-02-07 20:29:02'));
        $list->setDateModified(new \DateTime('2020-03-21 20:29:02'));
        $list->setCreatedByUser('Test User');

        $this->em->persist($list);
        $this->em->flush();
        $this->em->clear();

        $this->client->request('GET', sprintf('/s/segments/clone/%d', $list->getId()));

        $clientResponse = $this->client->getResponse();
        $this->assertSame(Response::HTTP_OK, $clientResponse->getStatusCode(), 'Return code must be 200.');
        self::assertStringContainsString('Segment clone', $clientResponse->getContent());
    }
}