Add test for Strings::isHex()
[friendica.git/.git] / tests / src / Util / StringsTest.php
1 <?php
2 /**
3  * @file tests/src/Util/StringsTest.php
4  */
5 namespace Friendica\Test\src\Util;
6
7 use Friendica\Util\Strings;
8 use PHPUnit\Framework\TestCase;
9
10 /**
11  * @brief Strings utility test class
12  */
13 class StringsTest extends TestCase
14 {
15     /**
16          * randomnames should be random, even length
17          */
18         public function testRandomEven()
19         {
20                 $randomname1 = Strings::getRandomName(10);
21                 $randomname2 = Strings::getRandomName(10);
22
23                 $this->assertNotEquals($randomname1, $randomname2);
24         }
25
26         /**
27          * randomnames should be random, odd length
28          */
29         public function testRandomOdd()
30         {
31                 $randomname1 = Strings::getRandomName(9);
32                 $randomname2 = Strings::getRandomName(9);
33
34                 $this->assertNotEquals($randomname1, $randomname2);
35         }
36
37         /**
38          * try to fail ramdonnames
39          */
40         public function testRandomNameNoLength()
41         {
42                 $randomname1 = Strings::getRandomName(0);
43                 $this->assertEquals(0, strlen($randomname1));
44         }
45
46         /**
47          * try to fail it with invalid input
48          *
49          * @todo What's corect behaviour here? An exception?
50          */
51         public function testRandomNameNegativeLength()
52         {
53                 $randomname1 = Strings::getRandomName(-23);
54                 $this->assertEquals(0, strlen($randomname1));
55         }
56
57         /**
58          * test with a length, that may be too short
59          */
60         public function testRandomNameLength1()
61         {
62                 $randomname1 = Strings::getRandomName(1);
63                 $this->assertEquals(1, strlen($randomname1));
64
65                 $randomname2 = Strings::getRandomName(1);
66                 $this->assertEquals(1, strlen($randomname2));
67     }
68     
69     /**
70          * test, that tags are escaped
71          */
72         public function testEscapeHtml()
73         {
74                 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
75
76                 $validstring = Strings::escapeTags($invalidstring);
77                 $escapedString = Strings::escapeHtml($invalidstring);
78
79                 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
80                 $this->assertEquals(
81                         "&lt;submit type=&quot;button&quot; onclick=&quot;alert('failed!');&quot; /&gt;",
82                         $escapedString
83                 );
84         }
85
86         public function dataIsHex()
87         {
88                 return [
89                         'validHex' => [
90                                 'input' => '90913473615bf00c122ac78338492980',
91                                 'valid' => true,
92                         ],
93                         'invalidHex' => [
94                                 'input' => '90913473615bf00c122ac7833849293',
95                                 'valid' => false,
96                         ],
97                         'emptyHex' => [
98                                 'input' => '',
99                                 'valid' => false,
100                         ],
101                         'nullHex' => [
102                                 'input' => null,
103                                 'valid' => false,
104                         ],
105                 ];
106         }
107
108         /**
109          * Tests if the string is a valid hexadecimal value
110          *
111          * @param string $input
112          * @param bool $valid
113          *
114          * @dataProvider dataIsHex
115          */
116         public function testIsHex($input, $valid)
117         {
118                 $this->assertEquals($valid, Strings::isHex($input));
119         }
120 }