2011-03-18 04:17:04 +01:00
< ? php
2016-06-16 06:57:19 +02:00
use SilverStripe\ORM\Versioning\Versioned ;
2011-03-18 04:17:04 +01:00
/**
2011-03-22 10:47:26 +01:00
* @ package cms
2011-03-18 04:17:04 +01:00
* @ subpackage tests
*/
class ModelAsControllerTest extends FunctionalTest {
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
protected $usesDatabase = true ;
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
protected $autoFollowRedirection = false ;
protected $orig = array ();
/**
2016-01-06 00:42:07 +01:00
* New tests require nested urls to be enabled , but the site might not
* support nested URLs .
2011-03-18 04:17:04 +01:00
* This setup will enable nested - urls for this test and resets the state
* after the tests have been performed .
*/
2012-09-19 12:07:46 +02:00
public function setUp () {
2011-03-18 04:17:04 +01:00
parent :: setUp ();
2013-03-18 11:47:15 +01:00
$this -> orig [ 'nested_urls' ] = SiteTree :: config () -> nested_urls ;
Config :: inst () -> update ( 'SiteTree' , 'nested_urls' , true );
2011-03-18 04:17:04 +01:00
}
/**
2016-01-06 00:42:07 +01:00
* New tests require nested urls to be enabled , but the site might not
* support nested URLs .
2011-03-18 04:17:04 +01:00
* This setup will enable nested - urls for this test and resets the state
* after the tests have been performed .
*/
2012-09-19 12:07:46 +02:00
public function tearDown () {
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
if ( isset ( $this -> orig [ 'nested_urls' ]) && ! $this -> orig [ 'nested_urls' ]) {
2013-03-18 11:47:15 +01:00
SiteTree :: config () -> nested_urls = false ;
2011-03-18 04:17:04 +01:00
}
2016-03-08 21:50:55 +01:00
parent :: tearDown ();
2011-03-18 04:17:04 +01:00
}
protected function generateNestedPagesFixture () {
$level1 = new Page ();
$level1 -> Title = 'First Level' ;
$level1 -> URLSegment = 'level1' ;
$level1 -> write ();
2016-04-01 05:17:37 +02:00
$level1 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$level1 -> URLSegment = 'newlevel1' ;
$level1 -> write ();
2016-04-01 05:17:37 +02:00
$level1 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$level2 = new Page ();
$level2 -> Title = 'Second Level' ;
$level2 -> URLSegment = 'level2' ;
$level2 -> ParentID = $level1 -> ID ;
$level2 -> write ();
2016-04-01 05:17:37 +02:00
$level2 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$level2 -> URLSegment = 'newlevel2' ;
$level2 -> write ();
2016-04-01 05:17:37 +02:00
$level2 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$level3 = New Page ();
$level3 -> Title = " Level 3 " ;
$level3 -> URLSegment = 'level3' ;
$level3 -> ParentID = $level2 -> ID ;
$level3 -> write ();
2016-04-01 05:17:37 +02:00
$level3 -> copyVersionToStage ( 'Stage' , 'Live' );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$level3 -> URLSegment = 'newlevel3' ;
$level3 -> write ();
2016-04-01 05:17:37 +02:00
$level3 -> copyVersionToStage ( 'Stage' , 'Live' );
2011-03-18 04:17:04 +01:00
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
/**
* We ' re building up a page hierarchy ( " nested URLs " ) and rename
* all the individual pages afterwards . The assumption is that
* all pages will be found by their old segments .
*
* NOTE : This test requires nested_urls
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:17:04 +01:00
* Original : level1 / level2 / level3
* Republished as : newlevel1 / newlevel2 / newlevel3
*/
public function testRedirectsNestedRenamedPages (){
$this -> generateNestedPagesFixture ();
2016-03-08 21:50:55 +01:00
2016-01-06 00:42:07 +01:00
// check a first level URLSegment
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'level1/action' );
$this -> assertEquals ( $response -> getStatusCode (), 301 );
$this -> assertEquals (
Controller :: join_links ( Director :: baseURL () . 'newlevel1/action' ),
$response -> getHeader ( 'Location' )
);
2016-03-08 21:50:55 +01:00
2016-01-06 00:42:07 +01:00
// check second level URLSegment
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'newlevel1/level2' );
$this -> assertEquals ( $response -> getStatusCode (), 301 );
$this -> assertEquals (
Controller :: join_links ( Director :: baseURL () . 'newlevel1/newlevel2/' ),
$response -> getHeader ( 'Location' )
);
2016-03-08 21:50:55 +01:00
2016-01-06 00:42:07 +01:00
// check third level URLSegment
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'newlevel1/newlevel2/level3' );
$this -> assertEquals ( $response -> getStatusCode (), 301 );
$this -> assertEquals (
Controller :: join_links ( Director :: baseURL () . 'newlevel1/newlevel2/newlevel3/' ),
$response -> getHeader ( 'Location' )
);
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'newlevel1/newlevel2/level3' );
}
2015-08-20 14:19:08 +02:00
/**
* Test that the redirect works even with a lot of nested pages
* Original : / oldurl / level2 / level3 / level4 / level5
* New : / newurl / level2 / level3 / level4 / level5
*/
public function testHeavilyNestedRenamedRedirectedPages () {
$page = new Page ();
$page -> Title = 'First Level' ;
$page -> URLSegment = 'oldurl' ;
$page -> write ();
2016-04-01 05:17:37 +02:00
$page -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2015-08-20 14:19:08 +02:00
$page -> URLSegment = 'newurl' ;
$page -> write ();
2016-04-01 05:17:37 +02:00
$page -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2015-08-20 14:19:08 +02:00
$page2 = new Page ();
$page2 -> Title = 'Second Level Page' ;
$page2 -> URLSegment = 'level2' ;
$page2 -> ParentID = $page -> ID ;
$page2 -> write ();
2016-04-01 05:17:37 +02:00
$page2 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2015-08-20 14:19:08 +02:00
$page3 = new Page ();
$page3 -> Title = 'Third Level Page' ;
$page3 -> URLSegment = 'level3' ;
$page3 -> ParentID = $page2 -> ID ;
$page3 -> write ();
2016-04-01 05:17:37 +02:00
$page3 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2015-08-20 14:19:08 +02:00
$page4 = new Page ();
$page4 -> Title = 'Fourth Level Page' ;
$page4 -> URLSegment = 'level4' ;
$page4 -> ParentID = $page3 -> ID ;
$page4 -> write ();
2016-04-01 05:17:37 +02:00
$page4 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2015-08-20 14:19:08 +02:00
$page5 = new Page ();
$page5 -> Title = 'Fifth Level Page' ;
$page5 -> URLSegment = 'level5' ;
$page5 -> ParentID = $page4 -> ID ;
$page5 -> write ();
2016-04-01 05:17:37 +02:00
$page5 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2015-08-20 14:19:08 +02:00
// Test that the redirect still works fine when trying to access the most nested page
$response = $this -> get ( 'oldurl/level2/level3/level4/level5/' );
$this -> assertEquals ( $response -> getStatusCode (), 301 );
$this -> assertEquals (
Controller :: join_links ( Director :: baseURL () . 'newurl/level2/level3/level4/level5/' ),
$response -> getHeader ( 'Location' )
);
}
2011-03-18 04:17:04 +01:00
public function testRedirectionForPreNestedurlsBookmarks (){
$this -> generateNestedPagesFixture ();
// Up-to-date URLs will be redirected to the appropriate subdirectory
$response = $this -> get ( 'newlevel3' );
$this -> assertEquals ( 301 , $response -> getStatusCode ());
$this -> assertEquals ( Director :: baseURL () . 'newlevel1/newlevel2/newlevel3/' ,
$response -> getHeader ( " Location " ));
// So will the legacy ones
$response = $this -> get ( 'level3' );
$this -> assertEquals ( 301 , $response -> getStatusCode ());
$this -> assertEquals ( Director :: baseURL () . 'newlevel1/newlevel2/newlevel3/' ,
$response -> getHeader ( " Location " ));
}
2012-09-19 12:07:46 +02:00
public function testDoesntRedirectToNestedChildrenOutsideOfOwnHierarchy () {
2011-03-18 04:17:04 +01:00
$this -> generateNestedPagesFixture ();
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$otherParent = new Page ( array (
'URLSegment' => 'otherparent'
));
$otherParent -> write ();
2016-04-01 05:17:37 +02:00
$otherParent -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'level1/otherparent' );
$this -> assertEquals ( $response -> getStatusCode (), 301 );
$response = $this -> get ( 'newlevel1/otherparent' );
$this -> assertEquals (
2016-01-06 00:42:07 +01:00
$response -> getStatusCode (),
2011-03-18 04:17:04 +01:00
404 ,
'Requesting an unrelated page on a renamed parent should be interpreted as a missing action, not a redirect'
);
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
/**
*
* NOTE : This test requires nested_urls
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:17:04 +01:00
*/
2012-09-19 12:07:46 +02:00
public function testRedirectsNestedRenamedPagesWithGetParameters () {
2011-03-18 04:17:04 +01:00
$this -> generateNestedPagesFixture ();
2016-03-08 21:50:55 +01:00
2016-01-06 00:42:07 +01:00
// check third level URLSegment
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'newlevel1/newlevel2/level3/?foo=bar&test=test' );
$this -> assertEquals ( $response -> getStatusCode (), 301 );
$this -> assertEquals (
Controller :: join_links ( Director :: baseURL () . 'newlevel1/newlevel2/newlevel3/' , '?foo=bar&test=test' ),
$response -> getHeader ( 'Location' )
);
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
/**
*
* NOTE : This test requires nested_urls
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:17:04 +01:00
*/
2012-09-19 12:07:46 +02:00
public function testDoesntRedirectToNestedRenamedPageWhenNewExists () {
2011-03-18 04:17:04 +01:00
$this -> generateNestedPagesFixture ();
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$otherLevel1 = new Page ( array (
'Title' => " Other Level 1 " ,
'URLSegment' => 'level1'
));
$otherLevel1 -> write ();
2016-04-01 05:17:37 +02:00
$otherLevel1 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'level1' );
$this -> assertEquals (
2016-01-06 00:42:07 +01:00
$response -> getStatusCode (),
2011-03-18 04:17:04 +01:00
200
);
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$response = $this -> get ( 'level1/newlevel2' );
$this -> assertEquals (
2016-01-06 00:42:07 +01:00
$response -> getStatusCode (),
404 ,
2011-03-18 04:17:04 +01:00
'The old newlevel2/ URLSegment is checked as an action on the new page, which shouldnt exist.'
);
}
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
/**
*
* NOTE : This test requires nested_urls
2016-01-06 00:42:07 +01:00
*
2011-03-18 04:17:04 +01:00
*/
2012-09-19 12:07:46 +02:00
public function testFindOldPage (){
2011-03-18 04:17:04 +01:00
$page = new Page ();
$page -> Title = 'First Level' ;
$page -> URLSegment = 'oldurl' ;
$page -> write ();
2016-04-01 05:17:37 +02:00
$page -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$page -> URLSegment = 'newurl' ;
$page -> write ();
2016-04-01 05:17:37 +02:00
$page -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2013-12-06 14:28:08 +01:00
$url = OldPageRedirector :: find_old_page ( 'oldurl' );
$matchedPage = SiteTree :: get_by_link ( $url );
$this -> assertEquals ( 'First Level' , $matchedPage -> Title );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$page2 = new Page ();
$page2 -> Title = 'Second Level Page' ;
$page2 -> URLSegment = 'oldpage2' ;
$page2 -> ParentID = $page -> ID ;
$page2 -> write ();
2016-04-01 05:17:37 +02:00
$page2 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2011-03-18 04:17:04 +01:00
$page2 -> URLSegment = 'newpage2' ;
$page2 -> write ();
2016-04-01 05:17:37 +02:00
$page2 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2016-03-08 21:50:55 +01:00
2013-12-06 14:28:08 +01:00
$url = OldPageRedirector :: find_old_page ( 'oldpage2' , $page2 -> ParentID );
$matchedPage = SiteTree :: get_by_link ( $url );
$this -> assertEquals ( 'Second Level Page' , $matchedPage -> Title );
2016-03-08 21:50:55 +01:00
2013-12-06 14:28:08 +01:00
$url = OldPageRedirector :: find_old_page ( 'oldpage2' , $page2 -> ID );
$matchedPage = SiteTree :: get_by_link ( $url );
$this -> assertEquals ( false , $matchedPage );
2011-03-18 04:17:04 +01:00
}
2012-07-18 06:47:17 +02:00
/**
* go to a page that ' s been published but is child of an unpublished page
*
* NOTE : This test requires nested_urls
*/
2012-09-19 12:07:46 +02:00
public function testChildOfDraft () {
2012-07-18 06:47:17 +02:00
RootURLController :: reset ();
2013-03-18 11:47:15 +01:00
Config :: inst () -> update ( 'SiteTree' , 'nested_urls' , true );
2012-07-18 06:47:17 +02:00
$draft = new Page ();
$draft -> Title = 'Root Leve Draft Page' ;
$draft -> URLSegment = 'root' ;
$draft -> write ();
$published = new Page ();
$published -> Title = 'Published Page Under Draft Page' ;
$published -> URLSegment = 'sub-root' ;
$published -> write ();
2016-04-01 05:17:37 +02:00
$published -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
2012-07-18 06:47:17 +02:00
$response = $this -> get ( 'root/sub-root' );
$this -> assertEquals (
$response -> getStatusCode (),
404 ,
'The page should not be found since its parent has not been published, in this case http://<yousitename>/root/sub-root or http://<yousitename>/sub-root'
);
}
2012-04-12 09:23:20 +02:00
}