Completed
Push — master ( c3eba2...34879d )
by amaury
05:12 queued 01:44
created

EventSubscriber/BlockFormTypeSubscriberTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace OpenOrchestra\FunctionalTests\BackofficeBundle\EventSubscriber;
4
5
use OpenOrchestra\FunctionalTests\Utils\AbstractAuthenticatedTest;
6
use OpenOrchestra\DisplayBundle\DisplayBlock\Strategies\ConfigurableContentStrategy;
7
use OpenOrchestra\DisplayBundle\DisplayBlock\Strategies\ContentListStrategy;
8
use OpenOrchestra\DisplayBundle\DisplayBlock\Strategies\VideoStrategy;
9
use OpenOrchestra\ModelBundle\Document\Block;
10
use OpenOrchestra\ModelInterface\Model\BlockInterface;
11
use Symfony\Component\Form\FormFactoryInterface;
12
use OpenOrchestra\ModelInterface\Repository\ReadContentRepositoryInterface;
13
use OpenOrchestra\ModelInterface\Repository\RepositoryTrait\KeywordableTraitInterface;
14
15
/**
16
 * Class BlockFormTypeSubscriberTest
17
 *
18
 * @group backofficeTest
19
 */
20
class BlockFormTypeSubscriberTest extends AbstractAuthenticatedTest
21
{
22
    /**
23
     * @var FormFactoryInterface
24
     */
25
    protected $formFactory;
26
    protected $keywords;
27
    protected $keywordRepository;
28
29
    /**
30
     * Set up the test
31
     */
32
    public function setUp()
33
    {
34
        parent::setUp();
35
        $this->keywordRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.keyword');
36
        $this->formFactory = static::$kernel->getContainer()->get('form.factory');
37
    }
38
39
    /**
40
     * Test video block : checkbox unique to uncheck
41
     */
42
    public function testVideoBlock()
43
    {
44
        $block = new Block();
45
        $block->setComponent(VideoStrategy::NAME);
46
        $block->addAttribute('videoType', 'youtube');
47
        $block->addAttribute('youtubeFs', true);
48
        $formType =  static::$kernel->getContainer()->get('open_orchestra_backoffice.generate_form_manager')->getFormType($block);
49
        $form = $this->formFactory->create($formType, $block, array('csrf_protection' => false));
50
51
         $form->submit(array(
52
             'style' => 'default',
53
             'videoType' => 'youtube',
54
             'youtubeVideoId' => 'videoId',
55
             'youtubeAutoplay' => true,
56
         ));
57
58
         $this->assertTrue($form->isSynchronized());
59
         /** @var BlockInterface $data */
60
         $data = $form->getConfig()->getData();
61
         $this->assertBlock($data);
62
         $this->assertSame('videoId', $data->getAttribute('youtubeVideoId'));
63
         $this->assertTrue($data->getAttribute('youtubeAutoplay'));
64
         $this->assertFalse($data->getAttribute('youtubeFs'));
65
    }
66
67
    /**
68
     * @param string $component
69
     * @param array  $value
70
     *
71
     * @dataProvider provideComponentAndData
72
     */
73
    public function testMultipleBlock($component, $value)
74
    {
75
        $block = new Block();
76
        $block->setComponent($component);
77
        $formType =  static::$kernel->getContainer()->get('open_orchestra_backoffice.generate_form_manager')->getFormType($block);
78
79
        $form = $this->formFactory->create($formType, $block, array('csrf_protection' => false));
80
81
        $submittedValue = array_merge(array('style' => 'default'), $value);
82
        $form->submit($submittedValue);
83
84
        $this->assertTrue($form->isSynchronized());
85
        /** @var BlockInterface $data */
86
        $data = $form->getConfig()->getData();
87
        $this->assertBlock($data);
88
        foreach ($value as $key => $sendData) {
89
            $this->assertSame($sendData, $data->getAttribute($key));
90
        }
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function provideComponentAndData()
97
    {
98
        return array(
99
            array(ContentListStrategy::NAME, array(
100
                'contentNodeId' => 'root',
101
                'contentSearch' => array(
102
                  'contentType' => 'news',
103
                  'choiceType' => 'choice_and',
104
                  'keywords' => null,
105
                ),
106
                'characterNumber' => 150,
107
                'contentTemplateEnabled' => true,
108
            )),
109
            array(ConfigurableContentStrategy::NAME, array(
110
                'contentSearch' => array(
111
                    'contentType' => 'car',
112
                    'choiceType' => ReadContentRepositoryInterface::CHOICE_AND,
113
                    'keywords' => null,
114
                    'contentId' => null,
115
                ),
116
                'contentTemplateEnabled' => true,
117
            ))
118
        );
119
    }
120
121
    /**
122
     * @param string $component
123
     * @param array  $value
124
     *
125
     * @dataProvider provideComponentAndDataAndTransformedValue
126
     */
127
    public function testMultipleBlockWithDataTransformation($component, $value)
128
    {
129
        $block = new Block();
130
        $block->setComponent($component);
131
        $formType =  static::$kernel->getContainer()->get('open_orchestra_backoffice.generate_form_manager')->getFormType($block);
132
133
        $form = $this->formFactory->create($formType, $block, array('csrf_protection' => false));
134
        $submittedValue = array_merge(array('style' => 'default'), $value);
135
        $value['contentSearch']['keywords'] = $this->replaceKeywordLabelById($value['contentSearch']['keywords']);
136
        $form->submit($submittedValue);
137
138
        $this->assertTrue($form->isSynchronized());
139
        /** @var BlockInterface $data */
140
        $data = $form->getConfig()->getData();
141
        $this->assertBlock($data);
142
        foreach ($value as $key => $receivedData) {
143
            $this->assertSame($receivedData, $data->getAttribute($key));
144
        }
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function provideComponentAndDataAndTransformedValue()
151
    {
152
        return array(
153
            array(ContentListStrategy::NAME, array(
154
                'contentNodeId' => 'root',
155
                'contentTemplateEnabled' => true,
156
                'characterNumber' => 150,
157
                'contentSearch' => array(
158
                    'contentType' => 'news',
159
                    'choiceType' => ReadContentRepositoryInterface::CHOICE_AND,
160
                    'keywords' => 'lorem AND ipsum',
161
                    )
162
            )),
163
        );
164
    }
165
166
    /**
167
     * @param $data
168
     */
169
    protected function assertBlock($data)
170
    {
171
        $this->assertInstanceOf('OpenOrchestra\ModelInterface\Model\BlockInterface', $data);
172
        $this->assertSame('default', $data->getStyle());
173
    }
174
175
    /**
176
     * @param string $condition
177
     *
178
     * @return array
179
     */
180 View Code Duplication
    protected function replaceKeywordLabelById($condition)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    {
182
        $conditionWithoutOperator = preg_replace(explode('|', KeywordableTraitInterface::OPERATOR_SPLIT), ' ', $condition);
183
        $conditionArray = explode(' ', $conditionWithoutOperator);
184
185
        foreach ($conditionArray as $keyword) {
186
            if ($keyword != '') {
187
                $keywordDocument = $this->keywordRepository->findOneByLabel($keyword);
188
                if (!is_null($keywordDocument)) {
189
                    $condition = str_replace($keyword, $keywordDocument->getId(), $condition);
190
                } else {
191
                    return '';
192
                }
193
            }
194
        }
195
196
        return $condition;
197
    }
198
}
199