ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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 
55  public function testMakeClickableWithGotoLinksAndInvalidRefId(string $expected, string $input, array $ref_to_obj, array $obj_to_title): void
56  {
57  $wrap_array = static fn(array $array): array => (
58  array_map(static fn(int $x): array => [$x], $array)
59  );
60 
61  $container = $this->mockClickableCall($input, $expected);
62 
63  $cache = $this->getMockBuilder(ilObjectDataCache::class)->disableOriginalConstructor()->getMock();
64 
65  $consecutive_id = [];
66  foreach($ref_to_obj as $k => $v) {
67  $consecutive_id[] = [$k, $v];
68  }
69  $cache->expects(self::exactly(count($ref_to_obj)))
70  ->method('lookupObjId')
71  ->willReturnCallback(
72  function ($id) use (&$consecutive_id) {
73  list($k, $v) = array_shift($consecutive_id);
74  $this->assertEquals($k, $id);
75  return $v;
76  }
77  );
78 
79  $consecutive_title = [];
80  foreach($obj_to_title as $k => $v) {
81  $consecutive_title[] = [$k, $v];
82  }
83  $cache->expects(self::exactly(count($obj_to_title)))
84  ->method('lookupTitle')
85  ->willReturnCallback(
86  function ($id) use (&$consecutive_title) {
87  list($k, $v) = array_shift($consecutive_title);
88  $this->assertEquals($k, $id);
89  return $v;
90  }
91  );
92 
93  $container->expects(self::exactly(count($ref_to_obj)))->method('offsetGet')->with('ilObjDataCache')->willReturn($cache);
94 
95  $GLOBALS['DIC'] = $container;
96 
97  $this->assertSame($expected, ilUtil::makeClickable($input, true));
98 
99  unset($GLOBALS['DIC']);
100  }
101 
102  public static function provideGotoLinkData(): array
103  {
104  // Please note that these test cases represent the current state, not necessarily the correct state.
105  // For example all anchor attributes are REMOVED and the target is ALWAYS set to target="_self".
106 
107  $tests = [
108  'Test with empty string.' => ['', '', [], []],
109  'Test with correct link and target = _self is added.' => [
110  'A link to <a href="%scrs_345%s" target="_self">a course</a>.',
111  'A link to <a href="%scrs_345%s">somewhere</a>.',
112  [345 => 5],
113  [5 => 'a course'],
114  ],
115  'Test with multiple correct links.' => [
116  '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>.',
117  'A link to <a href="%scrs_345%s">somewhere</a> and to <a href="%scrs_87%s">somewhere else</a>.',
118  [345 => 5, 87 => 45],
119  [5 => 'a course', 45 => 'another course'],
120  ],
121  'Test links with invalid ref id.' => [
122  'A link to <a href="%scrs_345%s">somewhere</a>.',
123  'A link to <a href="%scrs_345%s">somewhere</a>.',
124  [345 => 0],
125  [],
126  ],
127  'The target attribute is REPLACED with _self.' => [
128  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
129  'A link to <a target="bogus target" href="%scrs_345%s">somewhere</a>.',
130  [345 => 8],
131  [8 => 'some course'],
132  ],
133  'The attributes position does not matter, it is always replaced with target="_self".' => [
134  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
135  'A link to <a href="%scrs_345%s" target="bogus target">somewhere</a>.',
136  [345 => 8],
137  [8 => 'some course'],
138  ],
139  'All attributes are removed from the link.' => [
140  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
141  'A link to <a href="%scrs_345%s" class="very-important-css-class">somewhere</a>.',
142  [345 => 8],
143  [8 => 'some course'],
144  ],
145  ];
146 
147  $linkFormats = [
148  'With goto.php: ' => ['http://localhost/goto.php?target=', ''],
149  'With goto_*.html: ' => ['http://localhost/goto_', '.html'],
150  ];
151 
152  $repeatForFormat = function (string $format, array $values): array {
153  return array_merge(
154  ...array_fill(0, (count(explode('%s', $format)) - 1) / 2, $values)
155  );
156  };
157 
158  $allTests = [];
159  foreach ($linkFormats as $name => $args) {
160  foreach ($tests as $key => $array) {
161  $allTests[$name . $key] = array_merge([
162  sprintf($array[0], ...$repeatForFormat($array[0], $args)),
163  sprintf($array[1], ...$repeatForFormat($array[1], $args)),
164  ], array_slice($array, 2));
165  }
166  }
167 
168  return $allTests;
169  }
170 
171  private function mockClickableCall(string $input, string $transformed): Container
172  {
173  $transformation = $this->getMockBuilder(Transformation::class)->getMock();
174  $transformation->expects(self::once())->method('transform')->with($input)->willReturn($transformed);
175 
176  $string_group = $this->getMockBuilder(StringGroup::class)->disableOriginalConstructor()->getMock();
177  $string_group->expects(self::once())->method('makeClickable')->willReturn($transformation);
178 
179  $refinery = $this->getMockBuilder(Refinery::class)->disableOriginalConstructor()->getMock();
180  $refinery->expects(self::once())->method('string')->willReturn($string_group);
181 
182  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
183  $container->expects(self::once())->method('refinery')->willReturn($refinery);
184 
185  return $container;
186  }
187 }
static provideGotoLinkData()
Definition: ilUtilTest.php:102
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)
provideGotoLinkData
Definition: ilUtilTest.php:55
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
mockClickableCall(string $input, string $transformed)
Definition: ilUtilTest.php:171
static makeClickable(string $a_text, bool $detectGotoLinks=false)