diff --git a/src/lib/Form/EventListener/DisableSiteRootCheckboxIfRootLocationListener.php b/src/lib/Form/EventListener/DisableSiteRootCheckboxIfRootLocationListener.php index 7ffa4e9f05..26187e00a2 100644 --- a/src/lib/Form/EventListener/DisableSiteRootCheckboxIfRootLocationListener.php +++ b/src/lib/Form/EventListener/DisableSiteRootCheckboxIfRootLocationListener.php @@ -16,7 +16,7 @@ public function onPreSetData(FormEvent $event): void { $location = $event->getData()->getLocation(); - if (null === $location || 1 >= $location->depth) { + if (null === $location || 1 < $location->getDepth()) { return; } diff --git a/tests/lib/Form/EventListener/DisableSiteRootCheckboxIfRootLocationListenerTest.php b/tests/lib/Form/EventListener/DisableSiteRootCheckboxIfRootLocationListenerTest.php new file mode 100644 index 0000000000..bac1f5078b --- /dev/null +++ b/tests/lib/Form/EventListener/DisableSiteRootCheckboxIfRootLocationListenerTest.php @@ -0,0 +1,116 @@ +createMock(Location::class); + $location + ->method('getDepth') + ->willReturn(1); + + $data = new CustomUrlAddData($location); + $form = $this->createMock(FormInterface::class); + + $form + ->expects(self::once()) + ->method('add') + ->with( + 'site_root', + CheckboxType::class, + [ + 'required' => false, + 'label' => false, + 'disabled' => true, + ] + ); + + $event = $this->createMock(FormEvent::class); + + $event + ->method('getData') + ->willReturn($data); + + $event + ->method('getForm') + ->willReturn($form); + + $listener = new DisableSiteRootCheckboxIfRootLocationListener(); + + $listener->onPreSetData($event); + } + + public function testDoesNothingWhenLocationIsNull(): void + { + $data = new CustomUrlAddData(); + $form = $this->createMock(FormInterface::class); + + $form + ->expects(self::never()) + ->method('add'); + + $event = $this->createMock(FormEvent::class); + + $event + ->method('getData') + ->willReturn($data); + + $event + ->method('getForm') + ->willReturn($form); + + $listener = new DisableSiteRootCheckboxIfRootLocationListener(); + + $listener->onPreSetData($event); + + $form = $event->getForm(); + + self::assertNotTrue($form->has('site_root')); + } + + public function testDoesNothingWhenLocationDepthIsGreaterThanOne(): void + { + $location = $this->createMock(Location::class); + $location + ->method('getDepth') + ->willReturn(2); + + $data = new CustomUrlAddData($location); + $form = $this->createMock(FormInterface::class); + + $form + ->expects(self::never()) + ->method('add'); + + $event = $this->createMock(FormEvent::class); + + $event + ->method('getData') + ->willReturn($data); + + $event + ->method('getForm') + ->willReturn($form); + + $listener = new DisableSiteRootCheckboxIfRootLocationListener(); + + $listener->onPreSetData($event); + + self::assertNotTrue($form->has('site_root')); + } +}