3 * @file tests/src/Util/StringsTest.php
5 namespace Friendica\Test\src\Util;
7 use Friendica\Util\Strings;
8 use PHPUnit\Framework\TestCase;
11 * @brief Strings utility test class
13 class StringsTest extends TestCase
16 * randomnames should be random, even length
18 public function testRandomEven()
20 $randomname1 = Strings::getRandomName(10);
21 $randomname2 = Strings::getRandomName(10);
23 $this->assertNotEquals($randomname1, $randomname2);
27 * randomnames should be random, odd length
29 public function testRandomOdd()
31 $randomname1 = Strings::getRandomName(9);
32 $randomname2 = Strings::getRandomName(9);
34 $this->assertNotEquals($randomname1, $randomname2);
38 * try to fail ramdonnames
40 public function testRandomNameNoLength()
42 $randomname1 = Strings::getRandomName(0);
43 $this->assertEquals(0, strlen($randomname1));
47 * try to fail it with invalid input
49 * @todo What's corect behaviour here? An exception?
51 public function testRandomNameNegativeLength()
53 $randomname1 = Strings::getRandomName(-23);
54 $this->assertEquals(0, strlen($randomname1));
58 * test with a length, that may be too short
60 public function testRandomNameLength1()
62 $randomname1 = Strings::getRandomName(1);
63 $this->assertEquals(1, strlen($randomname1));
65 $randomname2 = Strings::getRandomName(1);
66 $this->assertEquals(1, strlen($randomname2));
70 * test, that tags are escaped
72 public function testEscapeHtml()
74 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
76 $validstring = Strings::escapeTags($invalidstring);
77 $escapedString = Strings::escapeHtml($invalidstring);
79 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
81 "<submit type="button" onclick="alert('failed!');" />",
86 public function dataIsHex()
90 'input' => '90913473615bf00c122ac78338492980',
94 'input' => '90913473615bf00c122ac7833849293',
109 * Tests if the string is a valid hexadecimal value
111 * @param string $input
114 * @dataProvider dataIsHex
116 public function testIsHex($input, $valid)
118 $this->assertEquals($valid, Strings::isHex($input));