Completed
Push — master ( b3079f...65c6d8 )
by
unknown
02:54
created

ApiBundle/Controller/NodeControllerTest.php (4 issues)

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\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
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
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
        $block = $node->getArea('main')->getBlocks()[0];
220
        $blocksHeader = $node->getArea('header')->getBlocks();
221
222
        $blocksHeaderJson = array();
223
        foreach ($blocksHeader as $blockHeader) {
224
            $blocksHeaderJson[] = array('id' => $blockHeader->getId());
225
        }
226
        $blocksHeaderJson[] = array('id' => $block->getId());
227
        $requestContent = json_encode(array(
228
            'areas' => array(
229
                'header' => array(
230
                    'blocks' => $blocksHeaderJson
231
                ),
232
                'main' => array(
233
                    'blocks' => array()
234
                )
235
            )
236
        ));
237
238
        $this->client->request(
239
            'PUT',
240
            "/api/node/update-block-position/".$node->getSiteId()."/".$node->getNodeId()."/".$node->getVersion()."/".$node->getLanguage(),
241
            array(),
242
            array(),
243
            array(),
244
            $requestContent
245
        );
246
247
        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
248
        $dm = static::$kernel->getContainer()->get('object_manager');
249
250
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
251
        $blocksMain = $node->getArea('main')->getBlocks();
252
        $this->assertCount(0, $blocksMain);
253
254
        $blocksHeaderNew = $node->getArea('header')->getBlocks();
255
        $this->assertCount(4, $blocksHeaderNew);
256
257
        $node->getArea('main')->addBlock($block);
258
        $node->getArea('header')->setBlocks(new ArrayCollection($blocksHeader->toArray()));
259
        $dm->flush();
260
    }
261
262
    /**
263
     * Test update block position not granted
264
     */
265
    public function testUpdateBlockPositionNotGranted()
266
    {
267
        $this->username = 'userNoAccess';
268
        $this->password = 'userNoAccess';
269
        $this->logIn();
270
271
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
272
273
        $this->client->request(
274
            'PUT',
275
            "/api/node/update-block-position/".$node->getSiteId()."/".$node->getNodeId()."/".$node->getVersion()."/".$node->getLanguage()
276
        );
277
278
        $this->assertSame(403, $this->client->getResponse()->getStatusCode());
279
    }
280
281
    /**
282
     * Test delete block not granted
283
     */
284
    public function testDeleteBlockInAreaNotGranted()
285
    {
286
        $this->username = 'userNoAccess';
287
        $this->password = 'userNoAccess';
288
        $this->logIn();
289
290
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
291
        $block = $node->getArea('main')->getBlocks()[0];
292
293
        $this->client->request(
294
            'DELETE',
295
            "/api/node/delete-block/".$node->getNodeId()."/".$node->getSiteId()."/".$node->getLanguage()."/".$node->getVersion()."/main/".$block->getId()
296
        );
297
        $this->assertSame(403, $this->client->getResponse()->getStatusCode());
298
    }
299
300
    /**
301
     * Test delete block
302
     */
303 View Code Duplication
    public function testDeleteBlockInArea()
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...
304
    {
305
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
306
        $block = $node->getArea('main')->getBlocks()[0];
307
308
        $this->client->request(
309
            'DELETE',
310
            "/api/node/delete-block/".$node->getNodeId()."/".$node->getSiteId()."/".$node->getLanguage()."/".$node->getVersion()."/main/".$block->getId()
311
        );
312
        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
313
        $this->assertNull($this->blockRepository->findById($block->getId()));
314
315
        $dm = static::$kernel->getContainer()->get('object_manager');
316
        $dm->detach($node);
317
        $dm->detach($block);
318
319
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
320
        $blocks = $node->getArea('main')->getBlocks();
321
        $this->assertCount(0, $blocks);
322
323
        $node->getArea('main')->addBlock($block);
324
        $dm->persist($block);
325
326
        $dm->flush();
327
    }
328
329
    /**
330
     * Test delete transverse block
331
     */
332 View Code Duplication
    public function testDeleteTransverseBlockInArea()
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...
333
    {
334
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
335
        $block = $node->getArea('footer')->getBlocks()[0];
336
337
        $this->client->request(
338
            'DELETE',
339
            "/api/node/delete-block/".$node->getNodeId()."/".$node->getSiteId()."/".$node->getLanguage()."/".$node->getVersion()."/footer/".$block->getId()
340
        );
341
        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
342
        $this->assertEquals($block, $this->blockRepository->findById($block->getId()));
343
344
        $dm = static::$kernel->getContainer()->get('object_manager');
345
        $dm->detach($node);
346
        $dm->detach($block);
347
348
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
349
        $blocks = $node->getArea('footer')->getBlocks();
350
        $this->assertCount(1, $blocks);
351
352
        $node->getArea('footer')->addBlock($block);
353
        $dm->persist($block);
354
        $dm->flush();
355
    }
356
357
    /**
358
     * Test add block in area action
359
     */
360
    public function testAddBlockInAreaAction()
361
    {
362
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
363
        $blocks = $this->blockRepository->findTransverseBlock('tiny_mce_wysiwyg', '2', 'fr');
364
        $block = $blocks[0];
365
366
        $this->client->request(
367
            'PUT',
368
            "/api/node/add-block-in-area/".$node->getNodeId()."/".$node->getLanguage()."/".$node->getVersion()."/".$block->getId()."/footer/1"
369
        );
370
371
        $dm = static::$kernel->getContainer()->get('object_manager');
372
        $dm->detach($node);
373
        $dm->clear();
374
        $node = $this->nodeRepository->findInLastVersion('root', 'fr', '2');
375
        $footerAreaBlocks = $node->getArea('footer')->getBlocks();
376
        $addedBlock =  $footerAreaBlocks[1];
377
378
        $this->assertSame($block->getId(), $addedBlock->getId());
379
    }
380
}
381