ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilUtilTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
27 class ilUtilTest extends TestCase
28 {
29  protected function setUp(): void
30  {
31  parent::setUp();
32  if (!defined("ILIAS_HTTP_PATH")) {
33  define("ILIAS_HTTP_PATH", "http://localhost");
34  }
35  }
36 
40  public function testMakeClickableWithoutGotoLinks(): void
41  {
42  $input = 'Small things make base men proud.';
43  $expected = 'I do desire we may be better strangers.';
44 
45  $GLOBALS['DIC'] = $this->mockClickableCall($input, $expected);
46 
47  $this->assertSame($expected, ilUtil::makeClickable($input));
48 
49  unset($GLOBALS['DIC']);
50  }
51 
52  #[\PHPUnit\Framework\Attributes\DataProvider('provideGotoLinkData')]
53  public function testMakeClickableWithGotoLinksAndInvalidRefId(string $expected, string $input, array $ref_to_obj, array $obj_to_title): void
54  {
55  $wrap_array = static fn(array $array): array => (
56  array_map(static fn(int $x): array => [$x], $array)
57  );
58 
59  $container = $this->mockClickableCall($input, $expected);
60 
61  $cache = $this->getMockBuilder(ilObjectDataCache::class)->disableOriginalConstructor()->getMock();
62 
63  $consecutive_id = [];
64  foreach($ref_to_obj as $k => $v) {
65  $consecutive_id[] = [$k, $v];
66  }
67  $cache->expects(self::exactly(count($ref_to_obj)))
68  ->method('lookupObjId')
69  ->willReturnCallback(
70  function ($id) use (&$consecutive_id) {
71  list($k, $v) = array_shift($consecutive_id);
72  $this->assertEquals($k, $id);
73  return $v;
74  }
75  );
76 
77  $consecutive_title = [];
78  foreach($obj_to_title as $k => $v) {
79  $consecutive_title[] = [$k, $v];
80  }
81  $cache->expects(self::exactly(count($obj_to_title)))
82  ->method('lookupTitle')
83  ->willReturnCallback(
84  function ($id) use (&$consecutive_title) {
85  list($k, $v) = array_shift($consecutive_title);
86  $this->assertEquals($k, $id);
87  return $v;
88  }
89  );
90 
91  $container->expects(self::exactly(count($ref_to_obj)))->method('offsetGet')->with('ilObjDataCache')->willReturn($cache);
92 
93  $GLOBALS['DIC'] = $container;
94 
95  $this->assertSame($expected, ilUtil::makeClickable($input, true));
96 
97  unset($GLOBALS['DIC']);
98  }
99 
100  public static function provideGotoLinkData(): array
101  {
102  // Please note that these test cases represent the current state, not necessarily the correct state.
103  // For example all anchor attributes are REMOVED and the target is ALWAYS set to target="_self".
104 
105  $tests = [
106  'Test with empty string.' => ['', '', [], []],
107  'Test with correct link and target = _self is added.' => [
108  'A link to <a href="%scrs_345%s" target="_self">a course</a>.',
109  'A link to <a href="%scrs_345%s">somewhere</a>.',
110  [345 => 5],
111  [5 => 'a course'],
112  ],
113  'Test with multiple correct links.' => [
114  'A link to <a href="%scrs_345%s" target="_self">a course</a> and to <a href="%scrs_87%s" target="_self">another course</a>.',
115  'A link to <a href="%scrs_345%s">somewhere</a> and to <a href="%scrs_87%s">somewhere else</a>.',
116  [345 => 5, 87 => 45],
117  [5 => 'a course', 45 => 'another course'],
118  ],
119  'Test links with invalid ref id.' => [
120  'A link to <a href="%scrs_345%s">somewhere</a>.',
121  'A link to <a href="%scrs_345%s">somewhere</a>.',
122  [345 => 0],
123  [],
124  ],
125  'The target attribute is REPLACED with _self.' => [
126  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
127  'A link to <a target="bogus target" href="%scrs_345%s">somewhere</a>.',
128  [345 => 8],
129  [8 => 'some course'],
130  ],
131  'The attributes position does not matter, it is always replaced with target="_self".' => [
132  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
133  'A link to <a href="%scrs_345%s" target="bogus target">somewhere</a>.',
134  [345 => 8],
135  [8 => 'some course'],
136  ],
137  'All attributes are removed from the link.' => [
138  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
139  'A link to <a href="%scrs_345%s" class="very-important-css-class">somewhere</a>.',
140  [345 => 8],
141  [8 => 'some course'],
142  ],
143  ];
144 
145  $linkFormats = [
146  'With goto.php: ' => ['http://localhost/goto.php?target=', ''],
147  'With goto_*.html: ' => ['http://localhost/goto_', '.html'],
148  ];
149 
150  $repeatForFormat = function (string $format, array $values): array {
151  return array_merge(
152  ...array_fill(0, (count(explode('%s', $format)) - 1) / 2, $values)
153  );
154  };
155 
156  $allTests = [];
157  foreach ($linkFormats as $name => $args) {
158  foreach ($tests as $key => $array) {
159  $allTests[$name . $key] = array_merge([
160  sprintf($array[0], ...$repeatForFormat($array[0], $args)),
161  sprintf($array[1], ...$repeatForFormat($array[1], $args)),
162  ], array_slice($array, 2));
163  }
164  }
165 
166  return $allTests;
167  }
168 
169  private function mockClickableCall(string $input, string $transformed): Container
170  {
171  $transformation = $this->getMockBuilder(Transformation::class)->getMock();
172  $transformation->expects(self::once())->method('transform')->with($input)->willReturn($transformed);
173 
174  $string_group = $this->getMockBuilder(StringGroup::class)->disableOriginalConstructor()->getMock();
175  $string_group->expects(self::once())->method('makeClickable')->willReturn($transformation);
176 
177  $refinery = $this->getMockBuilder(Refinery::class)->disableOriginalConstructor()->getMock();
178  $refinery->expects(self::once())->method('string')->willReturn($string_group);
179 
180  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
181  $container->expects(self::once())->method('refinery')->willReturn($refinery);
182 
183  return $container;
184  }
185 }
static provideGotoLinkData()
Definition: ilUtilTest.php:100
testMakeClickableWithoutGotoLinks()
ilUtil::makeClickable must call the refinery transformation make clickable.
Definition: ilUtilTest.php:40
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
$container
Definition: wac.php:36
$GLOBALS["DIC"]
Definition: wac.php:53
testMakeClickableWithGotoLinksAndInvalidRefId(string $expected, string $input, array $ref_to_obj, array $obj_to_title)
Definition: ilUtilTest.php:53
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
mockClickableCall(string $input, string $transformed)
Definition: ilUtilTest.php:169
static makeClickable(string $a_text, bool $detectGotoLinks=false)