From e9565dfb280ebd89788c9db4690702af29b831b3 Mon Sep 17 00:00:00 2001 From: Merci Jacob Date: Fri, 11 Oct 2024 12:11:21 +0200 Subject: [PATCH] Automate the page_js_debug test to include all given js files in the specs --- modules/core/output_modules.php | 9 ++-- .../modules/core/output_modules_debug.php | 53 ++++++++++++++++--- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/modules/core/output_modules.php b/modules/core/output_modules.php index e637f66892..2e5ec3eee8 100644 --- a/modules/core/output_modules.php +++ b/modules/core/output_modules.php @@ -552,10 +552,6 @@ protected function output() { } $core = false; $mods = $this->get('router_module_list'); - // Load navigation utilities used by subsequent modules' handlers - if (in_array('core', $mods)) { - $res .= ''; - } foreach (glob(APP_PATH.'modules'.DIRECTORY_SEPARATOR.'**', GLOB_ONLYDIR | GLOB_MARK) as $name) { $rel_name = str_replace(APP_PATH, '', $name); $mod = str_replace(array('modules', DIRECTORY_SEPARATOR), '', $rel_name); @@ -576,7 +572,10 @@ protected function output() { } } if ($core) { - $res = ''.$res; + $res = // Load navigation utilities used by subsequent modules' handlers + ''. + ''. + $res; /* Load navigation js modules * routes.js, navigation.js * They have to be loaded after each module's js files, because routes.js depend on the handlers defined in the modules. diff --git a/tests/phpunit/modules/core/output_modules_debug.php b/tests/phpunit/modules/core/output_modules_debug.php index 72087b7376..c956bdcf21 100644 --- a/tests/phpunit/modules/core/output_modules_debug.php +++ b/tests/phpunit/modules/core/output_modules_debug.php @@ -22,16 +22,57 @@ public function test_header_css_debug() { /** * @preserveGlobalState disabled * @runInSeparateProcess + * @dataProvider router_module_list_provider */ - public function test_page_js_debug() { + public function test_page_js_debug($given_router_module_list) { $test = new Output_Test('page_js', 'core'); - $test->handler_response = array('encrypt_ajax_requests' => true, 'router_module_list' => array('foo', 'core')); + $test->handler_response = array('encrypt_ajax_requests' => true, 'router_module_list' => $given_router_module_list); $res = $test->run(); - $this->assertEquals(array(''), $res->output_response); - $test->handler_response = array('encrypt_ajax_requests' => true, 'router_module_list' => array('imap')); - $res = $test->run(); - $this->assertEquals(array(''), $res->output_response); + $dependant_scripts = array('vendor/twbs/bootstrap/dist/js/bootstrap.bundle.min.js'); + $third_party_scripts = array('cash.min.js', 'resumable.min.js', 'ays-beforeunload-shim.js', 'jquery.are-you-sure.js', 'sortable.min.js', 'forge.min.js'); + $expected_scripts = array_merge($dependant_scripts, array_map(function($script) { return 'third_party/'.$script; }, $third_party_scripts)); + + // The navigation utils and core's site.js should be included before any other module + $expected_scripts[] = 'modules/core/navigation/utils.js'; + $expected_scripts[] = 'modules/core/site.js'; + + foreach (glob(APP_PATH.'modules'.DIRECTORY_SEPARATOR.'**', GLOB_ONLYDIR | GLOB_MARK) as $module) { + $name = str_replace(array(APP_PATH, 'modules', DIRECTORY_SEPARATOR), '', $module); + if (in_array($name, $given_router_module_list)) { + // js_modules + $directoriesPattern = str_replace('/', DIRECTORY_SEPARATOR, "{*,*/*}"); + foreach (glob($module.'js_modules' . DIRECTORY_SEPARATOR . $directoriesPattern . "*.js", GLOB_BRACE) as $js) { + $expected_scripts[] = WEB_ROOT.str_replace(APP_PATH, '', $js); + } + if ($name === 'core') { + continue; + } + if (is_readable($module.'site.js')) { + $expected_scripts[] = 'modules/' . $name . '/site.js'; + } + } + } + + // core navigation modules included at the end when handlers have been processed + $expected_scripts[] = 'modules/core/navigation/routes.js'; + $expected_scripts[] = 'modules/core/navigation/navigation.js'; + + $expected_output = ''; + foreach ($expected_scripts as $script) { + $expected_output .= ''; + } + + $this->assertEquals(array($expected_output), $res->output_response); + } + + static function router_module_list_provider() { + return [ + 'one module' => [['core']], + 'two modules' => [['core', 'imap']], + 'several modules' => [['core', 'imap', 'inline_message', 'local_contacts']] + ]; } + /** * @preserveGlobalState disabled * @runInSeparateProcess