|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: a non-coercible current-request parameter in a self-link degrades to a 4xx, |
| 5 | + * not an uncaught 500. |
| 6 | + */ |
| 7 | + |
| 8 | +use Nette\Application; |
| 9 | +use Nette\Application\UI\InvalidRequestParameterException; |
| 10 | +use Nette\Application\UI\InvalidLinkException; |
| 11 | +use Nette\Application\UI\Presenter; |
| 12 | +use Nette\Http; |
| 13 | +use Tester\Assert; |
| 14 | + |
| 15 | + |
| 16 | +require __DIR__ . '/../bootstrap.php'; |
| 17 | + |
| 18 | + |
| 19 | +class SelfLinkPresenter extends Presenter |
| 20 | +{ |
| 21 | + public string $op = ''; |
| 22 | + public mixed $observed = null; |
| 23 | + |
| 24 | + |
| 25 | + protected function startup(): void |
| 26 | + { |
| 27 | + parent::startup(); |
| 28 | + match ($this->op) { |
| 29 | + 'redirect-this' => $this->redirect('this', ['keep' => 'x']), |
| 30 | + 'redirect-explicit' => $this->redirect('default', ['tier' => 'abc']), |
| 31 | + 'link-this' => $this->observed = $this->link('this'), |
| 32 | + 'isLinkCurrent-this' => $this->observed = $this->isLinkCurrent('this'), |
| 33 | + }; |
| 34 | + |
| 35 | + $this->terminate(); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + public function renderDefault(?int $tier = null): void |
| 40 | + { |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +function runSelfLink(string $op, array $params): SelfLinkPresenter |
| 46 | +{ |
| 47 | + $factory = Mockery::mock(Application\IPresenterFactory::class); |
| 48 | + $factory->shouldReceive('getPresenterClass')->andReturnUsing(fn($name) => $name . 'Presenter'); |
| 49 | + |
| 50 | + $presenter = new SelfLinkPresenter; |
| 51 | + $presenter->op = $op; |
| 52 | + $presenter->autoCanonicalize = false; |
| 53 | + $presenter->invalidLinkMode = Presenter::InvalidLinkTextual; |
| 54 | + $presenter->injectPrimary( |
| 55 | + new Http\Request(new Http\UrlScript('http://localhost/index.php', '/index.php')), |
| 56 | + new Http\Response, |
| 57 | + $factory, |
| 58 | + new Application\Routers\SimpleRouter, |
| 59 | + ); |
| 60 | + $presenter->run(new Application\Request('Self', Http\Request::Get, $params + ['action' => 'default'])); |
| 61 | + return $presenter; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// BC: subtype of InvalidLinkException |
| 66 | +Assert::type(InvalidLinkException::class, new InvalidRequestParameterException); |
| 67 | + |
| 68 | + |
| 69 | +// self-link with a bad current param -> 4xx |
| 70 | +Assert::exception( |
| 71 | + fn() => runSelfLink('redirect-this', ['tier' => 'abc']), |
| 72 | + Application\BadRequestException::class, |
| 73 | + 'Argument $tier passed to SelfLinkPresenter::renderDefault() must be ?int, string given.', |
| 74 | +); |
| 75 | + |
| 76 | +// a valid value still redirects normally |
| 77 | +Assert::noError(fn() => runSelfLink('redirect-this', ['tier' => '5'])); |
| 78 | + |
| 79 | + |
| 80 | +// isLinkCurrent('this') -> false, no throw |
| 81 | +Assert::false(runSelfLink('isLinkCurrent-this', ['tier' => 'abc'])->observed); |
| 82 | + |
| 83 | + |
| 84 | +// link('this') stays graceful (#error), no leak |
| 85 | +Assert::same( |
| 86 | + '#error: Argument $tier passed to SelfLinkPresenter::renderDefault() must be ?int, string given.', |
| 87 | + runSelfLink('link-this', ['tier' => 'abc'])->observed, |
| 88 | +); |
| 89 | + |
| 90 | + |
| 91 | +// explicit bad arg -> InvalidLinkException (programmer error) |
| 92 | +Assert::exception( |
| 93 | + fn() => runSelfLink('redirect-explicit', []), |
| 94 | + InvalidLinkException::class, |
| 95 | + 'Argument $tier passed to SelfLinkPresenter::renderDefault() must be ?int, string given.', |
| 96 | +); |
0 commit comments