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 Kunstmaan\NodeBundle\Tests\Helper; |
||
4 | |||
5 | use Doctrine\Common\Collections\ArrayCollection; |
||
6 | use Doctrine\Common\Persistence\ObjectRepository; |
||
7 | use Doctrine\ORM\EntityManager; |
||
8 | use Doctrine\ORM\EntityManagerInterface; |
||
9 | use Kunstmaan\AdminBundle\Entity\User; |
||
10 | use Kunstmaan\AdminBundle\Helper\CloneHelper; |
||
11 | use Kunstmaan\NodeBundle\Entity\AbstractPage; |
||
12 | use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
||
13 | use Kunstmaan\NodeBundle\Entity\Node; |
||
14 | use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
||
15 | use Kunstmaan\NodeBundle\Entity\NodeVersion; |
||
16 | use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent; |
||
17 | use Kunstmaan\NodeBundle\Event\Events; |
||
18 | use Kunstmaan\NodeBundle\Event\NodeEvent; |
||
19 | use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent; |
||
20 | use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher; |
||
21 | use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper; |
||
22 | use Kunstmaan\NodeBundle\Helper\NodeHelper; |
||
23 | use Kunstmaan\NodeBundle\Repository\NodeRepository; |
||
24 | use Kunstmaan\NodeBundle\Repository\NodeTranslationRepository; |
||
25 | use Kunstmaan\NodeBundle\Repository\NodeVersionRepository; |
||
26 | use Symfony\Component\EventDispatcher\EventDispatcher; |
||
27 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
||
28 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
29 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||
30 | |||
31 | class TestPage extends AbstractPage implements HasNodeInterface |
||
32 | { |
||
33 | /** |
||
34 | * @return array |
||
35 | */ |
||
36 | public function getPossibleChildTypes() |
||
37 | { |
||
38 | return []; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | class NodeHelperTest extends \PHPUnit_Framework_TestCase |
||
43 | { |
||
44 | /** @var \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface $em */ |
||
45 | private $em; |
||
46 | |||
47 | /** @var \PHPUnit_Framework_MockObject_MockObject|NodeRepository */ |
||
48 | private $repository; |
||
49 | |||
50 | /** @var NodeHelper */ |
||
51 | private $nodeHelper; |
||
52 | |||
53 | /** @var \PHPUnit_Framework_MockObject_MockObject|NodeAdminPublisher */ |
||
54 | private $nodeAdminPublisher; |
||
55 | |||
56 | /** @var \PHPUnit_Framework_MockObject_MockObject|EventDispatcher */ |
||
57 | private $eventDispatcher; |
||
58 | |||
59 | /** @var \PHPUnit_Framework_MockObject_MockObject|TokenStorageInterface */ |
||
60 | private $tokenStorage; |
||
61 | |||
62 | /** @var \PHPUnit_Framework_MockObject_MockObject|NodeVersionLockHelper */ |
||
63 | private $nodeVersionLockHelper; |
||
64 | |||
65 | /** @var \PHPUnit_Framework_MockObject_MockObject|CloneHelper */ |
||
66 | private $cloneHelper; |
||
67 | |||
68 | /** @var string */ |
||
69 | private $locale = 'en'; |
||
70 | |||
71 | /** @var User */ |
||
72 | private $user; |
||
73 | |||
74 | public function setUp() |
||
75 | { |
||
76 | $this->createORM(); |
||
77 | $this->nodeHelper = $this->createNodeHelper(); |
||
78 | } |
||
79 | |||
80 | public function testUpdatePage() |
||
81 | { |
||
82 | /** |
||
83 | * @var TestPage $page |
||
84 | * @var NodeTranslation $nodeTranslation |
||
85 | */ |
||
86 | list($page, $nodeTranslation, $node) = $this->createNodeEntities(); |
||
87 | $nodeVersion = $nodeTranslation->getPublicNodeVersion(); |
||
88 | |||
89 | $this->em |
||
90 | ->expects($this->exactly(3)) |
||
91 | ->method('persist') |
||
92 | ->withConsecutive( |
||
93 | [$this->equalTo($nodeTranslation)], |
||
94 | [$this->equalTo($nodeVersion)], |
||
95 | [$this->equalTo($node)] |
||
96 | ); |
||
97 | |||
98 | $this->eventDispatcher |
||
99 | ->expects($this->exactly(2)) |
||
100 | ->method('dispatch') |
||
101 | ->withConsecutive( |
||
102 | [$this->equalTo(Events::PRE_PERSIST), $this->equalTo(new NodeEvent($node, $nodeTranslation, $nodeVersion, $page))], |
||
103 | [$this->equalTo(Events::POST_PERSIST), $this->equalTo(new NodeEvent($node, $nodeTranslation, $nodeVersion, $page))] |
||
104 | ); |
||
105 | |||
106 | $this->nodeHelper->updatePage( |
||
107 | $node, |
||
108 | $nodeTranslation, |
||
109 | $nodeTranslation->getPublicNodeVersion(), |
||
110 | $page, |
||
111 | false, |
||
112 | null |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | public function testCreatePage() |
||
117 | { |
||
118 | $title = 'Test page'; |
||
119 | $user = new User(); |
||
120 | |||
121 | list($homePage, , $nodeHomePage) = $this->createNodeEntities('Homepage'); |
||
122 | |||
123 | /** |
||
124 | * @var TestPage $page |
||
125 | * @var NodeTranslation $nodeTranslationChildPage |
||
126 | */ |
||
127 | list($page, $nodeTranslationChildPage, $nodeChildPage) = $this->createNodeEntities($title); |
||
128 | |||
129 | $expectedTestPageCreateNodeFor = new TestPage(); |
||
130 | $expectedTestPageCreateNodeFor->setTitle($title); |
||
131 | $expectedTestPageCreateNodeFor->setParent($homePage); |
||
132 | |||
133 | $nodeRepository = $this->getMockBuilder(NodeRepository::class) |
||
134 | ->disableOriginalConstructor() |
||
135 | ->getMock(); |
||
136 | $nodeRepository |
||
137 | ->expects($this->once()) |
||
138 | ->method('createNodeFor') |
||
139 | ->with( |
||
140 | $this->equalTo($expectedTestPageCreateNodeFor), |
||
141 | $this->equalTo($this->locale), |
||
142 | $this->equalTo($user) |
||
143 | ) |
||
144 | ->willReturn($nodeChildPage); |
||
145 | |||
146 | |||
147 | $nodeTranslationRepository = $this->getMockBuilder(NodeTranslationRepository::class) |
||
148 | ->disableOriginalConstructor() |
||
149 | ->getMock(); |
||
150 | $nodeTranslationRepository |
||
151 | ->expects($this->once()) |
||
152 | ->method('getMaxChildrenWeight') |
||
153 | ->willReturn(1); |
||
154 | |||
155 | $this->em |
||
156 | ->method('getRepository') |
||
157 | ->withConsecutive( |
||
158 | [$this->equalTo('KunstmaanNodeBundle:Node')], |
||
159 | [$this->equalTo('KunstmaanNodeBundle:NodeTranslation')] |
||
160 | ) |
||
161 | ->willReturnOnConsecutiveCalls( |
||
162 | $nodeRepository, |
||
163 | $nodeTranslationRepository |
||
164 | ); |
||
165 | |||
166 | $expectedEvent = new NodeEvent( |
||
167 | $nodeChildPage, $nodeTranslationChildPage, $nodeTranslationChildPage->getPublicNodeVersion(), $expectedTestPageCreateNodeFor |
||
168 | ); |
||
169 | $this->eventDispatcher |
||
170 | ->expects($this->once()) |
||
171 | ->method('dispatch') |
||
172 | ->with($this->equalTo(Events::ADD_NODE), $this->equalTo($expectedEvent)) |
||
173 | ; |
||
174 | |||
175 | $result = $this->nodeHelper->createPage(TestPage::class, $title, $this->locale, $nodeHomePage); |
||
176 | |||
177 | $this->assertInstanceOf(NodeTranslation::class, $result); |
||
178 | $this->assertEquals(2, $result->getWeight()); |
||
179 | $this->assertEquals($title, $result->getTitle()); |
||
180 | } |
||
181 | |||
182 | public function testDeletePage() |
||
183 | { |
||
184 | /** |
||
185 | * @var Node $nodeHomePage |
||
186 | * @var NodeTranslation $nodeTranslationHomePage |
||
187 | */ |
||
188 | list($homePage, $nodeTranslationHomePage, $nodeHomePage) = $this->createNodeEntities('Homepage'); |
||
189 | $nodeVersionHomePage = $nodeTranslationHomePage->getPublicNodeVersion(); |
||
190 | |||
191 | /** |
||
192 | * @var TestPage $page |
||
193 | * @var NodeTranslation $nodeTranslationChildPage |
||
194 | */ |
||
195 | list($page, $nodeTranslationChildPage, $nodeChildPage) = $this->createNodeEntities('Test page'); |
||
196 | $nodeVersionChildPage = $nodeTranslationChildPage->getPublicNodeVersion(); |
||
197 | $nodeHomePage->addNode($nodeChildPage); |
||
198 | |||
199 | $this->eventDispatcher |
||
200 | ->expects($this->exactly(4)) |
||
201 | ->method('dispatch') |
||
202 | ->withConsecutive( |
||
203 | [$this->equalTo(Events::PRE_DELETE), $this->equalTo(new NodeEvent($nodeHomePage, $nodeTranslationHomePage, $nodeVersionHomePage, $homePage))], |
||
204 | [$this->equalTo(Events::PRE_DELETE), $this->equalTo(new NodeEvent($nodeChildPage, $nodeTranslationChildPage, $nodeVersionChildPage, $page))], |
||
205 | [$this->equalTo(Events::POST_DELETE), $this->equalTo(new NodeEvent($nodeChildPage, $nodeTranslationChildPage, $nodeVersionChildPage, $page))], |
||
206 | [$this->equalTo(Events::POST_DELETE), $this->equalTo(new NodeEvent($nodeHomePage, $nodeTranslationHomePage, $nodeVersionHomePage, $homePage))] |
||
207 | ); |
||
208 | ; |
||
209 | |||
210 | $result = $this->nodeHelper->deletePage($nodeHomePage, $this->locale); |
||
211 | |||
212 | $this->assertTrue($result->getNode()->isDeleted()); |
||
213 | $this->assertTrue($nodeHomePage->isDeleted()); |
||
214 | $this->assertTrue($nodeChildPage->isDeleted()); |
||
215 | } |
||
216 | |||
217 | public function testPrepareNodeVersionForPublic() |
||
218 | { |
||
219 | $user = new User(); |
||
220 | |||
221 | $page = new TestPage(); |
||
222 | $page->setTitle('Test'); |
||
223 | $page->setId(1); |
||
224 | |||
225 | $nodeVersion = new NodeVersion(); |
||
226 | $nodeVersion->setType(NodeVersion::PUBLIC_VERSION); |
||
227 | $nodeVersion->setRef($page); |
||
228 | |||
229 | $nodeVersion = $this->getMockBuilder(NodeVersion::class)->getMock(); |
||
230 | $nodeVersion |
||
231 | ->method('getType') |
||
232 | ->willReturn(NodeVersion::PUBLIC_VERSION); |
||
233 | $nodeVersion |
||
234 | ->expects($this->once()) |
||
235 | ->method('getRef') |
||
236 | ->willReturn($page); |
||
237 | $nodeVersion |
||
238 | ->method('getUpdated') |
||
239 | ->willReturn((new \DateTime('-1 hour'))); |
||
240 | |||
241 | $nodeTranslation = new NodeTranslation(); |
||
242 | $nodeTranslation->setLang($this->locale); |
||
243 | $nodeTranslation->addNodeVersion($nodeVersion); |
||
244 | |||
245 | $this->nodeAdminPublisher |
||
246 | ->expects($this->once()) |
||
247 | ->method('createPublicVersion') |
||
248 | ->with( |
||
249 | $this->equalTo($page), |
||
250 | $this->equalTo($nodeTranslation), |
||
251 | $this->equalTo($nodeVersion), |
||
252 | $this->equalTo($user) |
||
253 | ); |
||
254 | |||
255 | $this->nodeHelper->prepareNodeVersion($nodeVersion, $nodeTranslation, 10, true); |
||
256 | } |
||
257 | |||
258 | public function testPrepareNodeVersionForDraft() |
||
259 | { |
||
260 | $page = new TestPage(); |
||
261 | $page->setTitle('Test'); |
||
262 | $page->setId(1); |
||
263 | |||
264 | $nodeVersion = new NodeVersion(); |
||
265 | $nodeVersion->setType(NodeVersion::PUBLIC_VERSION); |
||
266 | $nodeVersion->setRef($page); |
||
267 | |||
268 | $nodeVersion = $this->getMockBuilder(NodeVersion::class)->getMock(); |
||
269 | $nodeVersion |
||
270 | ->method('getType') |
||
271 | ->willReturn(NodeVersion::DRAFT_VERSION); |
||
272 | $nodeVersion |
||
273 | ->expects($this->once()) |
||
274 | ->method('getRef') |
||
275 | ->willReturn($page); |
||
276 | $nodeVersion |
||
277 | ->method('getUpdated') |
||
278 | ->willReturn((new \DateTime('-1 hour'))); |
||
279 | |||
280 | $nodeTranslation = new NodeTranslation(); |
||
281 | $nodeTranslation->setLang($this->locale); |
||
282 | $nodeTranslation->addNodeVersion($nodeVersion); |
||
283 | |||
284 | /** @var \PHPUnit_Framework_MockObject_MockObject|NodeHelper $nodeHelper */ |
||
285 | $nodeHelper = $this->getMockBuilder(NodeHelper::class) |
||
286 | ->setConstructorArgs([ |
||
287 | $this->em, |
||
288 | $this->nodeAdminPublisher, |
||
289 | $this->tokenStorage, |
||
290 | $this->cloneHelper, |
||
291 | $this->eventDispatcher |
||
292 | ]) |
||
293 | ->setMethods(['createDraftVersion']) |
||
294 | ->getMock(); |
||
295 | $nodeHelper |
||
296 | ->expects($this->once()) |
||
297 | ->method('createDraftVersion') |
||
298 | ->willReturn(true); |
||
299 | |||
300 | $nodeHelper->prepareNodeVersion($nodeVersion, $nodeTranslation, 10, true); |
||
301 | } |
||
302 | |||
303 | public function testCreateDraftVersion() |
||
304 | { |
||
305 | /** |
||
306 | * @var TestPage $page |
||
307 | * @var NodeTranslation $nodeTranslation |
||
308 | */ |
||
309 | list($page, $nodeTranslation, $node) = $this->createNodeEntities(); |
||
310 | $originalNodeVersion = new NodeVersion(); |
||
311 | |||
312 | $this->cloneHelper |
||
313 | ->expects($this->once()) |
||
314 | ->method('deepCloneAndSave') |
||
315 | ->willReturn($page); |
||
316 | |||
317 | $publicNodeVersion = new NodeVersion(); |
||
318 | $publicNodeVersion->setRef($page); |
||
319 | $publicNodeVersion->setType(NodeVersion::PUBLIC_VERSION); |
||
320 | |||
321 | $nodeVersionRepository = $this->getMockBuilder(NodeVersionRepository::class) |
||
322 | ->disableOriginalConstructor() |
||
323 | ->getMock(); |
||
324 | $nodeVersionRepository |
||
325 | ->method('createNodeVersionFor') |
||
326 | ->willReturn($publicNodeVersion); |
||
327 | |||
328 | $this->em->method('getRepository') |
||
329 | ->with('KunstmaanNodeBundle:NodeVersion') |
||
330 | ->willReturn($nodeVersionRepository); |
||
331 | |||
332 | $this->eventDispatcher |
||
333 | ->expects($this->once()) |
||
334 | ->method('dispatch') |
||
335 | ->with($this->equalTo(Events::CREATE_DRAFT_VERSION), $this->equalTo(new NodeEvent($node, $nodeTranslation, $originalNodeVersion, $page))); |
||
336 | |||
337 | $result = $this->nodeHelper->createDraftVersion($page, $nodeTranslation, $originalNodeVersion); |
||
338 | |||
339 | $this->assertInstanceOf(NodeVersion::class, $result); |
||
340 | $this->assertEquals(NodeVersion::DRAFT_VERSION, $result->getType()); |
||
341 | $this->assertEquals($publicNodeVersion, $result->getOrigin()); |
||
342 | } |
||
343 | |||
344 | public function testGetPageWithNodeInterface() |
||
345 | { |
||
346 | $refId = 10; |
||
347 | |||
348 | $page = new TestPage(); |
||
349 | $page->setTitle('Test'); |
||
350 | $page->setId($refId); |
||
351 | |||
352 | $nodeVersion = new NodeVersion(); |
||
353 | $nodeVersion->setType(NodeVersion::PUBLIC_VERSION); |
||
354 | $nodeVersion->setRef($page); |
||
355 | |||
356 | $nodeTranslation = new NodeTranslation(); |
||
357 | $nodeTranslation->setLang($this->locale); |
||
358 | $nodeTranslation->addNodeVersion($nodeVersion); |
||
359 | |||
360 | $node = new Node(); |
||
361 | $node->addNodeTranslation($nodeTranslation); |
||
362 | |||
363 | $repository = $this->getMockBuilder(ObjectRepository::class)->getMock(); |
||
364 | $repository |
||
365 | ->expects($this->once()) |
||
366 | ->method('find') |
||
367 | ->with($refId) |
||
368 | ->willReturn($page); |
||
369 | |||
370 | $this->em |
||
371 | ->expects($this->once()) |
||
372 | ->method('getRepository') |
||
373 | ->with($this->equalTo(TestPage::class)) |
||
374 | ->willReturn($repository); |
||
375 | |||
376 | $this->nodeHelper->getPageWithNodeInterface($node, $this->locale); |
||
377 | } |
||
378 | |||
379 | public function testCopyPageFromOtherLanguage() |
||
380 | { |
||
381 | $targetLocale = 'nl'; |
||
382 | $targetPage = new TestPage(); |
||
383 | |||
384 | /** |
||
385 | * @var TestPage $sourcePage |
||
386 | * @var NodeTranslation $sourceNodeTranslation |
||
387 | */ |
||
388 | list($sourcePage, $sourceNodeTranslation, $node) = $this->createNodeEntities(); |
||
389 | $sourceNodeNodeVersion = $sourceNodeTranslation->getPublicNodeVersion(); |
||
390 | |||
391 | $this->cloneHelper |
||
392 | ->expects($this->once()) |
||
393 | ->method('deepCloneAndSave') |
||
394 | ->with($sourcePage) |
||
395 | ->willReturn($targetPage); |
||
396 | |||
397 | $expectedNodeVersion = new NodeVersion(); |
||
398 | $expectedNodeVersion->setType(NodeVersion::PUBLIC_VERSION); |
||
399 | $expectedNodeVersion->setRef($targetPage); |
||
400 | |||
401 | $expectedNodeTranslation = new NodeTranslation(); |
||
402 | $expectedNodeTranslation->setNode($node); |
||
403 | $expectedNodeTranslation->setLang($targetLocale); |
||
404 | $expectedNodeTranslation->setPublicNodeVersion($expectedNodeVersion); |
||
405 | |||
406 | $repository = $this->getMockBuilder(NodeTranslationRepository::class) |
||
407 | ->disableOriginalConstructor() |
||
408 | ->getMock(); |
||
409 | $repository |
||
410 | ->expects($this->once()) |
||
411 | ->method('createNodeTranslationFor') |
||
412 | ->with($this->equalTo($targetPage), $this->equalTo($targetLocale), $this->equalTo($node), $this->equalTo($this->user)) |
||
413 | ->willReturn($expectedNodeTranslation); |
||
414 | |||
415 | $this->em |
||
416 | ->expects($this->once()) |
||
417 | ->method('getRepository') |
||
418 | ->with(NodeTranslation::class) |
||
419 | ->willReturn($repository); |
||
420 | |||
421 | $this->eventDispatcher |
||
422 | ->expects($this->once()) |
||
423 | ->method('dispatch') |
||
424 | ->with($this->equalTo(Events::COPY_PAGE_TRANSLATION), $this->equalTo(new CopyPageTranslationNodeEvent( |
||
425 | $node, |
||
426 | $expectedNodeTranslation, |
||
427 | $expectedNodeVersion, |
||
428 | $targetPage, |
||
429 | $sourceNodeTranslation, |
||
430 | $sourceNodeNodeVersion, |
||
431 | $sourcePage, |
||
432 | $this->locale))); |
||
433 | |||
434 | $result = $this->nodeHelper->copyPageFromOtherLanguage($node, $this->locale, $targetLocale); |
||
435 | |||
436 | $this->assertInstanceOf(NodeTranslation::class, $result); |
||
437 | $this->assertEquals($expectedNodeTranslation, $result); |
||
438 | } |
||
439 | |||
440 | public function testDuplicatePage() |
||
441 | { |
||
442 | $targetPage = new TestPage(); |
||
443 | |||
444 | list(, $nodeTranslationHomePage, $nodeHomePage) = $this->createNodeEntities('Homepage'); |
||
445 | |||
446 | /** |
||
447 | * @var TestPage $sourcePage |
||
448 | * @var NodeTranslation $sourceNodeTranslation |
||
449 | * @var Node $node |
||
450 | */ |
||
451 | list($sourcePage, $sourceNodeTranslation, $node) = $this->createNodeEntities(); |
||
452 | $node->setParent($nodeHomePage); |
||
453 | |||
454 | $this->cloneHelper |
||
455 | ->expects($this->once()) |
||
456 | ->method('deepCloneAndSave') |
||
457 | ->with($sourcePage) |
||
458 | ->willReturn($targetPage); |
||
459 | |||
460 | $expectedNodeTranslation = new NodeTranslation(); |
||
461 | $expectedNodeTranslation->setLang($this->locale); |
||
462 | |||
463 | $expectedNode = new Node(); |
||
464 | $expectedNode->addNodeTranslation($expectedNodeTranslation); |
||
465 | |||
466 | $repository = $this->getMockBuilder(NodeRepository::class) |
||
467 | ->disableOriginalConstructor() |
||
468 | ->getMock(); |
||
469 | $repository |
||
470 | ->expects($this->once()) |
||
471 | ->method('createNodeFor') |
||
472 | ->willReturn($expectedNode); |
||
473 | |||
474 | $this->em |
||
475 | ->expects($this->once()) |
||
476 | ->method('getRepository') |
||
477 | ->with(Node::class) |
||
478 | ->willReturn($repository); |
||
479 | |||
480 | $result = $this->nodeHelper->duplicatePage($node, $this->locale); |
||
481 | |||
482 | $this->assertInstanceOf(NodeTranslation::class, $result); |
||
483 | } |
||
484 | |||
485 | public function testCreatePageDraftFromOtherLanguage() |
||
486 | { |
||
487 | $targetLocale = 'nl'; |
||
488 | $targetPage = new TestPage(); |
||
489 | |||
490 | /** |
||
491 | * @var TestPage $sourcePage |
||
492 | * @var NodeTranslation $sourceNodeTranslation |
||
493 | */ |
||
494 | list($sourcePage, $sourceNodeTranslation, $node) = $this->createNodeEntities(); |
||
495 | $sourceNodeNodeVersion = $sourceNodeTranslation->getPublicNodeVersion(); |
||
496 | |||
497 | $this->cloneHelper |
||
498 | ->expects($this->once()) |
||
499 | ->method('deepCloneAndSave') |
||
500 | ->with($sourcePage) |
||
501 | ->willReturn($targetPage); |
||
502 | |||
503 | $expectedNodeVersion = new NodeVersion(); |
||
504 | $expectedNodeVersion->setType(NodeVersion::PUBLIC_VERSION); |
||
505 | $expectedNodeVersion->setRef($targetPage); |
||
506 | |||
507 | $expectedNodeTranslation = new NodeTranslation(); |
||
508 | $expectedNodeTranslation->setNode($node); |
||
509 | $expectedNodeTranslation->setLang($targetLocale); |
||
510 | $expectedNodeTranslation->setPublicNodeVersion($expectedNodeVersion); |
||
511 | |||
512 | $repository = $this->getMockBuilder(NodeTranslationRepository::class) |
||
513 | ->disableOriginalConstructor() |
||
514 | ->getMock(); |
||
515 | $repository |
||
516 | ->expects($this->once()) |
||
517 | ->method('find') |
||
518 | ->with($this->equalTo(1)) |
||
519 | ->willReturn($sourceNodeTranslation); |
||
520 | $repository |
||
521 | ->expects($this->once()) |
||
522 | ->method('addDraftNodeVersionFor') |
||
523 | ->with($this->equalTo($targetPage), $this->equalTo($targetLocale), $this->equalTo($node), $this->equalTo($this->user)) |
||
524 | ->willReturn($expectedNodeTranslation); |
||
525 | |||
526 | $this->em |
||
527 | ->expects($this->exactly(2)) |
||
528 | ->method('getRepository') |
||
529 | ->with(NodeTranslation::class) |
||
530 | ->willReturn($repository); |
||
531 | |||
532 | $this->eventDispatcher |
||
533 | ->expects($this->once()) |
||
534 | ->method('dispatch') |
||
535 | ->with($this->equalTo(Events::RECOPY_PAGE_TRANSLATION), $this->equalTo(new RecopyPageTranslationNodeEvent( |
||
536 | $node, |
||
537 | $expectedNodeTranslation, |
||
538 | $expectedNodeVersion, |
||
539 | $targetPage, |
||
540 | $sourceNodeTranslation, |
||
541 | $sourceNodeNodeVersion, |
||
542 | $sourcePage, |
||
543 | $this->locale))); |
||
544 | |||
545 | $result = $this->nodeHelper->createPageDraftFromOtherLanguage($node, 1, $targetLocale); |
||
546 | |||
547 | $this->assertInstanceOf(NodeTranslation::class, $result); |
||
548 | $this->assertEquals($expectedNodeTranslation, $result); |
||
549 | } |
||
550 | |||
551 | public function testCreateEmptyPage() |
||
552 | { |
||
553 | $targetPage = new TestPage(); |
||
554 | $targetPage->setTitle('No title'); |
||
555 | $node = new Node(); |
||
556 | $node->setRef($targetPage); |
||
557 | |||
558 | $expectedNodeVersion = new NodeVersion(); |
||
559 | $expectedNodeVersion->setType(NodeVersion::PUBLIC_VERSION); |
||
560 | $expectedNodeVersion->setRef($targetPage); |
||
561 | |||
562 | $expectedNodeTranslation = new NodeTranslation(); |
||
563 | $expectedNodeTranslation->setNode($node); |
||
564 | $expectedNodeTranslation->setLang($this->locale); |
||
565 | $expectedNodeTranslation->setPublicNodeVersion($expectedNodeVersion); |
||
566 | |||
567 | $repository = $this->getMockBuilder(NodeTranslationRepository::class) |
||
568 | ->disableOriginalConstructor() |
||
569 | ->getMock(); |
||
570 | $repository |
||
571 | ->expects($this->once()) |
||
572 | ->method('createNodeTranslationFor') |
||
573 | ->with($this->equalTo($targetPage), $this->equalTo($this->locale), $this->equalTo($node), $this->equalTo($this->user)) |
||
574 | ->willReturn($expectedNodeTranslation); |
||
575 | |||
576 | $this->em |
||
577 | ->expects($this->once()) |
||
578 | ->method('getRepository') |
||
579 | ->with(NodeTranslation::class) |
||
580 | ->willReturn($repository); |
||
581 | |||
582 | $this->eventDispatcher |
||
583 | ->expects($this->once()) |
||
584 | ->method('dispatch') |
||
585 | ->with($this->equalTo(Events::ADD_EMPTY_PAGE_TRANSLATION), $this->equalTo(new NodeEvent( |
||
586 | $node, |
||
587 | $expectedNodeTranslation, |
||
588 | $expectedNodeVersion, |
||
589 | $targetPage))); |
||
590 | |||
591 | $result = $this->nodeHelper->createEmptyPage($node, $this->locale); |
||
592 | |||
593 | $this->assertInstanceOf(NodeTranslation::class, $result); |
||
594 | $this->assertEquals($expectedNodeTranslation, $result); |
||
595 | } |
||
596 | |||
597 | private function createORM() |
||
598 | { |
||
599 | $this->repository = $this->getMockBuilder(ObjectRepository::class) |
||
600 | ->disableOriginalConstructor() |
||
601 | ->getMock(); |
||
602 | |||
603 | $this->em = $this->getMockBuilder(EntityManager::class) |
||
604 | ->disableOriginalConstructor() |
||
605 | ->getMock(); |
||
606 | } |
||
607 | |||
608 | /** |
||
609 | * @return NodeHelper |
||
610 | */ |
||
611 | private function createNodeHelper() |
||
612 | { |
||
613 | $this->user = new User(); |
||
614 | |||
615 | $token = $this->getMockBuilder(TokenInterface::class)->getMock(); |
||
616 | $token->method('getUser')->willReturn($this->user); |
||
617 | |||
618 | $this->tokenStorage = $this->getMockBuilder(TokenStorage::class) |
||
619 | ->disableOriginalConstructor() |
||
620 | ->getMock(); |
||
621 | $this->tokenStorage->method('getToken')->willReturn($token); |
||
622 | $this->nodeAdminPublisher = $this->getMockBuilder(NodeAdminPublisher::class) |
||
623 | ->disableOriginalConstructor() |
||
624 | ->getMock(); |
||
625 | $this->cloneHelper = $this->getMockBuilder(CloneHelper::class) |
||
626 | ->disableOriginalConstructor() |
||
627 | ->getMock(); |
||
628 | $this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class) |
||
629 | ->disableOriginalConstructor() |
||
630 | ->getMock(); |
||
631 | |||
632 | return new NodeHelper( |
||
633 | $this->em, |
||
634 | $this->nodeAdminPublisher, |
||
635 | $this->tokenStorage, |
||
636 | $this->cloneHelper, |
||
637 | $this->eventDispatcher |
||
638 | ); |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * @param string $title |
||
643 | * @return array |
||
0 ignored issues
–
show
|
|||
644 | */ |
||
645 | private function createNodeEntities($title = 'Test page') |
||
646 | { |
||
647 | $testPage = new TestPage(); |
||
648 | $testPage->setTitle($title); |
||
649 | |||
650 | $nodeVersionNewPage = $this->getMockBuilder(NodeVersion::class)->getMock(); |
||
651 | $nodeVersionNewPage |
||
652 | ->method('getRef') |
||
653 | ->with($this->em) |
||
654 | ->willReturn($testPage); |
||
655 | $nodeVersionNewPage |
||
656 | ->method('getType') |
||
657 | ->willReturn(NodeVersion::PUBLIC_VERSION); |
||
658 | |||
659 | $nodeTranslationNewPage = new NodeTranslation(); |
||
660 | $nodeTranslationNewPage->setTitle($title); |
||
661 | $nodeTranslationNewPage->setLang($this->locale); |
||
662 | $nodeTranslationNewPage->addNodeVersion($nodeVersionNewPage); |
||
663 | $nodeTranslationNewPage->setOnline(true); |
||
664 | |||
665 | $nodeNewPage = new Node(); |
||
666 | $nodeNewPage->setDeleted(false); |
||
667 | $nodeNewPage->addNodeTranslation($nodeTranslationNewPage); |
||
668 | |||
669 | return [$testPage, $nodeTranslationNewPage, $nodeNewPage]; |
||
670 | } |
||
671 | } |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.