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