/** * The test to ensure regular expression replacement is correct. */ public function testRegularExpressionReplacement() { // Instantiate a link mapping to use. $mapping = LinkMapping::create(array('LinkType' => 'Regular Expression', 'MappedLink' => '^wrong(.*)', 'RedirectLink' => 'correct\\1')); $mapping->setMatchedURL('wrong/page'); // Determine whether the regular expression replacement is correct. $this->assertEquals('/correct/page', $mapping->getLink()); }
/** * The test to ensure the request filter is functioning correctly. */ public function testRequestFilter() { // Instantiate link mappings to use. $mapping = LinkMapping::create(array('LinkType' => 'Simple', 'MappedLink' => 'wrong/page', 'RedirectLink' => 'pending')); $mapping->write(); LinkMapping::create(array('LinkType' => 'Simple', 'MappedLink' => 'pending', 'RedirectLink' => 'correct/page'))->write(); // The CMS module needs to be present to test page behaviour. if (ClassInfo::exists('SiteTree')) { // This is required to support multiple sites. $this->logInAs(Member::default_admin()); $parentID = ClassInfo::exists('Multisites') ? Multisites::inst()->getCurrentSiteId() : 0; // Instantiate pages to use. $first = SiteTree::create(array('URLSegment' => 'wrong', 'ParentID' => $parentID)); $first->writeToStage('Stage'); $first->writeToStage('Live'); $second = SiteTree::create(array('URLSegment' => 'page', 'ParentID' => $first->ID)); $second->writeToStage('Stage'); $second->writeToStage('Live'); } // Determine whether the enforce misdirection is functioning correctly. $response = $this->get('wrong/page'); $this->assertEquals($response->getStatusCode(), 303); $this->assertEquals($response->getHeader('Location'), '/correct/page'); // The CMS module needs to be present to test page behaviour. if (ClassInfo::exists('SiteTree')) { // Update the default enforce misdirection. Config::inst()->update('MisdirectionRequestFilter', 'enforce_misdirection', false); // Determine whether the page is now matched. $response = $this->get('wrong/page'); $this->assertEquals($response->getStatusCode(), 200); $this->assertEquals($response->getHeader('Location'), null); // Instantiate a fallback to use. $first->Fallback = 'Nearest'; $first->writeToStage('Stage'); $first->writeToStage('Live'); // The database needs to be cleaned up to prevent further testing conflict. $second->deleteFromStage('Live'); $second->deleteFromStage('Stage'); $mapping->delete(); // Determine whether the fallback is matched. $response = $this->get('wrong/page'); $this->assertEquals($response->getStatusCode(), 303); $this->assertEquals($response->getHeader('Location'), '/wrong/?misdirected=1'); } // Instantiate a director rule to use. Config::inst()->update('Director', 'rules', array('wrong/page' => 'Controller')); // Determine whether the director rule is matched. $response = $this->get('wrong/page'); $this->assertEquals($response->getStatusCode(), 200); $this->assertEquals($response->getHeader('Location'), null); // The database needs to be emptied to prevent further testing conflict. self::empty_temp_db(); }
/** * Purge any link mappings that point back to the same page. * * @parameter <{PAGE_URL}> string * @parameter <{PAGE_ID}> integer */ public function regulateMappings($pageLink, $pageID) { LinkMapping::get()->filter(array('MappedLink' => MisdirectionService::unify_URL(Director::makeRelative($pageLink)), 'RedirectType' => 'Page', 'RedirectPageID' => $pageID))->removeAll(); }
/** * Instantiate a new link mapping, redirecting a URL towards another URL. * * @parameter <{MAPPING_URL}> string * @parameter <{MAPPING_REDIRECT_URL}> string * @parameter <{MAPPING_PRIORITY}> integer * @return link mapping */ public function createURLMapping($URL, $redirectURL, $priority = 1) { // Retrieve an already existing link mapping if one exists. $existing = LinkMapping::get()->filter(array('MappedLink' => $URL, 'RedirectType' => 'Link', 'RedirectLink' => $redirectURL))->first(); if ($existing) { return $existing; } // Instantiate the new link mapping with appropriate default values. $mapping = LinkMapping::create(); $mapping->MappedLink = $URL; $mapping->RedirectType = 'Link'; $mapping->RedirectLink = $redirectURL; $mapping->Priority = (int) $priority; $mapping->write(); return $mapping; }
/** * The test to ensure the link mapping priority is correct. */ public function testMappingPriority() { // Instantiate link mappings to use. $first = LinkMapping::create(array('LinkType' => 'Simple', 'MappedLink' => 'wrong/page', 'Priority' => 1)); $first->write(); $second = LinkMapping::create(array('LinkType' => 'Simple', 'MappedLink' => 'wrong/page', 'Priority' => 1)); $second->write(); // Instantiate a request to use. $request = new SS_HTTPRequest('GET', 'wrong/page'); // Determine whether the link mapping first created is matched. $testing = true; $service = singleton('MisdirectionService'); $chain = $service->getMappingByRequest($request, $testing); $this->assertEquals(count($chain), 1); $match = end($chain); $this->assertEquals($match['LinkMapping']->ID, $first->ID); // Update the default link mapping priority. Config::inst()->update('LinkMapping', 'priority', 'DESC'); // Determine whether the link mapping most recently created is matched. $chain = $service->getMappingByRequest($request, $testing); $this->assertEquals(count($chain), 1); $match = end($chain); $this->assertEquals($match['LinkMapping']->ID, $second->ID); // Update the link mapping priority. $first->Priority = 2; $first->write(); // Determine whether the link mapping first created is matched. $chain = $service->getMappingByRequest($request, $testing); $this->assertEquals(count($chain), 1); $match = end($chain); $this->assertEquals($match['LinkMapping']->ID, $first->ID); // The database needs to be emptied to prevent further testing conflict. self::empty_temp_db(); }
/** * Overrides ModelAsController->getNestedController to find the nested controller * on a per-site basis **/ public function getNestedController() { $request = $this->request; $segment = $request->param('URLSegment'); $site = Multisites::inst()->getCurrentSiteId(); if (!$site) { return $this->httpError(404); } if (class_exists('Translatable')) { Translatable::disable_locale_filter(); } $page = SiteTree::get()->filter(array('ParentID' => $site, 'URLSegment' => rawurlencode($segment))); $page = $page->first(); if (class_exists('Translatable')) { Translatable::enable_locale_filter(); } if (!$page) { // Check to see if linkmapping module is installed and if so, if there a map for this request. if (class_exists('LinkMapping')) { if ($request->requestVars()) { $queryString = '?'; foreach ($request->requestVars() as $key => $value) { if ($key != 'url') { $queryString .= $key . '=' . $value . '&'; } } $queryString = rtrim($queryString, '&'); } $link = $queryString != '?' ? $request->getURL() . $queryString : $request->getURL(); $link = trim(Director::makeRelative($link)); $map = LinkMapping::get()->filter('MappedLink', $link)->first(); if ($map) { $this->response = new SS_HTTPResponse(); $this->response->redirect($map->getLink(), 301); return $this->response; } } // use OldPageRedirector if it exists, to find old page if (class_exists('OldPageRedirector')) { if ($redirect = OldPageRedirector::find_old_page(array($segment), Multisites::inst()->getCurrentSite())) { $redirect = SiteTree::get_by_link($redirect); } } else { $redirect = self::find_old_page($segment, $site); } if ($redirect) { $getVars = $request->getVars(); //remove the url var as it confuses the routing unset($getVars['url']); $url = Controller::join_links($redirect->Link(Controller::join_links($request->param('Action'), $request->param('ID'), $request->param('OtherID')))); if (!empty($getVars)) { $url .= '?' . http_build_query($getVars); } $this->response->redirect($url, 301); return $this->response; } return $this->httpError(404); } if (class_exists('Translatable') && $page->Locale) { Translatable::set_current_locale($page->Locale); } return self::controller_for($page, $request->param('Action')); }