UtilsTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Uri;
  5. use Grav\Common\Utils;
  6. /**
  7. * Class UtilsTest
  8. */
  9. class UtilsTest extends \Codeception\TestCase\Test
  10. {
  11. /** @var Grav $grav */
  12. protected $grav;
  13. /** @var Uri $uri */
  14. protected $uri;
  15. protected function _before()
  16. {
  17. $grav = Fixtures::get('grav');
  18. $this->grav = $grav();
  19. $this->uri = $this->grav['uri'];
  20. }
  21. protected function _after()
  22. {
  23. }
  24. public function testStartsWith()
  25. {
  26. $this->assertTrue(Utils::startsWith('english', 'en'));
  27. $this->assertTrue(Utils::startsWith('English', 'En'));
  28. $this->assertTrue(Utils::startsWith('ENGLISH', 'EN'));
  29. $this->assertTrue(Utils::startsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  30. 'EN'));
  31. $this->assertFalse(Utils::startsWith('english', 'En'));
  32. $this->assertFalse(Utils::startsWith('English', 'EN'));
  33. $this->assertFalse(Utils::startsWith('ENGLISH', 'en'));
  34. $this->assertFalse(Utils::startsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  35. 'e'));
  36. $this->assertTrue(Utils::startsWith('english', 'En', false));
  37. $this->assertTrue(Utils::startsWith('English', 'EN', false));
  38. $this->assertTrue(Utils::startsWith('ENGLISH', 'en', false));
  39. $this->assertTrue(Utils::startsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  40. 'e', false));
  41. }
  42. public function testEndsWith()
  43. {
  44. $this->assertTrue(Utils::endsWith('english', 'sh'));
  45. $this->assertTrue(Utils::endsWith('EngliSh', 'Sh'));
  46. $this->assertTrue(Utils::endsWith('ENGLISH', 'SH'));
  47. $this->assertTrue(Utils::endsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  48. 'ENGLISH'));
  49. $this->assertFalse(Utils::endsWith('english', 'de'));
  50. $this->assertFalse(Utils::endsWith('EngliSh', 'sh'));
  51. $this->assertFalse(Utils::endsWith('ENGLISH', 'Sh'));
  52. $this->assertFalse(Utils::endsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  53. 'DEUSTCH'));
  54. $this->assertTrue(Utils::endsWith('english', 'SH', false));
  55. $this->assertTrue(Utils::endsWith('EngliSh', 'sH', false));
  56. $this->assertTrue(Utils::endsWith('ENGLISH', 'sh', false));
  57. $this->assertTrue(Utils::endsWith('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  58. 'english', false));
  59. }
  60. public function testContains()
  61. {
  62. $this->assertTrue(Utils::contains('english', 'nglis'));
  63. $this->assertTrue(Utils::contains('EngliSh', 'gliSh'));
  64. $this->assertTrue(Utils::contains('ENGLISH', 'ENGLI'));
  65. $this->assertTrue(Utils::contains('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  66. 'ENGLISH'));
  67. $this->assertFalse(Utils::contains('EngliSh', 'GLI'));
  68. $this->assertFalse(Utils::contains('EngliSh', 'English'));
  69. $this->assertFalse(Utils::contains('ENGLISH', 'SCH'));
  70. $this->assertFalse(Utils::contains('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  71. 'DEUSTCH'));
  72. $this->assertTrue(Utils::contains('EngliSh', 'GLI', false));
  73. $this->assertTrue(Utils::contains('EngliSh', 'ENGLISH', false));
  74. $this->assertTrue(Utils::contains('ENGLISH', 'ish', false));
  75. $this->assertTrue(Utils::contains('ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH',
  76. 'english', false));
  77. }
  78. public function testSubstrToString()
  79. {
  80. $this->assertEquals('en', Utils::substrToString('english', 'glish'));
  81. $this->assertEquals('english', Utils::substrToString('english', 'test'));
  82. $this->assertNotEquals('en', Utils::substrToString('english', 'lish'));
  83. $this->assertEquals('en', Utils::substrToString('english', 'GLISH', false));
  84. $this->assertEquals('english', Utils::substrToString('english', 'TEST', false));
  85. $this->assertNotEquals('en', Utils::substrToString('english', 'LISH', false));
  86. }
  87. public function testMergeObjects()
  88. {
  89. $obj1 = new stdClass();
  90. $obj1->test1 = 'x';
  91. $obj2 = new stdClass();
  92. $obj2->test2 = 'y';
  93. $objMerged = Utils::mergeObjects($obj1, $obj2);
  94. $this->assertObjectHasAttribute('test1', $objMerged);
  95. $this->assertObjectHasAttribute('test2', $objMerged);
  96. }
  97. public function testDateFormats()
  98. {
  99. $dateFormats = Utils::dateFormats();
  100. $this->assertInternalType('array', $dateFormats);
  101. $this->assertContainsOnly('string', $dateFormats);
  102. $default_format = $this->grav['config']->get('system.pages.dateformat.default');
  103. if ($default_format !== null) {
  104. $this->assertArrayHasKey($default_format, $dateFormats);
  105. }
  106. }
  107. public function testTruncate()
  108. {
  109. $this->assertEquals('engli' . '&hellip;', Utils::truncate('english', 5));
  110. $this->assertEquals('english', Utils::truncate('english'));
  111. $this->assertEquals('This is a string to truncate', Utils::truncate('This is a string to truncate'));
  112. $this->assertEquals('Th' . '&hellip;', Utils::truncate('This is a string to truncate', 2));
  113. $this->assertEquals('engli' . '...', Utils::truncate('english', 5, true, " ", "..."));
  114. $this->assertEquals('english', Utils::truncate('english'));
  115. $this->assertEquals('This is a string to truncate', Utils::truncate('This is a string to truncate'));
  116. $this->assertEquals('This' . '&hellip;', Utils::truncate('This is a string to truncate', 3, true));
  117. $this->assertEquals('<input' . '&hellip;', Utils::truncate('<input type="file" id="file" multiple />', 6, true));
  118. }
  119. public function testSafeTruncate()
  120. {
  121. $this->assertEquals('This' . '&hellip;', Utils::safeTruncate('This is a string to truncate', 1));
  122. $this->assertEquals('This' . '&hellip;', Utils::safeTruncate('This is a string to truncate', 4));
  123. $this->assertEquals('This is' . '&hellip;', Utils::safeTruncate('This is a string to truncate', 5));
  124. }
  125. public function testTruncateHtml()
  126. {
  127. $this->assertEquals('T...', Utils::truncateHtml('This is a string to truncate', 1));
  128. $this->assertEquals('This is...', Utils::truncateHtml('This is a string to truncate', 7));
  129. $this->assertEquals('<p>T...</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 1));
  130. $this->assertEquals('<p>This...</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 4));
  131. $this->assertEquals('<p>This is a...</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 10));
  132. $this->assertEquals('<p>This is a string to truncate</p>', Utils::truncateHtml('<p>This is a string to truncate</p>', 100));
  133. $this->assertEquals('<input type="file" id="file" multiple />', Utils::truncateHtml('<input type="file" id="file" multiple />', 6));
  134. $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));
  135. $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));
  136. }
  137. public function testSafeTruncateHtml()
  138. {
  139. $this->assertEquals('This...', Utils::safeTruncateHtml('This is a string to truncate', 1));
  140. $this->assertEquals('This is a...', Utils::safeTruncateHtml('This is a string to truncate', 3));
  141. $this->assertEquals('<p>This...</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 1));
  142. $this->assertEquals('<p>This is...</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 2));
  143. $this->assertEquals('<p>This is a string to...</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 5));
  144. $this->assertEquals('<p>This is a string to truncate</p>', Utils::safeTruncateHtml('<p>This is a string to truncate</p>', 20));
  145. $this->assertEquals('<input type="file" id="file" multiple />', Utils::safeTruncateHtml('<input type="file" id="file" multiple />', 6));
  146. $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));
  147. }
  148. public function testGenerateRandomString()
  149. {
  150. $this->assertNotEquals(Utils::generateRandomString(), Utils::generateRandomString());
  151. $this->assertNotEquals(Utils::generateRandomString(20), Utils::generateRandomString(20));
  152. }
  153. public function download()
  154. {
  155. }
  156. public function testGetMimeByExtension()
  157. {
  158. $this->assertEquals('application/octet-stream', Utils::getMimeByExtension(''));
  159. $this->assertEquals('text/html', Utils::getMimeByExtension('html'));
  160. $this->assertEquals('application/json', Utils::getMimeByExtension('json'));
  161. $this->assertEquals('application/atom+xml', Utils::getMimeByExtension('atom'));
  162. $this->assertEquals('application/rss+xml', Utils::getMimeByExtension('rss'));
  163. $this->assertEquals('image/jpeg', Utils::getMimeByExtension('jpg'));
  164. $this->assertEquals('image/png', Utils::getMimeByExtension('png'));
  165. $this->assertEquals('text/plain', Utils::getMimeByExtension('txt'));
  166. $this->assertEquals('application/msword', Utils::getMimeByExtension('doc'));
  167. $this->assertEquals('application/octet-stream', Utils::getMimeByExtension('foo'));
  168. $this->assertEquals('foo/bar', Utils::getMimeByExtension('foo', 'foo/bar'));
  169. $this->assertEquals('text/html', Utils::getMimeByExtension('foo', 'text/html'));
  170. }
  171. public function testGetExtensionByMime()
  172. {
  173. $this->assertEquals('html', Utils::getExtensionByMime('*/*'));
  174. $this->assertEquals('html', Utils::getExtensionByMime('text/*'));
  175. $this->assertEquals('html', Utils::getExtensionByMime('text/html'));
  176. $this->assertEquals('json', Utils::getExtensionByMime('application/json'));
  177. $this->assertEquals('atom', Utils::getExtensionByMime('application/atom+xml'));
  178. $this->assertEquals('rss', Utils::getExtensionByMime('application/rss+xml'));
  179. $this->assertEquals('jpg', Utils::getExtensionByMime('image/jpeg'));
  180. $this->assertEquals('png', Utils::getExtensionByMime('image/png'));
  181. $this->assertEquals('txt', Utils::getExtensionByMime('text/plain'));
  182. $this->assertEquals('doc', Utils::getExtensionByMime('application/msword'));
  183. $this->assertEquals('html', Utils::getExtensionByMime('foo/bar'));
  184. $this->assertEquals('baz', Utils::getExtensionByMime('foo/bar', 'baz'));
  185. }
  186. public function testNormalizePath()
  187. {
  188. $this->assertEquals('/test', Utils::normalizePath('/test'));
  189. $this->assertEquals('test', Utils::normalizePath('test'));
  190. $this->assertEquals('test', Utils::normalizePath('../test'));
  191. $this->assertEquals('/test', Utils::normalizePath('/../test'));
  192. $this->assertEquals('/test2', Utils::normalizePath('/test/../test2'));
  193. $this->assertEquals('/test3', Utils::normalizePath('/test/../test2/../test3'));
  194. $this->assertEquals('//cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css', Utils::normalizePath('//cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css'));
  195. $this->assertEquals('//use.fontawesome.com/releases/v5.8.1/css/all.css', Utils::normalizePath('//use.fontawesome.com/releases/v5.8.1/css/all.css'));
  196. $this->assertEquals('//use.fontawesome.com/releases/v5.8.1/webfonts/fa-brands-400.eot', Utils::normalizePath('//use.fontawesome.com/releases/v5.8.1/css/../webfonts/fa-brands-400.eot'));
  197. $this->assertEquals('http://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css', Utils::normalizePath('http://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css'));
  198. $this->assertEquals('http://use.fontawesome.com/releases/v5.8.1/css/all.css', Utils::normalizePath('http://use.fontawesome.com/releases/v5.8.1/css/all.css'));
  199. $this->assertEquals('http://use.fontawesome.com/releases/v5.8.1/webfonts/fa-brands-400.eot', Utils::normalizePath('http://use.fontawesome.com/releases/v5.8.1/css/../webfonts/fa-brands-400.eot'));
  200. $this->assertEquals('https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css', Utils::normalizePath('https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css'));
  201. $this->assertEquals('https://use.fontawesome.com/releases/v5.8.1/css/all.css', Utils::normalizePath('https://use.fontawesome.com/releases/v5.8.1/css/all.css'));
  202. $this->assertEquals('https://use.fontawesome.com/releases/v5.8.1/webfonts/fa-brands-400.eot', Utils::normalizePath('https://use.fontawesome.com/releases/v5.8.1/css/../webfonts/fa-brands-400.eot'));
  203. }
  204. public function testIsFunctionDisabled()
  205. {
  206. $disabledFunctions = explode(',', ini_get('disable_functions'));
  207. if ($disabledFunctions[0]) {
  208. $this->assertEquals(Utils::isFunctionDisabled($disabledFunctions[0]), true);
  209. }
  210. }
  211. public function testTimezones()
  212. {
  213. $timezones = Utils::timezones();
  214. $this->assertInternalType('array', $timezones);
  215. $this->assertContainsOnly('string', $timezones);
  216. }
  217. public function testArrayFilterRecursive()
  218. {
  219. $array = [
  220. 'test' => '',
  221. 'test2' => 'test2'
  222. ];
  223. $array = Utils::arrayFilterRecursive($array, function ($k, $v) {
  224. return !(is_null($v) || $v === '');
  225. });
  226. $this->assertContainsOnly('string', $array);
  227. $this->assertArrayNotHasKey('test', $array);
  228. $this->assertArrayHasKey('test2', $array);
  229. $this->assertEquals('test2', $array['test2']);
  230. }
  231. public function testPathPrefixedByLangCode()
  232. {
  233. $languagesEnabled = $this->grav['config']->get('system.languages.supported', []);
  234. $arrayOfLanguages = ['en', 'de', 'it', 'es', 'dk', 'el'];
  235. $languagesNotEnabled = array_diff($arrayOfLanguages, $languagesEnabled);
  236. $oneLanguageNotEnabled = reset($languagesNotEnabled);
  237. if (count($languagesEnabled)) {
  238. $this->assertTrue(Utils::pathPrefixedByLangCode('/' . $languagesEnabled[0] . '/test'));
  239. }
  240. $this->assertFalse(Utils::pathPrefixedByLangCode('/' . $oneLanguageNotEnabled . '/test'));
  241. $this->assertFalse(Utils::pathPrefixedByLangCode('/test'));
  242. $this->assertFalse(Utils::pathPrefixedByLangCode('/xx'));
  243. $this->assertFalse(Utils::pathPrefixedByLangCode('/xx/'));
  244. $this->assertFalse(Utils::pathPrefixedByLangCode('/'));
  245. }
  246. public function testDate2timestamp()
  247. {
  248. $timestamp = strtotime('10 September 2000');
  249. $this->assertSame($timestamp, Utils::date2timestamp('10 September 2000'));
  250. $this->assertSame($timestamp, Utils::date2timestamp('2000-09-10 00:00:00'));
  251. }
  252. public function testResolve()
  253. {
  254. $array = [
  255. 'test' => [
  256. 'test2' => 'test2Value'
  257. ]
  258. ];
  259. $this->assertEquals('test2Value', Utils::resolve($array, 'test.test2'));
  260. }
  261. public function testGetDotNotation()
  262. {
  263. $array = [
  264. 'test' => [
  265. 'test2' => 'test2Value',
  266. 'test3' => [
  267. 'test4' => 'test4Value'
  268. ]
  269. ]
  270. ];
  271. $this->assertEquals('test2Value', Utils::getDotNotation($array, 'test.test2'));
  272. $this->assertEquals('test4Value', Utils::getDotNotation($array, 'test.test3.test4'));
  273. $this->assertEquals('defaultValue', Utils::getDotNotation($array, 'test.non_existent', 'defaultValue'));
  274. }
  275. public function testSetDotNotation()
  276. {
  277. $array = [
  278. 'test' => [
  279. 'test2' => 'test2Value',
  280. 'test3' => [
  281. 'test4' => 'test4Value'
  282. ]
  283. ]
  284. ];
  285. $new = [
  286. 'test1' => 'test1Value'
  287. ];
  288. Utils::setDotNotation($array, 'test.test3.test4' , $new);
  289. $this->assertEquals('test1Value', $array['test']['test3']['test4']['test1']);
  290. }
  291. public function testIsPositive()
  292. {
  293. $this->assertTrue(Utils::isPositive(true));
  294. $this->assertTrue(Utils::isPositive(1));
  295. $this->assertTrue(Utils::isPositive('1'));
  296. $this->assertTrue(Utils::isPositive('yes'));
  297. $this->assertTrue(Utils::isPositive('on'));
  298. $this->assertTrue(Utils::isPositive('true'));
  299. $this->assertFalse(Utils::isPositive(false));
  300. $this->assertFalse(Utils::isPositive(0));
  301. $this->assertFalse(Utils::isPositive('0'));
  302. $this->assertFalse(Utils::isPositive('no'));
  303. $this->assertFalse(Utils::isPositive('off'));
  304. $this->assertFalse(Utils::isPositive('false'));
  305. $this->assertFalse(Utils::isPositive('some'));
  306. $this->assertFalse(Utils::isPositive(2));
  307. }
  308. public function testGetNonce()
  309. {
  310. $this->assertInternalType('string', Utils::getNonce('test-action'));
  311. $this->assertInternalType('string', Utils::getNonce('test-action', true));
  312. $this->assertSame(Utils::getNonce('test-action'), Utils::getNonce('test-action'));
  313. $this->assertNotSame(Utils::getNonce('test-action'), Utils::getNonce('test-action2'));
  314. }
  315. public function testVerifyNonce()
  316. {
  317. $this->assertTrue(Utils::verifyNonce(Utils::getNonce('test-action'), 'test-action'));
  318. }
  319. public function testUrl()
  320. {
  321. $this->uri->initializeWithUrl('http://testing.dev/path1/path2')->init();
  322. // Fail hard
  323. $this->assertSame(false, Utils::url('', true));
  324. $this->assertSame(false, Utils::url(''));
  325. $this->assertSame(false, Utils::url(new stdClass()));
  326. $this->assertSame(false, Utils::url(['foo','bar','baz']));
  327. $this->assertSame(false, Utils::url('user://does/not/exist'));
  328. // Fail Gracefully
  329. $this->assertSame('/', Utils::url('/', false, true));
  330. $this->assertSame('/', Utils::url('', false, true));
  331. $this->assertSame('/', Utils::url(new stdClass(), false, true));
  332. $this->assertSame('/', Utils::url(['foo','bar','baz'], false, true));
  333. $this->assertSame('/user/does/not/exist', Utils::url('user://does/not/exist', false, true));
  334. // Simple paths
  335. $this->assertSame('/', Utils::url('/'));
  336. $this->assertSame('/path1', Utils::url('/path1'));
  337. $this->assertSame('/path1/path2', Utils::url('/path1/path2'));
  338. $this->assertSame('/random/path1/path2', Utils::url('/random/path1/path2'));
  339. $this->assertSame('/foobar.jpg', Utils::url('/foobar.jpg'));
  340. $this->assertSame('/path1/foobar.jpg', Utils::url('/path1/foobar.jpg'));
  341. $this->assertSame('/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg'));
  342. $this->assertSame('/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg'));
  343. // Simple paths with domain
  344. $this->assertSame('http://testing.dev/', Utils::url('/', true));
  345. $this->assertSame('http://testing.dev/path1', Utils::url('/path1', true));
  346. $this->assertSame('http://testing.dev/path1/path2', Utils::url('/path1/path2', true));
  347. $this->assertSame('http://testing.dev/random/path1/path2', Utils::url('/random/path1/path2', true));
  348. $this->assertSame('http://testing.dev/foobar.jpg', Utils::url('/foobar.jpg', true));
  349. $this->assertSame('http://testing.dev/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true));
  350. $this->assertSame('http://testing.dev/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg', true));
  351. $this->assertSame('http://testing.dev/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg', true));
  352. // Relative paths from Grav root.
  353. $this->assertSame('/subdir', Utils::url('subdir'));
  354. $this->assertSame('/subdir/path1', Utils::url('subdir/path1'));
  355. $this->assertSame('/subdir/path1/path2', Utils::url('subdir/path1/path2'));
  356. $this->assertSame('/path1', Utils::url('path1'));
  357. $this->assertSame('/path1/path2', Utils::url('path1/path2'));
  358. $this->assertSame('/foobar.jpg', Utils::url('foobar.jpg'));
  359. $this->assertSame('http://testing.dev/foobar.jpg', Utils::url('foobar.jpg', true));
  360. // Relative paths from Grav root with domain.
  361. $this->assertSame('http://testing.dev/foobar.jpg', Utils::url('foobar.jpg', true));
  362. $this->assertSame('http://testing.dev/foobar.jpg', Utils::url('/foobar.jpg', true));
  363. $this->assertSame('http://testing.dev/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true));
  364. // All Non-existing streams should be treated as external URI / protocol.
  365. $this->assertSame('http://domain.com/path', Utils::url('http://domain.com/path'));
  366. $this->assertSame('ftp://domain.com/path', Utils::url('ftp://domain.com/path'));
  367. $this->assertSame('sftp://domain.com/path', Utils::url('sftp://domain.com/path'));
  368. $this->assertSame('ssh://domain.com', Utils::url('ssh://domain.com'));
  369. $this->assertSame('pop://domain.com', Utils::url('pop://domain.com'));
  370. $this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz'));
  371. $this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', true));
  372. // $this->assertSame('mailto:joe@domain.com', Utils::url('mailto:joe@domain.com', true)); // FIXME <-
  373. }
  374. public function testUrlWithRoot()
  375. {
  376. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/path1/path2', '/subdir')->init();
  377. // Fail hard
  378. $this->assertSame(false, Utils::url('', true));
  379. $this->assertSame(false, Utils::url(''));
  380. $this->assertSame(false, Utils::url(new stdClass()));
  381. $this->assertSame(false, Utils::url(['foo','bar','baz']));
  382. $this->assertSame(false, Utils::url('user://does/not/exist'));
  383. // Fail Gracefully
  384. $this->assertSame('/subdir/', Utils::url('/', false, true));
  385. $this->assertSame('/subdir/', Utils::url('', false, true));
  386. $this->assertSame('/subdir/', Utils::url(new stdClass(), false, true));
  387. $this->assertSame('/subdir/', Utils::url(['foo','bar','baz'], false, true));
  388. $this->assertSame('/subdir/user/does/not/exist', Utils::url('user://does/not/exist', false, true));
  389. // Simple paths
  390. $this->assertSame('/subdir/', Utils::url('/'));
  391. $this->assertSame('/subdir/path1', Utils::url('/path1'));
  392. $this->assertSame('/subdir/path1/path2', Utils::url('/path1/path2'));
  393. $this->assertSame('/subdir/random/path1/path2', Utils::url('/random/path1/path2'));
  394. $this->assertSame('/subdir/foobar.jpg', Utils::url('/foobar.jpg'));
  395. $this->assertSame('/subdir/path1/foobar.jpg', Utils::url('/path1/foobar.jpg'));
  396. $this->assertSame('/subdir/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg'));
  397. $this->assertSame('/subdir/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg'));
  398. // Simple paths with domain
  399. $this->assertSame('http://testing.dev/subdir/', Utils::url('/', true));
  400. $this->assertSame('http://testing.dev/subdir/path1', Utils::url('/path1', true));
  401. $this->assertSame('http://testing.dev/subdir/path1/path2', Utils::url('/path1/path2', true));
  402. $this->assertSame('http://testing.dev/subdir/random/path1/path2', Utils::url('/random/path1/path2', true));
  403. $this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('/foobar.jpg', true));
  404. $this->assertSame('http://testing.dev/subdir/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true));
  405. $this->assertSame('http://testing.dev/subdir/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg', true));
  406. $this->assertSame('http://testing.dev/subdir/random/path1/path2/foobar.jpg', Utils::url('/random/path1/path2/foobar.jpg', true));
  407. // Paths including the grav base.
  408. $this->assertSame('/subdir/', Utils::url('/subdir'));
  409. $this->assertSame('/subdir/path1', Utils::url('/subdir/path1'));
  410. $this->assertSame('/subdir/path1/path2', Utils::url('/subdir/path1/path2'));
  411. $this->assertSame('/subdir/foobar.jpg', Utils::url('/subdir/foobar.jpg'));
  412. $this->assertSame('/subdir/path1/foobar.jpg', Utils::url('/subdir/path1/foobar.jpg'));
  413. // Relative paths from Grav root with domain.
  414. $this->assertSame('http://testing.dev/subdir/', Utils::url('/subdir', true));
  415. $this->assertSame('http://testing.dev/subdir/path1', Utils::url('/subdir/path1', true));
  416. $this->assertSame('http://testing.dev/subdir/path1/path2', Utils::url('/subdir/path1/path2', true));
  417. $this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('/subdir/foobar.jpg', true));
  418. $this->assertSame('http://testing.dev/subdir/path1/foobar.jpg', Utils::url('/subdir/path1/foobar.jpg', true));
  419. // Relative paths from Grav root.
  420. $this->assertSame('/subdir/subdir', Utils::url('subdir'));
  421. $this->assertSame('/subdir/subdir/path1', Utils::url('subdir/path1'));
  422. $this->assertSame('/subdir/subdir/path1/path2', Utils::url('subdir/path1/path2'));
  423. $this->assertSame('/subdir/path1', Utils::url('path1'));
  424. $this->assertSame('/subdir/path1/path2', Utils::url('path1/path2'));
  425. $this->assertSame('/subdir/foobar.jpg', Utils::url('foobar.jpg'));
  426. $this->assertSame('http://testing.dev/subdir/foobar.jpg', Utils::url('foobar.jpg', true));
  427. // All Non-existing streams should be treated as external URI / protocol.
  428. $this->assertSame('http://domain.com/path', Utils::url('http://domain.com/path'));
  429. $this->assertSame('ftp://domain.com/path', Utils::url('ftp://domain.com/path'));
  430. $this->assertSame('sftp://domain.com/path', Utils::url('sftp://domain.com/path'));
  431. $this->assertSame('ssh://domain.com', Utils::url('ssh://domain.com'));
  432. $this->assertSame('pop://domain.com', Utils::url('pop://domain.com'));
  433. $this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz'));
  434. $this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', true));
  435. // $this->assertSame('mailto:joe@domain.com', Utils::url('mailto:joe@domain.com', true)); // FIXME <-
  436. }
  437. public function testUrlWithStreams()
  438. {
  439. }
  440. public function testUrlwithExternals()
  441. {
  442. $this->uri->initializeWithUrl('http://testing.dev/path1/path2')->init();
  443. $this->assertSame('http://foo.com', Utils::url('http://foo.com'));
  444. $this->assertSame('https://foo.com', Utils::url('https://foo.com'));
  445. $this->assertSame('//foo.com', Utils::url('//foo.com'));
  446. $this->assertSame('//foo.com?param=x', Utils::url('//foo.com?param=x'));
  447. }
  448. }