2016-01-05 11:52:17 +01:00
< ? php
2016-01-06 17:30:39 +01:00
use Codeception\Util\Fixtures ;
2016-01-29 09:54:14 +01:00
use Grav\Common\Grav ;
2019-04-11 15:30:12 +02:00
use Grav\Common\Uri ;
2016-01-05 11:52:17 +01:00
use Grav\Common\Utils ;
2016-01-29 09:54:14 +01:00
/**
* Class UtilsTest
*/
2016-01-05 11:52:17 +01:00
class UtilsTest extends \Codeception\TestCase\Test
{
2016-01-29 09:54:14 +01:00
/** @var Grav $grav */
protected $grav ;
2019-04-11 15:30:12 +02:00
/** @var Uri $uri */
protected $uri ;
2021-02-01 10:14:53 +01:00
protected function _before () : void
2016-01-05 11:52:17 +01:00
{
2016-02-04 14:12:27 +01:00
$grav = Fixtures :: get ( 'grav' );
$this -> grav = $grav ();
2019-04-11 15:30:12 +02:00
$this -> uri = $this -> grav [ 'uri' ];
2016-01-05 11:52:17 +01:00
}
2021-02-01 10:14:53 +01:00
protected function _after () : void
2016-01-05 11:52:17 +01:00
{
}
2021-02-01 10:14:53 +01:00
public function testStartsWith () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: startsWith ( 'english' , 'en' ));
self :: assertTrue ( Utils :: startsWith ( 'English' , 'En' ));
self :: assertTrue ( Utils :: startsWith ( 'ENGLISH' , 'EN' ));
self :: assertTrue ( Utils :: startsWith (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'EN'
));
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
self :: assertFalse ( Utils :: startsWith ( 'english' , 'En' ));
self :: assertFalse ( Utils :: startsWith ( 'English' , 'EN' ));
self :: assertFalse ( Utils :: startsWith ( 'ENGLISH' , 'en' ));
self :: assertFalse ( Utils :: startsWith (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'e'
));
2018-11-28 00:39:30 +01:00
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: startsWith ( 'english' , 'En' , false ));
self :: assertTrue ( Utils :: startsWith ( 'English' , 'EN' , false ));
self :: assertTrue ( Utils :: startsWith ( 'ENGLISH' , 'en' , false ));
self :: assertTrue ( Utils :: startsWith (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'e' ,
false
));
2016-01-06 16:33:58 +01:00
}
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
public function testEndsWith () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: endsWith ( 'english' , 'sh' ));
self :: assertTrue ( Utils :: endsWith ( 'EngliSh' , 'Sh' ));
self :: assertTrue ( Utils :: endsWith ( 'ENGLISH' , 'SH' ));
self :: assertTrue ( Utils :: endsWith (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'ENGLISH'
));
2016-01-06 16:33:58 +01:00
2021-02-01 10:14:53 +01:00
self :: assertFalse ( Utils :: endsWith ( 'english' , 'de' ));
self :: assertFalse ( Utils :: endsWith ( 'EngliSh' , 'sh' ));
self :: assertFalse ( Utils :: endsWith ( 'ENGLISH' , 'Sh' ));
self :: assertFalse ( Utils :: endsWith (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'DEUSTCH'
));
2018-11-28 00:39:30 +01:00
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: endsWith ( 'english' , 'SH' , false ));
self :: assertTrue ( Utils :: endsWith ( 'EngliSh' , 'sH' , false ));
self :: assertTrue ( Utils :: endsWith ( 'ENGLISH' , 'sh' , false ));
self :: assertTrue ( Utils :: endsWith (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'english' ,
false
));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testContains () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: contains ( 'english' , 'nglis' ));
self :: assertTrue ( Utils :: contains ( 'EngliSh' , 'gliSh' ));
self :: assertTrue ( Utils :: contains ( 'ENGLISH' , 'ENGLI' ));
self :: assertTrue ( Utils :: contains (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'ENGLISH'
));
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
self :: assertFalse ( Utils :: contains ( 'EngliSh' , 'GLI' ));
self :: assertFalse ( Utils :: contains ( 'EngliSh' , 'English' ));
self :: assertFalse ( Utils :: contains ( 'ENGLISH' , 'SCH' ));
self :: assertFalse ( Utils :: contains (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'DEUSTCH'
));
2018-11-28 00:39:30 +01:00
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: contains ( 'EngliSh' , 'GLI' , false ));
self :: assertTrue ( Utils :: contains ( 'EngliSh' , 'ENGLISH' , false ));
self :: assertTrue ( Utils :: contains ( 'ENGLISH' , 'ish' , false ));
self :: assertTrue ( Utils :: contains (
2019-10-14 09:57:26 +02:00
'ENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISHENGLISH' ,
'english' ,
false
));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testSubstrToString () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'en' , Utils :: substrToString ( 'english' , 'glish' ));
self :: assertEquals ( 'english' , Utils :: substrToString ( 'english' , 'test' ));
self :: assertNotEquals ( 'en' , Utils :: substrToString ( 'english' , 'lish' ));
2018-11-28 00:39:30 +01:00
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'en' , Utils :: substrToString ( 'english' , 'GLISH' , false ));
self :: assertEquals ( 'english' , Utils :: substrToString ( 'english' , 'TEST' , false ));
self :: assertNotEquals ( 'en' , Utils :: substrToString ( 'english' , 'LISH' , false ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testMergeObjects () : void
2016-01-06 16:33:58 +01:00
{
$obj1 = new stdClass ();
$obj1 -> test1 = 'x' ;
$obj2 = new stdClass ();
$obj2 -> test2 = 'y' ;
2016-01-10 19:10:21 +01:00
2016-01-06 16:33:58 +01:00
$objMerged = Utils :: mergeObjects ( $obj1 , $obj2 );
2016-01-10 19:10:21 +01:00
2023-02-19 21:48:13 +01:00
self :: arrayHasKey ( 'test1' , ( array ) $objMerged );
self :: arrayHasKey ( 'test2' , ( array ) $objMerged );
2016-01-06 16:33:58 +01:00
}
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
public function testDateFormats () : void
2016-01-06 16:33:58 +01:00
{
2016-01-06 17:30:39 +01:00
$dateFormats = Utils :: dateFormats ();
2021-02-01 10:14:53 +01:00
self :: assertIsArray ( $dateFormats );
self :: assertContainsOnly ( 'string' , $dateFormats );
2016-01-10 19:10:21 +01:00
2016-01-29 09:08:57 +01:00
$default_format = $this -> grav [ 'config' ] -> get ( 'system.pages.dateformat.default' );
2016-01-10 19:10:21 +01:00
2016-01-06 17:30:39 +01:00
if ( $default_format !== null ) {
2021-02-01 10:14:53 +01:00
self :: assertArrayHasKey ( $default_format , $dateFormats );
2016-01-10 19:10:21 +01:00
}
}
2016-01-06 16:33:58 +01:00
2021-02-01 10:14:53 +01:00
public function testTruncate () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'engli' . '…' , Utils :: truncate ( 'english' , 5 ));
self :: assertEquals ( 'english' , Utils :: truncate ( 'english' ));
self :: assertEquals ( 'This is a string to truncate' , Utils :: truncate ( 'This is a string to truncate' ));
self :: assertEquals ( 'Th' . '…' , Utils :: truncate ( 'This is a string to truncate' , 2 ));
self :: assertEquals ( 'engli' . '...' , Utils :: truncate ( 'english' , 5 , true , " " , " ... " ));
self :: assertEquals ( 'english' , Utils :: truncate ( 'english' ));
self :: assertEquals ( 'This is a string to truncate' , Utils :: truncate ( 'This is a string to truncate' ));
self :: assertEquals ( 'This' . '…' , Utils :: truncate ( 'This is a string to truncate' , 3 , true ));
self :: assertEquals ( '<input' . '…' , Utils :: truncate ( '<input type="file" id="file" multiple />' , 6 , true ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testSafeTruncate () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'This' . '…' , Utils :: safeTruncate ( 'This is a string to truncate' , 1 ));
self :: assertEquals ( 'This' . '…' , Utils :: safeTruncate ( 'This is a string to truncate' , 4 ));
self :: assertEquals ( 'This is' . '…' , Utils :: safeTruncate ( 'This is a string to truncate' , 5 ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testTruncateHtml () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'T...' , Utils :: truncateHtml ( 'This is a string to truncate' , 1 ));
self :: assertEquals ( 'This is...' , Utils :: truncateHtml ( 'This is a string to truncate' , 7 ));
self :: assertEquals ( '<p>T...</p>' , Utils :: truncateHtml ( '<p>This is a string to truncate</p>' , 1 ));
self :: assertEquals ( '<p>This...</p>' , Utils :: truncateHtml ( '<p>This is a string to truncate</p>' , 4 ));
self :: assertEquals ( '<p>This is a...</p>' , Utils :: truncateHtml ( '<p>This is a string to truncate</p>' , 10 ));
self :: assertEquals ( '<p>This is a string to truncate</p>' , Utils :: truncateHtml ( '<p>This is a string to truncate</p>' , 100 ));
self :: assertEquals ( '<input type="file" id="file" multiple />' , Utils :: truncateHtml ( '<input type="file" id="file" multiple />' , 6 ));
self :: 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 ));
self :: 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 ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testSafeTruncateHtml () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'This...' , Utils :: safeTruncateHtml ( 'This is a string to truncate' , 1 ));
self :: assertEquals ( 'This is a...' , Utils :: safeTruncateHtml ( 'This is a string to truncate' , 3 ));
self :: assertEquals ( '<p>This...</p>' , Utils :: safeTruncateHtml ( '<p>This is a string to truncate</p>' , 1 ));
self :: assertEquals ( '<p>This is...</p>' , Utils :: safeTruncateHtml ( '<p>This is a string to truncate</p>' , 2 ));
self :: assertEquals ( '<p>This is a string to...</p>' , Utils :: safeTruncateHtml ( '<p>This is a string to truncate</p>' , 5 ));
self :: assertEquals ( '<p>This is a string to truncate</p>' , Utils :: safeTruncateHtml ( '<p>This is a string to truncate</p>' , 20 ));
self :: assertEquals ( '<input type="file" id="file" multiple />' , Utils :: safeTruncateHtml ( '<input type="file" id="file" multiple />' , 6 ));
self :: 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 ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testGenerateRandomString () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertNotEquals ( Utils :: generateRandomString (), Utils :: generateRandomString ());
self :: assertNotEquals ( Utils :: generateRandomString ( 20 ), Utils :: generateRandomString ( 20 ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function download () : void
2016-01-06 16:33:58 +01:00
{
2016-01-05 11:52:17 +01:00
}
2021-02-01 10:14:53 +01:00
public function testGetMimeByExtension () : void
2016-10-02 01:16:35 +02:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'application/octet-stream' , Utils :: getMimeByExtension ( '' ));
self :: assertEquals ( 'text/html' , Utils :: getMimeByExtension ( 'html' ));
self :: assertEquals ( 'application/json' , Utils :: getMimeByExtension ( 'json' ));
self :: assertEquals ( 'application/atom+xml' , Utils :: getMimeByExtension ( 'atom' ));
self :: assertEquals ( 'application/rss+xml' , Utils :: getMimeByExtension ( 'rss' ));
self :: assertEquals ( 'image/jpeg' , Utils :: getMimeByExtension ( 'jpg' ));
self :: assertEquals ( 'image/png' , Utils :: getMimeByExtension ( 'png' ));
self :: assertEquals ( 'text/plain' , Utils :: getMimeByExtension ( 'txt' ));
self :: assertEquals ( 'application/msword' , Utils :: getMimeByExtension ( 'doc' ));
self :: assertEquals ( 'application/octet-stream' , Utils :: getMimeByExtension ( 'foo' ));
self :: assertEquals ( 'foo/bar' , Utils :: getMimeByExtension ( 'foo' , 'foo/bar' ));
self :: assertEquals ( 'text/html' , Utils :: getMimeByExtension ( 'foo' , 'text/html' ));
2016-10-02 01:16:35 +02:00
}
2021-02-01 10:14:53 +01:00
public function testGetExtensionByMime () : void
2016-10-02 01:16:35 +02:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'html' , Utils :: getExtensionByMime ( '*/*' ));
self :: assertEquals ( 'html' , Utils :: getExtensionByMime ( 'text/*' ));
self :: assertEquals ( 'html' , Utils :: getExtensionByMime ( 'text/html' ));
self :: assertEquals ( 'json' , Utils :: getExtensionByMime ( 'application/json' ));
self :: assertEquals ( 'atom' , Utils :: getExtensionByMime ( 'application/atom+xml' ));
self :: assertEquals ( 'rss' , Utils :: getExtensionByMime ( 'application/rss+xml' ));
self :: assertEquals ( 'jpg' , Utils :: getExtensionByMime ( 'image/jpeg' ));
self :: assertEquals ( 'png' , Utils :: getExtensionByMime ( 'image/png' ));
self :: assertEquals ( 'txt' , Utils :: getExtensionByMime ( 'text/plain' ));
self :: assertEquals ( 'doc' , Utils :: getExtensionByMime ( 'application/msword' ));
self :: assertEquals ( 'html' , Utils :: getExtensionByMime ( 'foo/bar' ));
self :: assertEquals ( 'baz' , Utils :: getExtensionByMime ( 'foo/bar' , 'baz' ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testNormalizePath () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertEquals ( '/test' , Utils :: normalizePath ( '/test' ));
self :: assertEquals ( 'test' , Utils :: normalizePath ( 'test' ));
self :: assertEquals ( 'test' , Utils :: normalizePath ( '../test' ));
self :: assertEquals ( '/test' , Utils :: normalizePath ( '/../test' ));
self :: assertEquals ( '/test2' , Utils :: normalizePath ( '/test/../test2' ));
self :: assertEquals ( '/test3' , Utils :: normalizePath ( '/test/../test2/../test3' ));
2019-04-13 20:21:54 +02:00
2021-02-01 10:14:53 +01:00
self :: 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' ));
self :: assertEquals ( '//use.fontawesome.com/releases/v5.8.1/css/all.css' , Utils :: normalizePath ( '//use.fontawesome.com/releases/v5.8.1/css/all.css' ));
self :: 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' ));
2019-04-13 20:21:54 +02:00
2021-02-01 10:14:53 +01:00
self :: 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' ));
self :: 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' ));
self :: 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' ));
2019-04-13 20:21:54 +02:00
2021-02-01 10:14:53 +01:00
self :: 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' ));
self :: 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' ));
self :: 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' ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testIsFunctionDisabled () : void
2016-01-06 16:33:58 +01:00
{
2016-01-07 13:58:47 +01:00
$disabledFunctions = explode ( ',' , ini_get ( 'disable_functions' ));
2016-01-10 19:10:21 +01:00
2016-01-07 13:58:47 +01:00
if ( $disabledFunctions [ 0 ]) {
2021-02-01 10:14:53 +01:00
self :: assertEquals ( Utils :: isFunctionDisabled ( $disabledFunctions [ 0 ]), true );
2016-01-07 13:58:47 +01:00
}
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testTimezones () : void
2016-01-06 16:33:58 +01:00
{
2016-01-07 13:58:47 +01:00
$timezones = Utils :: timezones ();
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
self :: assertIsArray ( $timezones );
self :: assertContainsOnly ( 'string' , $timezones );
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testArrayFilterRecursive () : void
2016-01-06 16:33:58 +01:00
{
2016-01-07 13:58:47 +01:00
$array = [
2016-01-29 09:54:14 +01:00
'test' => '' ,
2016-01-07 13:58:47 +01:00
'test2' => 'test2'
];
2016-01-29 09:54:14 +01:00
$array = Utils :: arrayFilterRecursive ( $array , function ( $k , $v ) {
return ! ( is_null ( $v ) || $v === '' );
});
2016-01-06 16:33:58 +01:00
2021-02-01 10:14:53 +01:00
self :: assertContainsOnly ( 'string' , $array );
self :: assertArrayNotHasKey ( 'test' , $array );
self :: assertArrayHasKey ( 'test2' , $array );
self :: assertEquals ( 'test2' , $array [ 'test2' ]);
2016-01-06 16:33:58 +01:00
}
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
public function testPathPrefixedByLangCode () : void
2016-01-06 16:33:58 +01:00
{
2016-01-29 09:08:57 +01:00
$languagesEnabled = $this -> grav [ 'config' ] -> get ( 'system.languages.supported' , []);
2016-01-07 14:33:18 +01:00
$arrayOfLanguages = [ 'en' , 'de' , 'it' , 'es' , 'dk' , 'el' ];
2016-01-10 19:10:21 +01:00
$languagesNotEnabled = array_diff ( $arrayOfLanguages , $languagesEnabled );
2016-01-07 14:33:18 +01:00
$oneLanguageNotEnabled = reset ( $languagesNotEnabled );
2021-01-31 01:14:33 +01:00
2016-01-10 19:10:32 +01:00
if ( count ( $languagesEnabled )) {
2021-01-31 01:14:33 +01:00
$languageCodePathPrefix = Utils :: pathPrefixedByLangCode ( '/' . $languagesEnabled [ 0 ] . '/test' );
$this -> assertIsString ( $languageCodePathPrefix );
$this -> assertTrue ( in_array ( $languageCodePathPrefix , $languagesEnabled ));
2016-01-10 19:10:32 +01:00
}
2021-02-01 10:14:53 +01:00
self :: assertFalse ( Utils :: pathPrefixedByLangCode ( '/' . $oneLanguageNotEnabled . '/test' ));
self :: assertFalse ( Utils :: pathPrefixedByLangCode ( '/test' ));
self :: assertFalse ( Utils :: pathPrefixedByLangCode ( '/xx' ));
self :: assertFalse ( Utils :: pathPrefixedByLangCode ( '/xx/' ));
self :: assertFalse ( Utils :: pathPrefixedByLangCode ( '/' ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testDate2timestamp () : void
2016-01-06 16:33:58 +01:00
{
2016-01-07 17:38:25 +01:00
$timestamp = strtotime ( '10 September 2000' );
2021-02-01 10:14:53 +01:00
self :: assertSame ( $timestamp , Utils :: date2timestamp ( '10 September 2000' ));
self :: assertSame ( $timestamp , Utils :: date2timestamp ( '2000-09-10 00:00:00' ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testResolve () : void
2016-01-06 16:33:58 +01:00
{
2016-01-07 17:38:25 +01:00
$array = [
'test' => [
'test2' => 'test2Value'
]
];
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'test2Value' , Utils :: resolve ( $array , 'test.test2' ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testGetDotNotation () : void
2016-05-25 19:05:52 +02:00
{
$array = [
'test' => [
'test2' => 'test2Value' ,
'test3' => [
'test4' => 'test4Value'
]
]
];
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'test2Value' , Utils :: getDotNotation ( $array , 'test.test2' ));
self :: assertEquals ( 'test4Value' , Utils :: getDotNotation ( $array , 'test.test3.test4' ));
self :: assertEquals ( 'defaultValue' , Utils :: getDotNotation ( $array , 'test.non_existent' , 'defaultValue' ));
2016-05-25 19:05:52 +02:00
}
2021-02-01 10:14:53 +01:00
public function testSetDotNotation () : void
2016-05-25 19:12:56 +02:00
{
$array = [
'test' => [
'test2' => 'test2Value' ,
'test3' => [
'test4' => 'test4Value'
]
]
];
$new = [
'test1' => 'test1Value'
];
2019-10-14 09:57:26 +02:00
Utils :: setDotNotation ( $array , 'test.test3.test4' , $new );
2021-02-01 10:14:53 +01:00
self :: assertEquals ( 'test1Value' , $array [ 'test' ][ 'test3' ][ 'test4' ][ 'test1' ]);
2016-05-25 19:12:56 +02:00
}
2021-02-01 10:14:53 +01:00
public function testIsPositive () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: isPositive ( true ));
self :: assertTrue ( Utils :: isPositive ( 1 ));
self :: assertTrue ( Utils :: isPositive ( '1' ));
self :: assertTrue ( Utils :: isPositive ( 'yes' ));
self :: assertTrue ( Utils :: isPositive ( 'on' ));
self :: assertTrue ( Utils :: isPositive ( 'true' ));
self :: assertFalse ( Utils :: isPositive ( false ));
self :: assertFalse ( Utils :: isPositive ( 0 ));
self :: assertFalse ( Utils :: isPositive ( '0' ));
self :: assertFalse ( Utils :: isPositive ( 'no' ));
self :: assertFalse ( Utils :: isPositive ( 'off' ));
self :: assertFalse ( Utils :: isPositive ( 'false' ));
self :: assertFalse ( Utils :: isPositive ( 'some' ));
self :: assertFalse ( Utils :: isPositive ( 2 ));
2016-01-06 16:33:58 +01:00
}
2021-02-01 10:14:53 +01:00
public function testGetNonce () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertIsString ( Utils :: getNonce ( 'test-action' ));
self :: assertIsString ( Utils :: getNonce ( 'test-action' , true ));
self :: assertSame ( Utils :: getNonce ( 'test-action' ), Utils :: getNonce ( 'test-action' ));
self :: assertNotSame ( Utils :: getNonce ( 'test-action' ), Utils :: getNonce ( 'test-action2' ));
2016-01-06 16:33:58 +01:00
}
2016-01-10 19:10:21 +01:00
2021-02-01 10:14:53 +01:00
public function testVerifyNonce () : void
2016-01-06 16:33:58 +01:00
{
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: verifyNonce ( Utils :: getNonce ( 'test-action' ), 'test-action' ));
2016-01-06 16:33:58 +01:00
}
2019-04-11 15:30:12 +02:00
2021-03-17 20:54:26 +01:00
public function testGetPagePathFromToken () : void
{
self :: assertEquals ( '' , Utils :: getPagePathFromToken ( '' ));
self :: assertEquals ( '/test/path' , Utils :: getPagePathFromToken ( '/test/path' ));
}
2021-02-01 10:14:53 +01:00
public function testUrl () : void
2019-04-11 15:30:12 +02:00
{
$this -> uri -> initializeWithUrl ( 'http://testing.dev/path1/path2' ) -> init ();
2019-05-27 10:58:31 +02:00
// Fail hard
2021-02-01 10:14:53 +01:00
self :: assertSame ( false , Utils :: url ( '' , true ));
self :: assertSame ( false , Utils :: url ( '' ));
self :: assertSame ( false , Utils :: url ( new stdClass ()));
self :: assertSame ( false , Utils :: url ([ 'foo' , 'bar' , 'baz' ]));
self :: assertSame ( false , Utils :: url ( 'user://does/not/exist' ));
2019-05-27 10:58:31 +02:00
// Fail Gracefully
2021-02-01 10:14:53 +01:00
self :: assertSame ( '/' , Utils :: url ( '/' , false , true ));
self :: assertSame ( '/' , Utils :: url ( '' , false , true ));
self :: assertSame ( '/' , Utils :: url ( new stdClass (), false , true ));
self :: assertSame ( '/' , Utils :: url ([ 'foo' , 'bar' , 'baz' ], false , true ));
self :: assertSame ( '/user/does/not/exist' , Utils :: url ( 'user://does/not/exist' , false , true ));
2019-05-27 10:58:31 +02:00
2019-06-28 12:36:37 +02:00
// Simple paths
2021-02-01 10:14:53 +01:00
self :: assertSame ( '/' , Utils :: url ( '/' ));
self :: assertSame ( '/path1' , Utils :: url ( '/path1' ));
self :: assertSame ( '/path1/path2' , Utils :: url ( '/path1/path2' ));
self :: assertSame ( '/random/path1/path2' , Utils :: url ( '/random/path1/path2' ));
self :: assertSame ( '/foobar.jpg' , Utils :: url ( '/foobar.jpg' ));
self :: assertSame ( '/path1/foobar.jpg' , Utils :: url ( '/path1/foobar.jpg' ));
self :: assertSame ( '/path1/path2/foobar.jpg' , Utils :: url ( '/path1/path2/foobar.jpg' ));
self :: assertSame ( '/random/path1/path2/foobar.jpg' , Utils :: url ( '/random/path1/path2/foobar.jpg' ));
2019-06-28 12:36:37 +02:00
// Simple paths with domain
2021-02-01 10:14:53 +01:00
self :: assertSame ( 'http://testing.dev/' , Utils :: url ( '/' , true ));
self :: assertSame ( 'http://testing.dev/path1' , Utils :: url ( '/path1' , true ));
self :: assertSame ( 'http://testing.dev/path1/path2' , Utils :: url ( '/path1/path2' , true ));
self :: assertSame ( 'http://testing.dev/random/path1/path2' , Utils :: url ( '/random/path1/path2' , true ));
self :: assertSame ( 'http://testing.dev/foobar.jpg' , Utils :: url ( '/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/path1/foobar.jpg' , Utils :: url ( '/path1/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/path1/path2/foobar.jpg' , Utils :: url ( '/path1/path2/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/random/path1/path2/foobar.jpg' , Utils :: url ( '/random/path1/path2/foobar.jpg' , true ));
2019-06-28 12:36:37 +02:00
// Relative paths from Grav root.
2021-02-01 10:14:53 +01:00
self :: assertSame ( '/subdir' , Utils :: url ( 'subdir' ));
self :: assertSame ( '/subdir/path1' , Utils :: url ( 'subdir/path1' ));
self :: assertSame ( '/subdir/path1/path2' , Utils :: url ( 'subdir/path1/path2' ));
self :: assertSame ( '/path1' , Utils :: url ( 'path1' ));
self :: assertSame ( '/path1/path2' , Utils :: url ( 'path1/path2' ));
self :: assertSame ( '/foobar.jpg' , Utils :: url ( 'foobar.jpg' ));
self :: assertSame ( 'http://testing.dev/foobar.jpg' , Utils :: url ( 'foobar.jpg' , true ));
2019-06-28 12:36:37 +02:00
// Relative paths from Grav root with domain.
2021-02-01 10:14:53 +01:00
self :: assertSame ( 'http://testing.dev/foobar.jpg' , Utils :: url ( 'foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/foobar.jpg' , Utils :: url ( '/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/path1/foobar.jpg' , Utils :: url ( '/path1/foobar.jpg' , true ));
2019-05-27 10:58:31 +02:00
2019-06-28 12:36:37 +02:00
// All Non-existing streams should be treated as external URI / protocol.
2021-02-01 10:14:53 +01:00
self :: assertSame ( 'http://domain.com/path' , Utils :: url ( 'http://domain.com/path' ));
self :: assertSame ( 'ftp://domain.com/path' , Utils :: url ( 'ftp://domain.com/path' ));
self :: assertSame ( 'sftp://domain.com/path' , Utils :: url ( 'sftp://domain.com/path' ));
self :: assertSame ( 'ssh://domain.com' , Utils :: url ( 'ssh://domain.com' ));
self :: assertSame ( 'pop://domain.com' , Utils :: url ( 'pop://domain.com' ));
self :: assertSame ( 'foo://bar/baz' , Utils :: url ( 'foo://bar/baz' ));
self :: assertSame ( 'foo://bar/baz' , Utils :: url ( 'foo://bar/baz' , true ));
2024-04-20 16:42:42 +02:00
self :: assertSame ( 'mailto:joe@domain.com' , Utils :: url ( 'mailto:joe@domain.com' , true )); // FIXME <-
2019-04-11 15:30:12 +02:00
}
2021-02-01 10:14:53 +01:00
public function testUrlWithRoot () : void
2019-04-11 15:30:12 +02:00
{
$this -> uri -> initializeWithUrlAndRootPath ( 'http://testing.dev/subdir/path1/path2' , '/subdir' ) -> init ();
2019-05-27 10:58:31 +02:00
// Fail hard
2021-02-01 10:14:53 +01:00
self :: assertSame ( false , Utils :: url ( '' , true ));
self :: assertSame ( false , Utils :: url ( '' ));
self :: assertSame ( false , Utils :: url ( new stdClass ()));
self :: assertSame ( false , Utils :: url ([ 'foo' , 'bar' , 'baz' ]));
self :: assertSame ( false , Utils :: url ( 'user://does/not/exist' ));
2019-05-27 10:58:31 +02:00
// Fail Gracefully
2021-02-01 10:14:53 +01:00
self :: assertSame ( '/subdir/' , Utils :: url ( '/' , false , true ));
self :: assertSame ( '/subdir/' , Utils :: url ( '' , false , true ));
self :: assertSame ( '/subdir/' , Utils :: url ( new stdClass (), false , true ));
self :: assertSame ( '/subdir/' , Utils :: url ([ 'foo' , 'bar' , 'baz' ], false , true ));
self :: assertSame ( '/subdir/user/does/not/exist' , Utils :: url ( 'user://does/not/exist' , false , true ));
2019-05-27 10:58:31 +02:00
2019-06-28 12:36:37 +02:00
// Simple paths
2021-02-01 10:14:53 +01:00
self :: assertSame ( '/subdir/' , Utils :: url ( '/' ));
self :: assertSame ( '/subdir/path1' , Utils :: url ( '/path1' ));
self :: assertSame ( '/subdir/path1/path2' , Utils :: url ( '/path1/path2' ));
self :: assertSame ( '/subdir/random/path1/path2' , Utils :: url ( '/random/path1/path2' ));
self :: assertSame ( '/subdir/foobar.jpg' , Utils :: url ( '/foobar.jpg' ));
self :: assertSame ( '/subdir/path1/foobar.jpg' , Utils :: url ( '/path1/foobar.jpg' ));
self :: assertSame ( '/subdir/path1/path2/foobar.jpg' , Utils :: url ( '/path1/path2/foobar.jpg' ));
self :: assertSame ( '/subdir/random/path1/path2/foobar.jpg' , Utils :: url ( '/random/path1/path2/foobar.jpg' ));
2019-04-11 15:30:12 +02:00
2019-06-28 12:36:37 +02:00
// Simple paths with domain
2021-02-01 10:14:53 +01:00
self :: assertSame ( 'http://testing.dev/subdir/' , Utils :: url ( '/' , true ));
self :: assertSame ( 'http://testing.dev/subdir/path1' , Utils :: url ( '/path1' , true ));
self :: assertSame ( 'http://testing.dev/subdir/path1/path2' , Utils :: url ( '/path1/path2' , true ));
self :: assertSame ( 'http://testing.dev/subdir/random/path1/path2' , Utils :: url ( '/random/path1/path2' , true ));
self :: assertSame ( 'http://testing.dev/subdir/foobar.jpg' , Utils :: url ( '/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/subdir/path1/foobar.jpg' , Utils :: url ( '/path1/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/subdir/path1/path2/foobar.jpg' , Utils :: url ( '/path1/path2/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/subdir/random/path1/path2/foobar.jpg' , Utils :: url ( '/random/path1/path2/foobar.jpg' , true ));
2019-06-28 12:36:37 +02:00
2022-03-03 20:35:17 +01:00
// Absolute Paths including the grav base.
2022-03-04 23:37:03 +01:00
self :: assertSame ( '/subdir/' , Utils :: url ( '/subdir' ));
self :: assertSame ( '/subdir/' , Utils :: url ( '/subdir/' ));
self :: assertSame ( '/subdir/path1' , Utils :: url ( '/subdir/path1' ));
self :: assertSame ( '/subdir/path1/path2' , Utils :: url ( '/subdir/path1/path2' ));
self :: assertSame ( '/subdir/foobar.jpg' , Utils :: url ( '/subdir/foobar.jpg' ));
self :: assertSame ( '/subdir/path1/foobar.jpg' , Utils :: url ( '/subdir/path1/foobar.jpg' ));
2022-03-03 20:35:17 +01:00
// Absolute paths from Grav root with domain.
2022-03-04 23:37:03 +01:00
self :: assertSame ( 'http://testing.dev/subdir/' , Utils :: url ( '/subdir' , true ));
self :: assertSame ( 'http://testing.dev/subdir/' , Utils :: url ( '/subdir/' , true ));
self :: assertSame ( 'http://testing.dev/subdir/path1' , Utils :: url ( '/subdir/path1' , true ));
self :: assertSame ( 'http://testing.dev/subdir/path1/path2' , Utils :: url ( '/subdir/path1/path2' , true ));
self :: assertSame ( 'http://testing.dev/subdir/foobar.jpg' , Utils :: url ( '/subdir/foobar.jpg' , true ));
self :: assertSame ( 'http://testing.dev/subdir/path1/foobar.jpg' , Utils :: url ( '/subdir/path1/foobar.jpg' , true ));
2019-06-28 12:36:37 +02:00
// Relative paths from Grav root.
2022-03-05 17:22:12 +01:00
self :: assertSame ( '/subdir/sub' , Utils :: url ( '/sub' ));
2021-02-01 10:14:53 +01:00
self :: assertSame ( '/subdir/subdir' , Utils :: url ( 'subdir' ));
2022-03-05 17:22:12 +01:00
self :: assertSame ( '/subdir/subdir2/sub' , Utils :: url ( '/subdir2/sub' ));
2021-02-01 10:14:53 +01:00
self :: assertSame ( '/subdir/subdir/path1' , Utils :: url ( 'subdir/path1' ));
self :: assertSame ( '/subdir/subdir/path1/path2' , Utils :: url ( 'subdir/path1/path2' ));
self :: assertSame ( '/subdir/path1' , Utils :: url ( 'path1' ));
self :: assertSame ( '/subdir/path1/path2' , Utils :: url ( 'path1/path2' ));
self :: assertSame ( '/subdir/foobar.jpg' , Utils :: url ( 'foobar.jpg' ));
self :: assertSame ( 'http://testing.dev/subdir/foobar.jpg' , Utils :: url ( 'foobar.jpg' , true ));
2019-06-28 12:36:37 +02:00
// All Non-existing streams should be treated as external URI / protocol.
2021-02-01 10:14:53 +01:00
self :: assertSame ( 'http://domain.com/path' , Utils :: url ( 'http://domain.com/path' ));
self :: assertSame ( 'ftp://domain.com/path' , Utils :: url ( 'ftp://domain.com/path' ));
self :: assertSame ( 'sftp://domain.com/path' , Utils :: url ( 'sftp://domain.com/path' ));
self :: assertSame ( 'ssh://domain.com' , Utils :: url ( 'ssh://domain.com' ));
self :: assertSame ( 'pop://domain.com' , Utils :: url ( 'pop://domain.com' ));
self :: assertSame ( 'foo://bar/baz' , Utils :: url ( 'foo://bar/baz' ));
self :: assertSame ( 'foo://bar/baz' , Utils :: url ( 'foo://bar/baz' , true ));
// self::assertSame('mailto:joe@domain.com', Utils::url('mailto:joe@domain.com', true)); // FIXME <-
2019-04-11 15:30:12 +02:00
}
2021-02-01 10:14:53 +01:00
public function testUrlWithStreams () : void
2019-04-11 15:30:12 +02:00
{
}
2021-02-01 10:14:53 +01:00
public function testUrlwithExternals () : void
2019-04-11 15:30:12 +02:00
{
$this -> uri -> initializeWithUrl ( 'http://testing.dev/path1/path2' ) -> init ();
2021-02-01 10:14:53 +01:00
self :: assertSame ( 'http://foo.com' , Utils :: url ( 'http://foo.com' ));
self :: assertSame ( 'https://foo.com' , Utils :: url ( 'https://foo.com' ));
self :: assertSame ( '//foo.com' , Utils :: url ( '//foo.com' ));
self :: assertSame ( '//foo.com?param=x' , Utils :: url ( '//foo.com?param=x' ));
2019-04-11 15:30:12 +02:00
}
2020-11-11 18:30:57 +01:00
2021-02-01 10:14:53 +01:00
public function testCheckFilename () : void
2020-11-11 18:30:57 +01:00
{
// configure extension for consistent results
/** @var \Grav\Common\Config\Config $config */
$config = $this -> grav [ 'config' ];
$config -> set ( 'security.uploads_dangerous_extensions' , [ 'php' , 'html' , 'htm' , 'exe' , 'js' ]);
2021-02-01 10:14:53 +01:00
self :: assertFalse ( Utils :: checkFilename ( 'foo.php' ));
2023-10-24 11:20:22 +02:00
self :: assertFalse ( Utils :: checkFilename ( 'foo.PHP' ));
2021-02-01 10:14:53 +01:00
self :: assertFalse ( Utils :: checkFilename ( 'bar.js' ));
2020-11-11 18:30:57 +01:00
2021-02-01 10:14:53 +01:00
self :: assertTrue ( Utils :: checkFilename ( 'foo.json' ));
self :: assertTrue ( Utils :: checkFilename ( 'foo.xml' ));
self :: assertTrue ( Utils :: checkFilename ( 'foo.yaml' ));
self :: assertTrue ( Utils :: checkFilename ( 'foo.yml' ));
2020-11-11 18:30:57 +01:00
}
2016-05-25 19:05:52 +02:00
}