]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/StringsTest.php
Rename escapeTags to escapeHtml
[friendica.git] / tests / src / Util / StringsTest.php
1 <?php
2 /**
3  * @file tests/src/Util/StringsTest.php
4  */
5 namespace Friendica\Test\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::removeTags($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 }