From e7b8749c151956d69f0f5c4f2340e62a0dd38895 Mon Sep 17 00:00:00 2001 From: flyskyko Date: Mon, 10 Oct 2011 07:08:37 +0000 Subject: [PATCH] issue 355, fixed a bug for ordering front end files git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9526 201d5d3c-b55e-5fd7-737f-ddc643e51545 --- .../FrontEndFileHandler.class.php | 79 +++--- .../security/FrontEndFileHandlerTest.php | 256 ++++++++++++++++++ 2 files changed, 300 insertions(+), 35 deletions(-) create mode 100644 tests/classes/security/FrontEndFileHandlerTest.php diff --git a/classes/frontendfile/FrontEndFileHandler.class.php b/classes/frontendfile/FrontEndFileHandler.class.php index 9e4b4b55e..b54c8d4b4 100644 --- a/classes/frontendfile/FrontEndFileHandler.class.php +++ b/classes/frontendfile/FrontEndFileHandler.class.php @@ -87,10 +87,10 @@ $key = $file->filePath . $file->fileName . "\t" . $file->targetIe; } - if (!isset($map[$key]) || $map[$key]->index > $file->index) + if (!isset($map[$key]) || $mapIndex[$key] != $file->index) { - $file->index = ((int)$file->index + count($map)); - $map[$key] = $file; + $this->unloadFile($args[0], $args[2], $args[1]); + $map[$file->index][$key] = $file; $mapIndex[$key] = $file->index; } } @@ -106,15 +106,27 @@ if ($fileExtension == 'css') { $key .= "\t" . $media; - unset($this->cssMap[$key]); - unset($this->cssMapIndex[$key]); + if (isset($this->cssMapIndex[$key])) + { + $index = $this->cssMapIndex[$key]; + unset($this->cssMap[$index][$key]); + unset($this->cssMapIndex[$key]); + } } else { - unset($this->jsHeadMap[$key]); - unset($this->jsBodyMap[$key]); - unset($this->jsHeadMapIndex[$key]); - unset($this->jsBodyMapIndex[$key]); + if (isset($this->jsHeadMapIndex[$key])) + { + $index = $this->jsHeadMapIndex[$key]; + unset($this->jsHeadMap[$index][$key]); + unset($this->jsHeadMapIndex[$key]); + } + if (isset($this->jsBodyMapIndex[$key])) + { + $index = $this->jsBodyMapIndex[$key]; + unset($this->jsBodyMap[$index][$key]); + unset($this->jsBodyMapIndex[$key]); + } } } @@ -146,17 +158,20 @@ $useCdn = $dbInfo->use_cdn; $result = array(); - foreach($map as $file) + foreach($map as $indexedMap) { - if ($this->isSsl() == false && $useCdn == 'Y' && $file->useCdn && $file->cdnVersion != '%__XE_CDN_VERSION__%') + foreach($indexedMap as $file) { - $fullFilePath = $file->cdnPrefix . $file->cdnVersion . '/' . substr($file->cdnPath, 2) . '/' . $file->fileName; + if ($this->isSsl() == false && $useCdn == 'Y' && $file->useCdn && $file->cdnVersion != '%__XE_CDN_VERSION__%') + { + $fullFilePath = $file->cdnPrefix . $file->cdnVersion . '/' . substr($file->cdnPath, 2) . '/' . $file->fileName; + } + else + { + $fullFilePath = $file->filePath . '/' . $file->fileName; + } + $result[] = array('file' => $fullFilePath, 'media' => $file->media, 'targetie' => $file->targetIe); } - else - { - $fullFilePath = $file->filePath . '/' . $file->fileName; - } - $result[] = array('file' => $fullFilePath, 'media' => $file->media, 'targetie' => $file->targetIe); } return $result; @@ -181,17 +196,20 @@ $useCdn = $dbInfo->use_cdn; $result = array(); - foreach($map as $file) + foreach($map as $indexedMap) { - if ($this->isSsl() == false && $useCdn == 'Y' && $file->useCdn && $file->cdnVersion != '%__XE_CDN_VERSION__%') + foreach($indexedMap as $file) { - $fullFilePath = $file->cdnPrefix . $file->cdnVersion . '/' . substr($file->cdnPath, 2) . '/' . $file->fileName; + if ($this->isSsl() == false && $useCdn == 'Y' && $file->useCdn && $file->cdnVersion != '%__XE_CDN_VERSION__%') + { + $fullFilePath = $file->cdnPrefix . $file->cdnVersion . '/' . substr($file->cdnPath, 2) . '/' . $file->fileName; + } + else + { + $fullFilePath = $file->filePath . '/' . $file->fileName; + } + $result[] = array('file' => $fullFilePath, 'targetie' => $file->targetIe); } - else - { - $fullFilePath = $file->filePath . '/' . $file->fileName; - } - $result[] = array('file' => $fullFilePath, 'targetie' => $file->targetIe); } return $result; @@ -199,16 +217,7 @@ function _sortMap(&$map, &$index) { - - asort($index); - - $sortedMap = array(); - foreach($index as $key => $val) - { - $sortedMap[$key] = $map[$key]; - } - - $map = $sortedMap; + ksort($map); } function _normalizeFilePath($path) diff --git a/tests/classes/security/FrontEndFileHandlerTest.php b/tests/classes/security/FrontEndFileHandlerTest.php new file mode 100644 index 000000000..4d8b0ea3b --- /dev/null +++ b/tests/classes/security/FrontEndFileHandlerTest.php @@ -0,0 +1,256 @@ +loadFile(array('./common/js/jquery.js')); + $handler->loadFile(array('./common/js/js_app.js')); + $handler->loadFile(array('./common/js/common.js')); + $handler->loadFile(array('./common/js/xml_handler.js')); + $handler->loadFile(array('./common/js/xml_js_filter.js')); + + $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => ''); + $this->assertEquals($handler->getJsFileList(), $expected); + + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + // js(body) + $handler->loadFile(array('./common/js/jquery.js', 'body')); + $handler->loadFile(array('./common/js/js_app.js', 'body')); + $handler->loadFile(array('./common/js/common.js', 'body')); + $handler->loadFile(array('./common/js/xml_handler.js', 'body')); + $handler->loadFile(array('./common/js/xml_js_filter.js', 'body')); + + $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => ''); + $this->assertEquals($handler->getJsFileList('body'), $expected); + + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + // css + $handler->loadFile(array('./common/css/xe.css')); + $handler->loadFile(array('./common/css/common.css')); + + $expected[] = array('file' => '/xe/common/css/xe.css', 'media' => 'all', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => ''); + $this->assertEquals($handler->getCssFileList(), $expected); + + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + // order (duplicate) + $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/common.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/common.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); + + $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => ''); + $this->assertEquals($handler->getJsFileList(), $expected); + + // order (redefine) + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/common.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/jquery.js', 'head', '', 1)); + + $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => ''); + $this->assertEquals($handler->getJsFileList(), $expected); + + // unload + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/common.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000)); + $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000)); + $handler->unloadFile('./common/js/jquery.js', '', 'all'); + + $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => ''); + $this->assertEquals($handler->getJsFileList(), $expected); + + // target IE(js) + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie6')); + $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie7')); + $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie8')); + + $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => 'ie6'); + $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => 'ie7'); + $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => 'ie8'); + $this->assertEquals($handler->getJsFileList(), $expected); + + // target IE(css) + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('./common/css/common.css', null, 'ie6')); + $handler->loadFile(array('./common/css/common.css', null, 'ie7')); + $handler->loadFile(array('./common/css/common.css', null, 'ie8')); + + $expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => 'ie6'); + $expected[] = array('file' => '/xe/common/css/common.css','media'=>'all', 'targetie' => 'ie7'); + $expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => 'ie8'); + $this->assertEquals($handler->getCssFileList(), $expected); + + // media + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('./common/css/common.css', 'all')); + $handler->loadFile(array('./common/css/common.css', 'screen')); + $handler->loadFile(array('./common/css/common.css', 'handled')); + + $expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/css/common.css','media'=>'screen', 'targetie' => ''); + $expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'handled', 'targetie' => ''); + $this->assertEquals($handler->getCssFileList(), $expected); + + // CDN + unset($handler); + unset($expected); + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('./common/css/common.css'), true, 'http://static.xpressengine.com/core/', 'v'); + + $expected[] = array('file' => 'http://static.xpressengine.com/core/v/common/css/common.css', 'media'=>'all', 'targetie' => ''); + $this->assertEquals($handler->getCssFileList(), $expected); + + // CDN(no cdn setting) + unset($handler); + unset($expected); + $use_cdn = 'N'; + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('./common/css/common.css'), true, 'http://static.xpressengine.com/core/', 'v'); + + $expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => ''); + $this->assertEquals($handler->getCssFileList(), $expected); + + // CDN(use ssl) + unset($handler); + unset($expected); + $use_cdn = 'Y'; + $request_url = 'https://test.com'; + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('./common/css/common.css'), true, 'http://static.xpressengine.com/core/', 'v'); + + $expected[] = array('file' => '/xe/common/css/common.css', 'media'=>'all', 'targetie' => ''); + $this->assertEquals($handler->getCssFileList(), $expected); + + // external file + unset($handler); + unset($expected); + $use_cdn = 'Y'; + $request_url = 'http://test.com'; + $handler = new FrontEndFileHandler(); + + $handler->loadFile(array('http://external.com/css/style2.css')); + $handler->loadFile(array('http://external.com/css/style.css'), true, 'http://static.xpressengine.com/core/', 'v'); + + $expected[] = array('file' => 'http://external.com/css/style2.css', 'media'=>'all', 'targetie' => ''); + $expected[] = array('file' => 'http://external.com/css/style.css', 'media'=>'all', 'targetie' => ''); + $this->assertEquals($handler->getCssFileList(), $expected); + + } +} + +$mock_vars = array(); + +class Context +{ + public function gets() { + global $mock_vars; + + $args = func_get_args(); + $output = new stdClass; + + foreach($args as $name) { + $output->{$name} = $mock_vars[$name]; + } + + return $output; + } + + public function get($name) { + global $mock_vars; + return array_key_exists($name, $mock_vars)?$mock_vars[$name]:''; + } + + public function set($name, $value) { + global $mock_vars; + + $mock_vars[$name] = $value; + } + + public function getRequestUrl() { + global $request_url; + return $request_url; + } + public function getDBInfo() { + global $use_cdn; + $dbInfo->use_cdn = $use_cdn; + return $dbInfo; + } +} + +function debugPrint(){}