From 1835462ec4b37f256390785a3cf3531699a996cb Mon Sep 17 00:00:00 2001 From: Individual IT Services Date: Wed, 30 Sep 2015 09:54:29 +0545 Subject: [PATCH 1/2] reuse code --- lib/private/util.php | 81 +++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/lib/private/util.php b/lib/private/util.php index 69f01c22be..0bbb92a960 100644 --- a/lib/private/util.php +++ b/lib/private/util.php @@ -457,19 +457,12 @@ class OC_Util { */ public static function addScript($application, $file = null, $prepend = false) { $path = OC_Util::generatePath($application, 'js', $file); - //TODO eliminate double code - if (!in_array($path, self::$scripts)) { - // core js files need separate handling - if ($application !== 'core' && $file !== null) { - self::addTranslations($application); - } - if ($prepend===true) { - array_unshift(self::$scripts, $path); - } - else { - self::$scripts[] = $path; - } + + // core js files need separate handling + if ($application !== 'core' && $file !== null) { + self::addTranslations ( $application ); } + self::addExternalResource($application, $prepend, $path, "script"); } /** @@ -482,14 +475,7 @@ class OC_Util { */ public static function addVendorScript($application, $file = null, $prepend = false) { $path = OC_Util::generatePath($application, 'vendor', $file); - //TODO eliminate double code - if (! in_array ( $path, self::$scripts )) { - if ($prepend === true) { - array_unshift ( self::$scripts, $path ); - } else { - self::$scripts [] = $path; - } - } + self::addExternalResource($application, $prepend, $path, "script"); } /** @@ -508,14 +494,7 @@ class OC_Util { } else { $path = "l10n/$languageCode"; } - //TODO eliminate double code - if (!in_array($path, self::$scripts)) { - if ($prepend === true) { - array_unshift ( self::$scripts, $path ); - } else { - self::$scripts [] = $path; - } - } + self::addExternalResource($application, $prepend, $path, "script"); } /** @@ -528,14 +507,7 @@ class OC_Util { */ public static function addStyle($application, $file = null, $prepend = false) { $path = OC_Util::generatePath($application, 'css', $file); - //TODO eliminate double code - if (!in_array($path, self::$styles)) { - if ($prepend === true) { - array_unshift ( self::$styles, $path ); - } else { - self::$styles[] = $path; - } - } + self::addExternalResource($application, $prepend, $path, "style"); } /** @@ -548,13 +520,36 @@ class OC_Util { */ public static function addVendorStyle($application, $file = null, $prepend = false) { $path = OC_Util::generatePath($application, 'vendor', $file); - //TODO eliminate double code - if (!in_array($path, self::$styles)) { - if ($prepend === true) { - array_unshift ( self::$styles, $path ); - } else { - self::$styles[] = $path; - } + self::addExternalResource($application, $prepend, $path, "style"); + } + + /** + * add an external resource css/js file + * + * @param string $application application id + * @param bool $prepend prepend the file to the beginning of the list + * @param string $path + * @param string $type (script or style) + * @return void + */ + private static function addExternalResource($application, $prepend, $path, $type = "script") { + + if ($type === "style") { + if (!in_array($path, self::$styles)) { + if ($prepend === true) { + array_unshift ( self::$styles, $path ); + } else { + self::$styles[] = $path; + } + } + } elseif ($type === "script") { + if (!in_array($path, self::$scripts)) { + if ($prepend === true) { + array_unshift ( self::$scripts, $path ); + } else { + self::$scripts [] = $path; + } + } } } From 32ab973254678c1130559c977f59374484d92ca6 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 23 Nov 2015 11:02:35 +0100 Subject: [PATCH 2/2] add unit tests for OC_Util::add* methods --- tests/lib/util.php | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests/lib/util.php b/tests/lib/util.php index a328b1923e..c46d89130b 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -427,6 +427,97 @@ class Test_Util extends \Test\TestCase { $this->assertNotEmpty($errors); } } + + protected function setUp() { + parent::setUp(); + + \OC_Util::$scripts = []; + \OC_Util::$styles = []; + } + protected function tearDown() { + parent::tearDown(); + + \OC_Util::$scripts = []; + \OC_Util::$styles = []; + } + + public function testAddScript() { + \OC_Util::addScript('core', 'myFancyJSFile1'); + \OC_Util::addScript('myApp', 'myFancyJSFile2'); + \OC_Util::addScript('core', 'myFancyJSFile0', true); + \OC_Util::addScript('core', 'myFancyJSFile10', true); + // add duplicate + \OC_Util::addScript('core', 'myFancyJSFile1'); + + $this->assertEquals([ + 'core/js/myFancyJSFile10', + 'core/js/myFancyJSFile0', + 'core/js/myFancyJSFile1', + 'myApp/l10n/en', + 'myApp/js/myFancyJSFile2', + ], \OC_Util::$scripts); + $this->assertEquals([], \OC_Util::$styles); + } + + public function testAddVendorScript() { + \OC_Util::addVendorScript('core', 'myFancyJSFile1'); + \OC_Util::addVendorScript('myApp', 'myFancyJSFile2'); + \OC_Util::addVendorScript('core', 'myFancyJSFile0', true); + \OC_Util::addVendorScript('core', 'myFancyJSFile10', true); + // add duplicate + \OC_Util::addVendorScript('core', 'myFancyJSFile1'); + + $this->assertEquals([ + 'core/vendor/myFancyJSFile10', + 'core/vendor/myFancyJSFile0', + 'core/vendor/myFancyJSFile1', + 'myApp/vendor/myFancyJSFile2', + ], \OC_Util::$scripts); + $this->assertEquals([], \OC_Util::$styles); + } + + public function testAddTranslations() { + \OC_Util::addTranslations('appId', 'de'); + + $this->assertEquals([ + 'appId/l10n/de' + ], \OC_Util::$scripts); + $this->assertEquals([], \OC_Util::$styles); + } + + public function testAddStyle() { + \OC_Util::addStyle('core', 'myFancyCSSFile1'); + \OC_Util::addStyle('myApp', 'myFancyCSSFile2'); + \OC_Util::addStyle('core', 'myFancyCSSFile0', true); + \OC_Util::addStyle('core', 'myFancyCSSFile10', true); + // add duplicate + \OC_Util::addStyle('core', 'myFancyCSSFile1'); + + $this->assertEquals([], \OC_Util::$scripts); + $this->assertEquals([ + 'core/css/myFancyCSSFile10', + 'core/css/myFancyCSSFile0', + 'core/css/myFancyCSSFile1', + 'myApp/css/myFancyCSSFile2', + ], \OC_Util::$styles); + } + + public function testAddVendorStyle() { + \OC_Util::addVendorStyle('core', 'myFancyCSSFile1'); + \OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2'); + \OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true); + \OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true); + // add duplicate + \OC_Util::addVendorStyle('core', 'myFancyCSSFile1'); + + $this->assertEquals([], \OC_Util::$scripts); + $this->assertEquals([ + 'core/vendor/myFancyCSSFile10', + 'core/vendor/myFancyCSSFile0', + 'core/vendor/myFancyCSSFile1', + 'myApp/vendor/myFancyCSSFile2', + ], \OC_Util::$styles); + } } /**