ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ilTermsOfServiceUserHasLanguageCriterionTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
6 
12 {
14  protected $lng;
15 
17  protected $expectedInitialValue = 'en';
18 
21 
23  protected $englishLanguageTranslation = 'English';
24 
26  protected $germanLanguageTranslation = 'German';
27 
31  public function setUp() : void
32  {
33  parent::setUp();
34 
35  $this->lng = $this->getLanguageMock();
36 
37  $this->lng
38  ->expects($this->any())
39  ->method('txt')
40  ->willReturn('dummy');
41 
42  $this->lng
43  ->expects($this->any())
44  ->method('getInstalledLanguages')
45  ->willReturn([$this->expectedAfterFormSubmitValue, $this->expectedInitialValue]);
46  }
47 
52  {
54  }
55 
60  {
61  $criterion = $this->getInstance();
62 
63  $this->assertEquals('usr_language', $criterion->getTypeIdent());
64  $this->assertEquals(true, $criterion->hasUniqueNature());
65 
66  return $criterion;
67  }
68 
75  protected function buildForm(
77  string $httpCriterionSelectionBodyParameter
78  ) : ilPropertyFormGUI {
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(['lng' => $this->expectedInitialValue]));
91 
92  return $form;
93  }
94 
104  $httpCriterionSelectionBodyParameter = 'criterion';
105  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_lng';
106 
107  $gui = $criterion->ui($this->lng);
108 
109  $this->assertInstanceOf(ilTermsOfServiceUserHasLanguageCriterionGUI::class, $gui);
110 
111  $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
112 
113  $languageSelection = $form->getItemByPostVar($httpCriterionConfigBodyParameter);
114  $this->assertInstanceOf(ilSelectInputGUI::class, $languageSelection);
115  $this->assertEquals($languageSelection->getValue(), $this->expectedInitialValue);
116 
117  return $criterion;
118  }
119 
127  ) : void {
128  $httpCriterionSelectionBodyParameter = 'criterion';
129  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_lng';
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 () {
141  }));
142 
143  $value = $gui->getConfigByForm($form);
144 
145  $this->assertInstanceOf(ilTermsOfServiceCriterionConfig::class, $value);
146  $this->assertEquals($this->expectedAfterFormSubmitValue, $value['lng']);
147  $this->assertEquals($this->getCriterionConfig(['lng' => $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 languageProvider() : array
169  {
170  return [
173  'Invalid Languages' => ['invalid_lng', ''],
174  ];
175  }
176 
183  public function testValuePresentationMatchesExpectation(string $lng, string $translation) : void
184  {
185  $language = $this->getLanguageMock();
186 
187  $language
188  ->expects($this->any())
189  ->method('txt')
190  ->with('meta_l_' . $lng, '')
191  ->willReturn($translation);
192 
193  $criterion = new ilTermsOfServiceUserHasLanguageCriterion();
194  $gui = $criterion->ui($language);
195 
197  $actual = $gui->getValuePresentation(
198  $this->getCriterionConfig(['lng' => $lng]),
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(['lng' => 'en'])],
216  'Array' => [$criterion, $this->getCriterionConfig(['lng' => []])],
217  'Object' => [$criterion, $this->getCriterionConfig(['lng' => new stdClass()])],
218  'Double' => [$criterion, $this->getCriterionConfig(['lng' => 1.0])],
219  'Integer' => [$criterion, $this->getCriterionConfig(['lng' => 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(['lng' => 'de'])],
237  'German Language Uppercase' => [$criterion, $this->getCriterionConfig(['lng' => 'DE'])],
238  ];
239  }
240 
250  ) : void {
251  $user = $this->getUserMock();
252 
253  $user
254  ->expects($this->any())
255  ->method('getLanguage')
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('getLanguage')
276  ->willReturn('de');
277 
278  $this->assertTrue($criterion->evaluate($user, $config));
279  }
280 }
Interface ilTermsOfServiceCriterionTypeGUI.
This class represents a property form user interface.
testEvaluationSucceedsIfUserLanguageDoesMatchDefinedLanguage(ilTermsOfServiceUserHasLanguageCriterion $criterion, ilTermsOfServiceCriterionConfig $config)
Class ilTermsOfServiceCriterionBaseTest.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
Class ilTermsOfServiceCriterionConfig.
testFormUserInterfaceElementsAreProperlyBuilt(ilTermsOfServiceUserHasLanguageCriterion $criterion)
testValuesFromFormUserInterfaceElementsCanBeRetrieved(ilTermsOfServiceUserHasLanguageCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt
testEvaluationFailsIfUserLanguageDoesNotMatchDefinedLanguage(ilTermsOfServiceUserHasLanguageCriterion $criterion, ilTermsOfServiceCriterionConfig $config)
ui(ilLanguage $lng)
ilTermsOfServiceCriterionTypeGUI
evaluate(ilObjUser $user, ilTermsOfServiceCriterionConfig $config)
bool
appendOption(ilRadioGroupInputGUI $option, ilTermsOfServiceCriterionConfig $config)
buildForm(ilTermsOfServiceCriterionTypeGUI $gui, string $httpCriterionSelectionBodyParameter)
getTypeIdent()
Returns a unique id of the criterion type.string
Class ilTermsOfServiceUserHasLanguageCriterionTest.
testTypeIdentPresentationIsANonEmptyString(ilTermsOfServiceUserHasLanguageCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt