ElggCoreHelpersTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * Elgg Test helper functions
  4. *
  5. *
  6. * @package Elgg
  7. * @subpackage Test
  8. */
  9. class ElggCoreHelpersTest extends \ElggCoreUnitTest {
  10. /**
  11. * Called before each test object.
  12. */
  13. public function __construct() {
  14. parent::__construct();
  15. }
  16. /**
  17. * Called before each test method.
  18. */
  19. public function setUp() {
  20. }
  21. /**
  22. * Called after each test method.
  23. */
  24. public function tearDown() {
  25. global $CONFIG;
  26. unset($CONFIG->externals);
  27. unset($CONFIG->externals_map);
  28. }
  29. /**
  30. * Called after each test object.
  31. */
  32. public function __destruct() {
  33. // all __destruct() code should go above here
  34. parent::__destruct();
  35. }
  36. /**
  37. * Test elgg_instanceof()
  38. */
  39. public function testElggInstanceOf() {
  40. $entity = new \ElggObject();
  41. $entity->subtype = 'test_subtype';
  42. $entity->save();
  43. $this->assertTrue(elgg_instanceof($entity));
  44. $this->assertTrue(elgg_instanceof($entity, 'object'));
  45. $this->assertTrue(elgg_instanceof($entity, 'object', 'test_subtype'));
  46. $this->assertFalse(elgg_instanceof($entity, 'object', 'invalid_subtype'));
  47. $this->assertFalse(elgg_instanceof($entity, 'user', 'test_subtype'));
  48. $entity->delete();
  49. $bad_entity = false;
  50. $this->assertFalse(elgg_instanceof($bad_entity));
  51. $this->assertFalse(elgg_instanceof($bad_entity, 'object'));
  52. $this->assertFalse(elgg_instanceof($bad_entity, 'object', 'test_subtype'));
  53. remove_subtype('object', 'test_subtype');
  54. }
  55. /**
  56. * Test elgg_normalize_url()
  57. */
  58. public function testElggNormalizeURL() {
  59. $conversions = array(
  60. 'http://example.com' => 'http://example.com',
  61. 'https://example.com' => 'https://example.com',
  62. 'http://example-time.com' => 'http://example-time.com',
  63. '//example.com' => '//example.com',
  64. 'ftp://example.com/file' => 'ftp://example.com/file',
  65. 'mailto:brett@elgg.org' => 'mailto:brett@elgg.org',
  66. 'javascript:alert("test")' => 'javascript:alert("test")',
  67. 'app://endpoint' => 'app://endpoint',
  68. 'example.com' => 'http://example.com',
  69. 'example.com/subpage' => 'http://example.com/subpage',
  70. 'page/handler' => elgg_get_site_url() . 'page/handler',
  71. 'page/handler?p=v&p2=v2' => elgg_get_site_url() . 'page/handler?p=v&p2=v2',
  72. 'mod/plugin/file.php' => elgg_get_site_url() . 'mod/plugin/file.php',
  73. 'mod/plugin/file.php?p=v&p2=v2' => elgg_get_site_url() . 'mod/plugin/file.php?p=v&p2=v2',
  74. 'rootfile.php' => elgg_get_site_url() . 'rootfile.php',
  75. 'rootfile.php?p=v&p2=v2' => elgg_get_site_url() . 'rootfile.php?p=v&p2=v2',
  76. '/page/handler' => elgg_get_site_url() . 'page/handler',
  77. '/page/handler?p=v&p2=v2' => elgg_get_site_url() . 'page/handler?p=v&p2=v2',
  78. '/mod/plugin/file.php' => elgg_get_site_url() . 'mod/plugin/file.php',
  79. '/mod/plugin/file.php?p=v&p2=v2' => elgg_get_site_url() . 'mod/plugin/file.php?p=v&p2=v2',
  80. '/rootfile.php' => elgg_get_site_url() . 'rootfile.php',
  81. '/rootfile.php?p=v&p2=v2' => elgg_get_site_url() . 'rootfile.php?p=v&p2=v2',
  82. );
  83. foreach ($conversions as $input => $output) {
  84. $this->assertIdentical($output, elgg_normalize_url($input));
  85. }
  86. }
  87. /**
  88. * Test elgg_http_url_is_identical()
  89. */
  90. public function testHttpUrlIsIdentical() {
  91. $tests = array(
  92. 'http://example.com' => 'http://example.com',
  93. 'https://example.com' => 'https://example.com',
  94. 'http://example-time.com' => 'http://example-time.com',
  95. '//example.com' => '//example.com',
  96. 'ftp://example.com/file' => 'ftp://example.com/file',
  97. 'mailto:brett@elgg.org' => 'mailto:brett@elgg.org',
  98. 'javascript:alert("test")' => 'javascript:alert("test")',
  99. 'app://endpoint' => 'app://endpoint',
  100. 'example.com' => 'http://example.com',
  101. 'example.com/subpage' => 'http://example.com/subpage',
  102. 'page/handler' => elgg_get_site_url() . 'page/handler',
  103. 'page/handler?p=v&p2=v2' => elgg_get_site_url() . 'page/handler?p=v&p2=v2',
  104. 'mod/plugin/file.php' => elgg_get_site_url() . 'mod/plugin/file.php',
  105. 'mod/plugin/file.php?p=v&p2=v2' => elgg_get_site_url() . 'mod/plugin/file.php?p=v&p2=v2',
  106. 'search?foo.bar' => elgg_get_site_url() . 'search?foo.bar',
  107. 'rootfile.php' => elgg_get_site_url() . 'rootfile.php',
  108. 'rootfile.php?p=v&p2=v2' => elgg_get_site_url() . 'rootfile.php?p=v&p2=v2',
  109. '/page/handler' => elgg_get_site_url() . 'page/handler',
  110. '/page/handler?p=v&p2=v2' => elgg_get_site_url() . 'page/handler?p=v&p2=v2',
  111. '/mod/plugin/file.php' => elgg_get_site_url() . 'mod/plugin/file.php',
  112. '/mod/plugin/file.php?p=v&p2=v2' => elgg_get_site_url() . 'mod/plugin/file.php?p=v&p2=v2',
  113. '/rootfile.php' => elgg_get_site_url() . 'rootfile.php',
  114. '/rootfile.php?p=v&p2=v2' => elgg_get_site_url() . 'rootfile.php?p=v&p2=v2',
  115. );
  116. foreach ($tests as $input => $output) {
  117. $this->assertTrue(elgg_http_url_is_identical($output, $input), "Failed to determine URLs as identical for: '$output' and '$input'");
  118. $this->assertTrue(elgg_http_url_is_identical($input, $output), "Failed to determine URLs as identical for: '$input' and '$output'");
  119. }
  120. }
  121. /**
  122. * Test elgg_http_url_is_identical() for $ignore_params parameter handling
  123. */
  124. public function testHttpUrlIsIdenticalIgnoreParamsHandling() {
  125. $tests = array(
  126. array('page/handler', elgg_get_site_url() . 'page/handler', array('p', 'p2'), true),
  127. array('page/handler?p=v&p2=q2', elgg_get_site_url() . 'page/handler?p=q&p2=v2', array('p', 'p2'), true),
  128. array('/rootfile.php', elgg_get_site_url() . 'rootfile.php?param=23', array('param'), true),
  129. array('/rootfile.php?p=v&p2=v2', elgg_get_site_url() . 'rootfile.php?p=v&p2=q', array('p', 'p2'), true),
  130. array('mod/plugin/file.php?other_param=123', elgg_get_site_url() . 'mod/plugin/file.php', array('q', 'p2'), false),
  131. array('/rootfile.php', elgg_get_site_url() . 'rootfile.php?param=23', array(), false),
  132. );
  133. foreach ($tests as $test) {
  134. list($url1, $url2, $ignore_params, $result) = $test;
  135. $this->assertIdentical(elgg_http_url_is_identical($url1, $url2, $ignore_params), $result, "Failed to determine URLs as "
  136. . ($result ? 'identical' : 'different') . " for: '$url1', '$url2' and ignore params set to " . print_r($ignore_params, true));
  137. $this->assertIdentical(elgg_http_url_is_identical($url2, $url1, $ignore_params), $result, "Failed to determine URLs as "
  138. . ($result ? 'identical' : 'different') . " for: '$url2', '$url1' and ignore params set to " . print_r($ignore_params, true));
  139. }
  140. }
  141. /**
  142. * Test elgg_format_element()
  143. */
  144. public function testElggFormatElement() {
  145. $tests = array(
  146. '<span>a & b</span>' => array(
  147. 'tag_name' => 'span',
  148. 'text' => 'a & b',
  149. '_msg' => 'Basic formatting, span recognized as non-void element',
  150. ),
  151. '<span>a &amp; &amp; b</span>' => array(
  152. 'tag_name' => 'span',
  153. 'text' => 'a & &amp; b',
  154. 'opts' => array('encode_text' => true),
  155. '_msg' => 'HTML escaping, does not double encode',
  156. ),
  157. '<span>a &amp;times; b</span>' => array(
  158. 'tag_name' => 'span',
  159. 'text' => 'a &times; b',
  160. 'opts' => array('encode_text' => true, 'double_encode' => true),
  161. '_msg' => 'HTML escaping double encodes',
  162. ),
  163. '<IMG src="a &amp; b">' => array(
  164. 'tag_name' => 'IMG',
  165. 'attrs' => array('src' => 'a & b'),
  166. 'text' => 'should not appear',
  167. '_msg' => 'IMG recognized as void element, text ignored',
  168. ),
  169. '<foo />' => array(
  170. 'tag_name' => 'foo',
  171. 'opts' => array('is_void' => true, 'is_xml' => true),
  172. '_msg' => 'XML syntax for self-closing elements',
  173. ),
  174. );
  175. foreach ($tests as $expected => $vars) {
  176. $tag_name = $vars['tag_name'];
  177. $text = isset($vars['text']) ? $vars['text'] : null;
  178. $opts = isset($vars['opts']) ? $vars['opts'] : array();
  179. $attrs = isset($vars['attrs']) ? $vars['attrs'] : array();
  180. $message = isset($vars['_msg']) ? $vars['_msg'] : null;
  181. unset($vars['tag_name'], $vars['text'], $vars['_msg']);
  182. $this->assertEqual(elgg_format_element($tag_name, $attrs, $text, $opts), $expected, $message);
  183. }
  184. try {
  185. elgg_format_element(array());
  186. $this->fail('Failed to throw exception');
  187. } catch (InvalidArgumentException $e) {
  188. $this->pass();
  189. }
  190. }
  191. /**
  192. * Test elgg_register_js()
  193. */
  194. public function testElggRegisterJS() {
  195. global $CONFIG;
  196. // specify name
  197. $result = elgg_register_js('key', 'http://test1.com', 'footer');
  198. $this->assertTrue($result);
  199. $this->assertTrue(isset($CONFIG->externals_map['js']['key']));
  200. $item = $CONFIG->externals_map['js']['key'];
  201. $this->assertTrue($CONFIG->externals['js']->contains($item));
  202. $priority = $CONFIG->externals['js']->getPriority($item);
  203. $this->assertTrue($priority !== false);
  204. $item = $CONFIG->externals['js']->getElement($priority);
  205. $this->assertIdentical('http://test1.com', $item->url);
  206. // send a bad url
  207. $result = elgg_register_js('bad', null);
  208. $this->assertFalse($result);
  209. }
  210. /**
  211. * Test elgg_register_css()
  212. */
  213. public function testElggRegisterCSS() {
  214. global $CONFIG;
  215. // specify name
  216. $result = elgg_register_css('key', 'http://test1.com');
  217. $this->assertTrue($result);
  218. $this->assertTrue(isset($CONFIG->externals_map['css']['key']));
  219. $item = $CONFIG->externals_map['css']['key'];
  220. $this->assertTrue($CONFIG->externals['css']->contains($item));
  221. $priority = $CONFIG->externals['css']->getPriority($item);
  222. $this->assertTrue($priority !== false);
  223. $item = $CONFIG->externals['css']->getElement($priority);
  224. $this->assertIdentical('http://test1.com', $item->url);
  225. }
  226. /**
  227. * Test elgg_unregister_js()
  228. */
  229. public function testElggUnregisterJS() {
  230. global $CONFIG;
  231. $base = trim(elgg_get_site_url(), "/");
  232. $urls = array('id1' => "$base/urla", 'id2' => "$base/urlb", 'id3' => "$base/urlc");
  233. foreach ($urls as $id => $url) {
  234. elgg_register_js($id, $url);
  235. }
  236. $result = elgg_unregister_js('id1');
  237. $this->assertTrue($result);
  238. $js = $CONFIG->externals['js'];
  239. $elements = $js->getElements();
  240. $this->assertFalse(isset($CONFIG->externals_map['js']['id1']));
  241. foreach ($elements as $element) {
  242. if (isset($element->name)) {
  243. $this->assertFalse($element->name == 'id1');
  244. }
  245. }
  246. $result = elgg_unregister_js('id1');
  247. $this->assertFalse($result);
  248. $result = elgg_unregister_js('', 'does_not_exist');
  249. $this->assertFalse($result);
  250. $result = elgg_unregister_js('id2');
  251. $elements = $js->getElements();
  252. $this->assertFalse(isset($CONFIG->externals_map['js']['id2']));
  253. foreach ($elements as $element) {
  254. if (isset($element->name)) {
  255. $this->assertFalse($element->name == 'id2');
  256. }
  257. }
  258. $this->assertTrue(isset($CONFIG->externals_map['js']['id3']));
  259. $priority = $CONFIG->externals['js']->getPriority($CONFIG->externals_map['js']['id3']);
  260. $this->assertTrue($priority !== false);
  261. $item = $CONFIG->externals['js']->getElement($priority);
  262. $this->assertIdentical($urls['id3'], $item->url);
  263. }
  264. /**
  265. * Test elgg_load_js()
  266. */
  267. public function testElggLoadJS() {
  268. global $CONFIG;
  269. // load before register
  270. elgg_load_js('key');
  271. $result = elgg_register_js('key', 'http://test1.com', 'footer');
  272. $this->assertTrue($result);
  273. $js_urls = elgg_get_loaded_js('footer');
  274. $this->assertIdentical(array(500 => 'http://test1.com'), $js_urls);
  275. }
  276. /**
  277. * Test elgg_get_loaded_js()
  278. */
  279. public function testElggGetJS() {
  280. global $CONFIG;
  281. $base = trim(elgg_get_site_url(), "/");
  282. $urls = array(
  283. 'id1' => "$base/urla",
  284. 'id2' => "$base/urlb",
  285. 'id3' => "$base/urlc"
  286. );
  287. foreach ($urls as $id => $url) {
  288. elgg_register_js($id, $url);
  289. elgg_load_js($id);
  290. }
  291. $js_urls = elgg_get_loaded_js('head');
  292. $this->assertIdentical($js_urls[500], $urls['id1']);
  293. $this->assertIdentical($js_urls[501], $urls['id2']);
  294. $this->assertIdentical($js_urls[502], $urls['id3']);
  295. $js_urls = elgg_get_loaded_js('footer');
  296. $this->assertIdentical(array(), $js_urls);
  297. }
  298. /**
  299. * Test elgg_get_friendly_time()
  300. */
  301. public function testElggGetFriendlyTime() {
  302. $current_time = time();
  303. $offsets = array(
  304. '0' => elgg_echo('friendlytime:justnow'),
  305. '-120' => elgg_echo('friendlytime:minutes', array('2')),
  306. '-60' => elgg_echo('friendlytime:minutes:singular'),
  307. '-10800' => elgg_echo('friendlytime:hours', array('3')),
  308. '-86400' => elgg_echo('friendlytime:days:singular'),
  309. '120' => elgg_echo('friendlytime:future:minutes', array('2')),
  310. '86400' => elgg_echo('friendlytime:future:days:singular'),
  311. );
  312. foreach ($offsets as $num_seconds => $friendlytime) {
  313. $this->assertIdentical(elgg_get_friendly_time($current_time + $num_seconds, $current_time), $friendlytime);
  314. }
  315. }
  316. }