6 namespace Friendica\Test;
8 use PHPUnit_Framework_TestCase;
11 * Tests for text functions.
13 class TextTest extends PHPUnit_Framework_TestCase
17 *autonames should be random, even length
19 public function testAutonameEven()
21 $autoname1=autoname(10);
22 $autoname2=autoname(10);
24 $this->assertNotEquals($autoname1, $autoname2);
28 *autonames should be random, odd length
30 public function testAutonameOdd()
32 $autoname1=autoname(9);
33 $autoname2=autoname(9);
35 $this->assertNotEquals($autoname1, $autoname2);
39 * try to fail autonames
41 public function testAutonameNoLength()
43 $autoname1=autoname(0);
44 $this->assertEquals(0, strlen($autoname1));
48 * try to fail it with invalid input
50 * @todo What's corect behaviour here? An exception?
52 public function testAutonameNegativeLength()
54 $autoname1=autoname(-23);
55 $this->assertEquals(0, strlen($autoname1));
59 * test with a length, that may be too short
61 public function testAutonameLength1()
63 $autoname1=autoname(1);
64 $this->assertEquals(1, count($autoname1));
66 $autoname2=autoname(1);
67 $this->assertEquals(1, count($autoname2));
71 * test attribute contains
73 public function testAttributeContains1()
75 $testAttr="class1 notclass2 class3";
76 $this->assertTrue(attribute_contains($testAttr, "class3"));
77 $this->assertFalse(attribute_contains($testAttr, "class2"));
81 * test attribute contains
83 public function testAttributeContains2()
85 $testAttr="class1 not-class2 class3";
86 $this->assertTrue(attribute_contains($testAttr, "class3"));
87 $this->assertFalse(attribute_contains($testAttr, "class2"));
91 * test with empty input
93 public function testAttributeContainsEmpty()
96 $this->assertFalse(attribute_contains($testAttr, "class2"));
100 * test input with special chars
102 public function testAttributeContainsSpecialChars()
104 $testAttr="--... %\$รค() /(=?}";
105 $this->assertFalse(attribute_contains($testAttr, "class2"));
109 * test expand_acl, perfect input
111 public function testExpandAclNormal()
114 $this->assertEquals(array(1, 2, 3), expand_acl($text));
118 * test with a big number
120 public function testExpandAclBigNumber()
122 $text='<1><'.PHP_INT_MAX.'><15>';
123 $this->assertEquals(array(1, PHP_INT_MAX, 15), expand_acl($text));
127 * test with a string in it.
129 * @todo is this valid input? Otherwise: should there be an exception?
131 public function testExpandAclString()
133 $text="<1><279012><tt>";
134 $this->assertEquals(array(1, 279012), expand_acl($text));
138 * test with a ' ' in it.
140 * @todo is this valid input? Otherwise: should there be an exception?
142 public function testExpandAclSpace()
144 $text="<1><279 012><32>";
145 $this->assertEquals(array(1, "279", "32"), expand_acl($text));
151 public function testExpandAclEmpty()
154 $this->assertEquals(array(), expand_acl($text));
158 * test invalid input, no < at all
160 * @todo should there be an exception?
162 public function testExpandAclNoBrackets()
164 $text="According to documentation, that's invalid. "; //should be invalid
165 $this->assertEquals(array(), expand_acl($text));
169 * test invalid input, just open <
171 * @todo should there be an exception?
173 public function testExpandAclJustOneBracket1()
175 $text="<Another invalid string"; //should be invalid
176 $this->assertEquals(array(), expand_acl($text));
180 * test invalid input, just close >
182 * @todo should there be an exception?
184 public function testExpandAclJustOneBracket2()
186 $text="Another invalid> string"; //should be invalid
187 $this->assertEquals(array(), expand_acl($text));
191 * test invalid input, just close >
193 * @todo should there be an exception?
195 public function testExpandAclCloseOnly()
197 $text="Another> invalid> string>"; //should be invalid
198 $this->assertEquals(array(), expand_acl($text));
202 * test invalid input, just open <
204 * @todo should there be an exception?
206 public function testExpandAclOpenOnly()
208 $text="<Another< invalid string<"; //should be invalid
209 $this->assertEquals(array(), expand_acl($text));
213 * test invalid input, open and close do not match
215 * @todo should there be an exception?
217 public function testExpandAclNoMatching1()
219 $text="<Another<> invalid <string>"; //should be invalid
220 $this->assertEquals(array(), expand_acl($text));
224 * test invalid input, empty <>
226 * @todo should there be an exception? Or array(1, 3)
227 * (This should be array(1,3) - mike)
229 public function testExpandAclEmptyMatch()
232 $this->assertEquals(array(1,3), expand_acl($text));
236 * test, that tags are escaped
238 public function testEscapeTags()
240 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
242 $validstring=notags($invalidstring);
243 $escapedString=escape_tags($invalidstring);
245 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
247 "<submit type="button" onclick="alert('failed!');" />",
255 public function testXmlify()
257 $text="<tag>I want to break\n this!11!<?hard?></tag>";
259 $retext=unxmlify($text);
261 $this->assertEquals($text, $retext);
265 * xmlify and put in a document
267 public function testXmlifyDocument()
269 $tag="<tag>I want to break</tag>";
271 $text='<text>'.$xml.'</text>';
273 $xml_parser=xml_parser_create();
274 //should be possible to parse it
277 $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index));
280 array('TEXT'=>array(0)),
284 array(array('tag'=>'TEXT', 'type'=>'complete', 'level'=>1, 'value'=>$tag)),
288 xml_parser_free($xml_parser);
292 * test hex2bin and reverse
294 public function testHex2Bin()
296 $this->assertEquals(-3, hex2bin(bin2hex(-3)));
297 $this->assertEquals(0, hex2bin(bin2hex(0)));
298 $this->assertEquals(12, hex2bin(bin2hex(12)));
299 $this->assertEquals(PHP_INT_MAX, hex2bin(bin2hex(PHP_INT_MAX)));