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