Issues (3627)

Controller/Api/FormApiControllerFunctionalTest.php (4 issues)

1
<?php
2
3
namespace Mautic\FormBundle\Tests\Controller\Api;
4
5
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
6
use Symfony\Component\HttpFoundation\Response;
7
8
class FormApiControllerFunctionalTest extends MauticMysqlTestCase
9
{
10
    public function testFormWorkflow()
11
    {
12
        $payload = [
13
            'name'        => 'Form API test',
14
            'formType'    => 'standalone',
15
            'isPublished' => true,
16
            'description' => 'Functional API test',
17
            'fields'      => [
18
                [
19
                    'label'     => 'Email',
20
                    'alias'     => 'email',
21
                    'type'      => 'text',
22
                    'leadField' => 'email',
23
                ],
24
            ],
25
        ];
26
27
        // Create:
28
        $this->client->request('POST', '/api/forms/new', $payload);
29
        $clientResponse = $this->client->getResponse();
30
        $response       = json_decode($clientResponse->getContent(), true);
31
32
        if (!empty($response['errors'][0])) {
33
            $this->fail($response['errors'][0]['code'].': '.$response['errors'][0]['message']);
34
        }
35
        $this->assertSame(Response::HTTP_CREATED, $clientResponse->getStatusCode(), 'Return code must be 201.');
36
37
        $formId = $response['form']['id'];
38
        $this->assertGreaterThan(0, $formId);
39
        $this->assertEquals($payload['name'], $response['form']['name']);
40
        $this->assertEquals($payload['formType'], $response['form']['formType']);
41
        $this->assertEquals($payload['isPublished'], $response['form']['isPublished']);
42
        $this->assertEquals($payload['description'], $response['form']['description']);
43
        $this->assertIsArray($response['form']['fields']);
44
        $this->assertCount(count($payload['fields']), $response['form']['fields']);
45
        for ($i = 0; $i < count($payload['fields']); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
46
            $this->assertEquals($payload['fields'][$i]['label'], $response['form']['fields'][$i]['label']);
47
            $this->assertEquals($payload['fields'][$i]['alias'], $response['form']['fields'][$i]['alias']);
48
            $this->assertEquals($payload['fields'][$i]['type'], $response['form']['fields'][$i]['type']);
49
            $this->assertEquals($payload['fields'][$i]['leadField'], $response['form']['fields'][$i]['leadField']);
50
        }
51
52
        // Edit:
53
        $this->client->request('PATCH', "/api/forms/{$formId}/edit", ['name' => 'Form API renamed']);
54
        $clientResponse = $this->client->getResponse();
55
        $response       = json_decode($clientResponse->getContent(), true);
56
        $this->assertSame(Response::HTTP_OK, $clientResponse->getStatusCode());
57
        $this->assertSame($formId, $response['form']['id'], 'ID of the created form does not match with the edited one.');
58
        $this->assertEquals('Form API renamed', $response['form']['name']);
59
        $this->assertEquals($payload['formType'], $response['form']['formType']);
60
        $this->assertEquals($payload['isPublished'], $response['form']['isPublished']);
61
        $this->assertEquals($payload['description'], $response['form']['description']);
62
        $this->assertIsArray($response['form']['fields']);
63
        $this->assertCount(count($payload['fields']), $response['form']['fields']);
64
        for ($i = 0; $i < count($payload['fields']); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
65
            $this->assertEquals($payload['fields'][$i]['label'], $response['form']['fields'][$i]['label']);
66
            $this->assertEquals($payload['fields'][$i]['alias'], $response['form']['fields'][$i]['alias']);
67
            $this->assertEquals($payload['fields'][$i]['type'], $response['form']['fields'][$i]['type']);
68
            $this->assertEquals($payload['fields'][$i]['leadField'], $response['form']['fields'][$i]['leadField']);
69
        }
70
71
        // Get:
72
        $this->client->request('GET', "/api/forms/{$formId}");
73
        $clientResponse = $this->client->getResponse();
74
        $response       = json_decode($clientResponse->getContent(), true);
75
        $this->assertSame(Response::HTTP_OK, $clientResponse->getStatusCode());
76
        $this->assertSame($formId, $response['form']['id'], 'ID of the created form does not match with the fetched one.');
77
        $this->assertEquals('Form API renamed', $response['form']['name']);
78
        $this->assertEquals($payload['formType'], $response['form']['formType']);
79
        $this->assertEquals($payload['isPublished'], $response['form']['isPublished']);
80
        $this->assertEquals($payload['description'], $response['form']['description']);
81
        $this->assertIsArray($response['form']['fields']);
82
        $this->assertCount(count($payload['fields']), $response['form']['fields']);
83
        for ($i = 0; $i < count($payload['fields']); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
84
            $this->assertEquals($payload['fields'][$i]['label'], $response['form']['fields'][$i]['label']);
85
            $this->assertEquals($payload['fields'][$i]['alias'], $response['form']['fields'][$i]['alias']);
86
            $this->assertEquals($payload['fields'][$i]['type'], $response['form']['fields'][$i]['type']);
87
            $this->assertEquals($payload['fields'][$i]['leadField'], $response['form']['fields'][$i]['leadField']);
88
        }
89
90
        // Delete:
91
        $this->client->request('DELETE', "/api/forms/{$formId}/delete");
92
        $clientResponse = $this->client->getResponse();
93
        $this->assertSame(Response::HTTP_OK, $clientResponse->getStatusCode());
94
95
        // Get (ensure it's deleted):
96
        $this->client->request('GET', "/api/forms/{$formId}");
97
        $clientResponse = $this->client->getResponse();
98
        $response       = json_decode($clientResponse->getContent(), true);
99
100
        $this->assertSame(404, $clientResponse->getStatusCode());
101
        $this->assertSame(404, $response['errors'][0]['code']);
102
    }
103
104
    public function testFormWithChangeTagsAction()
105
    {
106
        // Create tag:
107
        $tag1Payload = ['tag' => 'add this'];
108
        $tag2Payload = ['tag' => 'remove this'];
109
110
        $this->client->request('POST', '/api/tags/new', $tag1Payload);
111
        $clientResponse = $this->client->getResponse();
112
        $response       = json_decode($clientResponse->getContent(), true);
113
        $tag1Id         = $response['tag']['id'];
114
115
        $this->client->request('POST', '/api/tags/new', $tag2Payload);
116
        $clientResponse = $this->client->getResponse();
117
        $response       = json_decode($clientResponse->getContent(), true);
118
        $tag2Id         = $response['tag']['id'];
119
120
        $payload = [
121
            'name'        => 'Form API test',
122
            'formType'    => 'standalone',
123
            'isPublished' => true,
124
            'description' => 'Functional API test',
125
            'fields'      => [
126
                [
127
                    'label'     => 'lab',
128
                    'alias'     => 'email',
129
                    'type'      => 'text',
130
                    'leadField' => 'email',
131
                ],
132
            ],
133
            'actions' => [
134
                [
135
                    'name'        => 'Add tags to contact',
136
                    'description' => 'action description',
137
                    'type'        => 'lead.changetags',
138
                    'order'       => 1,
139
                    'properties'  => [
140
                        'add_tags'    => [$tag1Id],
141
                        'remove_tags' => [$tag2Id],
142
                    ],
143
                ],
144
            ],
145
        ];
146
147
        // Create form with lead.changetags action:
148
        $this->client->request('POST', '/api/forms/new', $payload);
149
        $clientResponse = $this->client->getResponse();
150
        $response       = json_decode($clientResponse->getContent(), true);
151
152
        if (!empty($response['errors'][0])) {
153
            $this->fail($response['errors'][0]['code'].': '.$response['errors'][0]['message']);
154
        }
155
        $this->assertSame(Response::HTTP_CREATED, $clientResponse->getStatusCode(), 'Return code must be 201.');
156
157
        $formId = $response['form']['id'];
158
        $this->assertGreaterThan(0, $formId);
159
        $this->assertEquals($payload['name'], $response['form']['name']);
160
        $this->assertEquals($payload['formType'], $response['form']['formType']);
161
        $this->assertEquals($payload['isPublished'], $response['form']['isPublished']);
162
        $this->assertEquals($payload['description'], $response['form']['description']);
163
        $this->assertIsArray($response['form']['fields']);
164
        $this->assertCount(count($payload['fields']), $response['form']['fields']);
165
        for ($i = 0; $i < count($payload['fields']); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
166
            $this->assertEquals($payload['fields'][$i]['label'], $response['form']['fields'][$i]['label']);
167
            $this->assertEquals($payload['fields'][$i]['alias'], $response['form']['fields'][$i]['alias']);
168
            $this->assertEquals($payload['fields'][$i]['type'], $response['form']['fields'][$i]['type']);
169
            $this->assertEquals($payload['fields'][$i]['leadField'], $response['form']['fields'][$i]['leadField']);
170
        }
171
        $this->assertIsArray($response['form']['actions']);
172
        $this->assertCount(count($payload['actions']), $response['form']['actions']);
173
        $this->assertEquals($payload['actions'][0]['name'], $response['form']['actions'][0]['name']);
174
        $this->assertEquals($payload['actions'][0]['description'], $response['form']['actions'][0]['description']);
175
        $this->assertEquals($payload['actions'][0]['type'], $response['form']['actions'][0]['type']);
176
        $this->assertEquals($payload['actions'][0]['order'], $response['form']['actions'][0]['order']);
177
        $this->assertIsArray($response['form']['actions'][0]['properties']['add_tags']);
178
        $this->assertIsArray($response['form']['actions'][0]['properties']['remove_tags']);
179
        $this->assertEquals($tag1Payload['tag'], $response['form']['actions'][0]['properties']['add_tags'][0]);
180
        $this->assertEquals($tag2Payload['tag'], $response['form']['actions'][0]['properties']['remove_tags'][0]);
181
    }
182
}
183