ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
RemoveLocalLanguageChangesAmbiguityTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27
29{
30 use RealFieldsUiFactory;
32
34 {
35 $lng_objects = static fn(): array => [
36 ['obj_id' => 1, 'title' => 'de'],
37 ['obj_id' => 2, 'title' => 'de'],
38 ];
39 $factory_calls = [];
40 $obj_language_factory = static function (int $id) use (&$factory_calls): never {
41 $factory_calls[] = $id;
42 throw new \LogicException('must never be called for an ambiguous title');
43 };
44
45 $activity = $this->createActivity($lng_objects, $obj_language_factory);
46
47 $this->expectException(\RuntimeException::class);
48 $this->expectExceptionMessageMatches('/"de"/');
49
50 try {
51 $activity->perform(['language_keys' => 'de']);
52 } finally {
53 $this->assertSame([], $factory_calls);
54 }
55 }
56
62 {
63 $ambiguous_title = 'de';
64 $lng_objects = static fn(): array => [
65 ['obj_id' => 1, 'title' => $ambiguous_title],
66 ['obj_id' => 2, 'title' => $ambiguous_title],
67 ];
68 $obj_language_factory = static function (int $id): never {
69 throw new \LogicException('must never be called for an ambiguous title');
70 };
71
72 $activity = $this->createActivity($lng_objects, $obj_language_factory);
73
74 try {
75 $activity->perform(['language_keys' => $ambiguous_title]);
76 $this->fail('Expected an AmbiguousLanguageTitleException to be thrown.');
78 $this->assertStringContainsString($ambiguous_title, $e->getMessage());
79 $this->assertStringNotContainsString('&lt;script&gt;', $e->getMessage());
80 $this->assertStringNotContainsString('&quot;', $e->getMessage());
81 }
82 }
83
88 {
89 $rbac = $this->createStub(\ilRbacSystem::class);
90 $rbac->method('checkAccessOfUser')->willReturn(true);
91
92 $lng_objects = static fn(): array => [
93 ['obj_id' => 1, 'title' => 'de'],
94 ['obj_id' => 2, 'title' => 'de'],
95 ];
96 $factory_calls = [];
97 $obj_language_factory = static function (int $id) use (&$factory_calls): never {
98 $factory_calls[] = $id;
99 throw new \LogicException('must never be called for an ambiguous title');
100 };
101
102 $result = $this->createActivity($lng_objects, $obj_language_factory, $rbac)
103 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['language_keys' => 'de']);
104
105 $this->assertTrue($result->isError());
106 $this->assertInstanceOf(\RuntimeException::class, $result->error());
107 $this->assertSame([], $factory_calls);
108 }
109
114 {
115 $lng_objects = static fn(): array => [
116 ['obj_id' => 1, 'title' => 'de'],
117 ['obj_id' => 2, 'title' => 'de'],
118 ['obj_id' => 3, 'title' => 'fr'],
119 ];
120 $fr = new FakeRemoveLocalLanguageChangesObject(is_installed: true);
121 $obj_language_factory = static function (int $id) use ($fr): FakeRemoveLocalLanguageChangesObject {
122 if ($id === 3) {
123 return $fr;
124 }
125
126 throw new \LogicException('must never be called for the ambiguous "de" title');
127 };
128
129 $activity = $this->createActivity($lng_objects, $obj_language_factory);
130
131 $result = $activity->perform(['language_keys' => 'fr']);
132
133 $this->assertSame(['fr'], $result['removed_local_changes_language_keys']);
134 $this->assertSame(1, $fr->removeLocalChangesCallCount());
135 }
136
141 {
142 $de = new FakeRemoveLocalLanguageChangesObject(is_installed: true);
143
144 $lng_objects = static fn(): array => [
145 ['obj_id' => 1, 'title' => 'de'],
146 ['obj_id' => 2, 'title' => 'fr'],
147 ['obj_id' => 3, 'title' => 'fr'],
148 ];
149 $factory_calls = [];
150 $obj_language_factory = static function (int $id) use (&$factory_calls, $de): FakeRemoveLocalLanguageChangesObject {
151 $factory_calls[] = $id;
152 if ($id === 1) {
153 return $de;
154 }
155
156 throw new \LogicException('must never be called for the ambiguous "fr" title');
157 };
158
159 $activity = $this->createActivity($lng_objects, $obj_language_factory);
160
161 try {
162 // 'de' is listed before the ambiguous 'fr' on purpose
163 $activity->perform(['language_keys' => 'de,fr']);
164 $this->fail('Expected an AmbiguousLanguageTitleException to be thrown.');
166 $this->assertStringContainsString('"fr"', $e->getMessage());
167 // 'de' was never resolved, let alone changed
168 $this->assertSame([], $factory_calls);
169 $this->assertSame(0, $de->removeLocalChangesCallCount());
170 }
171 }
172
177 {
178 $rbac = $this->createStub(\ilRbacSystem::class);
179 $rbac->method('checkAccessOfUser')->willReturn(true);
180
181 $de = new FakeRemoveLocalLanguageChangesObject(is_installed: true);
182
183 $lng_objects = static fn(): array => [
184 ['obj_id' => 1, 'title' => 'de'],
185 ['obj_id' => 2, 'title' => 'fr'],
186 ['obj_id' => 3, 'title' => 'fr'],
187 ];
188 $obj_language_factory = static function (int $id) use ($de): FakeRemoveLocalLanguageChangesObject {
189 if ($id === 1) {
190 return $de;
191 }
192
193 throw new \LogicException('must never be called for the ambiguous "fr" title');
194 };
195
196 $result = $this->createActivity($lng_objects, $obj_language_factory, $rbac)
197 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['language_keys' => ['de', 'fr']]);
198
199 $this->assertTrue($result->isError());
200 $this->assertInstanceOf(AmbiguousLanguageTitleException::class, $result->error());
201 $this->assertSame(0, $de->removeLocalChangesCallCount());
202 }
203}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Stand-in for \ilObjLanguage, which needs the global $DIC; injected via RemoveLocalLanguageChanges's $...
testAmbiguousTitleViaMaybePerformAsReturnsResultErrorAndNeverCallsTheObjectFactory()
Via maybePerformAs() the exception must surface as a Result\Error, and the object factory stays untou...
testAnUnambiguousKeyBeforeAnAmbiguousOneNeverHasLocalChangesRemovedOnceTheWholeRequestIsRejected()
The ambiguity check runs for all requested keys before any removeLocalChanges() call.
testMaybePerformAsNeverRemovesLocalChangesForAnUnambiguousKeyWhenAnotherRequestedKeyIsAmbiguous()
Same, end-to-end through maybePerformAs().
testAmbiguousTitleExceptionMessageContainsTheRawUnescapedTitle()
The exception message embeds the ambiguous title unescaped: escaping is RendersActivityErrors::activi...
testUnambiguousLanguageKeyStillWorksAlongsideAnAmbiguousOneInTheSameLngObjectsList()
An ambiguous title must not affect a unique language key in the same request.
Class ilLanguageBaseTest.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createActivity(\Closure $lng_objects, \Closure $obj_language_factory, ?\ilRbacSystem $rbac=null, int $language_folder_ref_id=0, ?RepositoryProvider $repository=null,)