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