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