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