Completed
Push — master ( c67200...14b415 )
by amaury
06:24
created

NodeControllerTest::testAddBlockInAreaAction()   B

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 25
Code Lines 18

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace OpenOrchestra\FunctionalTests\ApiBundle\Controller;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use OpenOrchestra\FunctionalTests\Utils\AbstractAuthenticatedTest;
7
use OpenOrchestra\ModelInterface\Model\NodeInterface;
8
use OpenOrchestra\ModelInterface\Repository\BlockRepositoryInterface;
9
use OpenOrchestra\ModelInterface\Repository\NodeRepositoryInterface;
10
use OpenOrchestra\ModelInterface\Repository\StatusRepositoryInterface;
11
12
/**
13
 * Class NodeControllerTest
14
 *
15
 * @group apiFunctional
16
 */
17
class NodeControllerTest extends AbstractAuthenticatedTest
18
{
19
    /**
20
     * @var StatusRepositoryInterface
21
     */
22
    protected $statusRepository;
23
24
    /**
25
     * @var NodeRepositoryInterface
26
     */
27
    protected $nodeRepository;
28
29
    /** @var BlockRepositoryInterface */
30
    protected $blockRepository;
31
32
    /**
33
     * Set up the test
34
     */
35
    public function setUp()
36
    {
37
        parent::setUp();
38
        $this->nodeRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.node');
39
        $this->statusRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.status');
40
        $this->blockRepository = static::$kernel->getContainer()->get('open_orchestra_model.repository.block');
41
    }
42
43
    /**
44
     * Reset removing node after test
45
     */
46
    public function tearDown()
47
    {
48
        $nodes = $this->nodeRepository->findByNodeAndSite('fixture_page_contact', '2');
49
        $this->undeleteNodes($nodes);
50
        $this->republishNodes($nodes);
51
        static::$kernel->getContainer()->get('object_manager')->flush();
52
        parent::tearDown();
53
    }
54
55
    /**
56
     * Test delete action
57
     */
58
    public function testDeleteAction()
59
    {
60
        $this->markTestSkipped('To reactivate when API roles will be implemented');
61
62
        $node = $this->nodeRepository->findOnePublished('fixture_page_contact','fr','2');
63
        $node->getStatus()->setPublishedState(false);
64
        static::$kernel->getContainer()->get('object_manager')->flush();
65
66
        $nbNode = count($this->nodeRepository->findLastVersionByType('2'));
67
        $this->client->request('DELETE', '/api/node/fixture_page_contact/delete');
68
        $nodesDelete = $this->nodeRepository->findLastVersionByType('2');
69
70
        $this->assertCount($nbNode - 1, $nodesDelete);
71
    }
72
73
    /**
74
     * @param array $nodes
75
     */
76
    protected function undeleteNodes($nodes)
77
    {
78
        foreach ($nodes as $node) {
79
            $node->setDeleted(false);
80
        }
81
    }
82
83
    /**
84
     * @param array $nodes
85
     */
86
    protected function republishNodes($nodes)
87
    {
88
        foreach ($nodes as $node) {
89
            $node->getStatus()->setPublishedState(true);
90
        }
91
    }
92
93
    /**
94
     * Test node new version and references
95
     */
96
    public function testNewVersioneNode()
97
    {
98
        $this->markTestSkipped('To reactivate when API roles will be implemented');
99
100
        $node = $this->nodeRepository
101
            ->findInLastVersion('fixture_page_community', 'fr', '2');
102
        $this->client->request('POST', '/api/node/fixture_page_community/new-version?language=fr');
103
104
        $nodeLastVersion = $this->nodeRepository
105
            ->findInLastVersion('fixture_page_community', 'fr', '2');
106
107
        $this->assertSame($node->getVersion()+1, $nodeLastVersion->getVersion());
108
    }
109
110
    /**
111
     * Test creation of new language for a node
112
     */
113
    public function testCreateNewLanguageNode()
114
    {
115
        $this->markTestSkipped('To reactivate when API roles will be implemented');
116
117
        $this->client->request('GET', '/api/node/root/show-or-create', array('language' => 'de'));
118
119
        $node = $this->nodeRepository
120
            ->findInLastVersion('root', 'de', '2');
121
122
        $this->assertInstanceOf('OpenOrchestra\ModelInterface\Model\NodeInterface', $node);
123
        $this->assertSame(1, $node->getVersion());
124
        $this->assertSame('de', $node->getLanguage());
125
        static::$kernel->getContainer()->get('object_manager')->remove($node);
126
        static::$kernel->getContainer()->get('object_manager')->flush();
127
    }
128
129
    /**
130
     * @param NodeInterface $node
131
     *
132
     * @return int
133
     */
134
    public function countAreaRef(NodeInterface $node)
135
    {
136
        $areaRef = 0;
137
        foreach ($node->getBlocks() as $block) {
0 ignored issues
show
Bug introduced by Noël GILAIN
The method getBlocks() does not seem to exist on object<OpenOrchestra\Mod...ce\Model\NodeInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
            $areaRef = $areaRef + count($block->getAreas());
139
        }
140
141
        return $areaRef;
142
    }
143
144
    /**
145
     * @param string $name
146
     * @param int    $publishedVersion
147
     *
148
     * @dataProvider provideStatusNameAndPublishedVersion
149
     */
150
    public function testChangeNodeStatus($name, $publishedVersion)
151
    {
152
        $this->markTestSkipped('To reactivate when change status embed is fixed');
153
154
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
155
        $newStatus = $this->statusRepository->findOneByName($name);
0 ignored issues
show
Bug introduced by itkg-ngilain
The method findOneByName() does not seem to exist on object<OpenOrchestra\Mod...tusRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
        $requestContent = json_encode(array(
157
            'id' => $node->getId(),
158
            'status' => array(
159
                'id' => $newStatus->getId()
160
            )
161
        ));
162
        $this->client->request(
163
            'PUT',
164
            '/api/node/update-status',
165
            array(),
166
            array(),
167
            array(),
168
            $requestContent
169
        );
170
171
        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
172
        $newNode = $this->nodeRepository->findOnePublished('root', 'fr', '2');
173
        $this->assertEquals($publishedVersion, $newNode->getVersion());
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function provideStatusNameAndPublishedVersion()
180
    {
181
        return array(
182
            array('pending', 1),
183
            array('published', 2),
184
            array('draft', 1),
185
        );
186
    }
187
188
    /**
189
     * Test update not granted
190
     */
191
    public function testUpdateNotGranted()
192
    {
193
        $this->username = 'userNoAccess';
194
        $this->password = 'userNoAccess';
195
        $this->logIn();
196
197
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
198
        $requestContent = json_encode(array(
199
            'id' => $node->getId()
200
        ));
201
        $this->client->request(
202
            'PUT',
203
            '/api/node/update-status',
204
            array(),
205
            array(),
206
            array(),
207
            $requestContent
208
        );
209
210
        $this->assertSame(403, $this->client->getResponse()->getStatusCode());
211
    }
212
213
    /**
214
     * Test update block position
215
     */
216
    public function testUpdateBlockPosition()
217
    {
218
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
219
        $block0 = $node->getArea('main')->getBlocks()[0];
220
        $block1 = $node->getArea('main')->getBlocks()[1];
221
222
        $blocksMainJson = array();
223
        $blocksMainJson[] = array('id' => $block1->getId());
224
        $blocksMainJson[] = array('id' => $block0->getId());
225
226
        $requestContent = json_encode(array(
227
            'areas' => array(
228
                'main' => array(
229
                    'blocks' => $blocksMainJson
230
                )
231
            )
232
        ));
233
234
        $this->client->request(
235
            'PUT',
236
            "/api/node/update-block-position/".$node->getSiteId()."/".$node->getNodeId()."/".$node->getVersion()."/".$node->getLanguage(),
237
            array(),
238
            array(),
239
            array(),
240
            $requestContent
241
        );
242
243
        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
244
        $dm = static::$kernel->getContainer()->get('object_manager');
245
246
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
247
        $blocksMain = $node->getArea('main')->getBlocks();
248
        $this->assertCount(2, $blocksMain);
249
        $this->assertSame($node->getArea('main')->getBlocks()[0], $block1);
250
        $this->assertSame($node->getArea('main')->getBlocks()[1], $block0);
251
252
        $node->getArea('main')->setBlocks(new ArrayCollection(array($block0, $block1)));
253
        $dm->persist($node);
254
        $dm->flush();
255
    }
256
257
    /**
258
     * Test update block position not granted
259
     */
260
    public function testUpdateBlockPositionNotGranted()
261
    {
262
        $this->username = 'userNoAccess';
263
        $this->password = 'userNoAccess';
264
        $this->logIn();
265
266
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
267
268
        $this->client->request(
269
            'PUT',
270
            "/api/node/update-block-position/".$node->getSiteId()."/".$node->getNodeId()."/".$node->getVersion()."/".$node->getLanguage()
271
        );
272
273
        $this->assertSame(403, $this->client->getResponse()->getStatusCode());
274
    }
275
276
    /**
277
     * Test delete block not granted
278
     */
279
    public function testDeleteBlockInAreaNotGranted()
280
    {
281
        $this->username = 'userNoAccess';
282
        $this->password = 'userNoAccess';
283
        $this->logIn();
284
285
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
286
        $block = $node->getArea('main')->getBlocks()[0];
287
288
        $this->client->request(
289
            'DELETE',
290
            "/api/node/delete-block/".$node->getNodeId()."/".$node->getSiteId()."/".$node->getLanguage()."/".$node->getVersion()."/main/".$block->getId()
291
        );
292
        $this->assertSame(403, $this->client->getResponse()->getStatusCode());
293
    }
294
295
    /**
296
     * Test add block in area action
297
     */
298
    public function testAddBlockInAreaAction()
299
    {
300
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
301
        $blocks = $this->blockRepository->findBy(
0 ignored issues
show
Bug introduced by Amaury Lavieille
The method findBy() does not exist on OpenOrchestra\ModelInter...lockRepositoryInterface. Did you maybe mean findById()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
302
            array('component' => 'tiny_mce_wysiwyg'),
303
            array('siteId' => '2'),
304
            array('language' => 'fr'),
305
            array('transverse' => true)
306
        );
307
        $block = $blocks[0];
308
309
        $this->client->request(
310
            'PUT',
311
            "/api/node/add-block-in-area/".$node->getNodeId()."/".$node->getLanguage()."/".$node->getVersion()."/".$block->getId()."/main/1"
312
        );
313
314
        $dm = static::$kernel->getContainer()->get('object_manager');
315
        $dm->detach($node);
316
        $dm->clear();
317
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
318
        $mainAreaBlocks = $node->getArea('main')->getBlocks();
319
        $addedBlock = $mainAreaBlocks[1];
320
321
        $this->assertSame($block->getId(), $addedBlock->getId());
322
    }
323
324
    /**
325
     * Test delete block
326
     */
327 View Code Duplication
    public function testDeleteBlockInArea()
0 ignored issues
show
Duplication introduced by Amaury Lavieille
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...
328
    {
329
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
330
        $block = $node->getArea('main')->getBlocks()[0];
331
332
        $this->client->request(
333
            'DELETE',
334
            "/api/node/delete-block/".$node->getNodeId()."/".$node->getSiteId()."/".$node->getLanguage()."/".$node->getVersion()."/main/".$block->getId()
335
        );
336
        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
337
        $this->assertNull($this->blockRepository->findById($block->getId()));
338
339
        $dm = static::$kernel->getContainer()->get('object_manager');
340
        $dm->detach($node);
341
        $dm->detach($block);
342
343
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
344
        $blocks = $node->getArea('main')->getBlocks();
345
        $this->assertCount(1, $blocks);
346
        $node->getArea('main')->addBlock($block);
347
        $dm->persist($node);
348
        $dm->persist($block);
349
350
        $dm->flush();
351
    }
352
353
    /**
354
     * Test delete transverse block
355
     */
356 View Code Duplication
    public function testDeleteTransverseBlockInArea()
0 ignored issues
show
Duplication introduced by Amaury Lavieille
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...
357
    {
358
359
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
360
        $block = $node->getArea('main')->getBlocks()[0];
361
362
        $this->client->request(
363
            'DELETE',
364
            "/api/node/delete-block/".$node->getNodeId()."/".$node->getSiteId()."/".$node->getLanguage()."/".$node->getVersion()."/main/".$block->getId()
365
        );
366
        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
367
        $this->assertEquals($block, $this->blockRepository->findById($block->getId()));
368
369
        $dm = static::$kernel->getContainer()->get('object_manager');
370
        $dm->detach($node);
371
        $dm->detach($block);
372
373
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
374
        $blocks = $node->getArea('main')->getBlocks();
375
        $this->assertCount(1, $blocks);
376
377
        $node->getArea('main')->addBlock($block);
378
        $dm->persist($block);
379
        $dm->persist($node);
380
        $dm->flush();
381
    }
382
}
383