Completed
Push — master ( 9b5821...aba493 )
by Ruud
307:53 queued 297:17
created

NodeBundle/Tests/unit/Helper/NodeHelperTest.php (4 issues)

Severity

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 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\AdminBundle\Helper\FormWidgets\Tabs\TabPane;
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\Entity\PageTabInterface;
17
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
18
use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent;
19
use Kunstmaan\NodeBundle\Event\Events;
20
use Kunstmaan\NodeBundle\Event\NodeEvent;
21
use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent;
22
use Kunstmaan\NodeBundle\EventListener\NodeTabListener;
23
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
24
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper;
25
use Kunstmaan\NodeBundle\Helper\NodeHelper;
26
use Kunstmaan\NodeBundle\Repository\NodeRepository;
27
use Kunstmaan\NodeBundle\Repository\NodeTranslationRepository;
28
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
29
use Kunstmaan\NodeBundle\ValueObject\PageTab;
30
use PHPUnit\Framework\TestCase;
31
use Symfony\Component\EventDispatcher\EventDispatcher;
32
use Symfony\Component\Form\AbstractType;
33
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
34
use Symfony\Component\Form\FormBuilderInterface;
35
use Symfony\Component\Form\FormFactory;
36
use Symfony\Component\HttpFoundation\ParameterBag;
37
use Symfony\Component\HttpFoundation\Request;
38
use Symfony\Component\OptionsResolver\OptionsResolver;
39
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
40
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
41
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
42
43
class TestPage extends AbstractPage implements HasNodeInterface, PageTabInterface
44
{
45
    /**
46
     * @return array
47
     */
48
    public function getPossibleChildTypes()
49
    {
50
        return [];
51
    }
52
53
    /**
54
     * @return PageTab[]
55
     */
56
    public function getTabs()
57
    {
58
        return [
59
            (new PageTab('tab1_name', 'tab1_title', TestType::class)),
60
        ];
61
    }
62
}
63
64
class TestType extends AbstractType
65
{
66
    /**
67
     * @param FormBuilderInterface $builder
68
     * @param array                $options
69
     */
70
    public function buildForm(FormBuilderInterface $builder, array $options)
71
    {
72
        $builder->add('id', HiddenType::class);
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getBlockPrefix()
79
    {
80
        return 'test';
81
    }
82
83
    public function configureOptions(OptionsResolver $resolver)
84
    {
85
        $resolver->setDefaults(array(
86
            'data_class' => TestPage::class,
87
        ));
88
    }
89
}
90
91
class NodeHelperTest extends TestCase
92
{
93
    /** @var \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface $em */
94
    private $em;
95
96
    /** @var \PHPUnit_Framework_MockObject_MockObject|NodeRepository */
97
    private $repository;
98
99
    /** @var NodeHelper */
100
    private $nodeHelper;
101
102
    /** @var \PHPUnit_Framework_MockObject_MockObject|NodeAdminPublisher */
103
    private $nodeAdminPublisher;
104
105
    /** @var \PHPUnit_Framework_MockObject_MockObject|EventDispatcher */
106
    private $eventDispatcher;
107
108
    /** @var \PHPUnit_Framework_MockObject_MockObject|TokenStorageInterface */
109
    private $tokenStorage;
110
111
    /** @var \PHPUnit_Framework_MockObject_MockObject|NodeVersionLockHelper */
112
    private $nodeVersionLockHelper;
0 ignored issues
show
The property $nodeVersionLockHelper is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
113
114
    /** @var \PHPUnit_Framework_MockObject_MockObject|CloneHelper */
115
    private $cloneHelper;
116
117
    /** @var string */
118
    private $locale = 'en';
119
120
    /** @var User */
121
    private $user;
122
123
    public function setUp()
124
    {
125
        $this->createORM();
126
        $this->nodeHelper = $this->createNodeHelper();
127
    }
128
129
    public function testUpdatePage()
130
    {
131
        /**
132
         * @var TestPage
133
         * @var NodeTranslation $nodeTranslation
134
         */
135
        list($page, $nodeTranslation, $node) = $this->createNodeEntities();
136
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
137
138
        $this->em
139
            ->expects($this->exactly(3))
140
            ->method('persist')
141
            ->withConsecutive(
142
                [$this->equalTo($nodeTranslation)],
143
                [$this->equalTo($nodeVersion)],
144
                [$this->equalTo($node)]
145
            );
146
147
        $this->eventDispatcher
148
            ->expects($this->exactly(2))
149
            ->method('dispatch')
150
            ->withConsecutive(
151
                [$this->equalTo(Events::PRE_PERSIST), $this->equalTo(new NodeEvent($node, $nodeTranslation, $nodeVersion, $page))],
152
                [$this->equalTo(Events::POST_PERSIST), $this->equalTo(new NodeEvent($node, $nodeTranslation, $nodeVersion, $page))]
153
            );
154
155
        $this->nodeHelper->updatePage(
156
            $node,
157
            $nodeTranslation,
158
            $nodeTranslation->getPublicNodeVersion(),
159
            $page,
160
            false,
161
            null
162
        );
163
    }
164
165
    public function testCreatePage()
166
    {
167
        $title = 'Test page';
168
        $user = new User();
169
170
        list($homePage, , $nodeHomePage) = $this->createNodeEntities('Homepage');
171
172
        /**
173
         * @var TestPage
174
         * @var NodeTranslation $nodeTranslationChildPage
175
         */
176
        list($page, $nodeTranslationChildPage, $nodeChildPage) = $this->createNodeEntities($title);
0 ignored issues
show
The assignment to $page is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
177
178
        $expectedTestPageCreateNodeFor = new TestPage();
179
        $expectedTestPageCreateNodeFor->setTitle($title);
180
        $expectedTestPageCreateNodeFor->setParent($homePage);
181
182
        $nodeRepository = $this->getMockBuilder(NodeRepository::class)
183
            ->disableOriginalConstructor()
184
            ->getMock();
185
        $nodeRepository
186
            ->expects($this->once())
187
            ->method('createNodeFor')
188
            ->with(
189
                $this->equalTo($expectedTestPageCreateNodeFor),
190
                $this->equalTo($this->locale),
191
                $this->equalTo($user)
192
            )
193
            ->willReturn($nodeChildPage);
194
195
        $nodeTranslationRepository = $this->getMockBuilder(NodeTranslationRepository::class)
196
            ->disableOriginalConstructor()
197
            ->getMock();
198
        $nodeTranslationRepository
199
            ->expects($this->once())
200
            ->method('getMaxChildrenWeight')
201
            ->willReturn(1);
202
203
        $this->em
204
            ->method('getRepository')
205
            ->withConsecutive(
206
                [$this->equalTo('KunstmaanNodeBundle:Node')],
207
                [$this->equalTo('KunstmaanNodeBundle:NodeTranslation')]
208
            )
209
            ->willReturnOnConsecutiveCalls(
210
                $nodeRepository,
211
                $nodeTranslationRepository
212
            );
213
214
        $expectedEvent = new NodeEvent(
215
            $nodeChildPage, $nodeTranslationChildPage, $nodeTranslationChildPage->getPublicNodeVersion(), $expectedTestPageCreateNodeFor
216
        );
217
        $this->eventDispatcher
218
            ->expects($this->once())
219
            ->method('dispatch')
220
            ->with($this->equalTo(Events::ADD_NODE), $this->equalTo($expectedEvent))
221
        ;
222
223
        $result = $this->nodeHelper->createPage(TestPage::class, $title, $this->locale, $nodeHomePage);
224
225
        $this->assertInstanceOf(NodeTranslation::class, $result);
226
        $this->assertEquals(2, $result->getWeight());
227
        $this->assertEquals($title, $result->getTitle());
228
    }
229
230
    public function testDeletePage()
231
    {
232
        /**
233
         * @var Node
234
         * @var NodeTranslation $nodeTranslationHomePage
235
         */
236
        list($homePage, $nodeTranslationHomePage, $nodeHomePage) = $this->createNodeEntities('Homepage');
237
        $nodeVersionHomePage = $nodeTranslationHomePage->getPublicNodeVersion();
238
239
        /**
240
         * @var TestPage
241
         * @var NodeTranslation $nodeTranslationChildPage
242
         */
243
        list($page, $nodeTranslationChildPage, $nodeChildPage) = $this->createNodeEntities('Test page');
244
        $nodeVersionChildPage = $nodeTranslationChildPage->getPublicNodeVersion();
245
        $nodeHomePage->addNode($nodeChildPage);
246
247
        $this->eventDispatcher
248
            ->expects($this->exactly(4))
249
            ->method('dispatch')
250
            ->withConsecutive(
251
                [$this->equalTo(Events::PRE_DELETE), $this->equalTo(new NodeEvent($nodeHomePage, $nodeTranslationHomePage, $nodeVersionHomePage, $homePage))],
252
                [$this->equalTo(Events::PRE_DELETE), $this->equalTo(new NodeEvent($nodeChildPage, $nodeTranslationChildPage, $nodeVersionChildPage, $page))],
253
                [$this->equalTo(Events::POST_DELETE), $this->equalTo(new NodeEvent($nodeChildPage, $nodeTranslationChildPage, $nodeVersionChildPage, $page))],
254
                [$this->equalTo(Events::POST_DELETE), $this->equalTo(new NodeEvent($nodeHomePage, $nodeTranslationHomePage, $nodeVersionHomePage, $homePage))]
255
            );
256
257
        $result = $this->nodeHelper->deletePage($nodeHomePage, $this->locale);
258
259
        $this->assertTrue($result->getNode()->isDeleted());
260
        $this->assertTrue($nodeHomePage->isDeleted());
261
        $this->assertTrue($nodeChildPage->isDeleted());
262
    }
263
264
    public function testPrepareNodeVersionForPublic()
265
    {
266
        $user = new User();
267
268
        $page = new TestPage();
269
        $page->setTitle('Test');
270
        $page->setId(1);
271
272
        $nodeVersion = new NodeVersion();
273
        $nodeVersion->setType(NodeVersion::PUBLIC_VERSION);
274
        $nodeVersion->setRef($page);
275
276
        $nodeVersion = $this->getMockBuilder(NodeVersion::class)->getMock();
277
        $nodeVersion
278
            ->method('getType')
279
            ->willReturn(NodeVersion::PUBLIC_VERSION);
280
        $nodeVersion
281
            ->expects($this->once())
282
            ->method('getRef')
283
            ->willReturn($page);
284
        $nodeVersion
285
            ->method('getUpdated')
286
            ->willReturn((new \DateTime('-1 hour')));
287
288
        $nodeTranslation = new NodeTranslation();
289
        $nodeTranslation->setLang($this->locale);
290
        $nodeTranslation->addNodeVersion($nodeVersion);
291
292
        $this->nodeAdminPublisher
293
            ->expects($this->once())
294
            ->method('createPublicVersion')
295
            ->with(
296
                $this->equalTo($page),
297
                $this->equalTo($nodeTranslation),
298
                $this->equalTo($nodeVersion),
299
                $this->equalTo($user)
300
            );
301
302
        $this->nodeHelper->prepareNodeVersion($nodeVersion, $nodeTranslation, 10, true);
303
    }
304
305
    public function testPrepareNodeVersionForDraft()
306
    {
307
        $page = new TestPage();
308
        $page->setTitle('Test');
309
        $page->setId(1);
310
311
        $nodeVersion = new NodeVersion();
312
        $nodeVersion->setType(NodeVersion::PUBLIC_VERSION);
313
        $nodeVersion->setRef($page);
314
315
        $nodeVersion = $this->getMockBuilder(NodeVersion::class)->getMock();
316
        $nodeVersion
317
            ->method('getType')
318
            ->willReturn(NodeVersion::DRAFT_VERSION);
319
        $nodeVersion
320
            ->expects($this->once())
321
            ->method('getRef')
322
            ->willReturn($page);
323
        $nodeVersion
324
            ->method('getUpdated')
325
            ->willReturn((new \DateTime('-1 hour')));
326
327
        $nodeTranslation = new NodeTranslation();
328
        $nodeTranslation->setLang($this->locale);
329
        $nodeTranslation->addNodeVersion($nodeVersion);
330
331
        /** @var \PHPUnit_Framework_MockObject_MockObject|NodeHelper $nodeHelper */
332
        $nodeHelper = $this->getMockBuilder(NodeHelper::class)
333
            ->setConstructorArgs([
334
                $this->em,
335
                $this->nodeAdminPublisher,
336
                $this->tokenStorage,
337
                $this->cloneHelper,
338
                $this->eventDispatcher,
339
            ])
340
            ->setMethods(['createDraftVersion'])
341
            ->getMock();
342
        $nodeHelper
343
            ->expects($this->once())
344
            ->method('createDraftVersion')
345
            ->willReturn(true);
346
347
        $nodeHelper->prepareNodeVersion($nodeVersion, $nodeTranslation, 10, true);
348
    }
349
350
    public function testCreateDraftVersion()
351
    {
352
        /**
353
         * @var TestPage
354
         * @var NodeTranslation $nodeTranslation
355
         */
356
        list($page, $nodeTranslation, $node) = $this->createNodeEntities();
357
        $originalNodeVersion = new NodeVersion();
358
359
        $this->cloneHelper
360
            ->expects($this->once())
361
            ->method('deepCloneAndSave')
362
            ->willReturn($page);
363
364
        $publicNodeVersion = new NodeVersion();
365
        $publicNodeVersion->setRef($page);
366
        $publicNodeVersion->setType(NodeVersion::PUBLIC_VERSION);
367
368
        $nodeVersionRepository = $this->getMockBuilder(NodeVersionRepository::class)
369
            ->disableOriginalConstructor()
370
            ->getMock();
371
        $nodeVersionRepository
372
            ->method('createNodeVersionFor')
373
            ->willReturn($publicNodeVersion);
374
375
        $this->em->method('getRepository')
376
            ->with('KunstmaanNodeBundle:NodeVersion')
377
            ->willReturn($nodeVersionRepository);
378
379
        $this->eventDispatcher
380
            ->expects($this->once())
381
            ->method('dispatch')
382
            ->with($this->equalTo(Events::CREATE_DRAFT_VERSION), $this->equalTo(new NodeEvent($node, $nodeTranslation, $originalNodeVersion, $page)));
383
384
        $result = $this->nodeHelper->createDraftVersion($page, $nodeTranslation, $originalNodeVersion);
385
386
        $this->assertInstanceOf(NodeVersion::class, $result);
387
        $this->assertEquals(NodeVersion::DRAFT_VERSION, $result->getType());
388
        $this->assertEquals($publicNodeVersion, $result->getOrigin());
389
    }
390
391
    public function testGetPageWithNodeInterface()
392
    {
393
        $refId = 10;
394
395
        $page = new TestPage();
396
        $page->setTitle('Test');
397
        $page->setId($refId);
398
399
        $nodeVersion = new NodeVersion();
400
        $nodeVersion->setType(NodeVersion::PUBLIC_VERSION);
401
        $nodeVersion->setRef($page);
402
403
        $nodeTranslation = new NodeTranslation();
404
        $nodeTranslation->setLang($this->locale);
405
        $nodeTranslation->addNodeVersion($nodeVersion);
406
407
        $node = new Node();
408
        $node->addNodeTranslation($nodeTranslation);
409
410
        $repository = $this->getMockBuilder(ObjectRepository::class)->getMock();
411
        $repository
412
            ->expects($this->once())
413
            ->method('find')
414
            ->with($refId)
415
            ->willReturn($page);
416
417
        $this->em
418
            ->expects($this->once())
419
            ->method('getRepository')
420
            ->with($this->equalTo(TestPage::class))
421
            ->willReturn($repository);
422
423
        $this->nodeHelper->getPageWithNodeInterface($node, $this->locale);
424
    }
425
426
    public function testCopyPageFromOtherLanguage()
427
    {
428
        $targetLocale = 'nl';
429
        $targetPage = new TestPage();
430
431
        /**
432
         * @var TestPage
433
         * @var NodeTranslation $sourceNodeTranslation
434
         */
435
        list($sourcePage, $sourceNodeTranslation, $node) = $this->createNodeEntities();
436
        $sourceNodeNodeVersion = $sourceNodeTranslation->getPublicNodeVersion();
437
438
        $this->cloneHelper
439
            ->expects($this->once())
440
            ->method('deepCloneAndSave')
441
            ->with($sourcePage)
442
            ->willReturn($targetPage);
443
444
        $expectedNodeVersion = new NodeVersion();
445
        $expectedNodeVersion->setType(NodeVersion::PUBLIC_VERSION);
446
        $expectedNodeVersion->setRef($targetPage);
447
448
        $expectedNodeTranslation = new NodeTranslation();
449
        $expectedNodeTranslation->setNode($node);
450
        $expectedNodeTranslation->setLang($targetLocale);
451
        $expectedNodeTranslation->setPublicNodeVersion($expectedNodeVersion);
452
453
        $repository = $this->getMockBuilder(NodeTranslationRepository::class)
454
            ->disableOriginalConstructor()
455
            ->getMock();
456
        $repository
457
            ->expects($this->once())
458
            ->method('createNodeTranslationFor')
459
            ->with($this->equalTo($targetPage), $this->equalTo($targetLocale), $this->equalTo($node), $this->equalTo($this->user))
460
            ->willReturn($expectedNodeTranslation);
461
462
        $this->em
463
            ->expects($this->once())
464
            ->method('getRepository')
465
            ->with(NodeTranslation::class)
466
            ->willReturn($repository);
467
468
        $this->eventDispatcher
469
            ->expects($this->once())
470
            ->method('dispatch')
471
            ->with($this->equalTo(Events::COPY_PAGE_TRANSLATION), $this->equalTo(new CopyPageTranslationNodeEvent(
472
                $node,
473
                $expectedNodeTranslation,
474
                $expectedNodeVersion,
475
                $targetPage,
476
                $sourceNodeTranslation,
477
                $sourceNodeNodeVersion,
478
                $sourcePage,
479
                $this->locale)));
480
481
        $result = $this->nodeHelper->copyPageFromOtherLanguage($node, $this->locale, $targetLocale);
482
483
        $this->assertInstanceOf(NodeTranslation::class, $result);
484
        $this->assertEquals($expectedNodeTranslation, $result);
485
    }
486
487
    public function testDuplicatePage()
488
    {
489
        $targetPage = new TestPage();
490
491
        list(, $nodeTranslationHomePage, $nodeHomePage) = $this->createNodeEntities('Homepage');
0 ignored issues
show
The assignment to $nodeTranslationHomePage is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
492
493
        /**
494
         * @var TestPage
495
         * @var NodeTranslation $sourceNodeTranslation
496
         * @var Node            $node
497
         */
498
        list($sourcePage, $sourceNodeTranslation, $node) = $this->createNodeEntities();
0 ignored issues
show
The assignment to $sourceNodeTranslation is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
499
        $node->setParent($nodeHomePage);
500
501
        $this->cloneHelper
502
            ->expects($this->once())
503
            ->method('deepCloneAndSave')
504
            ->with($sourcePage)
505
            ->willReturn($targetPage);
506
507
        $expectedNodeTranslation = new NodeTranslation();
508
        $expectedNodeTranslation->setLang($this->locale);
509
510
        $expectedNode = new Node();
511
        $expectedNode->addNodeTranslation($expectedNodeTranslation);
512
513
        $repository = $this->getMockBuilder(NodeRepository::class)
514
            ->disableOriginalConstructor()
515
            ->getMock();
516
        $repository
517
            ->expects($this->once())
518
            ->method('createNodeFor')
519
            ->willReturn($expectedNode);
520
521
        $this->em
522
            ->expects($this->once())
523
            ->method('getRepository')
524
            ->with(Node::class)
525
            ->willReturn($repository);
526
527
        $result = $this->nodeHelper->duplicatePage($node, $this->locale);
528
529
        $this->assertInstanceOf(NodeTranslation::class, $result);
530
    }
531
532
    public function testCreatePageDraftFromOtherLanguage()
533
    {
534
        $targetLocale = 'nl';
535
        $targetPage = new TestPage();
536
537
        /**
538
         * @var TestPage
539
         * @var NodeTranslation $sourceNodeTranslation
540
         */
541
        list($sourcePage, $sourceNodeTranslation, $node) = $this->createNodeEntities();
542
        $sourceNodeNodeVersion = $sourceNodeTranslation->getPublicNodeVersion();
543
544
        $this->cloneHelper
545
            ->expects($this->once())
546
            ->method('deepCloneAndSave')
547
            ->with($sourcePage)
548
            ->willReturn($targetPage);
549
550
        $expectedNodeVersion = new NodeVersion();
551
        $expectedNodeVersion->setType(NodeVersion::PUBLIC_VERSION);
552
        $expectedNodeVersion->setRef($targetPage);
553
554
        $expectedNodeTranslation = new NodeTranslation();
555
        $expectedNodeTranslation->setNode($node);
556
        $expectedNodeTranslation->setLang($targetLocale);
557
        $expectedNodeTranslation->setPublicNodeVersion($expectedNodeVersion);
558
559
        $repository = $this->getMockBuilder(NodeTranslationRepository::class)
560
            ->disableOriginalConstructor()
561
            ->getMock();
562
        $repository
563
            ->expects($this->once())
564
            ->method('find')
565
            ->with($this->equalTo(1))
566
            ->willReturn($sourceNodeTranslation);
567
        $repository
568
            ->expects($this->once())
569
            ->method('addDraftNodeVersionFor')
570
            ->with($this->equalTo($targetPage), $this->equalTo($targetLocale), $this->equalTo($node), $this->equalTo($this->user))
571
            ->willReturn($expectedNodeTranslation);
572
573
        $this->em
574
            ->expects($this->exactly(2))
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::RECOPY_PAGE_TRANSLATION), $this->equalTo(new RecopyPageTranslationNodeEvent(
583
                $node,
584
                $expectedNodeTranslation,
585
                $expectedNodeVersion,
586
                $targetPage,
587
                $sourceNodeTranslation,
588
                $sourceNodeNodeVersion,
589
                $sourcePage,
590
                $this->locale)));
591
592
        $result = $this->nodeHelper->createPageDraftFromOtherLanguage($node, 1, $targetLocale);
593
594
        $this->assertInstanceOf(NodeTranslation::class, $result);
595
        $this->assertEquals($expectedNodeTranslation, $result);
596
    }
597
598
    public function testCreateEmptyPage()
599
    {
600
        $targetPage = new TestPage();
601
        $targetPage->setTitle('No title');
602
        $node = new Node();
603
        $node->setRef($targetPage);
604
605
        $expectedNodeVersion = new NodeVersion();
606
        $expectedNodeVersion->setType(NodeVersion::PUBLIC_VERSION);
607
        $expectedNodeVersion->setRef($targetPage);
608
609
        $expectedNodeTranslation = new NodeTranslation();
610
        $expectedNodeTranslation->setNode($node);
611
        $expectedNodeTranslation->setLang($this->locale);
612
        $expectedNodeTranslation->setPublicNodeVersion($expectedNodeVersion);
613
614
        $repository = $this->getMockBuilder(NodeTranslationRepository::class)
615
            ->disableOriginalConstructor()
616
            ->getMock();
617
        $repository
618
            ->expects($this->once())
619
            ->method('createNodeTranslationFor')
620
            ->with($this->equalTo($targetPage), $this->equalTo($this->locale), $this->equalTo($node), $this->equalTo($this->user))
621
            ->willReturn($expectedNodeTranslation);
622
623
        $this->em
624
            ->expects($this->once())
625
            ->method('getRepository')
626
            ->with(NodeTranslation::class)
627
            ->willReturn($repository);
628
629
        $this->eventDispatcher
630
            ->expects($this->once())
631
            ->method('dispatch')
632
            ->with($this->equalTo(Events::ADD_EMPTY_PAGE_TRANSLATION), $this->equalTo(new NodeEvent(
633
                $node,
634
                $expectedNodeTranslation,
635
                $expectedNodeVersion,
636
                $targetPage)));
637
638
        $result = $this->nodeHelper->createEmptyPage($node, $this->locale);
639
640
        $this->assertInstanceOf(NodeTranslation::class, $result);
641
        $this->assertEquals($expectedNodeTranslation, $result);
642
    }
643
644
    private function createORM()
645
    {
646
        $this->repository = $this->getMockBuilder(ObjectRepository::class)
647
            ->disableOriginalConstructor()
648
            ->getMock();
649
650
        $this->em = $this->getMockBuilder(EntityManager::class)
651
            ->disableOriginalConstructor()
652
            ->getMock();
653
    }
654
655
    /**
656
     * @return NodeHelper
657
     */
658
    private function createNodeHelper()
659
    {
660
        $this->user = new User();
661
662
        $token = $this->getMockBuilder(TokenInterface::class)->getMock();
663
        $token->method('getUser')->willReturn($this->user);
664
665
        $this->tokenStorage = $this->getMockBuilder(TokenStorage::class)
666
            ->disableOriginalConstructor()
667
            ->getMock();
668
        $this->tokenStorage->method('getToken')->willReturn($token);
669
        $this->nodeAdminPublisher = $this->getMockBuilder(NodeAdminPublisher::class)
670
            ->disableOriginalConstructor()
671
            ->getMock();
672
        $this->cloneHelper = $this->getMockBuilder(CloneHelper::class)
673
            ->disableOriginalConstructor()
674
            ->getMock();
675
        $this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class)
676
            ->disableOriginalConstructor()
677
            ->getMock();
678
679
        return new NodeHelper(
680
            $this->em,
681
            $this->nodeAdminPublisher,
682
            $this->tokenStorage,
683
            $this->cloneHelper,
684
            $this->eventDispatcher
685
        );
686
    }
687
688
    /**
689
     * @param string $title
690
     *
691
     * @return array
692
     */
693
    private function createNodeEntities($title = 'Test page')
694
    {
695
        $testPage = new TestPage();
696
        $testPage->setTitle($title);
697
698
        $nodeVersionNewPage = $this->getMockBuilder(NodeVersion::class)->getMock();
699
        $nodeVersionNewPage
700
            ->method('getRef')
701
            ->with($this->em)
702
            ->willReturn($testPage);
703
        $nodeVersionNewPage
704
            ->method('getType')
705
            ->willReturn(NodeVersion::PUBLIC_VERSION);
706
707
        $nodeTranslationNewPage = new NodeTranslation();
708
        $nodeTranslationNewPage->setTitle($title);
709
        $nodeTranslationNewPage->setLang($this->locale);
710
        $nodeTranslationNewPage->addNodeVersion($nodeVersionNewPage);
711
        $nodeTranslationNewPage->setOnline(true);
712
713
        $nodeNewPage = new Node();
714
        $nodeNewPage->setDeleted(false);
715
        $nodeNewPage->addNodeTranslation($nodeTranslationNewPage);
716
717
        return [$testPage, $nodeTranslationNewPage, $nodeNewPage];
718
    }
719
720
    public function testPageShouldHaveTab()
721
    {
722
        $request = new Request();
723
        $request->request = new ParameterBag();
724
725
        $formFactory = $this->getMockBuilder(FormFactory::class)
726
            ->disableOriginalConstructor()
727
            ->getMock();
728
729
        $entity = new TestPage();
730
731
        $tabPane = new TabPane('id', $request, $formFactory);
732
        $adaptFormEvent = new AdaptFormEvent($request, $tabPane, $entity);
733
734
        $nodeTabListener = new NodeTabListener();
735
        $nodeTabListener->adaptForm($adaptFormEvent);
736
737
        $tabs = $adaptFormEvent->getTabPane()->getTabs();
738
        $title = null;
739
740
        if (isset($tabs[0])) {
741
            $title = $tabs[0]->getTitle();
742
        }
743
744
        $this->assertEquals('tab1_title', $title);
745
    }
746
}
747