/**
  *	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();
 }
 /**
  *	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();
 }