|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ipl\Tests\Web\Common; |
| 4 | + |
| 5 | +use Icinga\Web\UrlParams; |
| 6 | +use ipl\Html\Form; |
| 7 | +use ipl\I18n\NoopTranslator; |
| 8 | +use ipl\I18n\StaticTranslator; |
| 9 | +use ipl\Web\Common\Controls; |
| 10 | +use ipl\Web\Control\ViewModeSwitcher; |
| 11 | +use ipl\Tests\Web\TestCase; |
| 12 | +use Psr\Http\Message\ServerRequestInterface; |
| 13 | + |
| 14 | +class ControlsTest extends TestCase |
| 15 | +{ |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + StaticTranslator::$instance = new NoopTranslator(); |
| 19 | + } |
| 20 | + |
| 21 | + public function testCreateViewModeSwitcher(): void |
| 22 | + { |
| 23 | + $controller = $this->controls(); |
| 24 | + |
| 25 | + $params = UrlParams::fromQueryString('view=minimal&foo=bar'); |
| 26 | + $switcher = $controller->createViewModeSwitcher($params); |
| 27 | + |
| 28 | + $this->assertSame('minimal', $switcher->getViewMode(), 'The view mode should be populated from the param'); |
| 29 | + $this->assertFalse($params->has('view'), 'The view mode param should be shifted out of the params'); |
| 30 | + $this->assertSame('bar', $params->get('foo'), 'Other params should be left untouched'); |
| 31 | + $this->assertContains($switcher, $controller->trackedControls(), 'The created switcher should be tracked'); |
| 32 | + |
| 33 | + $customParams = UrlParams::fromQueryString('layout=detailed'); |
| 34 | + $customSwitcher = $controller->createViewModeSwitcher($customParams, 'layout'); |
| 35 | + |
| 36 | + $this->assertSame( |
| 37 | + 'detailed', |
| 38 | + $customSwitcher->getViewMode(), |
| 39 | + 'The view mode should be read from the custom param' |
| 40 | + ); |
| 41 | + $this->assertFalse( |
| 42 | + $customParams->has('layout'), |
| 43 | + 'The custom view mode param should be shifted out of the params' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public function testCreateViewModeSwitcherResolvesTheDefaultViewMode(): void |
| 48 | + { |
| 49 | + $default = $this->controls()->createViewModeSwitcher(UrlParams::fromQueryString('foo=bar')); |
| 50 | + |
| 51 | + $this->assertSame( |
| 52 | + ViewModeSwitcher::DEFAULT_VIEW_MODE, |
| 53 | + $default->getViewMode(), |
| 54 | + 'Without a param the default view mode should apply' |
| 55 | + ); |
| 56 | + |
| 57 | + $default->setDefaultViewMode('detailed'); |
| 58 | + |
| 59 | + $this->assertSame( |
| 60 | + 'detailed', |
| 61 | + $default->getViewMode(), |
| 62 | + 'A default set after creation should be honored, as it is read lazily' |
| 63 | + ); |
| 64 | + |
| 65 | + $withParam = $this->controls()->createViewModeSwitcher(UrlParams::fromQueryString('view=minimal')); |
| 66 | + $withParam->setDefaultViewMode('detailed'); |
| 67 | + |
| 68 | + $this->assertSame( |
| 69 | + 'minimal', |
| 70 | + $withParam->getViewMode(), |
| 71 | + 'A present param should take precedence over the default' |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + public function testCreateViewModeSwitcherWithCustomClass(): void |
| 76 | + { |
| 77 | + $customClass = get_class(new class extends ViewModeSwitcher { |
| 78 | + }); |
| 79 | + |
| 80 | + $switcher = $this->controls()->createViewModeSwitcher( |
| 81 | + UrlParams::fromQueryString('view=minimal'), |
| 82 | + viewModeSwitcherClass: $customClass |
| 83 | + ); |
| 84 | + |
| 85 | + $this->assertInstanceOf($customClass, $switcher); |
| 86 | + } |
| 87 | + |
| 88 | + public function testHandleControls(): void |
| 89 | + { |
| 90 | + $request = $this->createMock(ServerRequestInterface::class); |
| 91 | + |
| 92 | + $first = $this->createMock(Form::class); |
| 93 | + $first->expects($this->once())->method('handleRequest')->with($request); |
| 94 | + |
| 95 | + $second = $this->createMock(Form::class); |
| 96 | + $second->expects($this->once())->method('handleRequest')->with($request); |
| 97 | + |
| 98 | + $controller = $this->controls(); |
| 99 | + $controller->track($first); |
| 100 | + $controller->track($second); |
| 101 | + |
| 102 | + $this->assertSame( |
| 103 | + [$first, $second], |
| 104 | + $controller->trackedControls(), |
| 105 | + 'Controls should be tracked in registration order' |
| 106 | + ); |
| 107 | + |
| 108 | + $controller->handle($request); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get a controller using the {@see Controls} trait, with public seams onto its protected orchestration |
| 113 | + */ |
| 114 | + private function controls(): object |
| 115 | + { |
| 116 | + return new class { |
| 117 | + use Controls; |
| 118 | + |
| 119 | + /** @return Form[] */ |
| 120 | + public function trackedControls(): array |
| 121 | + { |
| 122 | + return $this->trackedControls; |
| 123 | + } |
| 124 | + |
| 125 | + public function track(Form $control): void |
| 126 | + { |
| 127 | + $this->trackControl($control); |
| 128 | + } |
| 129 | + |
| 130 | + public function handle(ServerRequestInterface $request): void |
| 131 | + { |
| 132 | + $this->handleControls($request); |
| 133 | + } |
| 134 | + }; |
| 135 | + } |
| 136 | +} |
0 commit comments