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