ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilWebResourceParameterTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
23
28class ilWebResourceParameterTest extends TestCase
29{
30 protected function initDependencies(): void
31 {
32 $user = $this->getMockBuilder(ilObjUser::class)
33 ->disableOriginalConstructor()
34 ->onlyMethods(['getLogin', 'getId', 'getMatriculation'])
35 ->getMock();
36 $user->method('getLogin')->willReturn('login');
37 $user->method('getId')->willReturn(37);
38 $user->method('getMatriculation')->willReturn('matriculation');
39 }
40
41 public function testAppendToLink(): void
42 {
43 $user = $this->getMockBuilder(ilObjUser::class)
44 ->disableOriginalConstructor()
45 ->onlyMethods(['getLogin', 'getId', 'getMatriculation'])
46 ->getMock();
47 $user->method('getLogin')->willReturn('login');
48 $user->method('getId')->willReturn(37);
49 $user->method('getMatriculation')->willReturn('matriculation');
50
51 $link1 = 'link';
52 $link2 = 'li?nk';
53
55 $user,
56 3,
57 7,
58 13,
60 'name'
61 );
62
63 $this->assertSame(
64 'link?name=37',
65 $param->appendToLink($link1)
66 );
67 $this->assertSame(
68 'li?nk&name=37',
69 $param->appendToLink($link2)
70 );
71
73 $user,
74 3,
75 7,
76 13,
78 'name'
79 );
80
81 $this->assertSame(
82 'link?name=login',
83 $param->appendToLink($link1)
84 );
85 $this->assertSame(
86 'li?nk&name=login',
87 $param->appendToLink($link2)
88 );
89
91 $user,
92 3,
93 7,
94 13,
95 ilWebLinkBaseParameter::VALUES['matriculation'],
96 'name'
97 );
98
99 $this->assertSame(
100 'link?name=matriculation',
101 $param->appendToLink($link1)
102 );
103 $this->assertSame(
104 'li?nk&name=matriculation',
105 $param->appendToLink($link2)
106 );
107 }
108
109 public function testAppendToLinkException(): void
110 {
111 $user = $this->getMockBuilder(ilObjUser::class)
112 ->disableOriginalConstructor()
113 ->getMock();
114 $user->expects($this->never())
115 ->method($this->anything());
116
117 $link = 'link';
118 $param = new ilWebLinkParameter($user, 3, 7, 13, 1241, 'name');
119 $this->expectException(ilWebLinkParameterException::class);
120 $param->appendToLink($link);
121 }
122
123 public function testToXML(): void
124 {
125 $user = $this->getMockBuilder(ilObjUser::class)
126 ->disableOriginalConstructor()
127 ->getMock();
128 $user->expects($this->never())
129 ->method($this->anything());
130
131 $writer = $this->getMockBuilder(ilXmlWriter::class)
132 ->disableOriginalConstructor()
133 ->onlyMethods(['xmlElement'])
134 ->getMock();
135 /*
136 * willReturnCallback is a workaround to replace withConsecutive.
137 * The return value is irrelevant here, but if an unexpected parameter
138 * is passed, an exception will be thrown (instead of an assumption being
139 * broken as before).
140 * These tests should be rewritten to rely much less on PHPUnit for mocking.
141 */
142 $writer->expects($this->exactly(3))
143 ->method('xmlElement')
144 ->willReturnCallback(fn($tag, $attrs) => match([$tag, $attrs]) {
145 ['DynamicParameter', [
146 'id' => 7,
147 'name' => 'name1',
148 'type' => 'userId'
149 ]] => 1,
150 ['DynamicParameter', [
151 'id' => 8,
152 'name' => 'name2',
153 'type' => 'userName'
154 ]] => 2,
155 ['DynamicParameter', [
156 'id' => 9,
157 'name' => 'name3',
158 'type' => 'matriculation'
159 ]] => 3
160 });
161
163 $user,
164 0,
165 13,
166 7,
168 'name1'
169 );
170 $param->toXML($writer);
172 $user,
173 0,
174 13,
175 8,
177 'name2'
178 );
179 $param->toXML($writer);
181 $user,
182 0,
183 13,
184 9,
185 ilWebLinkBaseParameter::VALUES['matriculation'],
186 'name3'
187 );
188 $param->toXML($writer);
189 $param = new ilWebLinkParameter($user, 0, 13, 9, 987, 'name3');
190 $param->toXML($writer);
191 }
192
193 public function testGetInfo(): void
194 {
195 $user = $this->getMockBuilder(ilObjUser::class)
196 ->disableOriginalConstructor()
197 ->getMock();
198 $user->expects($this->never())
199 ->method($this->anything());
200
202 $user,
203 0,
204 13,
205 7,
207 'name1'
208 );
209 $this->assertSame('name1=USER_ID', $param->getInfo());
211 $user,
212 0,
213 13,
214 8,
216 'name2'
217 );
218 $this->assertSame('name2=LOGIN', $param->getInfo());
220 $user,
221 0,
222 13,
223 9,
224 ilWebLinkBaseParameter::VALUES['matriculation'],
225 'name3'
226 );
227 $this->assertSame('name3=MATRICULATION', $param->getInfo());
228 }
229
230 public function testGetInfoException(): void
231 {
232 $user = $this->getMockBuilder(ilObjUser::class)
233 ->disableOriginalConstructor()
234 ->getMock();
235 $user->expects($this->never())
236 ->method($this->anything());
237
238 $param = new ilWebLinkParameter($user, 0, 13, 7, 374, 'name1');
239 $this->expectException(ilWebLinkParameterException::class);
240 $param->getInfo();
241 }
242}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
const array VALUES
TODO Once the GUI is updated, undefined can be dropped.
Immutable class for parameters attached to Web Link items.
Unit tests for ilWebLinkParameter.
$param
Definition: xapitoken.php:46