6 namespace Friendica\Test;
8 //use PHPUnit_Framework_TestCase;
9 // backward compatibility
10 if (!class_exists('\PHPUnit\Framework\TestCase')) {
11 class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
15 * Tests for text functions.
17 class TextTest extends \PHPUnit\Framework\TestCase
21 *autonames should be random, even length
23 public function testAutonameEven()
25 $autoname1=autoname(10);
26 $autoname2=autoname(10);
28 $this->assertNotEquals($autoname1, $autoname2);
32 *autonames should be random, odd length
34 public function testAutonameOdd()
36 $autoname1=autoname(9);
37 $autoname2=autoname(9);
39 $this->assertNotEquals($autoname1, $autoname2);
43 * try to fail autonames
45 public function testAutonameNoLength()
47 $autoname1=autoname(0);
48 $this->assertEquals(0, strlen($autoname1));
52 * try to fail it with invalid input
54 * @todo What's corect behaviour here? An exception?
56 public function testAutonameNegativeLength()
58 $autoname1=autoname(-23);
59 $this->assertEquals(0, strlen($autoname1));
63 * test with a length, that may be too short
65 public function testAutonameLength1()
67 $autoname1=autoname(1);
68 $this->assertEquals(1, count($autoname1));
70 $autoname2=autoname(1);
71 $this->assertEquals(1, count($autoname2));
75 * test attribute contains
77 public function testAttributeContains1()
79 $testAttr="class1 notclass2 class3";
80 $this->assertTrue(attribute_contains($testAttr, "class3"));
81 $this->assertFalse(attribute_contains($testAttr, "class2"));
85 * test attribute contains
87 public function testAttributeContains2()
89 $testAttr="class1 not-class2 class3";
90 $this->assertTrue(attribute_contains($testAttr, "class3"));
91 $this->assertFalse(attribute_contains($testAttr, "class2"));
95 * test with empty input
97 public function testAttributeContainsEmpty()
100 $this->assertFalse(attribute_contains($testAttr, "class2"));
104 * test input with special chars
106 public function testAttributeContainsSpecialChars()
108 $testAttr="--... %\$รค() /(=?}";
109 $this->assertFalse(attribute_contains($testAttr, "class2"));
113 * test expand_acl, perfect input
115 public function testExpandAclNormal()
118 $this->assertEquals(array(1, 2, 3), expand_acl($text));
122 * test with a big number
124 public function testExpandAclBigNumber()
126 $text='<1><'.PHP_INT_MAX.'><15>';
127 $this->assertEquals(array(1, PHP_INT_MAX, 15), expand_acl($text));
131 * test with a string in it.
133 * @todo is this valid input? Otherwise: should there be an exception?
135 public function testExpandAclString()
137 $text="<1><279012><tt>";
138 $this->assertEquals(array(1, 279012), expand_acl($text));
142 * test with a ' ' in it.
144 * @todo is this valid input? Otherwise: should there be an exception?
146 public function testExpandAclSpace()
148 $text="<1><279 012><32>";
149 $this->assertEquals(array(1, "279", "32"), expand_acl($text));
155 public function testExpandAclEmpty()
158 $this->assertEquals(array(), expand_acl($text));
162 * test invalid input, no < at all
164 * @todo should there be an exception?
166 public function testExpandAclNoBrackets()
168 $text="According to documentation, that's invalid. "; //should be invalid
169 $this->assertEquals(array(), expand_acl($text));
173 * test invalid input, just open <
175 * @todo should there be an exception?
177 public function testExpandAclJustOneBracket1()
179 $text="<Another invalid string"; //should be invalid
180 $this->assertEquals(array(), expand_acl($text));
184 * test invalid input, just close >
186 * @todo should there be an exception?
188 public function testExpandAclJustOneBracket2()
190 $text="Another invalid> string"; //should be invalid
191 $this->assertEquals(array(), expand_acl($text));
195 * test invalid input, just close >
197 * @todo should there be an exception?
199 public function testExpandAclCloseOnly()
201 $text="Another> invalid> string>"; //should be invalid
202 $this->assertEquals(array(), expand_acl($text));
206 * test invalid input, just open <
208 * @todo should there be an exception?
210 public function testExpandAclOpenOnly()
212 $text="<Another< invalid string<"; //should be invalid
213 $this->assertEquals(array(), expand_acl($text));
217 * test invalid input, open and close do not match
219 * @todo should there be an exception?
221 public function testExpandAclNoMatching1()
223 $text="<Another<> invalid <string>"; //should be invalid
224 $this->assertEquals(array(), expand_acl($text));
228 * test invalid input, empty <>
230 * @todo should there be an exception? Or array(1, 3)
231 * (This should be array(1,3) - mike)
233 public function testExpandAclEmptyMatch()
236 $this->assertEquals(array(1,3), expand_acl($text));
240 * test, that tags are escaped
242 public function testEscapeTags()
244 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
246 $validstring=notags($invalidstring);
247 $escapedString=escape_tags($invalidstring);
249 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
251 "<submit type="button" onclick="alert('failed!');" />",
259 public function testXmlify()
261 $text="<tag>I want to break\n this!11!<?hard?></tag>";
263 $retext=unxmlify($text);
265 $this->assertEquals($text, $retext);
269 * xmlify and put in a document
271 public function testXmlifyDocument()
273 $tag="<tag>I want to break</tag>";
275 $text='<text>'.$xml.'</text>';
277 $xml_parser=xml_parser_create();
278 //should be possible to parse it
281 $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index));
284 array('TEXT'=>array(0)),
288 array(array('tag'=>'TEXT', 'type'=>'complete', 'level'=>1, 'value'=>$tag)),
292 xml_parser_free($xml_parser);
296 * test hex2bin and reverse
298 public function testHex2Bin()
300 $this->assertEquals(-3, hex2bin(bin2hex(-3)));
301 $this->assertEquals(0, hex2bin(bin2hex(0)));
302 $this->assertEquals(12, hex2bin(bin2hex(12)));
303 $this->assertEquals(PHP_INT_MAX, hex2bin(bin2hex(PHP_INT_MAX)));