mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 06:05:56 +00:00
Merge branch '3.4' into 3
This commit is contained in:
commit
6d9b4415f1
@ -38,43 +38,38 @@ class OldPageRedirector extends Extension {
|
|||||||
* Attempt to find an old/renamed page from some given the URL as an array
|
* Attempt to find an old/renamed page from some given the URL as an array
|
||||||
*
|
*
|
||||||
* @param array $params The array of URL, e.g. /foo/bar as array('foo', 'bar')
|
* @param array $params The array of URL, e.g. /foo/bar as array('foo', 'bar')
|
||||||
* @param SiteTree $parent The current parent in the recursive flow
|
* @param SiteTree|null $parent The current parent in the recursive flow
|
||||||
* @param boolean $redirect Whether we've found an old page worthy of a redirect
|
* @param boolean $redirect Whether we've found an old page worthy of a redirect
|
||||||
*
|
*
|
||||||
* @return string|boolean False, or the new URL
|
* @return string|boolean False, or the new URL
|
||||||
*/
|
*/
|
||||||
static public function find_old_page($params, $parent = null, $redirect = false) {
|
static public function find_old_page($params, $parent = null, $redirect = false) {
|
||||||
$parent = is_numeric($parent) ? SiteTree::get()->byId($parent) : $parent;
|
$parent = is_numeric($parent) ? SiteTree::get()->byID($parent) : $parent;
|
||||||
$params = (array)$params;
|
$params = (array)$params;
|
||||||
$URL = rawurlencode(array_shift($params));
|
$URL = rawurlencode(array_shift($params));
|
||||||
if (empty($URL)) { return false; }
|
if (empty($URL)) { return false; }
|
||||||
|
$pages = SiteTree::get()->filter(array(
|
||||||
|
'URLSegment' => $URL,
|
||||||
|
));
|
||||||
if ($parent) {
|
if ($parent) {
|
||||||
$page = SiteTree::get()->filter(array('ParentID' => $parent->ID, 'URLSegment' => $URL))->First();
|
$pages = $pages->filter(array(
|
||||||
} else {
|
'ParentID' => $parent->ID,
|
||||||
$page = SiteTree::get()->filter(array('URLSegment' => $URL))->First();
|
));
|
||||||
}
|
}
|
||||||
|
$page = $pages->first();
|
||||||
|
|
||||||
if (!$page) {
|
if (!$page) {
|
||||||
// If we haven't found a candidate, lets resort to finding an old page with this URL segment
|
// If we haven't found a candidate, lets resort to finding an old page with this URL segment
|
||||||
$oldFilter = array(
|
$pages = $pages
|
||||||
'"SiteTree_versions"."URLSegment"' => $URL,
|
->filter(array(
|
||||||
'"SiteTree_versions"."WasPublished"' => true
|
'WasPublished' => true,
|
||||||
);
|
))
|
||||||
if($parent) {
|
->sort('LastEdited', 'DESC')
|
||||||
$oldFilter[] = array('"SiteTree_versions"."ParentID"' => $parent->ID);
|
->setDataQueryParam("Versioned.mode", 'all_versions');
|
||||||
}
|
|
||||||
$query = new SQLSelect(
|
$record = $pages->first();
|
||||||
'"RecordID"',
|
|
||||||
'"SiteTree_versions"',
|
|
||||||
$oldFilter,
|
|
||||||
'"LastEdited" DESC',
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
1
|
|
||||||
);
|
|
||||||
$record = $query->execute()->first();
|
|
||||||
if ($record) {
|
if ($record) {
|
||||||
$page = SiteTree::get()->byID($record['RecordID']);
|
$page = SiteTree::get()->byID($record->RecordID);
|
||||||
$redirect = true;
|
$redirect = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,14 +4,12 @@
|
|||||||
* @subpackage tests
|
* @subpackage tests
|
||||||
*/
|
*/
|
||||||
class ModelAsControllerTest extends FunctionalTest {
|
class ModelAsControllerTest extends FunctionalTest {
|
||||||
|
|
||||||
protected $usesDatabase = true;
|
|
||||||
|
|
||||||
protected static $fixture_file = 'ModelAsControllerTest.yml';
|
|
||||||
|
|
||||||
protected $autoFollowRedirection = false;
|
|
||||||
|
|
||||||
protected $orig = array();
|
protected $usesDatabase = true;
|
||||||
|
|
||||||
|
protected static $fixture_file = 'ModelAsControllerTest.yml';
|
||||||
|
|
||||||
|
protected $autoFollowRedirection = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* New tests require nested urls to be enabled, but the site might not
|
* New tests require nested urls to be enabled, but the site might not
|
||||||
@ -22,24 +20,9 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
public function setUp() {
|
public function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->orig['nested_urls'] = SiteTree::config()->nested_urls;
|
|
||||||
Config::inst()->update('SiteTree', 'nested_urls', true);
|
Config::inst()->update('SiteTree', 'nested_urls', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* New tests require nested urls to be enabled, but the site might not
|
|
||||||
* support nested URLs.
|
|
||||||
* This setup will enable nested-urls for this test and resets the state
|
|
||||||
* after the tests have been performed.
|
|
||||||
*/
|
|
||||||
public function tearDown() {
|
|
||||||
|
|
||||||
if (isset($this->orig['nested_urls']) && !$this->orig['nested_urls']) {
|
|
||||||
SiteTree::config()->nested_urls = false;
|
|
||||||
}
|
|
||||||
parent::tearDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected function generateNestedPagesFixture() {
|
protected function generateNestedPagesFixture() {
|
||||||
$level1 = new Page();
|
$level1 = new Page();
|
||||||
@ -47,34 +30,34 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
$level1->URLSegment = 'level1';
|
$level1->URLSegment = 'level1';
|
||||||
$level1->write();
|
$level1->write();
|
||||||
$level1->publish('Stage', 'Live');
|
$level1->publish('Stage', 'Live');
|
||||||
|
|
||||||
$level1->URLSegment = 'newlevel1';
|
$level1->URLSegment = 'newlevel1';
|
||||||
$level1->write();
|
$level1->write();
|
||||||
$level1->publish('Stage', 'Live');
|
$level1->publish('Stage', 'Live');
|
||||||
|
|
||||||
$level2 = new Page();
|
$level2 = new Page();
|
||||||
$level2->Title = 'Second Level';
|
$level2->Title = 'Second Level';
|
||||||
$level2->URLSegment = 'level2';
|
$level2->URLSegment = 'level2';
|
||||||
$level2->ParentID = $level1->ID;
|
$level2->ParentID = $level1->ID;
|
||||||
$level2->write();
|
$level2->write();
|
||||||
$level2->publish('Stage', 'Live');
|
$level2->publish('Stage', 'Live');
|
||||||
|
|
||||||
$level2->URLSegment = 'newlevel2';
|
$level2->URLSegment = 'newlevel2';
|
||||||
$level2->write();
|
$level2->write();
|
||||||
$level2->publish('Stage', 'Live');
|
$level2->publish('Stage', 'Live');
|
||||||
|
|
||||||
$level3 = New Page();
|
$level3 = New Page();
|
||||||
$level3->Title = "Level 3";
|
$level3->Title = "Level 3";
|
||||||
$level3->URLSegment = 'level3';
|
$level3->URLSegment = 'level3';
|
||||||
$level3->ParentID = $level2->ID;
|
$level3->ParentID = $level2->ID;
|
||||||
$level3->write();
|
$level3->write();
|
||||||
$level3->publish('Stage','Live');
|
$level3->publish('Stage','Live');
|
||||||
|
|
||||||
$level3->URLSegment = 'newlevel3';
|
$level3->URLSegment = 'newlevel3';
|
||||||
$level3->write();
|
$level3->write();
|
||||||
$level3->publish('Stage','Live');
|
$level3->publish('Stage','Live');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We're building up a page hierarchy ("nested URLs") and rename
|
* We're building up a page hierarchy ("nested URLs") and rename
|
||||||
* all the individual pages afterwards. The assumption is that
|
* all the individual pages afterwards. The assumption is that
|
||||||
@ -87,7 +70,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
*/
|
*/
|
||||||
public function testRedirectsNestedRenamedPages(){
|
public function testRedirectsNestedRenamedPages(){
|
||||||
$this->generateNestedPagesFixture();
|
$this->generateNestedPagesFixture();
|
||||||
|
|
||||||
// check a first level URLSegment
|
// check a first level URLSegment
|
||||||
$response = $this->get('level1/action');
|
$response = $this->get('level1/action');
|
||||||
$this->assertEquals($response->getStatusCode(),301);
|
$this->assertEquals($response->getStatusCode(),301);
|
||||||
@ -95,7 +78,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
Controller::join_links(Director::baseURL() . 'newlevel1/action'),
|
Controller::join_links(Director::baseURL() . 'newlevel1/action'),
|
||||||
$response->getHeader('Location')
|
$response->getHeader('Location')
|
||||||
);
|
);
|
||||||
|
|
||||||
// check second level URLSegment
|
// check second level URLSegment
|
||||||
$response = $this->get('newlevel1/level2');
|
$response = $this->get('newlevel1/level2');
|
||||||
$this->assertEquals($response->getStatusCode(),301 );
|
$this->assertEquals($response->getStatusCode(),301 );
|
||||||
@ -103,7 +86,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
Controller::join_links(Director::baseURL() . 'newlevel1/newlevel2/'),
|
Controller::join_links(Director::baseURL() . 'newlevel1/newlevel2/'),
|
||||||
$response->getHeader('Location')
|
$response->getHeader('Location')
|
||||||
);
|
);
|
||||||
|
|
||||||
// check third level URLSegment
|
// check third level URLSegment
|
||||||
$response = $this->get('newlevel1/newlevel2/level3');
|
$response = $this->get('newlevel1/newlevel2/level3');
|
||||||
$this->assertEquals($response->getStatusCode(), 301);
|
$this->assertEquals($response->getStatusCode(), 301);
|
||||||
@ -111,7 +94,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
Controller::join_links(Director::baseURL() . 'newlevel1/newlevel2/newlevel3/'),
|
Controller::join_links(Director::baseURL() . 'newlevel1/newlevel2/newlevel3/'),
|
||||||
$response->getHeader('Location')
|
$response->getHeader('Location')
|
||||||
);
|
);
|
||||||
|
|
||||||
$response = $this->get('newlevel1/newlevel2/level3');
|
$response = $this->get('newlevel1/newlevel2/level3');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +109,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
$page->URLSegment = 'oldurl';
|
$page->URLSegment = 'oldurl';
|
||||||
$page->write();
|
$page->write();
|
||||||
$page->publish('Stage', 'Live');
|
$page->publish('Stage', 'Live');
|
||||||
|
|
||||||
$page->URLSegment = 'newurl';
|
$page->URLSegment = 'newurl';
|
||||||
$page->write();
|
$page->write();
|
||||||
$page->publish('Stage', 'Live');
|
$page->publish('Stage', 'Live');
|
||||||
@ -137,21 +120,21 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
$page2->ParentID = $page->ID;
|
$page2->ParentID = $page->ID;
|
||||||
$page2->write();
|
$page2->write();
|
||||||
$page2->publish('Stage', 'Live');
|
$page2->publish('Stage', 'Live');
|
||||||
|
|
||||||
$page3 = new Page();
|
$page3 = new Page();
|
||||||
$page3->Title = 'Third Level Page';
|
$page3->Title = 'Third Level Page';
|
||||||
$page3->URLSegment = 'level3';
|
$page3->URLSegment = 'level3';
|
||||||
$page3->ParentID = $page2->ID;
|
$page3->ParentID = $page2->ID;
|
||||||
$page3->write();
|
$page3->write();
|
||||||
$page3->publish('Stage', 'Live');
|
$page3->publish('Stage', 'Live');
|
||||||
|
|
||||||
$page4 = new Page();
|
$page4 = new Page();
|
||||||
$page4->Title = 'Fourth Level Page';
|
$page4->Title = 'Fourth Level Page';
|
||||||
$page4->URLSegment = 'level4';
|
$page4->URLSegment = 'level4';
|
||||||
$page4->ParentID = $page3->ID;
|
$page4->ParentID = $page3->ID;
|
||||||
$page4->write();
|
$page4->write();
|
||||||
$page4->publish('Stage', 'Live');
|
$page4->publish('Stage', 'Live');
|
||||||
|
|
||||||
$page5 = new Page();
|
$page5 = new Page();
|
||||||
$page5->Title = 'Fifth Level Page';
|
$page5->Title = 'Fifth Level Page';
|
||||||
$page5->URLSegment = 'level5';
|
$page5->URLSegment = 'level5';
|
||||||
@ -187,13 +170,13 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
|
|
||||||
public function testDoesntRedirectToNestedChildrenOutsideOfOwnHierarchy() {
|
public function testDoesntRedirectToNestedChildrenOutsideOfOwnHierarchy() {
|
||||||
$this->generateNestedPagesFixture();
|
$this->generateNestedPagesFixture();
|
||||||
|
|
||||||
$otherParent = new Page(array(
|
$otherParent = new Page(array(
|
||||||
'URLSegment' => 'otherparent'
|
'URLSegment' => 'otherparent'
|
||||||
));
|
));
|
||||||
$otherParent->write();
|
$otherParent->write();
|
||||||
$otherParent->publish('Stage', 'Live');
|
$otherParent->publish('Stage', 'Live');
|
||||||
|
|
||||||
$response = $this->get('level1/otherparent');
|
$response = $this->get('level1/otherparent');
|
||||||
$this->assertEquals($response->getStatusCode(), 301);
|
$this->assertEquals($response->getStatusCode(), 301);
|
||||||
|
|
||||||
@ -204,7 +187,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
'Requesting an unrelated page on a renamed parent should be interpreted as a missing action, not a redirect'
|
'Requesting an unrelated page on a renamed parent should be interpreted as a missing action, not a redirect'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* NOTE: This test requires nested_urls
|
* NOTE: This test requires nested_urls
|
||||||
@ -212,7 +195,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
*/
|
*/
|
||||||
public function testRedirectsNestedRenamedPagesWithGetParameters() {
|
public function testRedirectsNestedRenamedPagesWithGetParameters() {
|
||||||
$this->generateNestedPagesFixture();
|
$this->generateNestedPagesFixture();
|
||||||
|
|
||||||
// check third level URLSegment
|
// check third level URLSegment
|
||||||
$response = $this->get('newlevel1/newlevel2/level3/?foo=bar&test=test');
|
$response = $this->get('newlevel1/newlevel2/level3/?foo=bar&test=test');
|
||||||
$this->assertEquals($response->getStatusCode(), 301);
|
$this->assertEquals($response->getStatusCode(), 301);
|
||||||
@ -221,7 +204,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
$response->getHeader('Location')
|
$response->getHeader('Location')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* NOTE: This test requires nested_urls
|
* NOTE: This test requires nested_urls
|
||||||
@ -229,20 +212,20 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
*/
|
*/
|
||||||
public function testDoesntRedirectToNestedRenamedPageWhenNewExists() {
|
public function testDoesntRedirectToNestedRenamedPageWhenNewExists() {
|
||||||
$this->generateNestedPagesFixture();
|
$this->generateNestedPagesFixture();
|
||||||
|
|
||||||
$otherLevel1 = new Page(array(
|
$otherLevel1 = new Page(array(
|
||||||
'Title' => "Other Level 1",
|
'Title' => "Other Level 1",
|
||||||
'URLSegment' => 'level1'
|
'URLSegment' => 'level1'
|
||||||
));
|
));
|
||||||
$otherLevel1->write();
|
$otherLevel1->write();
|
||||||
$otherLevel1->publish('Stage', 'Live');
|
$otherLevel1->publish('Stage', 'Live');
|
||||||
|
|
||||||
$response = $this->get('level1');
|
$response = $this->get('level1');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$response->getStatusCode(),
|
$response->getStatusCode(),
|
||||||
200
|
200
|
||||||
);
|
);
|
||||||
|
|
||||||
$response = $this->get('level1/newlevel2');
|
$response = $this->get('level1/newlevel2');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$response->getStatusCode(),
|
$response->getStatusCode(),
|
||||||
@ -250,7 +233,7 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
'The old newlevel2/ URLSegment is checked as an action on the new page, which shouldnt exist.'
|
'The old newlevel2/ URLSegment is checked as an action on the new page, which shouldnt exist.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* NOTE: This test requires nested_urls
|
* NOTE: This test requires nested_urls
|
||||||
@ -262,30 +245,30 @@ class ModelAsControllerTest extends FunctionalTest {
|
|||||||
$page->URLSegment = 'oldurl';
|
$page->URLSegment = 'oldurl';
|
||||||
$page->write();
|
$page->write();
|
||||||
$page->publish('Stage', 'Live');
|
$page->publish('Stage', 'Live');
|
||||||
|
|
||||||
$page->URLSegment = 'newurl';
|
$page->URLSegment = 'newurl';
|
||||||
$page->write();
|
$page->write();
|
||||||
$page->publish('Stage', 'Live');
|
$page->publish('Stage', 'Live');
|
||||||
|
|
||||||
$url = OldPageRedirector::find_old_page('oldurl');
|
$url = OldPageRedirector::find_old_page('oldurl');
|
||||||
$matchedPage = SiteTree::get_by_link($url);
|
$matchedPage = SiteTree::get_by_link($url);
|
||||||
$this->assertEquals('First Level',$matchedPage->Title);
|
$this->assertEquals('First Level',$matchedPage->Title);
|
||||||
|
|
||||||
$page2 = new Page();
|
$page2 = new Page();
|
||||||
$page2->Title = 'Second Level Page';
|
$page2->Title = 'Second Level Page';
|
||||||
$page2->URLSegment = 'oldpage2';
|
$page2->URLSegment = 'oldpage2';
|
||||||
$page2->ParentID = $page->ID;
|
$page2->ParentID = $page->ID;
|
||||||
$page2->write();
|
$page2->write();
|
||||||
$page2->publish('Stage', 'Live');
|
$page2->publish('Stage', 'Live');
|
||||||
|
|
||||||
$page2->URLSegment = 'newpage2';
|
$page2->URLSegment = 'newpage2';
|
||||||
$page2->write();
|
$page2->write();
|
||||||
$page2->publish('Stage', 'Live');
|
$page2->publish('Stage', 'Live');
|
||||||
|
|
||||||
$url = OldPageRedirector::find_old_page('oldpage2',$page2->ParentID);
|
$url = OldPageRedirector::find_old_page('oldpage2',$page2->ParentID);
|
||||||
$matchedPage = SiteTree::get_by_link($url);
|
$matchedPage = SiteTree::get_by_link($url);
|
||||||
$this->assertEquals('Second Level Page',$matchedPage->Title);
|
$this->assertEquals('Second Level Page',$matchedPage->Title);
|
||||||
|
|
||||||
$url = OldPageRedirector::find_old_page('oldpage2',$page2->ID);
|
$url = OldPageRedirector::find_old_page('oldpage2',$page2->ID);
|
||||||
$matchedPage = SiteTree::get_by_link($url);
|
$matchedPage = SiteTree::get_by_link($url);
|
||||||
$this->assertEquals(false, $matchedPage);
|
$this->assertEquals(false, $matchedPage);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user