1<?php
2/* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilSamlMappedUserAttributeValueParserTest
6 */
7class ilSamlMappedUserAttributeValueParserTest extends \PHPUnit_Framework_TestCase
8{
9    /**
10     * @param string $exception_class
11     */
12    protected function assertException($exception_class)
13    {
14        if (version_compare(PHPUnit_Runner_Version::id(), '5.0', '>=')) {
15            $this->setExpectedException($exception_class);
16        }
17    }
18
19    /**
20     * @param $externalAttributeReference
21     * @return \ilExternalAuthUserAttributeMappingRule
22     */
23    protected function getMappingRuleMock($externalAttributeReference)
24    {
25        $rule = $this->getMockBuilder(ilExternalAuthUserAttributeMappingRule::class)->disableOriginalConstructor()->getMock();
26        $rule->expects($this->any())->method('getExternalAttribute')->will($this->returnValue($externalAttributeReference));
27        $rule->expects($this->any())->method('getAttribute')->will($this->returnValue($externalAttributeReference));
28
29        return $rule;
30    }
31
32    public function testValueGivenAsStringCanBeRetrievedForExternalAttribute()
33    {
34        $expectedValue = 'ILIAS';
35
36        $attributeKey = 'firstname';
37        $attributeValue = $expectedValue;
38
39        $userData = [$attributeKey => $attributeValue];
40
41        $parser = new ilSamlMappedUserAttributeValueParser($this->getMappingRuleMock($attributeKey), $userData);
42        $this->assertEquals($expectedValue, $parser->parse());
43    }
44
45    public function testValueGivenAsArrayCanBeRetrievedForExternalAttribute()
46    {
47        $expectedValue = 'ILIAS';
48
49        $attributeKey = 'firstname';
50        $attributeValue = [$expectedValue];
51
52        $userData = [$attributeKey => $attributeValue];
53
54        $parser = new ilSamlMappedUserAttributeValueParser($this->getMappingRuleMock($attributeKey), $userData);
55        $this->assertEquals($expectedValue, $parser->parse());
56    }
57
58    public function testValueGivenAsArrayCanBeRetrievedForExternalAttributeWithSpecificIndex()
59    {
60        $expectedValue = 'ILIAS';
61        $expectedValueIndex = 5;
62
63        $attributeKey = 'firstname';
64        $attributeValue = [$expectedValueIndex => $expectedValue];
65
66        $userData = [$attributeKey => $attributeValue];
67
68        $parser = new ilSamlMappedUserAttributeValueParser(
69            $this->getMappingRuleMock($attributeKey . '|' . $expectedValueIndex),
70            $userData
71        );
72        $this->assertEquals($expectedValue, $parser->parse());
73    }
74
75    public function testExceptionIsRaisedIfAnExpectedAttributeIsMissing()
76    {
77        $this->assertException(ilSamlException::class);
78
79        $attributeKey = 'firstname';
80        $userData = [];
81
82        $parser = new ilSamlMappedUserAttributeValueParser($this->getMappingRuleMock($attributeKey), $userData);
83        $parser->parse();
84    }
85
86    public function testExceptionIsRaisedIfAnExpectedValueCouldNotBeFoundForAnExpectedValueIndex()
87    {
88        $this->assertException(ilSamlException::class);
89
90        $expectedValue = 'ILIAS';
91        $expectedValueIndex = 5;
92
93        $attributeKey = 'firstname';
94        $attributeValue = [($expectedValueIndex + 1) => $expectedValue];
95
96        $userData = [$attributeKey => $attributeValue];
97
98        $parser = new ilSamlMappedUserAttributeValueParser(
99            $this->getMappingRuleMock($attributeKey . '|' . $expectedValueIndex),
100            $userData
101        );
102        $parser->parse();
103    }
104
105    public function testExceptionIsRaisedForNonScalarValues()
106    {
107        $this->assertException(ilSamlException::class);
108
109        $expectedValue = array('ILIAS');
110        $expectedValueIndex = 5;
111
112        $attributeKey = 'firstname';
113        $attributeValue = [$expectedValueIndex => $expectedValue];
114
115        $userData = [$attributeKey => $attributeValue];
116
117        $parser = new ilSamlMappedUserAttributeValueParser(
118            $this->getMappingRuleMock($attributeKey . '|' . $expectedValueIndex),
119            $userData
120        );
121        $parser->parse();
122    }
123}
124