|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Ibexa\Tests\AdminUi\Form\EventListener; |
| 9 | + |
| 10 | +use Ibexa\AdminUi\Form\Data\Content\CustomUrl\CustomUrlAddData; |
| 11 | +use Ibexa\AdminUi\Form\EventListener\DisableSiteRootCheckboxIfRootLocationListener; |
| 12 | +use Ibexa\Core\Repository\Values\Content\Location; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
| 15 | +use Symfony\Component\Form\FormEvent; |
| 16 | +use Symfony\Component\Form\FormInterface; |
| 17 | + |
| 18 | +class DisableSiteRootCheckboxIfRootLocationListenerTest extends TestCase |
| 19 | +{ |
| 20 | + public function testAddsCheckboxForRootLocation(): void |
| 21 | + { |
| 22 | + $location = $this->createMock(Location::class); |
| 23 | + $location |
| 24 | + ->method('getDepth') |
| 25 | + ->willReturn(1); |
| 26 | + |
| 27 | + $data = new CustomUrlAddData($location); |
| 28 | + $form = $this->createMock(FormInterface::class); |
| 29 | + |
| 30 | + $form |
| 31 | + ->expects(self::once()) |
| 32 | + ->method('add') |
| 33 | + ->with( |
| 34 | + 'site_root', |
| 35 | + CheckboxType::class, |
| 36 | + [ |
| 37 | + 'required' => false, |
| 38 | + 'label' => false, |
| 39 | + 'disabled' => true, |
| 40 | + ] |
| 41 | + ); |
| 42 | + |
| 43 | + $event = $this->createMock(FormEvent::class); |
| 44 | + |
| 45 | + $event |
| 46 | + ->method('getData') |
| 47 | + ->willReturn($data); |
| 48 | + |
| 49 | + $event |
| 50 | + ->method('getForm') |
| 51 | + ->willReturn($form); |
| 52 | + |
| 53 | + $listener = new DisableSiteRootCheckboxIfRootLocationListener(); |
| 54 | + |
| 55 | + $listener->onPreSetData($event); |
| 56 | + } |
| 57 | + |
| 58 | + public function testDoesNothingWhenLocationIsNull(): void |
| 59 | + { |
| 60 | + $data = new CustomUrlAddData(); |
| 61 | + $form = $this->createMock(FormInterface::class); |
| 62 | + |
| 63 | + $form |
| 64 | + ->expects(self::never()) |
| 65 | + ->method('add'); |
| 66 | + |
| 67 | + $event = $this->createMock(FormEvent::class); |
| 68 | + |
| 69 | + $event |
| 70 | + ->method('getData') |
| 71 | + ->willReturn($data); |
| 72 | + |
| 73 | + $event |
| 74 | + ->method('getForm') |
| 75 | + ->willReturn($form); |
| 76 | + |
| 77 | + $listener = new DisableSiteRootCheckboxIfRootLocationListener(); |
| 78 | + |
| 79 | + $listener->onPreSetData($event); |
| 80 | + |
| 81 | + $form = $event->getForm(); |
| 82 | + |
| 83 | + self::assertNotTrue($form->has('site_root')); |
| 84 | + } |
| 85 | + |
| 86 | + public function testDoesNothingWhenLocationDepthIsGreaterThanOne(): void |
| 87 | + { |
| 88 | + $location = $this->createMock(Location::class); |
| 89 | + $location |
| 90 | + ->method('getDepth') |
| 91 | + ->willReturn(2); |
| 92 | + |
| 93 | + $data = new CustomUrlAddData($location); |
| 94 | + $form = $this->createMock(FormInterface::class); |
| 95 | + |
| 96 | + $form |
| 97 | + ->expects(self::never()) |
| 98 | + ->method('add'); |
| 99 | + |
| 100 | + $event = $this->createMock(FormEvent::class); |
| 101 | + |
| 102 | + $event |
| 103 | + ->method('getData') |
| 104 | + ->willReturn($data); |
| 105 | + |
| 106 | + $event |
| 107 | + ->method('getForm') |
| 108 | + ->willReturn($form); |
| 109 | + |
| 110 | + $listener = new DisableSiteRootCheckboxIfRootLocationListener(); |
| 111 | + |
| 112 | + $listener->onPreSetData($event); |
| 113 | + |
| 114 | + self::assertNotTrue($form->has('site_root')); |
| 115 | + } |
| 116 | +} |
0 commit comments