ContextTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. final class ContextTest extends \PHPUnit\Framework\TestCase {
  5. /** @return Traversable<string,array{array<string,mixed>,int}> */
  6. public static function provideRedirectStates(): Traversable {
  7. yield 'default preference' => [[], 0];
  8. yield 'legacy unread filter' => [['state' => '2'], 2];
  9. yield 'legacy favourite filter' => [['state' => '4'], 4];
  10. yield 'legacy all articles' => [['state' => '3'], 3];
  11. yield 'automatic unread view' => [['state' => '2', 'stateForRedirect' => '0'], 0];
  12. yield 'automatic all view' => [['state' => '3', 'stateForRedirect' => '0'], 0];
  13. yield 'explicit unread view' => [['state' => '2', 'stateForRedirect' => '2'], 2];
  14. yield 'explicit favourite view' => [['state' => '4', 'stateForRedirect' => '4'], 4];
  15. yield 'original filter takes precedence' => [['state' => '2', 'stateForRedirect' => '4'], 4];
  16. yield 'integer zero remains automatic' => [['state' => 2, 'stateForRedirect' => 0], 0];
  17. yield 'invalid origin falls back' => [['state' => '2', 'stateForRedirect' => 'invalid'], 2];
  18. yield 'array origin falls back' => [['state' => '4', 'stateForRedirect' => ['0']], 4];
  19. }
  20. /** @param array<string,mixed> $params */
  21. #[DataProvider('provideRedirectStates')]
  22. public static function testStateForRedirect(array $params, int $expected): void {
  23. $original = Minz_Request::params();
  24. try {
  25. Minz_Request::_params($params);
  26. self::assertSame($expected, FreshRSS_Context::getStateForRedirect());
  27. self::assertSame($params, Minz_Request::params());
  28. } finally {
  29. Minz_Request::_params($original);
  30. }
  31. }
  32. public static function testAutomaticStateSurvivesUrlEncoding(): void {
  33. $url = Minz_Url::display(['params' => ['state' => 2, 'stateForRedirect' => 0]], 'ascii');
  34. self::assertStringContainsString('state=2&stateForRedirect=0', $url);
  35. }
  36. }