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
This commit is contained in:
flyskyko 2011-10-10 07:08:37 +00:00
parent afa365b44b
commit e7b8749c15
2 changed files with 300 additions and 35 deletions

View file

@ -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)

View file

@ -0,0 +1,256 @@
<?php
define('__DEBUG__', 1);
$xe_path = realpath(dirname(__FILE__).'/../../../');
require "{$xe_path}/classes/handler/Handler.class.php";
require "{$xe_path}/classes/frontendfile/FrontEndFileHandler.class.php";
error_reporting(E_ALL & ~E_NOTICE);
$_SERVER['SCRIPT_NAME'] = '/xe/index.php';
class FrontEndFileHandlerTest extends PHPUnit_Framework_TestCase
{
public function testHandler()
{
global $request_url, $use_cdn;
$request_url = 'http://test.com';
$use_cdn = 'Y';
$handler = new FrontEndFileHandler();
// js(head)
$handler->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(){}