From ab64f0f7bf4f6d8f10dcb74c7d54d60c14888b98 Mon Sep 17 00:00:00 2001 From: camer0n Date: Tue, 28 Nov 2023 12:17:11 -0800 Subject: [PATCH] Issue #5119 setFavicon() method added. --- e107_handlers/js_manager.php | 26 + e107_handlers/plugin_class.php | 2 +- e107_tests/tests/unit/e_jsmanagerTest.php | 757 +++++++++++----------- 3 files changed, 411 insertions(+), 374 deletions(-) diff --git a/e107_handlers/js_manager.php b/e107_handlers/js_manager.php index 34edb3bfb..3d80f6cd1 100644 --- a/e107_handlers/js_manager.php +++ b/e107_handlers/js_manager.php @@ -187,6 +187,8 @@ class e_jsmanager protected $_theme_css_processor = false; + private $_favicon = null; + /** * Constructor * @@ -2246,6 +2248,16 @@ class e_jsmanager return $this; } + /** + * Set a custom favicon (must be a .png file) + * @param string $path to a png file. eg. {e_PLUGIN}gallery/images/icon_32.png + * @return e_jsmanager + */ + public function setFavicon($path) + { + $this->_favicon = $path; + return $this; + } /** * Render favicon HTML code - used in header.php and header_default.php @@ -2257,6 +2269,20 @@ class e_jsmanager $ret = ''; + if(!empty($this->_favicon)) + { + $iconSizes = [16 => 'icon',32 => 'icon',48 => 'icon',192 => 'icon',167 => 'apple-touch-icon',180 => 'apple-touch-icon']; + + foreach($iconSizes as $size => $rel) + { + $sizes = $size.'x'.$size; + $url = e107::getParser()->thumbUrl($this->_favicon, ['w'=>$size, 'h'=>$size, 'crop'=>1]); + $ret .= "\n"; + } + + return $ret; + } + if(file_exists(e_THEME . $sitetheme . "/favicon.ico")) { $ret = "\n\n"; diff --git a/e107_handlers/plugin_class.php b/e107_handlers/plugin_class.php index 9f195de16..e9e16541f 100644 --- a/e107_handlers/plugin_class.php +++ b/e107_handlers/plugin_class.php @@ -378,7 +378,7 @@ class e_plugin if($opt === 'path') { - return e107::getParser()->createConstants(e_PLUGIN_ABS.$this->_plugdir.'/'.$link[$key]); + return e107::getParser()->createConstants(e_PLUGIN_ABS.$this->_plugdir.'/'.$link[$key], 2); } return "\"".$caption."\""; diff --git a/e107_tests/tests/unit/e_jsmanagerTest.php b/e107_tests/tests/unit/e_jsmanagerTest.php index 129640eb2..11114fa3e 100644 --- a/e107_tests/tests/unit/e_jsmanagerTest.php +++ b/e107_tests/tests/unit/e_jsmanagerTest.php @@ -1,409 +1,420 @@ js = $this->make('e_jsmanager'); + } + catch(Exception $e) + { + $this->assertTrue(false, "Couldn't load e_jsmanager object"); + } - try + } + + /* + public function testHeaderPlugin() { - $this->js = $this->make('e_jsmanager'); - } - catch(Exception $e) - { - $this->assertTrue(false, "Couldn't load e_jsmanager object"); + } - } - -/* - public function testHeaderPlugin() - { - - } - - public function testTryHeaderInline() - { - - } -*/ - public function testIsInAdmin() - { - $result = $this->js->isInAdmin(); - $this->assertFalse($result); - - } -/* - public function testRequireCoreLib() - { - - } - - public function testSetInAdmin() - { - - } - - public function testCoreCSS() - { - - } - - public function testResetDependency() - { - - } - - public function testJsSettings() - { - - } - - public function testGetInstance() - { - - } - - public function testFooterFile() - { - - } - - public function testSetData() - { - - } - - public function testLibraryCSS() - { - - } - - public function testTryHeaderFile() - { - - } - - public function testThemeCSS() - { - - } - - public function testOtherCSS() - { - - } - - public function testSetLastModfied() - { - - } - - public function testRenderLinks() - { - - } - - public function testThemeLib() - { - - } - - public function testRenderFile() - { - - } - - public function testHeaderCore() - { - - } - - public function testRenderInline() - { - - } - - public function testFooterTheme() - { - - } - - public function testGetData() - { - - } - - public function testRequirePluginLib() - { - - } - - public function testGetCacheId() - { - - } - - public function testHeaderTheme() - { - - } - - public function testInlineCSS() - { - - } -*/ - public function testHeaderFile() - { - $load = array( - 0 => array( - 'file' => '{e_PLUGIN}forum/js/forum.js', - 'zone' => 5, - 'opts' => [] - ), - 1 => array( - 'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js', - 'zone' => 1, - 'opts' => [] - ), - 2 => array( - 'file' => '{e_WEB}js/bootstrap-notify/js/bootstrap-notify.js', - 'zone' => 2, - 'opts' => [] - - ), - 3 => array( - 'file' => 'https://somewhere/something.min.js', - 'zone' => 3, - 'opts' => array('defer'=>true) - ), - 4 => array( - 'file' => 'https://somewhere/async.js', - 'zone' => 4, - 'opts' => array('defer', 'async') - ), - - - ); - - foreach($load as $t) + public function testTryHeaderInline() { - $this->js->headerFile($t['file'], $t['zone'], null, null, $t['opts']); + + } + */ + public function testIsInAdmin() + { + $result = $this->js->isInAdmin(); + $this->assertFalse($result); + + } + + /* + public function testRequireCoreLib() + { + } - // Test loaded files. - - $result = $this->js->renderJs('header', 1, true, true); - $this->assertStringContainsString('', $result); - $this->assertStringContainsString('zone #1', $result); - - $result = $this->js->renderJs('header', 3, true, true); - $this->assertStringContainsString('', $result); - $this->assertStringContainsString('zone #3', $result); - - $result = $this->js->renderJs('header', 4, true, true); - $this->assertStringContainsString('', $result); - $this->assertStringContainsString('zone #4', $result); - - } - - public function testFooterFile() - { - $load = array( - 0 => array( - 'file' => '{e_PLUGIN}forum/js/forum.js', - 'zone' => 5, - 'opts' => [] - ), - 1 => array( - 'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js', - 'zone' => 1, - 'opts' => [] - ), - 2 => array( - 'file' => '{e_WEB}js/bootstrap-notify/js/bootstrap-notify.js', - 'zone' => 2, - 'opts' => [] - - ), - 3 => array( - 'file' => 'https://somewhere/something.min.js', - 'zone' => 3, - 'opts' => array('defer'=>true) - ), - - 4 => array( - 'file' => 'https://somewhere/async.js', - 'zone' => 4, - 'opts' => array('defer', 'async') - ), - - - ); - - foreach($load as $t) + public function testSetInAdmin() { - $this->js->footerFile($t['file'], $t['zone'], null, null, $t['opts']); + } - // Test loaded files. + public function testCoreCSS() + { - $result = $this->js->renderJs('footer', 1, true, true); - $this->assertStringContainsString('', $result); - $this->assertStringContainsString('priority #1', $result); + } - $result = $this->js->renderJs('footer', 3, true, true); - $this->assertStringContainsString('', $result); - $this->assertStringContainsString('priority #3', $result); + public function testResetDependency() + { - $result = $this->js->renderJs('footer', 4, true, true); - $this->assertStringContainsString('', $result); - $this->assertStringContainsString('priority #4', $result); + } - } -/* - public function testSetDependency() + public function testJsSettings() + { + + } + + public function testGetInstance() + { + + } + + public function testFooterFile() + { + + } + + public function testSetData() + { + + } + + public function testLibraryCSS() + { + + } + + public function testTryHeaderFile() + { + + } + + public function testThemeCSS() + { + + } + + public function testOtherCSS() + { + + } + + public function testSetLastModfied() + { + + } + + public function testRenderLinks() + { + + } + + public function testThemeLib() + { + + } + + public function testRenderFile() + { + + } + + public function testHeaderCore() + { + + } + + public function testRenderInline() + { + + } + + public function testFooterTheme() + { + + } + + public function testGetData() + { + + } + + public function testRequirePluginLib() + { + + } + + public function testGetCacheId() + { + + } + + public function testHeaderTheme() + { + + } + + public function testInlineCSS() + { + + } + */ + public function testHeaderFile() + { + $load = array( + 0 => array( + 'file' => '{e_PLUGIN}forum/js/forum.js', + 'zone' => 5, + 'opts' => [] + ), + 1 => array( + 'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js', + 'zone' => 1, + 'opts' => [] + ), + 2 => array( + 'file' => '{e_WEB}js/bootstrap-notify/js/bootstrap-notify.js', + 'zone' => 2, + 'opts' => [] + + ), + 3 => array( + 'file' => 'https://somewhere/something.min.js', + 'zone' => 3, + 'opts' => array('defer' => true) + ), + 4 => array( + 'file' => 'https://somewhere/async.js', + 'zone' => 4, + 'opts' => array('defer', 'async') + ), + + + ); + + foreach($load as $t) { - + $this->js->headerFile($t['file'], $t['zone'], null, null, $t['opts']); } - public function testHeaderInline() + // Test loaded files. + + $result = $this->js->renderJs('header', 1, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('zone #1', $result); + + $result = $this->js->renderJs('header', 3, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('zone #3', $result); + + $result = $this->js->renderJs('header', 4, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('zone #4', $result); + + } + + public function testFooterFile() + { + $load = array( + 0 => array( + 'file' => '{e_PLUGIN}forum/js/forum.js', + 'zone' => 5, + 'opts' => [] + ), + 1 => array( + 'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js', + 'zone' => 1, + 'opts' => [] + ), + 2 => array( + 'file' => '{e_WEB}js/bootstrap-notify/js/bootstrap-notify.js', + 'zone' => 2, + 'opts' => [] + + ), + 3 => array( + 'file' => 'https://somewhere/something.min.js', + 'zone' => 3, + 'opts' => array('defer' => true) + ), + + 4 => array( + 'file' => 'https://somewhere/async.js', + 'zone' => 4, + 'opts' => array('defer', 'async') + ), + + + ); + + foreach($load as $t) { - + $this->js->footerFile($t['file'], $t['zone'], null, null, $t['opts']); } - public function testGetLastModfied() + // Test loaded files. + + $result = $this->js->renderJs('footer', 1, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('priority #1', $result); + + $result = $this->js->renderJs('footer', 3, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('priority #3', $result); + + $result = $this->js->renderJs('footer', 4, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('priority #4', $result); + + } + + /* + public function testSetDependency() + { + + } + + public function testHeaderInline() + { + + } + + public function testGetLastModfied() + { + + } + + public function testSetCacheId() + { + + } + + public function testGetCurrentTheme() + { + + } + + public function testPluginCSS() + { + + } + + public function testCheckLibDependence() + { + + } + + public function testRenderCached() + { + + } + + public function testGetCurrentLocation() + { + + } + + public function testFooterInline() + { + + } + + public function testAddLibPref() + { + + } + */ + public function testAddLink() + { + $tests = array( + 0 => array( + 'expected' => '', + 'input' => array('rel' => 'preload', 'href' => 'https://fonts.googleapis.com/css?family=Nunito&display=swap', 'as' => 'style', 'onload' => "this.onload=null;"), + 'cacheid' => false, + ), + 1 => array( + 'expected' => '', // partial + 'input' => 'rel="preload" href="{THEME}assets/fonts/fontawesome-webfont.woff2?v=4.7.0" as="font" type="font/woff2" crossorigin', + 'cacheid' => false, + ), + 2 => array( + 'expected' => '', + 'input' => array('rel' => 'preload', 'href' => '{e_WEB}script.js', 'as' => 'script'), + 'cacheid' => true, + ), + + /* Static URLs enabled from this point. */ + + 3 => array( + 'expected' => '', + 'input' => array('rel' => 'preload', 'href' => '{e_WEB}script.js', 'as' => 'script'), + 'cacheid' => true, + 'static' => true, + ), + ); + + $tp = e107::getParser(); + + foreach($tests as $var) { + $static = !empty($var['static']) ? 'https://static.mydomain.com/' : null; + $tp->setStaticUrl($static); + $this->js->addLink($var['input'], $var['cacheid']); + // $this->assertSame($var['expected'],$actual); } - public function testSetCacheId() + $actual = $this->js->renderLinks(true); + + foreach($tests as $var) { - + $result = (strpos($actual, $var['expected']) !== false); + $this->assertTrue($result, $var['expected'] . " was not found in the rendered links. Render links result:" . $actual . "\n\n"); } - public function testGetCurrentTheme() - { - - } - - public function testPluginCSS() - { - - } - - public function testCheckLibDependence() - { - - } - - public function testRenderCached() - { - - } - - public function testGetCurrentLocation() - { - - } - - public function testFooterInline() - { - - } - - public function testAddLibPref() - { - - } -*/ - public function testAddLink() - { - $tests = array( - 0 => array( - 'expected' => '', - 'input' => array('rel'=>'preload', 'href'=>'https://fonts.googleapis.com/css?family=Nunito&display=swap', 'as'=>'style', 'onload' => "this.onload=null;"), - 'cacheid' => false, - ), - 1 => array( - 'expected' => '', // partial - 'input' => 'rel="preload" href="{THEME}assets/fonts/fontawesome-webfont.woff2?v=4.7.0" as="font" type="font/woff2" crossorigin', - 'cacheid' => false, - ), - 2 => array( - 'expected' => '', - 'input' => array('rel'=>'preload', 'href'=>'{e_WEB}script.js', 'as'=>'script'), - 'cacheid' => true, - ), - - /* Static URLs enabled from this point. */ - - 3 => array( - 'expected' => '', - 'input' => array('rel'=>'preload', 'href'=>'{e_WEB}script.js', 'as'=>'script'), - 'cacheid' => true, - 'static' => true, - ), - ); - - $tp = e107::getParser(); - - foreach($tests as $var) - { - $static = !empty($var['static']) ? 'https://static.mydomain.com/' : null; - $tp->setStaticUrl($static); - - $this->js->addLink($var['input'], $var['cacheid']); - // $this->assertSame($var['expected'],$actual); - } - - $actual = $this->js->renderLinks(true); - - foreach($tests as $var) - { - $result = (strpos($actual, $var['expected']) !== false); - $this->assertTrue($result, $var['expected']." was not found in the rendered links. Render links result:".$actual."\n\n"); - } - - $tp->setStaticUrl(null); - - - } -/* - public function testLibDisabled() - { - - } - - public function testArrayMergeDeepArray() - { - - } - - public function testRenderJs() - { - - } - - public function testRemoveLibPref() - { - - } -*/ + $tp->setStaticUrl(null); } + /* + public function testLibDisabled() + { + + } + + public function testArrayMergeDeepArray() + { + + } + + public function testRenderJs() + { + + } + + public function testRemoveLibPref() + { + + } + */ + + + function testRenderFavicon() + { + $file = e_PLUGIN."gsitemap/images/icon.png"; + $result = $this->js->renderFavicon($file); + self::assertNotEmpty($result); + + + } + +}