]> source.dussan.org Git - nextcloud-server.git/commitdiff
Improve unit testing for Util::addScript() and Util::getScript()
authorJonas Meurer <jonas@freesources.org>
Fri, 17 Dec 2021 12:26:43 +0000 (13:26 +0100)
committerJonas Meurer <jonas@freesources.org>
Mon, 20 Dec 2021 10:40:17 +0000 (11:40 +0100)
Instead of checking for a predefined order of the scripts, test the
logic: core first, dependencies before their children, no duplicates and
all scripts still listed.

Signed-off-by: Jonas Meurer <jonas@freesources.org>
tests/lib/UtilTest.php

index 1b430fd7ecd6f78fb0c4138f42a844087ee5bfb3..1b244d29dd5d95d294b8b7f93116ed865ce10c53 100644 (file)
@@ -238,22 +238,70 @@ class UtilTest extends \Test\TestCase {
        public function testAddScript() {
                \OCP\Util::addScript('core', 'myFancyJSFile1');
                \OCP\Util::addScript('files', 'myFancyJSFile2', 'core');
+               \OCP\Util::addScript('myApp5', 'myApp5JSFile', 'myApp2');
                \OCP\Util::addScript('myApp', 'myFancyJSFile3');
                \OCP\Util::addScript('core', 'myFancyJSFile4');
                // after itself
                \OCP\Util::addScript('core', 'myFancyJSFile5', 'core');
                // add duplicate
                \OCP\Util::addScript('core', 'myFancyJSFile1');
-
-               $this->assertEquals([
+               // dependency chain
+               \OCP\Util::addScript('myApp4', 'myApp4JSFile', 'myApp3');
+               \OCP\Util::addScript('myApp3', 'myApp3JSFile', 'myApp2');
+               \OCP\Util::addScript('myApp2', 'myApp2JSFile', 'myApp');
+
+               // Core should appear first
+               $this->assertEquals(
+                       0,
+                       array_search('core/js/myFancyJSFile1', \OCP\Util::getScripts(), true)
+               );
+               $this->assertEquals(
+                       1,
+                       array_search('core/js/myFancyJSFile4', \OCP\Util::getScripts(), true)
+               );
+
+               // Dependencies should appear before their children
+               $this->assertLessThan(
+                       array_search('files/js/myFancyJSFile2', \OCP\Util::getScripts(), true),
+                       array_search('core/js/myFancyJSFile3', \OCP\Util::getScripts(), true)
+               );
+               $this->assertLessThan(
+                       array_search('myApp2/js/myApp2JSFile', \OCP\Util::getScripts(), true),
+                       array_search('myApp/js/myFancyJSFile3', \OCP\Util::getScripts(), true)
+               );
+               $this->assertLessThan(
+                       array_search('myApp3/js/myApp3JSFile', \OCP\Util::getScripts(), true),
+                       array_search('myApp2/js/myApp2JSFile', \OCP\Util::getScripts(), true)
+               );
+               $this->assertLessThan(
+                       array_search('myApp4/js/myApp4JSFile', \OCP\Util::getScripts(), true),
+                       array_search('myApp3/js/myApp3JSFile', \OCP\Util::getScripts(), true)
+               );
+               $this->assertLessThan(
+                       array_search('myApp5/js/myApp5JSFile', \OCP\Util::getScripts(), true),
+                       array_search('myApp2/js/myApp2JSFile', \OCP\Util::getScripts(), true)
+               );
+
+               // No duplicates
+               $this->assertEquals(
+                       \OCP\Util::getScripts(),
+                       array_unique(\OCP\Util::getScripts())
+               );
+
+               // All scripts still there
+               $scripts = [
                        'core/js/myFancyJSFile1',
                        'core/js/myFancyJSFile4',
                        'files/js/myFancyJSFile2',
                        'core/js/myFancyJSFile5',
-                       'files/l10n/en',
-                       'myApp/l10n/en',
                        'myApp/js/myFancyJSFile3',
-               ], array_values(\OCP\Util::getScripts()));
+                       'myApp2/js/myApp2JSFile',
+                       'myApp3/js/myApp3JSFile',
+                       'myApp4/js/myApp4JSFile',
+               ];
+               foreach ($scripts as $script) {
+                       $this->assertContains($script, \OCP\Util::getScripts());
+               }
        }
 
        public function testAddVendorScript() {