File size: 5,163 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
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Controller;

use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\LeadField;
use PHPUnit\Framework\Assert;
use Symfony\Component\DomCrawler\Field\InputFormField;
use Symfony\Component\HttpFoundation\Request;

class FieldFunctionalTest extends MauticMysqlTestCase
{
    protected $useCleanupRollback = false;

    /**
     * @dataProvider provideFieldLength
     */
    public function testNewFieldVarcharFieldLength(int $expectedLength, ?int $inputLength = null): void
    {
        $fieldModel = static::getContainer()->get('mautic.lead.model.field');
        $field      = $this->createField('a', 'text', [], $inputLength);
        $fieldModel->saveEntity($field);

        $tablePrefix = static::getContainer()->getParameter('mautic.db_table_prefix');
        $columns     = $this->connection->createSchemaManager()->listTableColumns("{$tablePrefix}leads");
        $this->assertEquals($expectedLength, $columns[$field->getAlias()]->getLength());
    }

    public function testNewMultiSelectField(): void
    {
        $fieldModel = static::getContainer()->get('mautic.lead.model.field');
        $field      = $this->createField('s', 'select', ['properties' => ['list' => ['choice_a' => 'Choice A']]]);
        $fieldModel->saveEntity($field);

        $tablePrefix = static::getContainer()->getParameter('mautic.db_table_prefix');
        $columns     = $this->connection->createSchemaManager()->listTableColumns("{$tablePrefix}leads");
        $this->assertArrayHasKey('field_s', $columns);
    }

    public function testNewDateField(): void
    {
        $crawler = $this->client->request(Request::METHOD_GET, 's/contacts/fields/new');

        Assert::assertTrue($this->client->getResponse()->isOk(), $this->client->getResponse()->getContent());

        $form = $crawler->selectButton('Save')->form();

        $form['leadfield[label]']->setValue('Best Date Ever');
        $form['leadfield[type]']->setValue('date');

        $this->client->submit($form);

        $text = strip_tags($this->client->getResponse()->getContent());

        Assert::assertTrue($this->client->getResponse()->isOk(), $text);
        Assert::assertStringNotContainsString('New Custom Field', $text);
        Assert::assertStringNotContainsString('This form should not contain extra fields.', $text);
        Assert::assertStringContainsString('Edit Custom Field - Best Date Ever', $text);
    }

    public function testNewSelectField(): void
    {
        $crawler = $this->client->request(Request::METHOD_GET, 's/contacts/fields/new');

        Assert::assertTrue($this->client->getResponse()->isOk(), $this->client->getResponse()->getContent());

        $domDocument = $crawler->getNode(0)->ownerDocument;
        $inputLabel  = $domDocument->createElement('input');
        $inputLabel->setAttribute('type', 'text');

        $inputLabel->setAttribute('name', 'leadfield[properties][list][0][label]');
        $inputValue  = $domDocument->createElement('input');
        $inputValue->setAttribute('type', 'text');
        $inputValue->setAttribute('name', 'leadfield[properties][list][0][value]');

        $form        = $crawler->selectButton('Save')->form();
        $form->set(new InputFormField($inputLabel));
        $form->set(new InputFormField($inputValue));

        $form['leadfield[label]']->setValue('Test select field');
        $form['leadfield[type]']->setValue('select');
        $form['leadfield[properties][list][0][label]']->setValue('Label 1');
        $form['leadfield[properties][list][0][value]']->setValue('Value 1');

        $this->client->submit($form);

        $text = strip_tags($this->client->getResponse()->getContent());

        Assert::assertTrue($this->client->getResponse()->isOk(), $text);
        Assert::assertStringNotContainsString('New Custom Field', $text);
        Assert::assertStringNotContainsString('This form should not contain extra fields.', $text);
        Assert::assertStringContainsString('Edit Custom Field - Test select field', $text);
    }

    /**
     * @param array<string, mixed> $parameters
     */
    private function createField(string $suffix, string $type = 'text', array $parameters = [], ?int $charLength = null): LeadField
    {
        $field = new LeadField();
        $field->setName("Field $suffix");
        $field->setAlias("field_$suffix");
        $field->setDateAdded(new \DateTime());
        $field->setDateAdded(new \DateTime());
        $field->setDateModified(new \DateTime());
        $field->setType($type);
        if (!empty($charLength)) {
            $field->setCharLengthLimit($charLength);
        }
        $field->setObject('lead');
        isset($parameters['properties']) && $field->setProperties($parameters['properties']);

        return $field;
    }

    /**
     * @return iterable<array<mixed>>
     */
    public function provideFieldLength(): iterable
    {
        yield [ClassMetadataBuilder::MAX_VARCHAR_INDEXED_LENGTH, ClassMetadataBuilder::MAX_VARCHAR_INDEXED_LENGTH];
        yield [64, null];
    }
}