ILIAS  release_8 Revision v8.23
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 {
32  public function testMakeClickableWithoutGotoLinks(): void
33  {
34  $input = 'Small things make base men proud.';
35  $expected = 'I do desire we may be better strangers.';
36 
37  $GLOBALS['DIC'] = $this->mockClickableCall($input, $expected);
38 
39  $this->assertSame($expected, ilUtil::makeClickable($input));
40 
41  unset($GLOBALS['DIC']);
42  }
43 
47  public function testMakeClickableWithGotoLinksAndInvalidRefId(string $expected, string $input, array $ref_to_obj, array $obj_to_title): void
48  {
49  $wrap_array = static fn (array $array): array => (
50  array_map(static fn (int $x): array => [$x], $array)
51  );
52 
53  $container = $this->mockClickableCall($input, $expected);
54 
55  $cache = $this->getMockBuilder(ilObjectDataCache::class)->disableOriginalConstructor()->getMock();
56 
57  $cache->expects(self::exactly(count($ref_to_obj)))
58  ->method('lookupObjId')
59  ->withConsecutive(...$wrap_array(array_keys($ref_to_obj)))
60  ->willReturnOnConsecutiveCalls(...array_values($ref_to_obj));
61 
62  $cache->expects(self::exactly(count($obj_to_title)))
63  ->method('lookupTitle')
64  ->withConsecutive(...$wrap_array(array_keys($obj_to_title)))
65  ->willReturnOnConsecutiveCalls(...array_values($obj_to_title));
66 
67  $container->expects(self::exactly(count($ref_to_obj)))->method('offsetGet')->with('ilObjDataCache')->willReturn($cache);
68 
69  $GLOBALS['DIC'] = $container;
70 
71  $this->assertSame($expected, ilUtil::makeClickable($input, true));
72 
73  unset($GLOBALS['DIC']);
74  }
75 
76  public function provideGotoLinkData(): array
77  {
78  // Please note that these test cases represent the current state, not necessarily the correct state.
79  // For example all anchor attributes are REMOVED and the target is ALWAYS set to target="_self".
80 
81  $tests = [
82  'Test with empty string.' => ['', '', [], []],
83  'Test with correct link and target = _self is added.' => [
84  'A link to <a href="%scrs_345%s" target="_self">a course</a>.',
85  'A link to <a href="%scrs_345%s">somewhere</a>.',
86  [345 => 5],
87  [5 => 'a course'],
88  ],
89  'Test with multiple correct links.' => [
90  '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>.',
91  'A link to <a href="%scrs_345%s">somewhere</a> and to <a href="%scrs_87%s">somewhere else</a>.',
92  [345 => 5, 87 => 45],
93  [5 => 'a course', 45 => 'another course'],
94  ],
95  'Test links with invalid ref id.' => [
96  'A link to <a href="%scrs_345%s">somewhere</a>.',
97  'A link to <a href="%scrs_345%s">somewhere</a>.',
98  [345 => 0],
99  [],
100  ],
101  'The target attribute is REPLACED with _self.' => [
102  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
103  'A link to <a target="bogus target" href="%scrs_345%s">somewhere</a>.',
104  [345 => 8],
105  [8 => 'some course'],
106  ],
107  'The attributes position does not matter, it is always replaced with target="_self".' => [
108  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
109  'A link to <a href="%scrs_345%s" target="bogus target">somewhere</a>.',
110  [345 => 8],
111  [8 => 'some course'],
112  ],
113  'All attributes are removed from the link.' => [
114  'A link to <a href="%scrs_345%s" target="_self">some course</a>.',
115  'A link to <a href="%scrs_345%s" class="very-important-css-class">somewhere</a>.',
116  [345 => 8],
117  [8 => 'some course'],
118  ],
119  ];
120 
121  $linkFormats = [
122  'With goto.php: ' => ['http://localhost/goto.php?target=', ''],
123  'With goto_*.html: ' => ['http://localhost/goto_', '.html'],
124  ];
125 
126  $allTests = [];
127  foreach ($linkFormats as $name => $args) {
128  foreach ($tests as $key => $array) {
129  $allTests[$name . $key] = array_merge([
130  sprintf($array[0], ...$this->repeatForFormat($array[0], $args)),
131  sprintf($array[1], ...$this->repeatForFormat($array[1], $args)),
132  ], array_slice($array, 2));
133  }
134  }
135 
136  return $allTests;
137  }
138 
139  private function mockClickableCall(string $input, string $transformed): Container
140  {
141  $transformation = $this->getMockBuilder(Transformation::class)->getMock();
142  $transformation->expects(self::once())->method('transform')->with($input)->willReturn($transformed);
143 
144  $string_group = $this->getMockBuilder(StringGroup::class)->disableOriginalConstructor()->getMock();
145  $string_group->expects(self::once())->method('makeClickable')->willReturn($transformation);
146 
147  $refinery = $this->getMockBuilder(Refinery::class)->disableOriginalConstructor()->getMock();
148  $refinery->expects(self::once())->method('string')->willReturn($string_group);
149 
150  $container = $this->getMockBuilder(Container::class)->disableOriginalConstructor()->getMock();
151  $container->expects(self::once())->method('refinery')->willReturn($refinery);
152 
153  return $container;
154  }
155 
156  private function repeatForFormat(string $format, array $values): array
157  {
158  return array_merge(
159  ...array_fill(0, (count(explode('%s', $format)) - 1) / 2, $values)
160  );
161  }
162 }
repeatForFormat(string $format, array $values)
Definition: ilUtilTest.php:156
provideGotoLinkData()
Definition: ilUtilTest.php:76
testMakeClickableWithoutGotoLinks()
ilUtil::makeClickable must call the refinery transformation make clickable.
Definition: ilUtilTest.php:32
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:31
$container
Definition: wac.php:14
if($format !==null) $name
Definition: metadata.php:247
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
string $key
Consumer key/client ID value.
Definition: System.php:193
$format
Definition: metadata.php:235
testMakeClickableWithGotoLinksAndInvalidRefId(string $expected, string $input, array $ref_to_obj, array $obj_to_title)
provideGotoLinkData
Definition: ilUtilTest.php:47
mockClickableCall(string $input, string $transformed)
Definition: ilUtilTest.php:139
static makeClickable(string $a_text, bool $detectGotoLinks=false)
Refinery Factory $refinery