FIX extra_requirements to accept non associative arrays.

Added unit tests for extra_requirements_js and extra_requirements_css.

Fixed YAML documentation to indicate list syntax.
This commit is contained in:
Will Rossiter 2013-06-02 13:37:34 +12:00
parent 1cebfc5d51
commit db85f3ca16
6 changed files with 85 additions and 13 deletions

View File

@ -378,29 +378,58 @@ class LeftAndMain extends Controller implements PermissionProvider {
$ie = isset($_SERVER['HTTP_USER_AGENT']) ? strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') : false; $ie = isset($_SERVER['HTTP_USER_AGENT']) ? strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') : false;
if($ie) { if($ie) {
$version = substr($_SERVER['HTTP_USER_AGENT'], $ie + 5, 3); $version = substr($_SERVER['HTTP_USER_AGENT'], $ie + 5, 3);
if($version == 7) Requirements::css(FRAMEWORK_ADMIN_DIR . '/css/ie7.css');
else if($version == 8) Requirements::css(FRAMEWORK_ADMIN_DIR . '/css/ie8.css'); if($version == 7) {
Requirements::css(FRAMEWORK_ADMIN_DIR . '/css/ie7.css');
} else if($version == 8) {
Requirements::css(FRAMEWORK_ADMIN_DIR . '/css/ie8.css');
}
} }
// Custom requirements // Custom requirements
$extraJs = $this->stat('extra_requirements_javascript'); $extraJs = $this->stat('extra_requirements_javascript');
if($extraJs) foreach($extraJs as $file => $config) { if($extraJs) {
Requirements::javascript($file); foreach($extraJs as $file => $config) {
if(is_numeric($file)) {
$file = $config;
}
Requirements::javascript($file);
}
} }
$extraCss = $this->stat('extra_requirements_css'); $extraCss = $this->stat('extra_requirements_css');
if($extraCss) foreach($extraCss as $file => $config) {
Requirements::css($file, isset($config['media']) ? $config['media'] : null); if($extraCss) {
foreach($extraCss as $file => $config) {
if(is_numeric($file)) {
$file = $config;
$config = array();
}
Requirements::css($file, isset($config['media']) ? $config['media'] : null);
}
} }
$extraThemedCss = $this->stat('extra_requirements_themedCss'); $extraThemedCss = $this->stat('extra_requirements_themedCss');
if($extraThemedCss) foreach ($extraThemedCss as $file => $config) {
Requirements::themedCSS($file, isset($config['media']) ? $config['media'] : null); if($extraThemedCss) {
foreach ($extraThemedCss as $file => $config) {
if(is_numeric($file)) {
$file = $config;
$config = array();
}
Requirements::themedCSS($file, isset($config['media']) ? $config['media'] : null);
}
} }
$dummy = null; $dummy = null;
$this->extend('init', $dummy); $this->extend('init', $dummy);
// The user's theme shouldn't affect the CMS, if, for example, they have replaced TableListField.ss or Form.ss. // The user's theme shouldn't affect the CMS, if, for example, they have
// replaced TableListField.ss or Form.ss.
Config::inst()->update('SSViewer', 'theme_enabled', false); Config::inst()->update('SSViewer', 'theme_enabled', false);
} }

View File

@ -1,6 +1,7 @@
<?php <?php
/** /**
* @package cms * @package framework
* @subpackage tests * @subpackage tests
*/ */
class LeftAndMainTest extends FunctionalTest { class LeftAndMainTest extends FunctionalTest {
@ -9,14 +10,49 @@ class LeftAndMainTest extends FunctionalTest {
protected $extraDataObjects = array('LeftAndMainTest_Object'); protected $extraDataObjects = array('LeftAndMainTest_Object');
protected $backupCss, $backupJs, $backupCombined;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
// @todo fix controller stack problems and re-activate // @todo fix controller stack problems and re-activate
//$this->autoFollowRedirection = false; //$this->autoFollowRedirection = false;
CMSMenu::populate_menu(); CMSMenu::populate_menu();
$this->backupCss = Config::inst()->get('LeftAndMain', 'extra_requirements_css');
$this->backupJs = Config::inst()->get('LeftAndMain', 'extra_requirements_javascript');
$this->backupCombined = Requirements::get_combined_files_enabled();
Config::inst()->update('LeftAndMain', 'extra_requirements_css', array(
FRAMEWORK_DIR . '/tests/assets/LeftAndMainTest.css'
));
Config::inst()->update('LeftAndMain', 'extra_requirements_javascript', array(
FRAMEWORK_DIR . '/tests/assets/LeftAndMainTest.js'
));
Requirements::set_combined_files_enabled(false);
}
public function tearDown() {
parent::tearDown();
Config::inst()->update('LeftAndMain', 'extra_requirements_css', $this->backupCss);
Config::inst()->update('LeftAndMain', 'extra_requirements_javascript', $this->backupJs);
Requirements::set_combined_files_enabled($this->backupCombined);
} }
public function testExtraCssAndJavascript() {
$admin = $this->objFromFixture('Member', 'admin');
$this->session()->inst_set('loggedInAs', $admin->ID);
$response = $this->get('LeftAndMainTest_Controller');
$this->assertRegExp('/tests\/assets\/LeftAndMainTest.css/i', $response->getBody(), "body should contain custom css");
$this->assertRegExp('/tests\/assets\/LeftAndMainTest.js/i', $response->getBody(), "body should contain custom js");
}
/** /**
* Note: This test would typically rely on SiteTree and CMSMain, but is mocked by * Note: This test would typically rely on SiteTree and CMSMain, but is mocked by
* LeftAndMain_Controller and LeftAndMain_Object here to remove this dependency. * LeftAndMain_Controller and LeftAndMain_Object here to remove this dependency.
@ -158,15 +194,22 @@ class LeftAndMainTest extends FunctionalTest {
$this->session()->inst_set('loggedInAs', null); $this->session()->inst_set('loggedInAs', null);
} }
} }
/**
* @package framework
* @subpackage tests
*/
class LeftAndMainTest_Controller extends LeftAndMain implements TestOnly { class LeftAndMainTest_Controller extends LeftAndMain implements TestOnly {
protected $template = 'BlankPage'; protected $template = 'BlankPage';
private static $tree_class = 'LeftAndMainTest_Object'; private static $tree_class = 'LeftAndMainTest_Object';
} }
/**
* @package framework
* @subpackage tests
*/
class LeftAndMainTest_Object extends DataObject implements TestOnly { class LeftAndMainTest_Object extends DataObject implements TestOnly {
private static $db = array( private static $db = array(

View File

@ -67,7 +67,7 @@ Load the new CSS file into the CMS, by setting the `LeftAndMain.extra_requiremen
:::yml :::yml
LeftAndMain: LeftAndMain:
extra_requirements_css: extra_requirements_css:
mysite/css/BookmarkedPages.css: - mysite/css/BookmarkedPages.css:
## Create a "bookmark" flag on pages ## ## Create a "bookmark" flag on pages ##

View File

@ -69,7 +69,7 @@ to the `LeftAndMain.extra_requirements_javascript` [configuration value](/topics
:::yml :::yml
LeftAndMain LeftAndMain
extra_requirements_javascript: extra_requirements_javascript:
mysite/javascript/MyLeftAndMain.Preview.js: - mysite/javascript/MyLeftAndMain.Preview.js:
In order to find out which configuration values are available, the source code In order to find out which configuration values are available, the source code
is your best reference at the moment - have a look in `framework/admin/javascript/LeftAndMain.Preview.js`. is your best reference at the moment - have a look in `framework/admin/javascript/LeftAndMain.Preview.js`.

View File

View File