6 namespace Friendica\Test;
8 // backward compatibility
9 if (!class_exists('\PHPUnit\Framework\TestCase')) {
10 class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
14 * Tests for text functions.
16 class TextTest extends \PHPUnit\Framework\TestCase
20 *autonames should be random, even length
22 public function testAutonameEven()
24 $autoname1=autoname(10);
25 $autoname2=autoname(10);
27 $this->assertNotEquals($autoname1, $autoname2);
31 *autonames should be random, odd length
33 public function testAutonameOdd()
35 $autoname1=autoname(9);
36 $autoname2=autoname(9);
38 $this->assertNotEquals($autoname1, $autoname2);
42 * try to fail autonames
44 public function testAutonameNoLength()
46 $autoname1=autoname(0);
47 $this->assertEquals(0, strlen($autoname1));
51 * try to fail it with invalid input
53 * @todo What's corect behaviour here? An exception?
55 public function testAutonameNegativeLength()
57 $autoname1=autoname(-23);
58 $this->assertEquals(0, strlen($autoname1));
62 * test with a length, that may be too short
64 public function testAutonameLength1()
66 $autoname1=autoname(1);
67 $this->assertEquals(1, strlen($autoname1));
69 $autoname2=autoname(1);
70 $this->assertEquals(1, strlen($autoname2));
74 * test attribute contains
76 public function testAttributeContains1()
78 $testAttr="class1 notclass2 class3";
79 $this->assertTrue(attribute_contains($testAttr, "class3"));
80 $this->assertFalse(attribute_contains($testAttr, "class2"));
84 * test attribute contains
86 public function testAttributeContains2()
88 $testAttr="class1 not-class2 class3";
89 $this->assertTrue(attribute_contains($testAttr, "class3"));
90 $this->assertFalse(attribute_contains($testAttr, "class2"));
94 * test with empty input
96 public function testAttributeContainsEmpty()
99 $this->assertFalse(attribute_contains($testAttr, "class2"));
103 * test input with special chars
105 public function testAttributeContainsSpecialChars()
107 $testAttr="--... %\$รค() /(=?}";
108 $this->assertFalse(attribute_contains($testAttr, "class2"));
112 * test expand_acl, perfect input
114 public function testExpandAclNormal()
117 $this->assertEquals(array(1, 2, 3), expand_acl($text));
121 * test with a big number
123 public function testExpandAclBigNumber()
125 $text='<1><'.PHP_INT_MAX.'><15>';
126 $this->assertEquals(array(1, PHP_INT_MAX, 15), expand_acl($text));
130 * test with a string in it.
132 * @todo is this valid input? Otherwise: should there be an exception?
134 public function testExpandAclString()
136 $text="<1><279012><tt>";
137 $this->assertEquals(array(1, 279012), expand_acl($text));
141 * test with a ' ' in it.
143 * @todo is this valid input? Otherwise: should there be an exception?
145 public function testExpandAclSpace()
147 $text="<1><279 012><32>";
148 $this->assertEquals(array(1, "279", "32"), expand_acl($text));
154 public function testExpandAclEmpty()
157 $this->assertEquals(array(), expand_acl($text));
161 * test invalid input, no < at all
163 * @todo should there be an exception?
165 public function testExpandAclNoBrackets()
167 $text="According to documentation, that's invalid. "; //should be invalid
168 $this->assertEquals(array(), expand_acl($text));
172 * test invalid input, just open <
174 * @todo should there be an exception?
176 public function testExpandAclJustOneBracket1()
178 $text="<Another invalid string"; //should be invalid
179 $this->assertEquals(array(), expand_acl($text));
183 * test invalid input, just close >
185 * @todo should there be an exception?
187 public function testExpandAclJustOneBracket2()
189 $text="Another invalid> string"; //should be invalid
190 $this->assertEquals(array(), expand_acl($text));
194 * test invalid input, just close >
196 * @todo should there be an exception?
198 public function testExpandAclCloseOnly()
200 $text="Another> invalid> string>"; //should be invalid
201 $this->assertEquals(array(), expand_acl($text));
205 * test invalid input, just open <
207 * @todo should there be an exception?
209 public function testExpandAclOpenOnly()
211 $text="<Another< invalid string<"; //should be invalid
212 $this->assertEquals(array(), expand_acl($text));
216 * test invalid input, open and close do not match
218 * @todo should there be an exception?
220 public function testExpandAclNoMatching1()
222 $text="<Another<> invalid <string>"; //should be invalid
223 $this->assertEquals(array(), expand_acl($text));
227 * test invalid input, empty <>
229 * @todo should there be an exception? Or array(1, 3)
230 * (This should be array(1,3) - mike)
232 public function testExpandAclEmptyMatch()
235 $this->assertEquals(array(1,3), expand_acl($text));
239 * test, that tags are escaped
241 public function testEscapeTags()
243 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
245 $validstring=notags($invalidstring);
246 $escapedString=escape_tags($invalidstring);
248 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
250 "<submit type="button" onclick="alert('failed!');" />",
258 public function testXmlify()
260 $text="<tag>I want to break\n this!11!<?hard?></tag>";
262 $retext=unxmlify($text);
264 $this->assertEquals($text, $retext);
268 * xmlify and put in a document
270 public function testXmlifyDocument()
272 $tag="<tag>I want to break</tag>";
274 $text='<text>'.$xml.'</text>';
276 $xml_parser=xml_parser_create();
277 //should be possible to parse it
280 $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index));
283 array('TEXT'=>array(0)),
287 array(array('tag'=>'TEXT', 'type'=>'complete', 'level'=>1, 'value'=>$tag)),
291 xml_parser_free($xml_parser);
295 * test hex2bin and reverse
297 public function testHex2Bin()
299 $this->assertEquals(-3, hex2bin(bin2hex(-3)));
300 $this->assertEquals(0, hex2bin(bin2hex(0)));
301 $this->assertEquals(12, hex2bin(bin2hex(12)));
302 $this->assertEquals(PHP_INT_MAX, hex2bin(bin2hex(PHP_INT_MAX)));