Burn notices, burn
[friendica.git/.git] / tests / TextTest.php
1 <?php
2 /**
3  * TextTest class.
4  */
5
6 namespace Friendica\Test;
7
8 use PHPUnit\Framework\TestCase;
9
10 /**
11  * Tests for text functions.
12  */
13 class TextTest extends TestCase
14 {
15
16         /**
17          *autonames should be random, even length
18          */
19         public function testAutonameEven()
20         {
21                 $autoname1=autoname(10);
22                 $autoname2=autoname(10);
23
24                 $this->assertNotEquals($autoname1, $autoname2);
25         }
26
27         /**
28          *autonames should be random, odd length
29          */
30         public function testAutonameOdd()
31         {
32                 $autoname1=autoname(9);
33                 $autoname2=autoname(9);
34
35                 $this->assertNotEquals($autoname1, $autoname2);
36         }
37
38         /**
39          * try to fail autonames
40          */
41         public function testAutonameNoLength()
42         {
43                 $autoname1=autoname(0);
44                 $this->assertEquals(0, strlen($autoname1));
45         }
46
47         /**
48          * try to fail it with invalid input
49          *
50          * @todo What's corect behaviour here? An exception?
51          */
52         public function testAutonameNegativeLength()
53         {
54                 $autoname1=autoname(-23);
55                 $this->assertEquals(0, strlen($autoname1));
56         }
57
58         /**
59          * test with a length, that may be too short
60          */
61         public function testAutonameLength1()
62         {
63                 $autoname1=autoname(1);
64                 $this->assertEquals(1, strlen($autoname1));
65
66                 $autoname2=autoname(1);
67                 $this->assertEquals(1, strlen($autoname2));
68         }
69
70         /**
71          * test attribute contains
72          */
73         public function testAttributeContains1()
74         {
75                 $testAttr="class1 notclass2 class3";
76                 $this->assertTrue(attribute_contains($testAttr, "class3"));
77                 $this->assertFalse(attribute_contains($testAttr, "class2"));
78         }
79
80         /**
81          * test attribute contains
82          */
83         public function testAttributeContains2()
84         {
85                 $testAttr="class1 not-class2 class3";
86                 $this->assertTrue(attribute_contains($testAttr, "class3"));
87                 $this->assertFalse(attribute_contains($testAttr, "class2"));
88         }
89
90         /**
91          * test with empty input
92          */
93         public function testAttributeContainsEmpty()
94         {
95                 $testAttr="";
96                 $this->assertFalse(attribute_contains($testAttr, "class2"));
97         }
98
99         /**
100          * test input with special chars
101          */
102         public function testAttributeContainsSpecialChars()
103         {
104                 $testAttr="--... %\$รค() /(=?}";
105                 $this->assertFalse(attribute_contains($testAttr, "class2"));
106         }
107
108         /**
109          * test expand_acl, perfect input
110          */
111         public function testExpandAclNormal()
112         {
113                 $text='<1><2><3>';
114                 $this->assertEquals(array(1, 2, 3), expand_acl($text));
115         }
116
117         /**
118          * test with a big number
119          */
120         public function testExpandAclBigNumber()
121         {
122                 $text='<1><'.PHP_INT_MAX.'><15>';
123                 $this->assertEquals(array(1, PHP_INT_MAX, 15), expand_acl($text));
124         }
125
126         /**
127          * test with a string in it.
128          *
129          * @todo is this valid input? Otherwise: should there be an exception?
130          */
131         public function testExpandAclString()
132         {
133                 $text="<1><279012><tt>";
134                 $this->assertEquals(array(1, 279012), expand_acl($text));
135         }
136
137         /**
138          * test with a ' ' in it.
139          *
140          * @todo is this valid input? Otherwise: should there be an exception?
141          */
142         public function testExpandAclSpace()
143         {
144                 $text="<1><279 012><32>";
145                 $this->assertEquals(array(1, "279", "32"), expand_acl($text));
146         }
147
148         /**
149          * test empty input
150          */
151         public function testExpandAclEmpty()
152         {
153                 $text="";
154                 $this->assertEquals(array(), expand_acl($text));
155         }
156
157         /**
158          * test invalid input, no < at all
159          *
160          * @todo should there be an exception?
161          */
162         public function testExpandAclNoBrackets()
163         {
164                 $text="According to documentation, that's invalid. "; //should be invalid
165                 $this->assertEquals(array(), expand_acl($text));
166         }
167
168         /**
169          * test invalid input, just open <
170          *
171          * @todo should there be an exception?
172          */
173         public function testExpandAclJustOneBracket1()
174         {
175                 $text="<Another invalid string"; //should be invalid
176                 $this->assertEquals(array(), expand_acl($text));
177         }
178
179         /**
180          * test invalid input, just close >
181          *
182          * @todo should there be an exception?
183          */
184         public function testExpandAclJustOneBracket2()
185         {
186                 $text="Another invalid> string"; //should be invalid
187                 $this->assertEquals(array(), expand_acl($text));
188         }
189
190         /**
191          * test invalid input, just close >
192          *
193          * @todo should there be an exception?
194          */
195         public function testExpandAclCloseOnly()
196         {
197                 $text="Another> invalid> string>"; //should be invalid
198                 $this->assertEquals(array(), expand_acl($text));
199         }
200
201         /**
202          * test invalid input, just open <
203          *
204          * @todo should there be an exception?
205          */
206         public function testExpandAclOpenOnly()
207         {
208                 $text="<Another< invalid string<"; //should be invalid
209                 $this->assertEquals(array(), expand_acl($text));
210         }
211
212         /**
213          * test invalid input, open and close do not match
214          *
215          * @todo should there be an exception?
216          */
217         public function testExpandAclNoMatching1()
218         {
219                 $text="<Another<> invalid <string>"; //should be invalid
220                 $this->assertEquals(array(), expand_acl($text));
221         }
222
223         /**
224          * test invalid input, empty <>
225          *
226          * @todo should there be an exception? Or array(1, 3)
227          * (This should be array(1,3) - mike)
228          */
229         public function testExpandAclEmptyMatch()
230         {
231                 $text="<1><><3>";
232                 $this->assertEquals(array(1,3), expand_acl($text));
233         }
234
235         /**
236          * test, that tags are escaped
237          */
238         public function testEscapeTags()
239         {
240                 $invalidstring='<submit type="button" onclick="alert(\'failed!\');" />';
241
242                 $validstring=notags($invalidstring);
243                 $escapedString=escape_tags($invalidstring);
244
245                 $this->assertEquals('[submit type="button" onclick="alert(\'failed!\');" /]', $validstring);
246                 $this->assertEquals(
247                         "&lt;submit type=&quot;button&quot; onclick=&quot;alert('failed!');&quot; /&gt;",
248                         $escapedString
249                 );
250         }
251
252         /**
253          *xmlify and unxmlify
254          */
255         public function testXmlify()
256         {
257                 $text="<tag>I want to break\n this!11!<?hard?></tag>";
258                 $xml=xmlify($text);
259                 $retext=unxmlify($text);
260
261                 $this->assertEquals($text, $retext);
262         }
263
264         /**
265          * xmlify and put in a document
266          */
267         public function testXmlifyDocument()
268         {
269                 $tag="<tag>I want to break</tag>";
270                 $xml=xmlify($tag);
271                 $text='<text>'.$xml.'</text>';
272
273                 $xml_parser=xml_parser_create();
274                 //should be possible to parse it
275                 $values=array();
276                 $index=array();
277                 $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index));
278
279                 $this->assertEquals(
280                         array('TEXT'=>array(0)),
281                         $index
282                 );
283                 $this->assertEquals(
284                         array(array('tag'=>'TEXT', 'type'=>'complete', 'level'=>1, 'value'=>$tag)),
285                         $values
286                 );
287
288                 xml_parser_free($xml_parser);
289         }
290
291         /**
292          * test hex2bin and reverse
293          */
294         public function testHex2Bin()
295         {
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)));
300         }
301 }