Issues (3627)

CoreBundle/Tests/Unit/Form/Type/ConfigTypeTest.php (1 issue)

1
<?php
2
3
namespace Mautic\CoreBundle\Tests\Unit\Form\Type;
4
5
use Mautic\CoreBundle\Factory\IpLookupFactory;
6
use Mautic\CoreBundle\Form\Type\ConfigType;
7
use Mautic\CoreBundle\Helper\LanguageHelper;
8
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
9
use Mautic\PageBundle\Entity\PageRepository;
10
use Mautic\PageBundle\Form\Type\PageListType;
11
use Mautic\PageBundle\Model\PageModel;
12
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
13
use Symfony\Component\Form\FormBuilderInterface;
14
use Symfony\Component\Form\PreloadedExtension;
15
use Symfony\Component\Form\Test\TypeTestCase;
16
use Symfony\Component\Translation\TranslatorInterface;
17
use Symfony\Component\Validator\Validation;
18
19
class ConfigTypeTest extends TypeTestCase
20
{
21
    private $formBuilder;
22
    private $formType;
23
24
    protected function setUp(): void
25
    {
26
        $this->formBuilder = $this->createMock(FormBuilderInterface::class);
27
        $this->formType    = $this->getConfigFormType();
28
        parent::setUp();
29
    }
30
31
    public function testSubmitValidData()
32
    {
33
        $formData = [
34
            'site_url'             => 'http://example.com',
35
            'cache_path'           => 'tmp',
36
            'log_path'             => '/var/log',
37
            'image_path'           => '/tmp/sample-image.png',
38
            'cached_data_timeout'  => 30000,
39
            'date_format_full'     => 'F j, Y g:i:s a T',
40
            'date_format_short'    => 'D, M d - g:i:s a',
41
            'date_format_dateonly' => 'F j, Y',
42
            'date_format_timeonly' => 'g:i:s a',
43
        ];
44
45
        // $formData will retrieve data from the form submission; pass it as the second argument
46
        $form = $this->factory->create(ConfigType::class, $formData);
47
48
        // submit the data to the form directly
49
        $form->submit($formData);
50
51
        // This check ensures there are no transformation failures
52
        $this->assertTrue($form->isSynchronized());
53
54
        // check that $formData was modified as expected when the form was submitted
55
        $this->assertTrue($form->isValid());
56
    }
57
58
    private function getConfigFormType()
59
    {
60
        $translator      = $this->createMock(TranslatorInterface::class);
61
        $languageHelper  = $this->createMock(LanguageHelper::class);
62
        $ipLookupFactory = $this->createMock(IpLookupFactory::class);
63
64
        $languageHelper->expects($this->any())
65
                       ->method('fetchLanguages')
66
                       ->willReturn(['en' => ['name'=>'English']]);
67
68
        return new ConfigType($translator, $languageHelper, $ipLookupFactory, [], null);
69
    }
70
71
    protected function getExtensions()
72
    {
73
        $validator = Validation::createValidator();
0 ignored issues
show
The assignment to $validator is dead and can be removed.
Loading history...
74
75
        // or if you also need to read constraints from annotations
76
        $validator = Validation::createValidatorBuilder()
77
            ->getValidator();
78
        // create a type instance with the mocked dependencies
79
        $configType = $this->getConfigFormType();
80
81
        $repoMock = $this->createMock(PageRepository::class);
82
        $repoMock->expects($this->any())
83
                 ->method('getPageList')
84
                 ->willReturn([]);
85
86
        $pageModelMock = $this->createMock(PageModel::class);
87
        $pageModelMock->expects($this->any())
88
                      ->method('getRepository')
89
                      ->willReturn($repoMock);
90
        $permsMock    = $this->createMock(CorePermissions::class);
91
        $pageListType = new PageListType($pageModelMock, $permsMock);
92
93
        return [
94
            // register the type instances with the PreloadedExtension
95
            new ValidatorExtension($validator),
96
            new PreloadedExtension([$configType, $pageListType], []),
97
        ];
98
    }
99
}
100