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