ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilTermsOfServiceUserHasLanguageCriterionTest.php
Go to the documentation of this file.
1<?php
2
5
11{
13 protected $lng;
14
16 protected $expectedInitialValue = 'en';
17
20
22 protected $englishLanguageTranslation = 'English';
23
25 protected $germanLanguageTranslation = 'German';
26
30 public function setUp()
31 {
32 parent::setUp();
33
34 $this->lng = $this->getLanguageMock();
35
36 $this->lng
37 ->expects($this->any())
38 ->method('txt')
39 ->willReturn('dummy');
40
41 $this->lng
42 ->expects($this->any())
43 ->method('getInstalledLanguages')
44 ->willReturn([$this->expectedAfterFormSubmitValue, $this->expectedInitialValue]);
45 }
46
51 {
52 return new \ilTermsOfServiceUserHasLanguageCriterion();
53 }
54
59 {
60 $criterion = $this->getInstance();
61
62 $this->assertEquals('usr_language', $criterion->getTypeIdent());
63 $this->assertEquals(true, $criterion->hasUniqueNature());
64
65 return $criterion;
66 }
67
73 protected function buildForm(
75 string $httpCriterionSelectionBodyParameter
77 $form = $this->getFormMock();
78
79 $radioGroup = $this->getRadioGroupMock();
80
81 $radioGroup
82 ->expects($this->any())
83 ->method('getPostVar')
84 ->willReturn($httpCriterionSelectionBodyParameter);
85
86 $form->addItem($radioGroup);
87
88 $gui->appendOption($radioGroup, $this->getCriterionConfig(['lng' => $this->expectedInitialValue]));
89
90 return $form;
91 }
92
99 {
100 $httpCriterionSelectionBodyParameter = 'criterion';
101 $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_lng';
102
103 $gui = $criterion->ui($this->lng);
104
105 $this->assertInstanceOf(\ilTermsOfServiceUserHasLanguageCriterionGUI::class, $gui);
106
107 $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
108
109 $languageSelection = $form->getItemByPostVar($httpCriterionConfigBodyParameter);
110 $this->assertInstanceOf(\ilSelectInputGUI::class, $languageSelection);
111 $this->assertEquals($languageSelection->getValue(), $this->expectedInitialValue);
112
113 return $criterion;
114 }
115
121 {
122 $httpCriterionSelectionBodyParameter = 'criterion';
123 $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_lng';
124
125 $gui = $criterion->ui($this->lng);
126
127 $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
128
129 $form
130 ->expects($this->once())
131 ->method('getInput')
132 ->with($httpCriterionConfigBodyParameter)
133 ->will($this->returnCallback(function () {
134 return $this->expectedAfterFormSubmitValue;
135 }));
136
137 $value = $gui->getConfigByForm($form);
138
139 $this->assertInstanceOf(\ilTermsOfServiceCriterionConfig::class, $value);
140 $this->assertEquals($this->expectedAfterFormSubmitValue, $value['lng']);
141 $this->assertEquals($this->getCriterionConfig(['lng' => $this->expectedAfterFormSubmitValue]), $value);
142 }
143
149 {
150 $gui = $criterion->ui($this->lng);
151
152 $actual = $gui->getIdentPresentation();
153
154 $this->assertInternalType('string', $actual);
155 $this->assertNotEmpty($actual);
156 }
157
161 public function languageProvider() : array
162 {
163 return [
164 [$this->expectedInitialValue, $this->englishLanguageTranslation],
165 [$this->expectedAfterFormSubmitValue, $this->germanLanguageTranslation],
166 ['invalid_lng', ''],
167 ];
168 }
169
175 public function testValuePresentationMatchesExpectation(string $lng, string $translation)
176 {
177 $language = $this->getLanguageMock();
178
180 ->expects($this->any())
181 ->method('txt')
182 ->with('meta_l_' . $lng, '')
183 ->willReturn($translation);
184
185
186 $criterion = new \ilTermsOfServiceUserHasLanguageCriterion();
187 $gui = $criterion->ui($language);
188
190 $actual = $gui->getValuePresentation(
191 $this->getCriterionConfig(['lng' => $lng]),
192 $this->dic->ui()->factory()
193 );
194
195 $this->assertInstanceOf(Component::class, $actual);
196 $this->assertInstanceOf(Legacy::class, $actual);
197 $this->assertEquals($translation, $actual->getContent());
198 }
199
203 public function failingConfigProvider() : array
204 {
205 $criterion = $this->getInstance();
206
207 return [
208 [$criterion, $this->getCriterionConfig(['lng' => 'en'])],
209 [$criterion, $this->getCriterionConfig(['lng' => []])],
210 [$criterion, $this->getCriterionConfig(['lng' => new stdClass()])],
211 [$criterion, $this->getCriterionConfig(['lng' => 1.0])],
212 [$criterion, $this->getCriterionConfig(['lng' => 1])],
213 [$criterion, $this->getCriterionConfig(['another_config_key' => true])],
214 [$criterion, $this->getCriterionConfig()],
215 ];
216 }
217
221 public function succeedingConfigProvider() : array
222 {
223 $criterion = $this->getInstance();
224
225 return [
226 [$criterion, $this->getCriterionConfig(['lng' => 'de'])],
227 [$criterion, $this->getCriterionConfig(['lng' => 'DE'])],
228 ];
229 }
230
239 ) {
240 $user = $this->getUserMock();
241
242 $user
243 ->expects($this->any())
244 ->method('getLanguage')
245 ->willReturn('de');
246
247 $this->assertFalse($criterion->evaluate($user, $config));
248 }
249
258 ) {
259 $user = $this->getUserMock();
260
261 $user
262 ->expects($this->any())
263 ->method('getLanguage')
264 ->willReturn('de');
265
266 $this->assertTrue($criterion->evaluate($user, $config));
267 }
268}
An exception for terminatinating execution or to throw for unit testing.
This class represents a property form user interface.
Class ilTermsOfServiceCriterionBaseTest.
Class ilTermsOfServiceCriterionConfig.
buildForm(\ilTermsOfServiceCriterionTypeGUI $gui, string $httpCriterionSelectionBodyParameter)
testFormUserInterfaceElementsAreProperlyBuilt(\ilTermsOfServiceUserHasLanguageCriterion $criterion)
testValuesFromFormUserInterfaceElementsCanBeRetrieved(\ilTermsOfServiceUserHasLanguageCriterion $criterion)
@depends testFormUserInterfaceElementsAreProperlyBuilt
testTypeIdentPresentationIsANonEmptyString(\ilTermsOfServiceUserHasLanguageCriterion $criterion)
@depends testFormUserInterfaceElementsAreProperlyBuilt
testEvaluationFailsIfUserLanguageDoesNotMatchDefinedLanguage(\ilTermsOfServiceUserHasLanguageCriterion $criterion, \ilTermsOfServiceCriterionConfig $config)
testEvaluationSucceedsIfUserLanguageDoesMatchDefinedLanguage(\ilTermsOfServiceUserHasLanguageCriterion $criterion, \ilTermsOfServiceCriterionConfig $config)
evaluate(\ilObjUser $user, \ilTermsOfServiceCriterionConfig $config)
bool
ui(\ilLanguage $lng)
\ilTermsOfServiceCriterionTypeGUI
getTypeIdent()
Returns a unique id of the criterion type.string
A component is the most general form of an entity in the UI.
Definition: Component.php:14
Interface ilTermsOfServiceCriterionTypeGUI.
appendOption(\ilRadioGroupInputGUI $option, \ilTermsOfServiceCriterionConfig $config)
getValuePresentation(\ilTermsOfServiceCriterionConfig $config, Factory $uiFactory)
getConfigByForm(\ilPropertyFormGUI $form)
$config
Definition: bootstrap.php:15
$user
Definition: migrateto20.php:57
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
if(isset($_POST['submit'])) $form
$lng