From e280929b299f0f06f85c63c3c0bd2ca18fa347cf Mon Sep 17 00:00:00 2001 From: Paul Meyrick Date: Fri, 18 Mar 2011 16:17:04 +1300 Subject: [PATCH] Minor Moved from /sapphire/tests/control/ to cms module cms/tests --- tests/ContentControllerPermissionsTest.php | 35 +++ tests/ContentControllerTest.php | 144 +++++++++++++ tests/ContentControllerTest.yml | 46 ++++ tests/ModelAsControllerTest.php | 237 +++++++++++++++++++++ tests/RootURLControllerTest.php | 76 +++++++ tests/RootURLControllerTest.yml | 18 ++ 6 files changed, 556 insertions(+) create mode 100644 tests/ContentControllerPermissionsTest.php create mode 100755 tests/ContentControllerTest.php create mode 100755 tests/ContentControllerTest.yml create mode 100644 tests/ModelAsControllerTest.php create mode 100755 tests/RootURLControllerTest.php create mode 100755 tests/RootURLControllerTest.yml diff --git a/tests/ContentControllerPermissionsTest.php b/tests/ContentControllerPermissionsTest.php new file mode 100644 index 00000000..96625371 --- /dev/null +++ b/tests/ContentControllerPermissionsTest.php @@ -0,0 +1,35 @@ +URLSegment = 'testpage'; + $page->write(); + $page->publish('Stage', 'Live'); + + $response = $this->get('/testpage'); + $this->assertEquals($response->getStatusCode(), 200); + + $response = $this->get('/testpage/?stage=Live'); + $this->assertEquals($response->getStatusCode(), 200); + + $response = $this->get('/testpage/?stage=Stage'); + // should redirect to login + $this->assertEquals($response->getStatusCode(), 302); + + $this->logInWithPermission('CMS_ACCESS_CMSMain'); + + $response = $this->get('/testpage/?stage=Stage'); + $this->assertEquals($response->getStatusCode(), 200); + } + + +} \ No newline at end of file diff --git a/tests/ContentControllerTest.php b/tests/ContentControllerTest.php new file mode 100755 index 00000000..287e96b5 --- /dev/null +++ b/tests/ContentControllerTest.php @@ -0,0 +1,144 @@ +assertEquals('Home Page', $this->get('/')->getBody()); + $this->assertEquals('Home Page', $this->get('/home/index/')->getBody()); + $this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody()); + + $this->assertEquals('Second Level Page', $this->get('/home/second-level/')->getBody()); + $this->assertEquals('Second Level Page', $this->get('/home/second-level/index/')->getBody()); + $this->assertEquals('Second Level Page', $this->get('/home/second-level/second-index/')->getBody()); + + $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/')->getBody()); + $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/index/')->getBody()); + $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/second-index/')->getBody()); + + RootURLController::reset(); + SiteTree::disable_nested_urls(); + + $this->assertEquals('Home Page', $this->get('/')->getBody()); + $this->assertEquals('Home Page', $this->get('/home/')->getBody()); + $this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody()); + + $this->assertEquals('Second Level Page', $this->get('/second-level/')->getBody()); + $this->assertEquals('Second Level Page', $this->get('/second-level/index/')->getBody()); + $this->assertEquals('Second Level Page', $this->get('/second-level/second-index/')->getBody()); + + $this->assertEquals('Third Level Page', $this->get('/third-level/')->getBody()); + $this->assertEquals('Third Level Page', $this->get('/third-level/index/')->getBody()); + $this->assertEquals('Third Level Page', $this->get('/third-level/second-index/')->getBody()); + } + + /** + * Tests {@link ContentController::ChildrenOf()} + */ + public function testChildrenOf() { + $controller = new ContentController(); + + SiteTree::enable_nested_urls(); + + $this->assertEquals(1, $controller->ChildrenOf('/')->Count()); + $this->assertEquals(1, $controller->ChildrenOf('/home/')->Count()); + $this->assertEquals(2, $controller->ChildrenOf('/home/second-level/')->Count()); + $this->assertEquals(0, $controller->ChildrenOf('/home/second-level/third-level/')->Count()); + + SiteTree::disable_nested_urls(); + + $this->assertEquals(1, $controller->ChildrenOf('/')->Count()); + $this->assertEquals(1, $controller->ChildrenOf('/home/')->Count()); + $this->assertEquals(2, $controller->ChildrenOf('/second-level/')->Count()); + $this->assertEquals(0, $controller->ChildrenOf('/third-level/')->Count()); + } + + public function testDeepNestedURLs() { + SiteTree::enable_nested_urls(); + + $page = new Page(); + $page->URLSegment = 'base-page'; + $page->write(); + + for($i = 0; $i < 10; $i++) { + $parentID = $page->ID; + + $page = new ContentControllerTest_Page(); + $page->ParentID = $parentID; + $page->Title = "Page Level $i"; + $page->URLSegment = "level-$i"; + $page->write(); + + $relativeLink = Director::makeRelative($page->Link()); + $this->assertEquals($page->Title, $this->get($relativeLink)->getBody()); + } + + + SiteTree::disable_nested_urls(); + } + + public function testViewDraft(){ + + // test when user does not have permission, should get login form + $this->logInWithPermission('EDITOR'); + $this->assertEquals('403', $this->get('/contact/?stage=Stage')->getstatusCode()); + + + // test when user does have permission, should show page title and header ok. + $this->logInWithPermission('ADMIN'); + $this->assertEquals('200', $this->get('/contact/?stage=Stage')->getstatusCode()); + + + } + + public function testLinkShortcodes() { + $linkedPage = new SiteTree(); + $linkedPage->URLSegment = 'linked-page'; + $linkedPage->write(); + $linkedPage->publish('Stage', 'Live'); + + $page = new SiteTree(); + $page->URLSegment = 'linking-page'; + $page->Content = sprintf('Testlink', $linkedPage->ID); + $page->write(); + $page->publish('Stage', 'Live'); + + $this->assertContains( + sprintf('Testlink', $linkedPage->Link()), + $this->get($page->RelativeLink())->getBody(), + '"sitetree_link" shortcodes get parsed properly' + ); + } + +} + +class ContentControllerTest_Page extends Page { } + +class ContentControllerTest_Page_Controller extends Page_Controller { + + public static $allowed_actions = array ( + 'second_index' + ); + + public function index() { + return $this->Title; + } + + public function second_index() { + return $this->index(); + } + +} \ No newline at end of file diff --git a/tests/ContentControllerTest.yml b/tests/ContentControllerTest.yml new file mode 100755 index 00000000..d0a102a3 --- /dev/null +++ b/tests/ContentControllerTest.yml @@ -0,0 +1,46 @@ +Group: + editors: + Title: Editors + admins: + Title: Administrators + +Permission: + admins: + Code: VIEW_DRAFT_CONTENT + Group: =>Group.admins + editors: + Group: =>Group.editors + +Member: + editor: + FirstName: Test + Surname: Editor + Groups: =>Group.editors + admin: + FirstName: Test + Surname: Administrator + Groups: =>Group.admins + + +ContentControllerTest_Page: + root_page: + Title: Home Page + URLSegment: home + second_level_page: + Title: Second Level Page + URLSegment: second-level + Parent: =>ContentControllerTest_Page.root_page + third_level_page: + Title: Third Level Page + URLSegment: third-level + Parent: =>ContentControllerTest_Page.second_level_page + third_level_page_2: + Title: Third Level Page Two + URLSegment: third-level-2 + Parent: =>ContentControllerTest_Page.second_level_page + contact_page: + Title: Contact Page + URLSegment: contact + CanViewType: OnlyTheseUsers + EditorGroups: =>Group.admins + \ No newline at end of file diff --git a/tests/ModelAsControllerTest.php b/tests/ModelAsControllerTest.php new file mode 100644 index 00000000..fd780b5a --- /dev/null +++ b/tests/ModelAsControllerTest.php @@ -0,0 +1,237 @@ +orig['nested_urls'] = SiteTree::nested_urls(); + SiteTree::enable_nested_urls(); + } + + /** + * 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. + */ + function tearDown() { + + if (isset($this->orig['nested_urls']) && !$this->orig['nested_urls']) { + SiteTree::disable_nested_urls(); + } + parent::tearDown(); + } + + + protected function generateNestedPagesFixture() { + $level1 = new Page(); + $level1->Title = 'First Level'; + $level1->URLSegment = 'level1'; + $level1->write(); + $level1->publish('Stage', 'Live'); + + $level1->URLSegment = 'newlevel1'; + $level1->write(); + $level1->publish('Stage', 'Live'); + + $level2 = new Page(); + $level2->Title = 'Second Level'; + $level2->URLSegment = 'level2'; + $level2->ParentID = $level1->ID; + $level2->write(); + $level2->publish('Stage', 'Live'); + + $level2->URLSegment = 'newlevel2'; + $level2->write(); + $level2->publish('Stage', 'Live'); + + $level3 = New Page(); + $level3->Title = "Level 3"; + $level3->URLSegment = 'level3'; + $level3->ParentID = $level2->ID; + $level3->write(); + $level3->publish('Stage','Live'); + + $level3->URLSegment = 'newlevel3'; + $level3->write(); + $level3->publish('Stage','Live'); + } + + /** + * 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 + * + * Original: level1/level2/level3 + * Republished as: newlevel1/newlevel2/newlevel3 + */ + public function testRedirectsNestedRenamedPages(){ + $this->generateNestedPagesFixture(); + + // check a first level URLSegment + $response = $this->get('level1/action'); + $this->assertEquals($response->getStatusCode(),301); + $this->assertEquals( + Controller::join_links(Director::baseURL() . 'newlevel1/action'), + $response->getHeader('Location') + ); + + // check second level URLSegment + $response = $this->get('newlevel1/level2'); + $this->assertEquals($response->getStatusCode(),301 ); + $this->assertEquals( + Controller::join_links(Director::baseURL() . 'newlevel1/newlevel2/'), + $response->getHeader('Location') + ); + + // check third level URLSegment + $response = $this->get('newlevel1/newlevel2/level3'); + $this->assertEquals($response->getStatusCode(), 301); + $this->assertEquals( + Controller::join_links(Director::baseURL() . 'newlevel1/newlevel2/newlevel3/'), + $response->getHeader('Location') + ); + + $response = $this->get('newlevel1/newlevel2/level3'); + } + + 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")); + } + + function testDoesntRedirectToNestedChildrenOutsideOfOwnHierarchy() { + $this->generateNestedPagesFixture(); + + $otherParent = new Page(array( + 'URLSegment' => 'otherparent' + )); + $otherParent->write(); + $otherParent->publish('Stage', 'Live'); + + $response = $this->get('level1/otherparent'); + $this->assertEquals($response->getStatusCode(), 301); + + $response = $this->get('newlevel1/otherparent'); + $this->assertEquals( + $response->getStatusCode(), + 404, + 'Requesting an unrelated page on a renamed parent should be interpreted as a missing action, not a redirect' + ); + } + + /** + * + * NOTE: This test requires nested_urls + * + */ + function testRedirectsNestedRenamedPagesWithGetParameters() { + $this->generateNestedPagesFixture(); + + // check third level URLSegment + $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') + ); + } + + /** + * + * NOTE: This test requires nested_urls + * + */ + function testDoesntRedirectToNestedRenamedPageWhenNewExists() { + $this->generateNestedPagesFixture(); + + $otherLevel1 = new Page(array( + 'Title' => "Other Level 1", + 'URLSegment' => 'level1' + )); + $otherLevel1->write(); + $otherLevel1->publish('Stage', 'Live'); + + $response = $this->get('level1'); + $this->assertEquals( + $response->getStatusCode(), + 200 + ); + + $response = $this->get('level1/newlevel2'); + $this->assertEquals( + $response->getStatusCode(), + 404, + 'The old newlevel2/ URLSegment is checked as an action on the new page, which shouldnt exist.' + ); + } + + /** + * + * NOTE: This test requires nested_urls + * + */ + function testFindOldPage(){ + $page = new Page(); + $page->Title = 'First Level'; + $page->URLSegment = 'oldurl'; + $page->write(); + $page->publish('Stage', 'Live'); + + $page->URLSegment = 'newurl'; + $page->write(); + $page->publish('Stage', 'Live'); + + $response = ModelAsController::find_old_page('oldurl'); + $this->assertEquals('First Level',$response->Title); + + $page2 = new Page(); + $page2->Title = 'Second Level Page'; + $page2->URLSegment = 'oldpage2'; + $page2->ParentID = $page->ID; + $page2->write(); + $page2->publish('Stage', 'Live'); + + $page2->URLSegment = 'newpage2'; + $page2->write(); + $page2->publish('Stage', 'Live'); + + $response = ModelAsController::find_old_page('oldpage2',$page2->ParentID); + $this->assertEquals('Second Level Page',$response->Title); + + $response = ModelAsController::find_old_page('oldpage2',$page2->ID); + $this->assertEquals(false, $response ); + } + +} \ No newline at end of file diff --git a/tests/RootURLControllerTest.php b/tests/RootURLControllerTest.php new file mode 100755 index 00000000..921277f4 --- /dev/null +++ b/tests/RootURLControllerTest.php @@ -0,0 +1,76 @@ + 'page1', + 'www.page.co.nz' => 'page1', + 'help.com' => 'page1', + 'www.help.com' => 'page1', + 'something.com' => 'page1', + 'www.something.com' => 'page1', + + 'other.co.nz' => 'page2', + 'www.other.co.nz' => 'page2', + 'right' => 'page2', + 'www. right' => 'page2', + + 'only.com' => 'page3', + 'www.only.com' => 'page3', + + 'www.somethingelse.com' => 'home', + 'somethingelse.com' => 'home', + + // Test some potential false matches to page2 and page3 + 'alternate.only.com' => 'home', + 'www.alternate.only.com' => 'home', + 'alternate.something.com' => 'home', + ); + + foreach($tests as $domain => $urlSegment) { + RootURLController::reset(); + $_SERVER['HTTP_HOST'] = $domain; + + $this->assertEquals( + $urlSegment, + RootURLController::get_homepage_link(), + "Testing $domain matches $urlSegment" + ); + } + + $_SERVER['HTTP_HOST'] = $originalHost; + } + + public function testGetHomepageLink() { + $default = $this->objFromFixture('Page', 'home'); + $nested = $this->objFromFixture('Page', 'nested'); + + SiteTree::disable_nested_urls(); + $this->assertEquals('home', RootURLController::get_homepage_link()); + SiteTree::enable_nested_urls(); + $this->assertEquals('home', RootURLController::get_homepage_link()); + + $nested->HomepageForDomain = str_replace('www.', null, $_SERVER['HTTP_HOST']); + $nested->write(); + + RootURLController::reset(); + SiteTree::disable_nested_urls(); + $this->assertEquals('nested-home', RootURLController::get_homepage_link()); + + RootURLController::reset(); + SiteTree::enable_nested_urls(); + $this->assertEquals('home/nested-home', RootURLController::get_homepage_link()); + + $nested->HomepageForDomain = null; + $nested->write(); + } + +} \ No newline at end of file diff --git a/tests/RootURLControllerTest.yml b/tests/RootURLControllerTest.yml new file mode 100755 index 00000000..6f129f8a --- /dev/null +++ b/tests/RootURLControllerTest.yml @@ -0,0 +1,18 @@ +Page: + home: + Title: Home + nested: + Title: Nested Home + Parent: =>Page.home + page1: + Title: First Page + URLSegment: page1 + HomepageForDomain: page.co.nz, help.com,something.com, www.something.com + page2: + Title: Second Page + URLSegment: page2 + HomepageForDomain: other.co.nz, right + page3: + Title: Third Page + URLSegment: page3 + HomepageForDomain: only.com \ No newline at end of file