ILIAS  trunk Revision v11.0_alpha-2658-ge2404539063
EncodingTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 
26 class EncodingTest extends TestCase
27 {
28  private ?Group $group = null;
29 
30  public function setUp(): void
31  {
32  $this->group = new Group();
33  }
34 
35  public static function latin1StringProvider(): array
36  {
37  // generate 500 random strings with ISO-8859-1 encoding. unfortunately, I was not able to find a list to copy
38  // here which keeps it's encoding, therefore we must generate them randomly
39  $strings = [];
40  for ($i = 0; $i < 500; $i++) {
41  $length = random_int(50, 500);
42  $string = '';
43  for ($j = 0; $j < $length; $j++) {
44  $string .= chr(random_int(0, 255));
45  }
46  $strings[] = [$string, @utf8_encode($string)]; // we must suppress the deprecation warning here
47  }
48  return $strings;
49  }
50 
51  #[DataProvider('latin1StringProvider')]
52  public function testLatin1ToUTF8(
53  string $latin_1_string,
54  string $expected_utf8
55  ): void {
56  $this->assertTrue(mb_check_encoding($latin_1_string, 'ISO-8859-1'));
57  $result = $this->group->latin1ToUtf8()->transform($latin_1_string);
58  $this->assertTrue(mb_check_encoding($result, 'UTF-8'));
59  $this->assertEquals($expected_utf8, $result);
60  }
61 
62  public static function asciiStringProvider(): array
63  {
64  // generate 500 random strings with US-ASCII encosing.
65  $strings = [];
66  for ($i = 0; $i < 500; $i++) {
67  $length = random_int(50, 500);
68  $string = '';
69  for ($j = 0; $j < $length; $j++) {
70  $string .= chr(random_int(0, 127));
71  }
72  $strings[] = [$string, $string];
73  }
74  return $strings;
75  }
76 
77  #[DataProvider('asciiStringProvider')]
78  public function testAsciiToUTF8(
79  string $latin_1_string,
80  string $expected_utf8
81  ): void {
82  $this->assertTrue(mb_check_encoding($latin_1_string, 'US-ASCII'));
83  $result = $this->group->asciiToUtf8()->transform($latin_1_string);
84  $this->assertTrue(mb_check_encoding($result, 'UTF-8'));
85  $this->assertEquals($expected_utf8, $result);
86  }
87 
88 }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
testAsciiToUTF8(string $latin_1_string, string $expected_utf8)
testLatin1ToUTF8(string $latin_1_string, string $expected_utf8)