ILIAS  release_8 Revision v8.24
ilTermsOfServiceUserHasCountryCriterionTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use PHPUnit\Framework\MockObject\MockObject;
24
30{
32 protected ilLanguage $lng;
33 protected string $expectedInitialValue = 'EN';
34 protected string $expectedAfterFormSubmitValue = 'DE';
35 protected string $englishLanguageTranslation = 'English';
36 protected string $germanLanguageTranslation = 'German';
38 protected array $countries = [];
39
40 protected function setUp(): void
41 {
42 parent::setUp();
43
44 $this->lng = $this->getLanguageMock();
45
46 $this->lng
47 ->method('txt')
48 ->willReturn('dummy');
49
50 $this->countries = ['EN', 'DE'];
51 }
52
54 {
55 return new ilTermsOfServiceUserHasCountryCriterion($this->countries);
56 }
57
59 {
60 $criterion = $this->getInstance();
61
62 $this->assertSame('usr_country', $criterion->getTypeIdent());
63 $this->assertTrue($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 ->method('getPostVar')
83 ->willReturn($httpCriterionSelectionBodyParameter);
84
85 $form->addItem($radioGroup);
86
87 $gui->appendOption($radioGroup, $this->getCriterionConfig(['country' => $this->expectedInitialValue]));
88
89 return $form;
90 }
91
100 $httpCriterionSelectionBodyParameter = 'criterion';
101 $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_country';
102
103 $gui = $criterion->ui($this->lng);
104
105 $this->assertInstanceOf(ilTermsOfServiceUserHasCountryCriterionGUI::class, $gui);
106
107 $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
108
109 $countrySelection = $form->getItemByPostVar($httpCriterionConfigBodyParameter);
110 $this->assertInstanceOf(ilSelectInputGUI::class, $countrySelection);
111 $this->assertSame($countrySelection->getValue(), $this->expectedInitialValue);
112
113 return $criterion;
114 }
115
122 ): void {
123 $httpCriterionSelectionBodyParameter = 'criterion';
124 $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_country';
125
126 $gui = $criterion->ui($this->lng);
127
128 $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
129
130 $form
131 ->expects($this->once())
132 ->method('getInput')
133 ->with($httpCriterionConfigBodyParameter)
134 ->willReturnCallback(function () {
135 return $this->expectedAfterFormSubmitValue;
136 });
137
138 $value = $gui->getConfigByForm($form);
139
140 $this->assertInstanceOf(ilTermsOfServiceCriterionConfig::class, $value);
141 $this->assertSame($this->expectedAfterFormSubmitValue, $value['country']);
142 $this->assertEquals($this->getCriterionConfig(['country' => $this->expectedAfterFormSubmitValue]), $value);
143 }
144
151 ): void {
152 $gui = $criterion->ui($this->lng);
153
154 $actual = $gui->getIdentPresentation();
155
156 $this->assertIsString($actual);
157 $this->assertNotEmpty($actual);
158 }
159
163 public function countryProvider(): array
164 {
165 return [
166 'English Language' => [$this->expectedInitialValue, $this->englishLanguageTranslation],
167 'German Language' => [$this->expectedAfterFormSubmitValue, $this->germanLanguageTranslation],
168 'Invalid Languages' => ['invalid_country', ''],
169 ];
170 }
171
177 public function testValuePresentationMatchesExpectation(string $country, string $translation): void
178 {
179 $language = $this->getLanguageMock();
180
181 $language
182 ->method('txt')
183 ->with('meta_c_' . $country, '')
184 ->willReturn($translation);
185
186 $criterion = new ilTermsOfServiceUserHasCountryCriterion($this->countries);
187 $gui = $criterion->ui($language);
188
190 $actual = $gui->getValuePresentation(
191 $this->getCriterionConfig(['country' => $country]),
192 $this->getUiFactoryMock()
193 );
194
195 $this->assertInstanceOf(Component::class, $actual);
196 $this->assertInstanceOf(Legacy::class, $actual);
197 $this->assertSame($translation, $actual->getContent());
198 }
199
200 public function failingConfigProvider(): array
201 {
202 $criterion = $this->getInstance();
203
204 return [
205 'English Language where German is Expected' => [$criterion, $this->getCriterionConfig(['country' => 'en'])],
206 'Array' => [$criterion, $this->getCriterionConfig(['country' => []])],
207 'Object' => [$criterion, $this->getCriterionConfig(['country' => new stdClass()])],
208 'Double' => [$criterion, $this->getCriterionConfig(['country' => 1.0])],
209 'Integer' => [$criterion, $this->getCriterionConfig(['country' => 1])],
210 'Wrong Key Provided for Extracting Language' => [
211 $criterion,
212 $this->getCriterionConfig(['another_config_key' => true])
213 ],
214 'Empty Configuration' => [$criterion, $this->getCriterionConfig()],
215 ];
216 }
217
218 public function succeedingConfigProvider(): array
219 {
220 $criterion = $this->getInstance();
221
222 return [
223 'German Language' => [$criterion, $this->getCriterionConfig(['country' => 'de'])],
224 'German Language Uppercase' => [$criterion, $this->getCriterionConfig(['country' => 'DE'])],
225 ];
226 }
227
236 ): void {
237 $user = $this->getUserMock();
238
239 $user
240 ->method('getSelectedCountry')
241 ->willReturn('de');
242
243 $this->assertFalse($criterion->evaluate($user, $config));
244 }
245
254 ): void {
255 $user = $this->getUserMock();
256
257 $user
258 ->method('getSelectedCountry')
259 ->willReturn('de');
260
261 $this->assertTrue($criterion->evaluate($user, $config));
262 }
263}
language handling
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)
evaluate(ilObjUser $user, ilTermsOfServiceCriterionConfig $config)
A component is the most general form of an entity in the UI.
Definition: Component.php:28
Interface ilTermsOfServiceCriterionTypeGUI.
appendOption(ilRadioGroupInputGUI $group, ilTermsOfServiceCriterionConfig $config)
getConfigByForm(ilPropertyFormGUI $form)
getValuePresentation(ilTermsOfServiceCriterionConfig $config, Factory $uiFactory)
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85