Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
6.3KB

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