From 8dd119402a19d5ce131cc68340e9db9f0ebdd382 Mon Sep 17 00:00:00 2001 From: Jonas Meurer Date: Fri, 17 Dec 2021 13:26:43 +0100 Subject: [PATCH] Improve unit testing for Util::addScript() and Util::getScript() 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 --- tests/lib/UtilTest.php | 58 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/tests/lib/UtilTest.php b/tests/lib/UtilTest.php index 1b430fd7ecd..1b244d29dd5 100644 --- a/tests/lib/UtilTest.php +++ b/tests/lib/UtilTest.php @@ -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() { -- 2.39.5