19declare(strict_types=1);
22use PHPUnit\Framework\MockObject\MockBuilder;
23use PHPUnit\Framework\MockObject\MockObject;
26use PHPUnit\Framework\Attributes\DataProvider;
36 return new class ($org_unit_user_service, $il_mail_environment_helper, $mail_user_helper, $language_helper) extends
38 public function
getId(): string
48 public function getDescription(): string
53 public function getSpecificPlaceholders(): array
58 public function resolveSpecificPlaceholder(
59 string $placeholder_id,
60 array $context_parameters,
76 for ($i = 1; $i <= $amount; $i++) {
77 $user = $mock_builder()
78 ->disableOriginalConstructor()
79 ->onlyMethods([
'getUserId',])
81 $user->expects($this->atLeastOnce())->method(
'getUserId')->willReturn($i);
98 [
'gender' =>
'm',
'num_superiors' => 2,],
99 [
'gender' =>
'n',
'num_superiors' => 1,],
100 [
'gender' =>
'f',
'num_superiors' => 0,],
101 [
'gender' =>
'',
'num_superiors' => 3,],
107 $user_callable =
function (Closure $mock_builder) use ($definition):
ilObjUser&MockObject {
108 $user = $mock_builder()
109 ->disableOriginalConstructor()
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);
136 $ou_user_callable =
function (Closure $mock_builder) use ($definition): array {
137 $ou_user = $mock_builder()
138 ->disableOriginalConstructor()
139 ->onlyMethods([
'getSuperiors',])
142 $superiors = $this->generateOrgUnitUsers($mock_builder, $definition[
'num_superiors']);
143 $ou_user->expects($this->atLeastOnce())->method(
'getSuperiors')->willReturn($superiors);
145 return [$ou_user, $superiors];
149 'User with gender "%s" and %s superiors',
150 $definition[
'gender'],
151 $definition[
'num_superiors']
152 )] = [$user_callable, $ou_user_callable];
162 #[DataProvider('userProvider')]
163 public function testGlobalPlaceholdersCanBeResolvedWithCorrespondingValues(
164 callable $user_callable,
165 callable $ou_user_callable
167 $mock_builder_user_callable = fn(): MockBuilder => $this->getMockBuilder(
ilObjUser::class);
168 $mock_builder_ou_user_callable = fn(): MockBuilder => $this->getMockBuilder(
ilOrgUnitUser::class);
170 $user_callable = Closure::bind($user_callable, $this, self::class);
171 $ou_user_callable = Closure::bind($ou_user_callable, $this, self::class);
173 $user = $user_callable($mock_builder_user_callable);
174 [$ou_user, $ou_superiors] = $ou_user_callable($mock_builder_ou_user_callable);
176 $ou_service = $this->getMockBuilder(OrgUnitUserService::class)
177 ->disableOriginalConstructor()
178 ->onlyMethods([
'getUsers',])
181 $lng = $this->getMockBuilder(ilLanguage::class)
182 ->disableOriginalConstructor()
183 ->onlyMethods([
'txt',
'loadLanguageModule',])
186 $env_helper = $this->getMockBuilder(ilMailEnvironmentHelper::class)
187 ->disableOriginalConstructor()
188 ->onlyMethods([
'getClientId',
'getHttpPath',])
191 $lng_helper = $this->getMockBuilder(ilMailLanguageHelper::class)
192 ->disableOriginalConstructor()
193 ->onlyMethods([
'getLanguageByIsoCode',
'getCurrentLanguage',])
196 $user_helper = $this->getMockBuilder(ilMailUserHelper::class)
197 ->disableOriginalConstructor()
198 ->onlyMethods([
'getUsernameMapForIds',])
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);
208 $expected_ids_constraint = [];
209 if ($ou_superiors !== []) {
210 $expected_ids_constraint = self::logicalAnd(
212 static function (
ilOrgUnitUser $user): \PHPUnit\Framework\Constraint\TraversableContainsEqual {
213 return self::containsEqual($user->
getUserId());
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));
224 $user_helper->expects($this->atLeastOnce())->method(
'getUsernameMapForIds')
225 ->with($expected_ids_constraint)
226 ->willReturn($first_and_last_names);
228 $context = $this->getAnonymousTemplateContext(
235 $mail_salutation_expected = $user->getGender() ===
''
236 ?
'mail_salutation_n'
237 :
'mail_salutation_' . $user->getGender();
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###',
249 $engine =
new class ($this, $expected_values) implements \
ILIAS\
Mail\TemplateEngine\TemplateEngineInterface {
251 private readonly \PHPUnit\Framework\TestCase $test_case,
253 private readonly array $expected_values
257 public function render(
string $template, array|
object $context): string
259 $this->test_case->assertInstanceOf(MailTemplateContextAdapter::class, $context);
260 if (!str_contains($template,
'{{')) {
264 foreach ($this->expected_values as $placeholder => $expected_value) {
265 $this->test_case->assertSame(
267 $context->{$placeholder},
268 sprintf(
'Context value for placeholder "%s" does not match.', $placeholder)
278 $message = implode(
'', array_map(
279 static fn(
string $key):
string =>
'{{' . $key .
'}}',
280 array_keys($expected_values)
282 $placeholder_resolver->resolve($context, $message, $user);
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)
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.