|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Auth\ProcessingFilter; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleSAML\Auth\ProcessingFilter as AuthProcFilter; |
| 10 | +use SimpleSAML\Module\core\Auth\Process\AttributeAlter; |
| 11 | + |
| 12 | +/** |
| 13 | + * Test for the ProccessingFilter. |
| 14 | + * |
| 15 | + * @covers \SimpleSAML\Auth\ProcessingFilter |
| 16 | + */ |
| 17 | +class AttributeAlterTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Helper function to run the filter with a given configuration. |
| 21 | + * |
| 22 | + * @param array $config The filter configuration. |
| 23 | + * @param array $request The request state. |
| 24 | + * @return \SimpleSAML\Auth\ProcessFilter. |
| 25 | + */ |
| 26 | + private static function processFilter(array $config, array $request): AuthProcFilter |
| 27 | + { |
| 28 | + return new AttributeAlter($config, null); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * Test that a filter without precondition will run. |
| 34 | + */ |
| 35 | + public function testWithoutPrecondition(): void |
| 36 | + { |
| 37 | + $config = [ |
| 38 | + 'subject' => 'test', |
| 39 | + 'pattern' => '/wrong/', |
| 40 | + 'replacement' => 'right', |
| 41 | + ]; |
| 42 | + |
| 43 | + $request = [ |
| 44 | + 'Attributes' => [ |
| 45 | + 'test' => ['somethingiswrong'], |
| 46 | + ], |
| 47 | + ]; |
| 48 | + |
| 49 | + $filter = self::processFilter($config, $request); |
| 50 | + $this->assertTrue($filter->checkPrecondition($request)); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * Test that a filter with a precondition evaluating to true will run. |
| 56 | + */ |
| 57 | + public function testWithPreconditionTrue(): void |
| 58 | + { |
| 59 | + $config = [ |
| 60 | + '%precondition' => 'return true;', |
| 61 | + 'subject' => 'test', |
| 62 | + 'pattern' => '/wrong/', |
| 63 | + 'replacement' => 'right', |
| 64 | + ]; |
| 65 | + |
| 66 | + $request = [ |
| 67 | + 'Attributes' => [ |
| 68 | + 'test' => ['somethingiswrong'], |
| 69 | + ], |
| 70 | + ]; |
| 71 | + |
| 72 | + $filter = self::processFilter($config, $request); |
| 73 | + $this->assertTrue($filter->checkPrecondition($request)); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Test that a filter with a precondition evaluating to false will not run. |
| 79 | + */ |
| 80 | + public function testWithPreconditionFalse(): void |
| 81 | + { |
| 82 | + $config = [ |
| 83 | + '%precondition' => 'return false;', |
| 84 | + 'subject' => 'test', |
| 85 | + 'pattern' => '/wrong/', |
| 86 | + 'replacement' => 'right', |
| 87 | + ]; |
| 88 | + |
| 89 | + $request = [ |
| 90 | + 'Attributes' => [ |
| 91 | + 'test' => ['somethingiswrong'], |
| 92 | + ], |
| 93 | + ]; |
| 94 | + |
| 95 | + $filter = self::processFilter($config, $request); |
| 96 | + $this->assertFalse($filter->checkPrecondition($request)); |
| 97 | + } |
| 98 | +} |
0 commit comments