ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilMailTemplateContextTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
28 {
29  public function getAnonymousTemplateContext(
30  OrgUnitUserService $org_unit_user_service,
31  ilMailEnvironmentHelper $il_mail_environment_helper,
32  ilMailUserHelper $mail_user_helper,
33  ilMailLanguageHelper $language_helper
35  return new class ($org_unit_user_service, $il_mail_environment_helper, $mail_user_helper, $language_helper) extends
37  public function getId(): string
38  {
39  return 'phpunuit';
40  }
41 
42  public function getTitle(): string
43  {
44  return 'phpunuit';
45  }
46 
47  public function getDescription(): string
48  {
49  return 'phpunuit';
50  }
51 
52  public function getSpecificPlaceholders(): array
53  {
54  return [];
55  }
56 
57  public function resolveSpecificPlaceholder(
58  string $placeholder_id,
59  array $context_parameters,
60  ?ilObjUser $recipient = null
61  ): string {
62  return '';
63  }
64  };
65  }
66 
71  private function generateOrgUnitUsers(Closure $mock_builder, int $amount): array
72  {
73  $users = [];
74 
75  for ($i = 1; $i <= $amount; $i++) {
76  $user = $mock_builder()
77  ->disableOriginalConstructor()
78  ->onlyMethods(['getUserId',])
79  ->getMock();
80  $user->expects($this->atLeastOnce())->method('getUserId')->willReturn($i);
81 
82  $users[$i] = $user;
83  }
84 
85  return $users;
86  }
87 
91  public static function userProvider(): array
92  {
93  $test_users = [];
94 
95  foreach (
96  [
97  ['gender' => 'm', 'num_superiors' => 2,],
98  ['gender' => 'n', 'num_superiors' => 1,],
99  ['gender' => 'f', 'num_superiors' => 0,],
100  ['gender' => '', 'num_superiors' => 3,],
101  ] as $definition
102  ) {
106  $user_callable = function (Closure $mock_builder) use ($definition): ilObjUser&MockObject {
107  $user = $mock_builder()
108  ->disableOriginalConstructor()
109  ->onlyMethods([
110  'getLanguage',
111  'getUTitle',
112  'getLogin',
113  'getLastname',
114  'getFirstname',
115  'getGender',
116  'getId',
117  ])
118  ->getMock();
119 
120  $user->expects($this->atLeastOnce())->method('getLanguage')->willReturn('de');
121  $user->expects($this->atLeastOnce())->method('getUTitle')->willReturn('###Dr. Ing###');
122  $user->expects($this->atLeastOnce())->method('getLogin')->willReturn('###phpunit###');
123  $user->expects($this->atLeastOnce())->method('getLastname')->willReturn('###Unit###');
124  $user->expects($this->atLeastOnce())->method('getFirstname')->willReturn('###PHP###');
125  $user->expects($this->atLeastOnce())->method('getGender')->willReturn($definition['gender']);
126  $user->expects($this->atLeastOnce())->method('getId')->willReturn(4711);
127 
128  return $user;
129  };
130 
135  $ou_user_callable = function (Closure $mock_builder) use ($definition): array {
136  $ou_user = $mock_builder()
137  ->disableOriginalConstructor()
138  ->onlyMethods(['getSuperiors',])
139  ->getMock();
140 
141  $superiors = $this->generateOrgUnitUsers($mock_builder, $definition['num_superiors']);
142  $ou_user->expects($this->atLeastOnce())->method('getSuperiors')->willReturn($superiors);
143 
144  return [$ou_user, $superiors];
145  };
146 
147  $test_users[sprintf(
148  'User with gender "%s" and %s superiors',
149  $definition['gender'],
150  $definition['num_superiors']
151  )] = [$user_callable, $ou_user_callable];
152  }
153 
154  return $test_users;
155  }
156 
161  #[DataProvider('userProvider')]
163  callable $user_callable,
164  callable $ou_user_callable
165  ): void {
166  $mock_builder_user_callable = fn(): MockBuilder => $this->getMockBuilder(ilObjUser::class);
167  $mock_builder_ou_user_callable = fn(): MockBuilder => $this->getMockBuilder(ilOrgUnitUser::class);
168 
169  $user_callable = Closure::bind($user_callable, $this, self::class);
170  $ou_user_callable = Closure::bind($ou_user_callable, $this, self::class);
171 
172  $user = $user_callable($mock_builder_user_callable);
173  [$ou_user, $ou_superiors] = $ou_user_callable($mock_builder_ou_user_callable);
174 
175  $ou_service = $this->getMockBuilder(OrgUnitUserService::class)
176  ->disableOriginalConstructor()
177  ->onlyMethods(['getUsers',])
178  ->getMock();
179 
180  $lng = $this->getMockBuilder(ilLanguage::class)
181  ->disableOriginalConstructor()
182  ->onlyMethods(['txt', 'loadLanguageModule',])
183  ->getMock();
184 
185  $env_helper = $this->getMockBuilder(ilMailEnvironmentHelper::class)
186  ->disableOriginalConstructor()
187  ->onlyMethods(['getClientId', 'getHttpPath',])
188  ->getMock();
189 
190  $lng_helper = $this->getMockBuilder(ilMailLanguageHelper::class)
191  ->disableOriginalConstructor()
192  ->onlyMethods(['getLanguageByIsoCode', 'getCurrentLanguage',])
193  ->getMock();
194 
195  $user_helper = $this->getMockBuilder(ilMailUserHelper::class)
196  ->disableOriginalConstructor()
197  ->onlyMethods(['getUsernameMapForIds',])
198  ->getMock();
199 
200  $ou_service->expects($this->atLeastOnce())->method('getUsers')->willReturn([$ou_user,]);
201  $lng->expects($this->atLeastOnce())->method('txt')->willReturnArgument(0);
202  $env_helper->expects($this->atLeastOnce())->method('getClientId')->willReturn('###phpunit_client###');
203  $env_helper->expects($this->atLeastOnce())->method('getHttpPath')->willReturn('###http_ilias###');
204  $lng_helper->expects($this->atLeastOnce())->method('getLanguageByIsoCode')->willReturn($lng);
205  $lng_helper->expects($this->atLeastOnce())->method('getCurrentLanguage')->willReturn($lng);
206 
207  $expected_ids_constraint = [];
208  if ($ou_superiors !== []) {
209  $expected_ids_constraint = self::logicalAnd(
210  ...array_map(
211  static function (ilOrgUnitUser $user): \PHPUnit\Framework\Constraint\TraversableContainsEqual {
212  return self::containsEqual($user->getUserId());
213  },
214  $ou_superiors
215  )
216  );
217  }
218 
219  $first_and_last_names = array_map(static function (ilOrgUnitUser $user, int $key): string {
220  return "PhpSup$key UnitSup$key";
221  }, $ou_superiors, array_keys($ou_superiors));
222 
223  $user_helper->expects($this->atLeastOnce())->method('getUsernameMapForIds')
224  ->with($expected_ids_constraint)
225  ->willReturn($first_and_last_names);
226 
228  $ou_service,
229  $env_helper,
230  $user_helper,
231  $lng_helper
232  );
233 
234  $mustache = new Mustache_Engine();
235  $placeholder_resolver = new ilMailTemplatePlaceholderResolver($mustache);
236 
237  $message = implode('', [
238  '{{MAIL_SALUTATION}}',
239  '{{FIRST_NAME}}',
240  '{{LAST_NAME}}',
241  '{{LOGIN}}',
242  '{{TITLE}}',
243  '{{FIRSTNAME_LASTNAME_SUPERIOR}}',
244  '{{ILIAS_URL}}',
245  '{{INSTALLATION_NAME}}',
246  ]);
247  $replace_message = $placeholder_resolver->resolve($context, $message, $user);
248 
249  $this->assertStringContainsString('###Dr. Ing###', $replace_message);
250  $this->assertStringContainsString('###phpunit###', $replace_message);
251  $this->assertStringContainsString('###Unit###', $replace_message);
252  $this->assertStringContainsString('###PHP###', $replace_message);
253  $this->assertStringContainsString('###phpunit_client###', $replace_message);
254  $this->assertStringContainsString('###http_ilias###', $replace_message);
255  $this->assertStringContainsString('mail_salutation_' . $user->getGender(), $replace_message);
256 
257  foreach ($first_and_last_names as $first_and_lastname) {
258  $this->assertStringContainsString($first_and_lastname, $replace_message);
259  }
260  }
261 }
$context
Definition: webdav.php:31
generateOrgUnitUsers(Closure $mock_builder, int $amount)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getAnonymousTemplateContext(OrgUnitUserService $org_unit_user_service, ilMailEnvironmentHelper $il_mail_environment_helper, ilMailUserHelper $mail_user_helper, ilMailLanguageHelper $language_helper)
testGlobalPlaceholdersCanBeResolvedWithCorrespondingValues(callable $user_callable, callable $ou_user_callable)
global $lng
Definition: privfeed.php:31
$message
Definition: xapiexit.php:31