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