ILIAS  release_7 Revision v7.30-3-g800a261c036
ilTermsOfServiceUserHasCountryCriterionTest.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
5use PHPUnit\Framework\MockObject\MockObject;
6
12{
14 protected $lng;
15
17 protected $expectedInitialValue = 'EN';
18
21
23 protected $englishLanguageTranslation = 'English';
24
26 protected $germanLanguageTranslation = 'German';
27
29 protected $countries = [];
30
34 public function setUp() : void
35 {
36 parent::setUp();
37
38 $this->lng = $this->getLanguageMock();
39
40 $this->lng
41 ->expects($this->any())
42 ->method('txt')
43 ->willReturn('dummy');
44
45 $this->countries = ['EN', 'DE'];
46 }
47
52 {
53 return new ilTermsOfServiceUserHasCountryCriterion($this->countries);
54 }
55
60 {
61 $criterion = $this->getInstance();
62
63 $this->assertEquals('usr_country', $criterion->getTypeIdent());
64 $this->assertEquals(true, $criterion->hasUniqueNature());
65
66 return $criterion;
67 }
68
75 protected function buildForm(
77 string $httpCriterionSelectionBodyParameter
79 $form = $this->getFormMock();
80
81 $radioGroup = $this->getRadioGroupMock();
82
83 $radioGroup
84 ->expects($this->any())
85 ->method('getPostVar')
86 ->willReturn($httpCriterionSelectionBodyParameter);
87
88 $form->addItem($radioGroup);
89
90 $gui->appendOption($radioGroup, $this->getCriterionConfig(['country' => $this->expectedInitialValue]));
91
92 return $form;
93 }
94
104 $httpCriterionSelectionBodyParameter = 'criterion';
105 $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_country';
106
107 $gui = $criterion->ui($this->lng);
108
109 $this->assertInstanceOf(ilTermsOfServiceUserHasCountryCriterionGUI::class, $gui);
110
111 $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
112
113 $countrySelection = $form->getItemByPostVar($httpCriterionConfigBodyParameter);
114 $this->assertInstanceOf(ilSelectInputGUI::class, $countrySelection);
115 $this->assertEquals($countrySelection->getValue(), $this->expectedInitialValue);
116
117 return $criterion;
118 }
119
127 ) : void {
128 $httpCriterionSelectionBodyParameter = 'criterion';
129 $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_country';
130
131 $gui = $criterion->ui($this->lng);
132
133 $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
134
135 $form
136 ->expects($this->once())
137 ->method('getInput')
138 ->with($httpCriterionConfigBodyParameter)
139 ->will($this->returnCallback(function () {
140 return $this->expectedAfterFormSubmitValue;
141 }));
142
143 $value = $gui->getConfigByForm($form);
144
145 $this->assertInstanceOf(ilTermsOfServiceCriterionConfig::class, $value);
146 $this->assertEquals($this->expectedAfterFormSubmitValue, $value['country']);
147 $this->assertEquals($this->getCriterionConfig(['country' => $this->expectedAfterFormSubmitValue]), $value);
148 }
149
156 ) : void {
157 $gui = $criterion->ui($this->lng);
158
159 $actual = $gui->getIdentPresentation();
160
161 $this->assertIsString($actual);
162 $this->assertNotEmpty($actual);
163 }
164
168 public function countryProvider() : array
169 {
170 return [
171 'English Language' => [$this->expectedInitialValue, $this->englishLanguageTranslation],
172 'German Language' => [$this->expectedAfterFormSubmitValue, $this->germanLanguageTranslation],
173 'Invalid Languages' => ['invalid_country', ''],
174 ];
175 }
176
183 public function testValuePresentationMatchesExpectation(string $country, string $translation) : void
184 {
185 $language = $this->getLanguageMock();
186
187 $language
188 ->expects($this->any())
189 ->method('txt')
190 ->with('meta_c_' . $country, '')
191 ->willReturn($translation);
192
193 $criterion = new ilTermsOfServiceUserHasCountryCriterion($this->countries);
194 $gui = $criterion->ui($language);
195
197 $actual = $gui->getValuePresentation(
198 $this->getCriterionConfig(['country' => $country]),
199 $this->getUiFactoryMock()
200 );
201
202 $this->assertInstanceOf(Component::class, $actual);
203 $this->assertInstanceOf(Legacy::class, $actual);
204 $this->assertEquals($translation, $actual->getContent());
205 }
206
210 public function failingConfigProvider() : array
211 {
212 $criterion = $this->getInstance();
213
214 return [
215 'English Language where German is Expected' => [$criterion, $this->getCriterionConfig(['country' => 'en'])],
216 'Array' => [$criterion, $this->getCriterionConfig(['country' => []])],
217 'Object' => [$criterion, $this->getCriterionConfig(['country' => new stdClass()])],
218 'Double' => [$criterion, $this->getCriterionConfig(['country' => 1.0])],
219 'Integer' => [$criterion, $this->getCriterionConfig(['country' => 1])],
220 'Wrong Key Provided for Extracting Language' => [
221 $criterion,
222 $this->getCriterionConfig(['another_config_key' => true])
223 ],
224 'Empty Configuration' => [$criterion, $this->getCriterionConfig()],
225 ];
226 }
227
231 public function succeedingConfigProvider() : array
232 {
233 $criterion = $this->getInstance();
234
235 return [
236 'German Language' => [$criterion, $this->getCriterionConfig(['country' => 'de'])],
237 'German Language Uppercase' => [$criterion, $this->getCriterionConfig(['country' => 'DE'])],
238 ];
239 }
240
250 ) : void {
251 $user = $this->getUserMock();
252
253 $user
254 ->expects($this->any())
255 ->method('getSelectedCountry')
256 ->willReturn('de');
257
258 $this->assertFalse($criterion->evaluate($user, $config));
259 }
260
270 ) : void {
271 $user = $this->getUserMock();
272
273 $user
274 ->expects($this->any())
275 ->method('getSelectedCountry')
276 ->willReturn('de');
277
278 $this->assertTrue($criterion->evaluate($user, $config));
279 }
280}
An exception for terminatinating execution or to throw for unit testing.
This class represents a property form user interface.
Class ilTermsOfServiceCriterionBaseTest.
Class ilTermsOfServiceCriterionConfig.
Class ilTermsOfServiceUserHasCountryCriterionTest.
testTypeIdentPresentationIsANonEmptyString(ilTermsOfServiceUserHasCountryCriterion $criterion)
@depends testFormUserInterfaceElementsAreProperlyBuilt
testEvaluationSucceedsIfUserCountryDoesMatchDefinedLanguage(ilTermsOfServiceUserHasCountryCriterion $criterion, ilTermsOfServiceCriterionConfig $config)
buildForm(ilTermsOfServiceCriterionTypeGUI $gui, string $httpCriterionSelectionBodyParameter)
testEvaluationFailsIfUserCountryDoesNotMatchDefinedLanguage(ilTermsOfServiceUserHasCountryCriterion $criterion, ilTermsOfServiceCriterionConfig $config)
testValuesFromFormUserInterfaceElementsCanBeRetrieved(ilTermsOfServiceUserHasCountryCriterion $criterion)
@depends testFormUserInterfaceElementsAreProperlyBuilt
testFormUserInterfaceElementsAreProperlyBuilt(ilTermsOfServiceUserHasCountryCriterion $criterion)
ui(ilLanguage $lng)
ilTermsOfServiceCriterionTypeGUI
getTypeIdent()
Returns a unique id of the criterion type.string
evaluate(ilObjUser $user, ilTermsOfServiceCriterionConfig $config)
bool
A component is the most general form of an entity in the UI.
Definition: Component.php:14
Interface ilTermsOfServiceCriterionTypeGUI.
getConfigByForm(ilPropertyFormGUI $form)
getValuePresentation(ilTermsOfServiceCriterionConfig $config, Factory $uiFactory)
appendOption(ilRadioGroupInputGUI $option, ilTermsOfServiceCriterionConfig $config)
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
ui()
Definition: ui.php:5