ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
EstimatedReadingTimeTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 require_once 'vendor/composer/vendor/autoload.php';
24 
29 use stdClass;
31 
32 class EstimatedReadingTimeTest extends TestCase
33 {
34  private const TEXT = <<<EOT
35 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
36 EOT;
37  private const HTML = <<<EOT
38 <div>Lorem ipsum dolor <span style="color: red;">sit amet</span>, <img src="#" /> consetetur sadipscing elitr, sed diam nonumy eirmod <img src="#" /> tempor invidunt <img src="#" /> ut labore et dolore <img src="#" /> magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor <img src="#" /> sit amet. <img src="#" /> Lorem ipsum dolor <img src="#" /> sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, <img src="#" /> sed diam voluptua. <img src="#" /> At vero eos et accusam et justo duo dolores et ea rebum. Stet <img src="#" /> clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</div>
39 EOT;
40 
41  private const HTML_WITH_NON_XHTML_URLS = <<<EOT
42 <a href="http://www.ilias.de=foo=1&bar=2">ILIAS</a>
43 EOT;
44 
45  private Refinery $refinery;
46 
47  protected function setUp(): void
48  {
49  $this->refinery = new Refinery(
50  $this->createMock(DataFactory::class),
51  $this->createMock(Language::class)
52  );
53 
54  parent::setUp();
55  }
56 
60  public static function inputProvider(): array
61  {
62  return [
63  [5],
64  [6.3],
65  [[]],
66  [new stdClass()],
67  [true],
68  [null],
69  [static function (): void {
70  }],
71  ];
72  }
73 
77  public static function unsupportedButKnownEntitiesProvider(): array
78  {
79  return array_map(static function (string $entity): array {
80  return [
81  '<div>Lorem ipsum dolor <' . $entity . '></' . $entity . '></div>'
82  ];
83  }, [
84  'figure',
85  'figcaption',
86  'nav',
87  'section',
88  'header',
89  'article',
90  'footer',
91  'aside',
92  'noindex',
93  ]);
94  }
95 
100  public function testExceptionIsRaisedIfInputIsNotAString($from): void
101  {
102  $this->expectException(InvalidArgumentException::class);
103  $readingTimeTrafo = $this->refinery->string()->estimatedReadingTime(true);
104 
105  $readingTimeTrafo->transform($from);
106  }
107 
108  public function testReadingTimeForPlainTextCanBeDetermined(): void
109  {
110  $readingTimeTrafo = $this->refinery->string()->estimatedReadingTime(true);
111  $this->assertEquals(
112  1,
113  $readingTimeTrafo->transform(self::TEXT)
114  );
115  }
116 
117  public function testReadingTimeForHtmlFragmentCanBeDetermined(): void
118  {
119  $text = self::HTML;
120 
121  $readingTimeTrafo = $this->refinery->string()->estimatedReadingTime(true);
122  $this->assertEquals(
123  2,
124  $readingTimeTrafo->transform($text)
125  );
126 
127  $onlyTextReadingTimeInfo = $this->refinery->string()->estimatedReadingTime();
128  $this->assertEquals(
129  1,
130  $onlyTextReadingTimeInfo->transform($text)
131  );
132  }
133 
134  public function testSolitaryPunctuationCharactersMustNotAffectReadingTime(): void
135  {
136  $textSegmentWithPunctuation = 'Lorem ipsum <img src="#" />, and some other text... ';
137  $repetitions = 300; // 275 repetitions result in an additional minute, if the `,` would be considered
138 
139  $readingTimeTrafo = $this->refinery->string()->estimatedReadingTime(true);
140 
141  $text = str_repeat($textSegmentWithPunctuation, $repetitions);
142 
143  $timeInMinutes = $readingTimeTrafo->transform($text);
144  $this->assertEquals(23, $timeInMinutes);
145 
146  $textSegmentWithoutPunctuation = 'Lorem ipsum <img src="#" /> and some other text... ';
147  $text = str_repeat($textSegmentWithoutPunctuation, $repetitions);
148 
149  $timeInMinutes = $readingTimeTrafo->transform($text);
150  $this->assertEquals(23, $timeInMinutes);
151  }
152 
153  public function testXTHMLCommentsMustNotAffectReadingTime(): void
154  {
155  $text = self::HTML;
156 
157  $comment = '<script><!--a comment--></script>';
158  $repetitions = 300;
159  $text .= str_repeat($comment, $repetitions);
160 
161  $onlyTextReadingTimeInfo = $this->refinery->string()->estimatedReadingTime();
162  $this->assertEquals(
163  1,
164  $onlyTextReadingTimeInfo->transform($text)
165  );
166  }
167 
172  public function testNoExceptionIsRaisedIfHtmlContainsUnsupportedEntities(string $text): void
173  {
174  $reading_time_trafo = $this->refinery->string()->estimatedReadingTime(true);
175 
176  $reading_time = $reading_time_trafo->transform($text);
177 
178  $this->assertIsInt($reading_time);
179  $this->assertGreaterThan(0, $reading_time);
180  }
181 
182  public function testNoExceptionIsRaisedIfHtmlContainsAmpersandInUrls(): void
183  {
184  $reading_time_trafo = $this->refinery->string()->estimatedReadingTime(true);
185 
186  $reading_time = $reading_time_trafo->transform(self::HTML_WITH_NON_XHTML_URLS);
187 
188  $this->assertIsInt($reading_time);
189  $this->assertGreaterThan(0, $reading_time);
190  }
191 }
Interface Observer Contains several chained tasks and infos about them.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$comment
Definition: buildRTE.php:72