public function testXhtmltagReplacement()
    {
        $tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html>
				<head><% base_tag %></head>
				<body>
				<form action="#">
					<select>
							<option>aa</option>
							<option selected = "selected">bb</option>
							<option selected="selected">cc</option>
							<option class="foo" selected>dd</option>
							<option>ee</option>
							<option selected value="">ll</option>
					</select>
					<input type="checkbox">ff
					<input type="checkbox" checked = "checked">gg
					<input type="checkbox" checked="checked">hh
					<input class="bar" type="checkbox" checked>ii
					<input type="checkbox" checked class="foo">jj
					<input type="submit">
				</form>
				<body>
			</html>';
        // Check that the content negotiator converts to the equally legal formats
        $negotiator = new ContentNegotiator();
        $response = new HTTPResponse($this->render($tmpl1));
        $negotiator->xhtml($response);
        ////////////////////////
        // XHTML select options
        ////////////////////////
        $this->assertRegExp('/<option>aa<\\/option>/', $response->getBody());
        $this->assertRegExp('/<option selected = "selected">bb<\\/option>/', $response->getBody());
        $this->assertRegExp('/<option selected="selected">cc<\\/option>/', $response->getBody());
        // Just transform this
        $this->assertRegExp('/<option class="foo" selected="selected">dd<\\/option>/', $response->getBody());
        $this->assertRegExp('/<option selected="selected" value="">ll<\\/option>/', $response->getBody());
        ////////////////////////////////////////////////
        // XHTML checkbox options + XHTML input closure
        ////////////////////////////////////////////////
        $this->assertRegExp('/<input type="checkbox"\\/>ff/', $response->getBody());
        $this->assertRegExp('/<input type="checkbox" checked = "checked"\\/>g/', $response->getBody());
        $this->assertRegExp('/<input type="checkbox" checked="checked"\\/>hh/', $response->getBody());
        // Just transform this
        $this->assertRegExp('/<input class="bar" type="checkbox" checked="checked"\\/>ii/', $response->getBody());
        $this->assertRegExp('/<input type="checkbox" checked="checked" class="foo"\\/>jj/', $response->getBody());
    }
 /**
  * Prepare the response (we can receive an assortment of response types (strings/objects/HTTPResponses) and
  * changes the controller response object appropriately
  *
  * @param HTTPResponse|Object $response
  */
 protected function prepareResponse($response)
 {
     if ($response instanceof HTTPResponse) {
         if (isset($_REQUEST['debug_request'])) {
             Debug::message("Request handler returned HTTPResponse object to {$this->class} controller;" . "returning it without modification.");
         }
         $this->setResponse($response);
     } else {
         if ($response instanceof Object && $response->hasMethod('getViewer')) {
             if (isset($_REQUEST['debug_request'])) {
                 Debug::message("Request handler {$response->class} object to {$this->class} controller;" . "rendering with template returned by {$response->class}::getViewer()");
             }
             $response = $response->getViewer($this->getAction())->process($response);
         }
         $this->getResponse()->setBody($response);
     }
     //deal with content if appropriate
     ContentNegotiator::process($this->getResponse());
     //add cache headers
     HTTP::add_cache_headers($this->getResponse());
 }
 /**
  * @uses LeftAndMainExtension->init()
  * @uses LeftAndMainExtension->accessedCMS()
  * @uses CMSMenu
  */
 protected function init()
 {
     parent::init();
     SSViewer::config()->update('rewrite_hash_links', false);
     ContentNegotiator::config()->update('enabled', false);
     // set language
     $member = Member::currentUser();
     if (!empty($member->Locale)) {
         i18n::set_locale($member->Locale);
     }
     if (!empty($member->DateFormat)) {
         i18n::config()->date_format = $member->DateFormat;
     }
     if (!empty($member->TimeFormat)) {
         i18n::config()->time_format = $member->TimeFormat;
     }
     // can't be done in cms/_config.php as locale is not set yet
     CMSMenu::add_link('Help', _t('LeftAndMain.HELP', 'Help', 'Menu title'), $this->config()->help_link, -2, array('target' => '_blank'));
     // Allow customisation of the access check by a extension
     // Also all the canView() check to execute Controller::redirect()
     if (!$this->canView() && !$this->getResponse()->isFinished()) {
         // When access /admin/, we should try a redirect to another part of the admin rather than be locked out
         $menu = $this->MainMenu();
         foreach ($menu as $candidate) {
             if ($candidate->Link && $candidate->Link != $this->Link() && $candidate->MenuItem->controller && singleton($candidate->MenuItem->controller)->canView()) {
                 $this->redirect($candidate->Link);
                 return;
             }
         }
         if (Member::currentUser()) {
             Session::set("BackURL", null);
         }
         // if no alternate menu items have matched, return a permission error
         $messageSet = array('default' => _t('LeftAndMain.PERMDEFAULT', "You must be logged in to access the administration area; please enter your credentials below."), 'alreadyLoggedIn' => _t('LeftAndMain.PERMALREADY', "I'm sorry, but you can't access that part of the CMS.  If you want to log in as someone else, do" . " so below."), 'logInAgain' => _t('LeftAndMain.PERMAGAIN', "You have been logged out of the CMS.  If you would like to log in again, enter a username and" . " password below."));
         Security::permissionFailure($this, $messageSet);
         return;
     }
     // Don't continue if there's already been a redirection request.
     if ($this->redirectedTo()) {
         return;
     }
     // Audit logging hook
     if (empty($_REQUEST['executeForm']) && !$this->getRequest()->isAjax()) {
         $this->extend('accessedCMS');
     }
     // Set the members html editor config
     if (Member::currentUser()) {
         HTMLEditorConfig::set_active_identifier(Member::currentUser()->getHtmlEditorConfigForCMS());
     }
     // Set default values in the config if missing.  These things can't be defined in the config
     // file because insufficient information exists when that is being processed
     $htmlEditorConfig = HTMLEditorConfig::get_active();
     $htmlEditorConfig->setOption('language', i18n::get_tinymce_lang());
     Requirements::customScript("\n\t\t\twindow.ss = window.ss || {};\n\t\t\twindow.ss.config = " . $this->getCombinedClientConfig() . ";\n\t\t");
     Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/vendor.js');
     Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/bundle.js');
     Requirements::css(ltrim(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css', '/'));
     Requirements::add_i18n_javascript(ltrim(FRAMEWORK_DIR . '/client/lang', '/'), false, true);
     Requirements::add_i18n_javascript(FRAMEWORK_ADMIN_DIR . '/client/lang', false, true);
     if ($this->config()->session_keepalive_ping) {
         Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/LeftAndMain.Ping.js');
     }
     if (Director::isDev()) {
         // TODO Confuses jQuery.ondemand through document.write()
         Requirements::javascript(ADMIN_THIRDPARTY_DIR . '/jquery-entwine/src/jquery.entwine.inspector.js');
         Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/leaktools.js');
     }
     // Custom requirements
     $extraJs = $this->stat('extra_requirements_javascript');
     if ($extraJs) {
         foreach ($extraJs as $file => $config) {
             if (is_numeric($file)) {
                 $file = $config;
             }
             Requirements::javascript($file);
         }
     }
     $extraCss = $this->stat('extra_requirements_css');
     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');
     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;
     $this->extend('init', $dummy);
     // Assign default cms theme and replace user-specified themes
     SSViewer::set_themes($this->config()->admin_themes);
     //set the reading mode for the admin to stage
     Versioned::set_stage(Versioned::DRAFT);
 }
    public function testBaseTagGeneration()
    {
        // XHTML wil have a closed base tag
        $tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html>
				<head><% base_tag %></head>
				<body><p>test</p><body>
			</html>';
        $this->assertRegExp('/<head><base href=".*" \\/><\\/head>/', $this->render($tmpl1));
        // HTML4 and 5 will only have it for IE
        $tmpl2 = '<!DOCTYPE html>
			<html>
				<head><% base_tag %></head>
				<body><p>test</p><body>
			</html>';
        $this->assertRegExp('/<head><base href=".*"><!--\\[if lte IE 6\\]><\\/base><!\\[endif\\]--><\\/head>/', $this->render($tmpl2));
        $tmpl3 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
			<html>
				<head><% base_tag %></head>
				<body><p>test</p><body>
			</html>';
        $this->assertRegExp('/<head><base href=".*"><!--\\[if lte IE 6\\]><\\/base><!\\[endif\\]--><\\/head>/', $this->render($tmpl3));
        // Check that the content negotiator converts to the equally legal formats
        $negotiator = new ContentNegotiator();
        $response = new HTTPResponse($this->render($tmpl1));
        $negotiator->html($response);
        $this->assertRegExp('/<head><base href=".*"><!--\\[if lte IE 6\\]><\\/base><!\\[endif\\]--><\\/head>/', $response->getBody());
        $response = new HTTPResponse($this->render($tmpl1));
        $negotiator->xhtml($response);
        $this->assertRegExp('/<head><base href=".*" \\/><\\/head>/', $response->getBody());
    }