ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
ilMailTemplateContextTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\MockObject\MockBuilder;
23use PHPUnit\Framework\MockObject\MockObject;
26use PHPUnit\Framework\Attributes\DataProvider;
27
29{
31 OrgUnitUserService $org_unit_user_service,
32 ilMailEnvironmentHelper $il_mail_environment_helper,
33 ilMailUserHelper $mail_user_helper,
34 ilMailLanguageHelper $language_helper
36 return new class ($org_unit_user_service, $il_mail_environment_helper, $mail_user_helper, $language_helper) extends
38 public function getId(): string
39 {
40 return 'phpunuit';
41 }
42
43 public function getTitle(): string
44 {
45 return 'phpunuit';
46 }
47
48 public function getDescription(): string
49 {
50 return 'phpunuit';
51 }
52
53 public function getSpecificPlaceholders(): array
54 {
55 return [];
56 }
57
58 public function resolveSpecificPlaceholder(
59 string $placeholder_id,
60 array $context_parameters,
61 ?ilObjUser $recipient = null
62 ): string {
63 return '';
64 }
65 };
66 }
67
72 private function generateOrgUnitUsers(Closure $mock_builder, int $amount): array
73 {
74 $users = [];
75
76 for ($i = 1; $i <= $amount; $i++) {
77 $user = $mock_builder()
78 ->disableOriginalConstructor()
79 ->onlyMethods(['getUserId',])
80 ->getMock();
81 $user->expects($this->atLeastOnce())->method('getUserId')->willReturn($i);
82
83 $users[$i] = $user;
84 }
85
86 return $users;
87 }
88
92 public static function userProvider(): array
93 {
94 $test_users = [];
95
96 foreach (
97 [
98 ['gender' => 'm', 'num_superiors' => 2,],
99 ['gender' => 'n', 'num_superiors' => 1,],
100 ['gender' => 'f', 'num_superiors' => 0,],
101 ['gender' => '', 'num_superiors' => 3,],
102 ] as $definition
103 ) {
107 $user_callable = function (Closure $mock_builder) use ($definition): ilObjUser&MockObject {
108 $user = $mock_builder()
109 ->disableOriginalConstructor()
110 ->onlyMethods([
111 'getLanguage',
112 'getUTitle',
113 'getLogin',
114 'getLastname',
115 'getFirstname',
116 'getGender',
117 'getId',
118 ])
119 ->getMock();
120
121 $user->expects($this->atLeastOnce())->method('getLanguage')->willReturn('de');
122 $user->expects($this->atLeastOnce())->method('getUTitle')->willReturn('###Dr. Ing###');
123 $user->expects($this->atLeastOnce())->method('getLogin')->willReturn('###phpunit###');
124 $user->expects($this->atLeastOnce())->method('getLastname')->willReturn('###Unit###');
125 $user->expects($this->atLeastOnce())->method('getFirstname')->willReturn('###PHP###');
126 $user->expects($this->atLeastOnce())->method('getGender')->willReturn($definition['gender']);
127 $user->expects($this->atLeastOnce())->method('getId')->willReturn(4711);
128
129 return $user;
130 };
131
136 $ou_user_callable = function (Closure $mock_builder) use ($definition): array {
137 $ou_user = $mock_builder()
138 ->disableOriginalConstructor()
139 ->onlyMethods(['getSuperiors',])
140 ->getMock();
141
142 $superiors = $this->generateOrgUnitUsers($mock_builder, $definition['num_superiors']);
143 $ou_user->expects($this->atLeastOnce())->method('getSuperiors')->willReturn($superiors);
144
145 return [$ou_user, $superiors];
146 };
147
148 $test_users[sprintf(
149 'User with gender "%s" and %s superiors',
150 $definition['gender'],
151 $definition['num_superiors']
152 )] = [$user_callable, $ou_user_callable];
153 }
154
155 return $test_users;
156 }
157
162 #[DataProvider('userProvider')]
163 public function testGlobalPlaceholdersCanBeResolvedWithCorrespondingValues(
164 callable $user_callable,
165 callable $ou_user_callable
166 ): void {
167 $mock_builder_user_callable = fn(): MockBuilder => $this->getMockBuilder(ilObjUser::class);
168 $mock_builder_ou_user_callable = fn(): MockBuilder => $this->getMockBuilder(ilOrgUnitUser::class);
169
170 $user_callable = Closure::bind($user_callable, $this, self::class);
171 $ou_user_callable = Closure::bind($ou_user_callable, $this, self::class);
172
173 $user = $user_callable($mock_builder_user_callable);
174 [$ou_user, $ou_superiors] = $ou_user_callable($mock_builder_ou_user_callable);
175
176 $ou_service = $this->getMockBuilder(OrgUnitUserService::class)
177 ->disableOriginalConstructor()
178 ->onlyMethods(['getUsers',])
179 ->getMock();
180
181 $lng = $this->getMockBuilder(ilLanguage::class)
182 ->disableOriginalConstructor()
183 ->onlyMethods(['txt', 'loadLanguageModule',])
184 ->getMock();
185
186 $env_helper = $this->getMockBuilder(ilMailEnvironmentHelper::class)
187 ->disableOriginalConstructor()
188 ->onlyMethods(['getClientId', 'getHttpPath',])
189 ->getMock();
190
191 $lng_helper = $this->getMockBuilder(ilMailLanguageHelper::class)
192 ->disableOriginalConstructor()
193 ->onlyMethods(['getLanguageByIsoCode', 'getCurrentLanguage',])
194 ->getMock();
195
196 $user_helper = $this->getMockBuilder(ilMailUserHelper::class)
197 ->disableOriginalConstructor()
198 ->onlyMethods(['getUsernameMapForIds',])
199 ->getMock();
200
201 $ou_service->expects($this->atLeastOnce())->method('getUsers')->willReturn([$ou_user,]);
202 $lng->expects($this->atLeastOnce())->method('txt')->willReturnArgument(0);
203 $env_helper->expects($this->atLeastOnce())->method('getClientId')->willReturn('###phpunit_client###');
204 $env_helper->expects($this->atLeastOnce())->method('getHttpPath')->willReturn('###http_ilias###');
205 $lng_helper->expects($this->atLeastOnce())->method('getLanguageByIsoCode')->willReturn($lng);
206 $lng_helper->method('getCurrentLanguage')->willReturn($lng);
207
208 $expected_ids_constraint = [];
209 if ($ou_superiors !== []) {
210 $expected_ids_constraint = self::logicalAnd(
211 ...array_map(
212 static function (ilOrgUnitUser $user): \PHPUnit\Framework\Constraint\TraversableContainsEqual {
213 return self::containsEqual($user->getUserId());
214 },
215 $ou_superiors
216 )
217 );
218 }
219
220 $first_and_last_names = array_map(static function (ilOrgUnitUser $user, int $key): string {
221 return "PhpSup$key UnitSup$key";
222 }, $ou_superiors, array_keys($ou_superiors));
223
224 $user_helper->expects($this->atLeastOnce())->method('getUsernameMapForIds')
225 ->with($expected_ids_constraint)
226 ->willReturn($first_and_last_names);
227
228 $context = $this->getAnonymousTemplateContext(
229 $ou_service,
230 $env_helper,
231 $user_helper,
232 $lng_helper
233 );
234
235 $mail_salutation_expected = $user->getGender() === ''
236 ? 'mail_salutation_n'
237 : 'mail_salutation_' . $user->getGender();
238 $expected_values = [
239 'MAIL_SALUTATION' => $mail_salutation_expected,
240 'FIRST_NAME' => '###PHP###',
241 'LAST_NAME' => '###Unit###',
242 'LOGIN' => '###phpunit###',
243 'TITLE' => '###Dr. Ing###',
244 'FIRSTNAME_LASTNAME_SUPERIOR' => implode(', ', $first_and_last_names),
245 'ILIAS_URL' => '###http_ilias### ',
246 'INSTALLATION_NAME' => '###phpunit_client###',
247 ];
248
249 $engine = new class ($this, $expected_values) implements \ILIAS\Mail\TemplateEngine\TemplateEngineInterface {
250 public function __construct(
251 private readonly \PHPUnit\Framework\TestCase $test_case,
253 private readonly array $expected_values
254 ) {
255 }
256
257 public function render(string $template, array|object $context): string
258 {
259 $this->test_case->assertInstanceOf(MailTemplateContextAdapter::class, $context);
260 if (!str_contains($template, '{{')) {
261 return $template;
262 }
263
264 foreach ($this->expected_values as $placeholder => $expected_value) {
265 $this->test_case->assertSame(
266 $expected_value,
267 $context->{$placeholder},
268 sprintf('Context value for placeholder "%s" does not match.', $placeholder)
269 );
270 }
271
272 return '';
273 }
274 };
275
276 $placeholder_resolver = new ilMailTemplatePlaceholderResolver($engine);
277
278 $message = implode('', array_map(
279 static fn(string $key): string => '{{' . $key . '}}',
280 array_keys($expected_values)
281 ));
282 $placeholder_resolver->resolve($context, $message, $user);
283 }
284}
This class forms an interface between the existing ILIAS mail contexts and the requirements of the te...
generateOrgUnitUsers(Closure $mock_builder, int $amount)
getAnonymousTemplateContext(OrgUnitUserService $org_unit_user_service, ilMailEnvironmentHelper $il_mail_environment_helper, ilMailUserHelper $mail_user_helper, ilMailLanguageHelper $language_helper)
User class.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
RFC 822 Email address list validation Utility.
global $lng
Definition: privfeed.php:31