1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Module\core\Auth\Process;
use Exception;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Error;
use SimpleSAML\Module\core\Auth\Process\PHP;
/**
* Test for the core:PHP filter.
*/
class PHPTest extends TestCase
{
/**
* Helper function to run the filter with a given configuration.
*
* @param array $config The filter configuration.
* @param array $request The request state.
*
* @return array The state array after processing.
*/
private static function processFilter(array $config, array $request): array
{
$filter = new PHP($config, null);
@$filter->process($request);
return $request;
}
/**
* Test the configuration of the filter.
* @return void
*/
public function testInvalidConfiguration(): void
{
$config = [];
$this->expectException(Error\Exception::class);
$this->expectExceptionMessage(
"core:PHP: missing mandatory configuration option 'code'."
);
new PHP($config, null);
}
/**
* Check that defining the code works as expected.
* @return void
*/
public function testCodeDefined(): void
{
$config = [
'code' => '
$attributes["key"] = array("value");
',
];
$request = ['Attributes' => []];
$expected = [
'Attributes' => [
'key' => ['value'],
],
];
$this->assertEquals($expected, $this->processFilter($config, $request));
}
/**
* Check that the incoming attributes are also available after processing
* @return void
*/
public function testPreserveIncomingAttributes(): void
{
$config = [
'code' => '
$attributes["orig2"] = array("value0");
',
];
$request = [
'Attributes' => [
'orig1' => ['value1', 'value2'],
'orig2' => ['value3'],
'orig3' => ['value4']
]
];
$expected = [
'Attributes' => [
'orig1' => ['value1', 'value2'],
'orig2' => ['value0'],
'orig3' => ['value4']
],
];
$this->assertEquals($expected, $this->processFilter($config, $request));
}
/**
* Check that throwing an Exception inside the PHP code of the
* filter (a documented use case) works.
* @return void
*/
public function testThrowExceptionFromFilter(): void
{
$config = [
'code' => '
if (empty($attributes["uid"])) {
throw new Exception("Missing uid attribute.");
}
$attributes["uid"][0] = strtoupper($attributes["uid"][0]);
',
];
$request = [
'Attributes' => [
'orig1' => ['value1', 'value2'],
]
];
$this->expectException(Exception::class);
$this->expectExceptionMessage("Missing uid attribute.");
$this->processFilter($config, $request);
}
/**
* Check that the entire state can be adjusted.
* @return void
*/
public function testStateCanBeModified(): void
{
$config = [
'code' => '
$attributes["orig2"] = array("value0");
$state["newKey"] = ["newValue"];
$state["Destination"]["attributes"][] = "givenName";
',
];
$request = [
'Attributes' => [
'orig1' => ['value1', 'value2'],
'orig2' => ['value3'],
'orig3' => ['value4']
],
'Destination' => [
'attributes' => ['eduPersonPrincipalName']
],
];
$expected = [
'Attributes' => [
'orig1' => ['value1', 'value2'],
'orig2' => ['value0'],
'orig3' => ['value4']
],
'Destination' => [
'attributes' => ['eduPersonPrincipalName', 'givenName']
],
'newKey' => ['newValue']
];
$this->assertEquals($expected, $this->processFilter($config, $request));
}
}
|