UtilsTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Utils;
  5. /**
  6. * Class UtilsTest
  7. */
  8. class UtilsTest extends \Codeception\TestCase\Test
  9. {
  10. /** @var Grav $grav */
  11. protected $grav;
  12. protected function _before()
  13. {
  14. $grav = Fixtures::get('grav');
  15. $this->grav = $grav();
  16. }
  17. protected function _after()
  18. {
  19. }
  20. public function testStartsWith()
  21. {
  22. $this->assertTrue(Utils::startsWith('english', 'en'));
  23. $this->assertTrue(Utils::startsWith('English', 'En'));
  24. $this->assertTrue(Utils::startsWith('ENGLISH', 'EN'));
  25. $this->assertTrue(Utils::startsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  26. 'EN'));
  27. $this->assertFalse(Utils::startsWith('english', 'En'));
  28. $this->assertFalse(Utils::startsWith('English', 'EN'));
  29. $this->assertFalse(Utils::startsWith('ENGLISH', 'en'));
  30. $this->assertFalse(Utils::startsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  31. 'e'));
  32. }
  33. public function testEndsWith()
  34. {
  35. $this->assertTrue(Utils::endsWith('english', 'sh'));
  36. $this->assertTrue(Utils::endsWith('EngliSh', 'Sh'));
  37. $this->assertTrue(Utils::endsWith('ENGLISH', 'SH'));
  38. $this->assertTrue(Utils::endsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  39. 'ENGLISH'));
  40. $this->assertFalse(Utils::endsWith('english', 'de'));
  41. $this->assertFalse(Utils::endsWith('EngliSh', 'sh'));
  42. $this->assertFalse(Utils::endsWith('ENGLISH', 'Sh'));
  43. $this->assertFalse(Utils::endsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  44. 'DEUSTCH'));
  45. }
  46. public function testContains()
  47. {
  48. $this->assertTrue(Utils::contains('english', 'nglis'));
  49. $this->assertTrue(Utils::contains('EngliSh', 'gliSh'));
  50. $this->assertTrue(Utils::contains('ENGLISH', 'ENGLI'));
  51. $this->assertTrue(Utils::contains('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  52. 'ENGLISH'));
  53. $this->assertFalse(Utils::contains('EngliSh', 'GLI'));
  54. $this->assertFalse(Utils::contains('EngliSh', 'English'));
  55. $this->assertFalse(Utils::contains('ENGLISH', 'SCH'));
  56. $this->assertFalse(Utils::contains('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  57. 'DEUSTCH'));
  58. }
  59. public function testSubstrToString()
  60. {
  61. $this->assertEquals('en', Utils::substrToString('english', 'glish'));
  62. $this->assertEquals('english', Utils::substrToString('english', 'test'));
  63. $this->assertNotEquals('en', Utils::substrToString('english', 'lish'));
  64. }
  65. public function testMergeObjects()
  66. {
  67. $obj1 = new stdClass();
  68. $obj1->test1 = 'x';
  69. $obj2 = new stdClass();
  70. $obj2->test2 = 'y';
  71. $objMerged = Utils::mergeObjects($obj1, $obj2);
  72. $this->assertObjectHasAttribute('test1', $objMerged);
  73. $this->assertObjectHasAttribute('test2', $objMerged);
  74. }
  75. public function testDateFormats()
  76. {
  77. $dateFormats = Utils::dateFormats();
  78. $this->assertInternalType('array', $dateFormats);
  79. $this->assertContainsOnly('string', $dateFormats);
  80. $default_format = $this->grav['config']->get('system.pages.dateformat.default');
  81. if ($default_format !== null) {
  82. $this->assertArrayHasKey($default_format, $dateFormats);
  83. }
  84. }
  85. public function testTruncate()
  86. {
  87. $this->assertEquals('engli' . '&hellip;', Utils::truncate('english', 5));
  88. $this->assertEquals('english', Utils::truncate('english'));
  89. $this->assertEquals('This is a string to truncate', Utils::truncate('This is a string to truncate'));
  90. $this->assertEquals('Th' . '&hellip;', Utils::truncate('This is a string to truncate', 2));
  91. $this->assertEquals('engli' . '...', Utils::truncate('english', 5, true, " ", "..."));
  92. $this->assertEquals('english', Utils::truncate('english'));
  93. $this->assertEquals('This is a string to truncate', Utils::truncate('This is a string to truncate'));
  94. $this->assertEquals('This' . '&hellip;', Utils::truncate('This is a string to truncate', 3, true));
  95. $this->assertEquals('<input' . '&hellip;', Utils::truncate('<input type="file" id="file" multiple />', 6, true));
  96. }
  97. public function testSafeTruncate()
  98. {
  99. $this->assertEquals('This' . '&hellip;', Utils::safeTruncate('This is a string to truncate', 1));
  100. $this->assertEquals('This' . '&hellip;', Utils::safeTruncate('This is a string to truncate', 4));
  101. $this->assertEquals('This is' . '&hellip;', Utils::safeTruncate('This is a string to truncate', 5));
  102. }
  103. public function testTruncateHtml()
  104. {
  105. $this->assertEquals('<p>T...</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 1));
  106. $this->assertEquals('<p>This...</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 4));
  107. $this->assertEquals('<p>This is a...</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 10));
  108. $this->assertEquals('<p>This is a string to truncate</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 100));
  109. $this->assertEquals('<input type="file" id="file" multiple />', Utils::truncateHtml('<input type="file" id="file" multiple />', 6));
  110. $this->assertEquals('<ol><li>item 1 <i>so...</i></li></ol>', Utils::truncateHtml('<ol><li>item 1 <i>something</i></li><li>item 2 <strong>bold</strong></li></ol>', 10));
  111. $this->assertEquals("<p>This is a string.</p>\n<p>It splits two lines.</p>", Utils::truncateHtml("<p>This is a string.</p>\n<p>It splits two lines.</p>", 100));
  112. }
  113. public function testSafeTruncateHtml()
  114. {
  115. $this->assertEquals('<p>This...</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 1));
  116. $this->assertEquals('<p>This is...</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 2));
  117. $this->assertEquals('<p>This is a string to...</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 5));
  118. $this->assertEquals('<p>This is a string to truncate</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 20));
  119. $this->assertEquals('<input type="file" id="file" multiple />', Utils::safeTruncateHtml('<input type="file" id="file" multiple />', 6));
  120. $this->assertEquals('<ol><li>item 1 <i>something</i></li><li>item 2...</li></ol>', Utils::safeTruncateHtml('<ol><li>item 1 <i>something</i></li><li>item 2 <strong>bold</strong></li></ol>', 5));
  121. }
  122. public function testGenerateRandomString()
  123. {
  124. $this->assertNotEquals(Utils::generateRandomString(), Utils::generateRandomString());
  125. $this->assertNotEquals(Utils::generateRandomString(20), Utils::generateRandomString(20));
  126. }
  127. public function download()
  128. {
  129. }
  130. public function testGetMimeByExtension()
  131. {
  132. $this->assertEquals('application/octet-stream', Utils::getMimeByExtension(''));
  133. $this->assertEquals('text/html', Utils::getMimeByExtension('html'));
  134. $this->assertEquals('application/json', Utils::getMimeByExtension('json'));
  135. $this->assertEquals('application/atom+xml', Utils::getMimeByExtension('atom'));
  136. $this->assertEquals('application/rss+xml', Utils::getMimeByExtension('rss'));
  137. $this->assertEquals('image/jpeg', Utils::getMimeByExtension('jpg'));
  138. $this->assertEquals('image/png', Utils::getMimeByExtension('png'));
  139. $this->assertEquals('text/plain', Utils::getMimeByExtension('txt'));
  140. $this->assertEquals('application/msword', Utils::getMimeByExtension('doc'));
  141. $this->assertEquals('application/octet-stream', Utils::getMimeByExtension('foo'));
  142. $this->assertEquals('foo/bar', Utils::getMimeByExtension('foo', 'foo/bar'));
  143. $this->assertEquals('text/html', Utils::getMimeByExtension('foo', 'text/html'));
  144. }
  145. public function testGetExtensionByMime()
  146. {
  147. $this->assertEquals('html', Utils::getExtensionByMime('*/*'));
  148. $this->assertEquals('html', Utils::getExtensionByMime('text/*'));
  149. $this->assertEquals('html', Utils::getExtensionByMime('text/html'));
  150. $this->assertEquals('json', Utils::getExtensionByMime('application/json'));
  151. $this->assertEquals('atom', Utils::getExtensionByMime('application/atom+xml'));
  152. $this->assertEquals('rss', Utils::getExtensionByMime('application/rss+xml'));
  153. $this->assertEquals('jpg', Utils::getExtensionByMime('image/jpeg'));
  154. $this->assertEquals('png', Utils::getExtensionByMime('image/png'));
  155. $this->assertEquals('txt', Utils::getExtensionByMime('text/plain'));
  156. $this->assertEquals('doc', Utils::getExtensionByMime('application/msword'));
  157. $this->assertEquals('html', Utils::getExtensionByMime('foo/bar'));
  158. $this->assertEquals('baz', Utils::getExtensionByMime('foo/bar', 'baz'));
  159. }
  160. public function testNormalizePath()
  161. {
  162. $this->assertEquals('/test', Utils::normalizePath('/test'));
  163. $this->assertEquals('test', Utils::normalizePath('test'));
  164. $this->assertEquals('test', Utils::normalizePath('../test'));
  165. $this->assertEquals('/test', Utils::normalizePath('/../test'));
  166. $this->assertEquals('/test2', Utils::normalizePath('/test/../test2'));
  167. $this->assertEquals('/test/test2', Utils::normalizePath('/test/./test2'));
  168. }
  169. public function testIsFunctionDisabled()
  170. {
  171. $disabledFunctions = explode(',', ini_get('disable_functions'));
  172. if ($disabledFunctions[0]) {
  173. $this->assertEquals(Utils::isFunctionDisabled($disabledFunctions[0]), true);
  174. }
  175. }
  176. public function testTimezones()
  177. {
  178. $timezones = Utils::timezones();
  179. $this->assertInternalType('array', $timezones);
  180. $this->assertContainsOnly('string', $timezones);
  181. }
  182. public function testArrayFilterRecursive()
  183. {
  184. $array = [
  185. 'test' => '',
  186. 'test2' => 'test2'
  187. ];
  188. $array = Utils::arrayFilterRecursive($array, function ($k, $v) {
  189. return !(is_null($v) || $v === '');
  190. });
  191. $this->assertContainsOnly('string', $array);
  192. $this->assertArrayNotHasKey('test', $array);
  193. $this->assertArrayHasKey('test2', $array);
  194. $this->assertEquals('test2', $array['test2']);
  195. }
  196. public function testPathPrefixedByLangCode()
  197. {
  198. $languagesEnabled = $this->grav['config']->get('system.languages.supported', []);
  199. $arrayOfLanguages = ['en', 'de', 'it', 'es', 'dk', 'el'];
  200. $languagesNotEnabled = array_diff($arrayOfLanguages, $languagesEnabled);
  201. $oneLanguageNotEnabled = reset($languagesNotEnabled);
  202. if (count($languagesEnabled)) {
  203. $this->assertTrue(Utils::pathPrefixedByLangCode('/' . $languagesEnabled[0] . '/test'));
  204. }
  205. $this->assertFalse(Utils::pathPrefixedByLangCode('/' . $oneLanguageNotEnabled . '/test'));
  206. $this->assertFalse(Utils::pathPrefixedByLangCode('/test'));
  207. $this->assertFalse(Utils::pathPrefixedByLangCode('/xx'));
  208. $this->assertFalse(Utils::pathPrefixedByLangCode('/xx/'));
  209. $this->assertFalse(Utils::pathPrefixedByLangCode('/'));
  210. }
  211. public function testDate2timestamp()
  212. {
  213. $timestamp = strtotime('10 September 2000');
  214. $this->assertSame($timestamp, Utils::date2timestamp('10 September 2000'));
  215. $this->assertSame($timestamp, Utils::date2timestamp('2000-09-10 00:00:00'));
  216. }
  217. public function testResolve()
  218. {
  219. $array = [
  220. 'test' => [
  221. 'test2' => 'test2Value'
  222. ]
  223. ];
  224. $this->assertEquals('test2Value', Utils::resolve($array, 'test.test2'));
  225. }
  226. public function testGetDotNotation()
  227. {
  228. $array = [
  229. 'test' => [
  230. 'test2' => 'test2Value',
  231. 'test3' => [
  232. 'test4' => 'test4Value'
  233. ]
  234. ]
  235. ];
  236. $this->assertEquals('test2Value', Utils::getDotNotation($array, 'test.test2'));
  237. $this->assertEquals('test4Value', Utils::getDotNotation($array, 'test.test3.test4'));
  238. $this->assertEquals('defaultValue', Utils::getDotNotation($array, 'test.non_existent', 'defaultValue'));
  239. }
  240. public function testSetDotNotation()
  241. {
  242. $array = [
  243. 'test' => [
  244. 'test2' => 'test2Value',
  245. 'test3' => [
  246. 'test4' => 'test4Value'
  247. ]
  248. ]
  249. ];
  250. $new = [
  251. 'test1' => 'test1Value'
  252. ];
  253. Utils::setDotNotation($array, 'test.test3.test4' , $new);
  254. $this->assertEquals('test1Value', $array['test']['test3']['test4']['test1']);
  255. }
  256. public function testIsPositive()
  257. {
  258. $this->assertTrue(Utils::isPositive(true));
  259. $this->assertTrue(Utils::isPositive(1));
  260. $this->assertTrue(Utils::isPositive('1'));
  261. $this->assertTrue(Utils::isPositive('yes'));
  262. $this->assertTrue(Utils::isPositive('on'));
  263. $this->assertTrue(Utils::isPositive('true'));
  264. $this->assertFalse(Utils::isPositive(false));
  265. $this->assertFalse(Utils::isPositive(0));
  266. $this->assertFalse(Utils::isPositive('0'));
  267. $this->assertFalse(Utils::isPositive('no'));
  268. $this->assertFalse(Utils::isPositive('off'));
  269. $this->assertFalse(Utils::isPositive('false'));
  270. $this->assertFalse(Utils::isPositive('some'));
  271. $this->assertFalse(Utils::isPositive(2));
  272. }
  273. public function testGetNonce()
  274. {
  275. $this->assertInternalType('string', Utils::getNonce('test-action'));
  276. $this->assertInternalType('string', Utils::getNonce('test-action', true));
  277. $this->assertSame(Utils::getNonce('test-action'), Utils::getNonce('test-action'));
  278. $this->assertNotSame(Utils::getNonce('test-action'), Utils::getNonce('test-action2'));
  279. }
  280. public function testVerifyNonce()
  281. {
  282. $this->assertTrue(Utils::verifyNonce(Utils::getNonce('test-action'), 'test-action'));
  283. }
  284. }