SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/sproutTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\Sprout;
  15.  
  16.  
  17. /**
  18. * Test suite
  19. **/
  20. class sproutTest extends PHPUnit_Framework_TestCase
  21. {
  22.  
  23. public function testIndexDotPhp()
  24. {
  25. $this->assertTrue(constant('IN_PRODUCTION') !== null);
  26. $this->assertTrue(constant('DOCROOT') !== null);
  27. $this->assertTrue(constant('APPPATH') !== null);
  28. }
  29.  
  30. public function testCheckRedirect()
  31. {
  32. $this->assertTrue(Sprout::checkRedirect("http://www.google.com.au"));
  33. $this->assertTrue(Sprout::checkRedirect("http://maps.google.com.au"));
  34. $this->assertTrue(Sprout::checkRedirect("/about_us"));
  35.  
  36. $this->assertFalse(Sprout::checkRedirect("//"));
  37. $this->assertFalse(Sprout::checkRedirect("http://"));
  38. $this->assertFalse(Sprout::checkRedirect("maps.google.com.au"));
  39.  
  40. $this->assertFalse(Sprout::checkRedirect(array()));
  41. }
  42.  
  43. public function testAbsRoot()
  44. {
  45. $this->assertTrue(preg_match('!http://!', Sprout::absRoot()) !== false);
  46. $this->assertTrue(preg_match('!http://!', Sprout::absRoot('http')) !== false);
  47. $this->assertTrue(preg_match('!https://!', Sprout::absRoot('https')) !== false);
  48.  
  49. $result = Sprout::absRoot();
  50. $this->assertContains('http://', $result);
  51. $this->assertNotContains('http:///', $result);
  52. $this->assertContains(Kohana::config('config.site_domain'), $result);
  53. $this->assertNotFalse(preg_match('!/$!', $result));
  54. }
  55.  
  56. public function testRandStr()
  57. {
  58. $rand = Sprout::randStr();
  59. $this->assertTrue(strlen($rand) == 16);
  60.  
  61. $rand = Sprout::randStr(10);
  62. $this->assertTrue(strlen($rand) == 10);
  63.  
  64. $rand = Sprout::randStr(30, 'ab');
  65. $this->assertEquals(30, strlen($rand));
  66. $this->assertContains('a', $rand);
  67. $this->assertContains('b', $rand);
  68. $this->assertNotContains('c', $rand);
  69. }
  70.  
  71. public function testTimeAgo()
  72. {
  73. $this->assertTrue(Sprout::timeAgo(0.35) == 'Just now');
  74. $this->assertTrue(Sprout::timeAgo(1.999) == 'Just now');
  75. $this->assertTrue(Sprout::timeAgo(0) == 'Just now');
  76. $this->assertTrue(Sprout::timeAgo(1) == 'Just now');
  77. $this->assertTrue(Sprout::timeAgo(2) == '2 seconds ago');
  78. $this->assertTrue(Sprout::timeAgo(59) == '59 seconds ago');
  79. $this->assertTrue(Sprout::timeAgo(60) == '1 minute ago');
  80. $this->assertTrue(Sprout::timeAgo(61) == '1 minute ago');
  81. $this->assertTrue(Sprout::timeAgo(60 + 59) == '1 minute ago');
  82. $this->assertTrue(Sprout::timeAgo(60 * 2) == '2 minutes ago');
  83. $this->assertTrue(Sprout::timeAgo(60 * 3) == '3 minutes ago');
  84. $this->assertTrue(Sprout::timeAgo(60 * 60 - 1) == '59 minutes ago');
  85. $this->assertTrue(Sprout::timeAgo(60 * 60) == '1 hour ago');
  86. $this->assertTrue(Sprout::timeAgo(60 * 60 + 1) == '1 hour ago');
  87. $this->assertTrue(Sprout::timeAgo(60 * 60 * 2) == '2 hours ago');
  88. $this->assertTrue(Sprout::timeAgo(60 * 60 * 3) == '3 hours ago');
  89. $this->assertTrue(Sprout::timeAgo(60 * 60 * 23) == '23 hours ago');
  90. $this->assertTrue(Sprout::timeAgo(60 * 60 * 23 + 1) == '23 hours ago');
  91. $this->assertTrue(Sprout::timeAgo(60 * 60 * 24 - 1) == '23 hours ago');
  92. $this->assertTrue(Sprout::timeAgo(60 * 60 * 24) == '1 day ago');
  93. $this->assertTrue(Sprout::timeAgo(60 * 60 * 24 + 1) == '1 day ago');
  94. $this->assertTrue(Sprout::timeAgo(60 * 60 * 24 * 2) == '2 days ago');
  95.  
  96. $this->assertTrue(Sprout::timeAgo(array()) == 'Just now');
  97. }
  98.  
  99.  
  100. /**
  101.   * Special file links which should be updated
  102.   **/
  103. public function dataSpecialFileLinksWorking()
  104. {
  105. return array(
  106. array('<p><a href="files/531_ted303_kis_strategy_overview_fa.pdf" title="A Strategy">A Strategy</a></p>'),
  107. array('<p><a href="files/531.pdf">A Strategy</a></p>'),
  108. array('<p><a href="files/hey_ya.pdf">A Strategy</a></p>'),
  109. array('<p><a href="files/hey ya.pdf">A Strategy</a></p>'),
  110. array('<p><a href="/files/hey_ya.pdf">A Strategy</a></p>'),
  111. array('<p><a href="files/hey_ya.pdf">A Strategy</a></p> <p><a href="files/hey_ya.pdf">A Strategy</a></p>'),
  112. array('<p><a href="files/hey ya.doc">A Strategy</a></p>'),
  113. array('<p><a href="files/hey ya.doc" class="button">A Strategy</a></p>'),
  114. array('<p><a title="whee" href="files/531.pdf">A Strategy</a></p>'),
  115. );
  116. }
  117.  
  118. /**
  119.   * Special file links which should be updated
  120.   * @dataProvider dataSpecialFileLinksWorking
  121.   **/
  122. public function testSpecialFileLinksWorking($html)
  123. {
  124. $html = Sprout::specialFileLinks($html);
  125. $this->assertContains('class="document', $html);
  126. $this->assertContains('target="_blank"', $html);
  127. $this->assertContains('onclick="ga(', $html);
  128. }
  129.  
  130. /**
  131.   * Remote file links which shouldn't be updated
  132.   **/
  133. public function testRemoteSpecialFileLinksWorking()
  134. {
  135. $html = Sprout::specialFileLinks('<p><a href="http://www.southaustralia.biz/files/something_remote.pdf">Remote files R grate</a></p>');
  136. $this->assertContains('href="http://www.southaustralia.biz/files', $html);
  137. }
  138.  
  139. /**
  140.   * Special file links which have a class in the original HTML
  141.   **/
  142. public function testSpecialFileLinksClass()
  143. {
  144. $html = Sprout::specialFileLinks('<p><a href="files/hey ya.doc" class="button">A Strategy</a></p>');
  145. $this->assertContains('class="document document-doc button"', $html);
  146. $this->assertContains('target="_blank"', $html);
  147. $this->assertContains('onclick="ga(', $html);
  148. $this->assertContains('>A Strategy<', $html);
  149. }
  150.  
  151. /**
  152.   * File name containing quote to be properly escaped in JS
  153.   **/
  154. public function testSpecialFileLinksJSQuote()
  155. {
  156. $html = Sprout::specialFileLinks('<p><a href="files/hey\'ya.doc">A Strategy</a></p>');
  157. $this->assertContains('onclick="ga(', $html);
  158. $this->assertContains("'hey\'ya.doc'", $html);
  159. }
  160.  
  161. /**
  162.   * File name or link content containing HTML entities
  163.   **/
  164. public function testSpecialFileLinksHTMLEntities()
  165. {
  166. $html = Sprout::specialFileLinks('<p><a href="files/hey&amp;ya.doc" class="test&amp;test">A &amp; Strategy</a></p>');
  167. $this->assertContains('hey&amp;ya.doc', $html);
  168. $this->assertContains('A &amp; Strategy', $html);
  169. $this->assertContains('class="document document-doc test&amp;test"', $html);
  170. }
  171.  
  172. /**
  173.   * Special file links which shouldn't be updated
  174.   **/
  175. public function dataSpecialFileLinksNotWorking()
  176. {
  177. return array(
  178. array('<p><a href="files/blah.pdf">A Strategy<br>Of things</a></p>'),
  179. array('<p><a href="files/blah.pdf"><img src="aaa"></a></p>'),
  180. array('<p><a href="files/blah.pdf">aaa <img src="aaa"> aaa</a></p>'),
  181. array('<p><a href="library/blah.pdf">aaa</a></p>'),
  182. array('<p><a href="blah.pdf">aaa</a></p>'),
  183. array('<p>aaaa</p>'),
  184. array('<p>files/blah.pdf</p>'),
  185. array('<p>blah.pdf</p>'),
  186. );
  187. }
  188.  
  189. /**
  190.   * Special file links which shouldn't be updated
  191.   * @dataProvider dataSpecialFileLinksNotWorking
  192.   **/
  193. public function testSpecialFileLinksNotWorking($html)
  194. {
  195. $html = Sprout::specialFileLinks($html);
  196. $this->assertFalse(strpos($html, 'class="document'));
  197. }
  198.  
  199.  
  200. public function testIpaddressPlain()
  201. {
  202. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.1')));
  203. $this->assertFalse(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.2')));
  204. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.2', '192.168.1.1')));
  205. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.1', '192.168.1.2')));
  206. $this->assertFalse(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.2', '192.168.1.3')));
  207. }
  208.  
  209. public function testIpaddressCIDR()
  210. {
  211. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.1/32')));
  212. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.0/31')));
  213. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.0/24')));
  214. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.168.0.0/16')));
  215. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('192.0.0.0/8')));
  216. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('128.0.0.0/1')));
  217. $this->assertTrue(Sprout::ipaddressInArray('192.168.1.1', array('0.0.0.0/0')));
  218.  
  219. $this->assertFalse(Sprout::ipaddressInArray('192.168.1.1', array('192.168.1.2/32')));
  220. $this->assertFalse(Sprout::ipaddressInArray('192.168.1.1', array('192.168.2.0/24')));
  221. $this->assertFalse(Sprout::ipaddressInArray('192.168.1.1', array('192.262.1.1/16')));
  222. $this->assertFalse(Sprout::ipaddressInArray('192.168.1.1', array('0.0.0.0/1')));
  223. }
  224.  
  225. public function testInstanceSuccess()
  226. {
  227. $this->assertInstanceOf('Sprout\Helpers\Pdb', Sprout::instance('Sprout\Helpers\Pdb'));
  228. }
  229.  
  230. /**
  231.   * @expectedException InvalidArgumentException
  232.   **/
  233. public function testInstanceAbstractClass()
  234. {
  235. Sprout::instance('Sprout\Helpers\WorkerBase');
  236. }
  237.  
  238. /**
  239.   * @expectedException InvalidArgumentException
  240.   **/
  241. public function testInstanceMissingClass()
  242. {
  243. Sprout::instance('Sprout\Helpers\MissingClass');
  244. }
  245.  
  246.  
  247. public function dataInstanceNotImplements()
  248. {
  249. return [
  250. ['Sprout\Helpers\Enc', 'Sprout\Controllers\Controller'],
  251. ['Sprout\Helpers\Enc', ['Sprout\Controllers\Controller']],
  252. ['Sprout\Helpers\Enc', 'Sprout\Helpers\FrontEndEntrance'],
  253. ['Sprout\Helpers\Enc', ['Sprout\Helpers\FrontEndEntrance']],
  254. ['Sprout\Helpers\Enc', ['Sprout\Controllers\Controller', 'Sprout\Helpers\FrontEndEntrance']],
  255. ];
  256. }
  257.  
  258. /**
  259.   * @dataProvider dataInstanceNotImplements
  260.   * @expectedException InvalidArgumentException
  261.   **/
  262. public function testInstanceNotImplements($class, $base_class)
  263. {
  264. Sprout::instance($class, $base_class);
  265. }
  266.  
  267. public function testIterableFirst()
  268. {
  269. $this->assertEquals(Sprout::iterableFirst(['a' => 'b']), ['a', 'b']);
  270. $this->assertEquals(Sprout::iterableFirst([0 => 'b']), [0, 'b']);
  271. $this->assertEquals(Sprout::iterableFirst([1 => 2]), [1, 2]);
  272. $this->assertEquals(Sprout::iterableFirst(['a' => 'b', 'c' => 'd', 'e' => 'h']), ['a', 'b']);
  273. $this->assertEquals(Sprout::iterableFirst([]), null);
  274. }
  275.  
  276. public function testIterableFirstKey()
  277. {
  278. $this->assertEquals(Sprout::iterableFirstKey(['a' => 'b']), 'a');
  279. $this->assertEquals(Sprout::iterableFirstKey([2 => 'test']), 2);
  280. $this->assertEquals(Sprout::iterableFirstKey(['a' => 'b', 'c' => 'd', 'e' => 'h']), 'a');
  281. $this->assertEquals(Sprout::iterableFirstKey([]), null);
  282. }
  283.  
  284. public function testIterableFirstValue()
  285. {
  286. $this->assertEquals(Sprout::iterableFirstValue(['a' => 'b']), 'b');
  287. $this->assertEquals(Sprout::iterableFirstValue([2 => 'test']), 'test');
  288. $this->assertEquals(Sprout::iterableFirstValue(['a' => 'b', 'c' => 'd', 'e' => 'h']), 'b');
  289. $this->assertEquals(Sprout::iterableFirstValue([]), null);
  290. }
  291. }
  292.  
  293.