2008-08-12 04:59:27 +02:00
< ? php
2016-06-16 06:57:19 +02:00
2017-08-09 04:53:38 +02:00
namespace SilverStripe\CMS\Tests\Controllers ;
2017-08-09 03:25:12 +02:00
2017-06-21 06:29:40 +02:00
use Psr\SimpleCache\CacheInterface ;
use SilverStripe\Admin\CMSBatchActionHandler ;
2016-07-22 01:32:32 +02:00
use SilverStripe\CMS\Controllers\CMSMain ;
2018-03-21 05:44:24 +01:00
use SilverStripe\CMS\Model\RedirectorPage ;
2016-07-22 01:32:32 +02:00
use SilverStripe\CMS\Model\SiteTree ;
2017-06-21 06:29:40 +02:00
use SilverStripe\Control\Controller ;
2018-09-04 03:13:33 +02:00
use SilverStripe\Control\HTTPRequest ;
2017-06-21 06:29:40 +02:00
use SilverStripe\Control\HTTPResponse_Exception ;
2016-08-23 04:36:06 +02:00
use SilverStripe\Core\ClassInfo ;
use SilverStripe\Core\Config\Config ;
2017-06-21 06:29:40 +02:00
use SilverStripe\Core\Convert ;
use SilverStripe\Core\Injector\Injector ;
2016-08-23 04:36:06 +02:00
use SilverStripe\Dev\CSSContentParser ;
use SilverStripe\Dev\FunctionalTest ;
2017-06-21 06:29:40 +02:00
use SilverStripe\Dev\TestOnly ;
use SilverStripe\Forms\FieldList ;
use SilverStripe\ORM\DataObject ;
use SilverStripe\ORM\DB ;
2017-08-09 04:53:38 +02:00
use SilverStripe\Security\Member ;
2017-06-21 06:29:40 +02:00
use SilverStripe\Security\Security ;
use SilverStripe\SiteConfig\SiteConfig ;
use SilverStripe\Versioned\Versioned ;
2017-08-09 03:25:12 +02:00
2017-01-25 21:59:25 +01:00
class CMSMainTest extends FunctionalTest
{
protected static $fixture_file = 'CMSMainTest.yml' ;
2020-04-19 06:18:01 +02:00
protected static $orig = [];
2017-01-25 21:59:25 +01:00
2021-10-27 23:40:52 +02:00
protected function setUp () : void
2017-01-25 21:59:25 +01:00
{
parent :: setUp ();
// Clear automatically created siteconfigs (in case one was created outside of the specified fixtures).
$ids = $this -> allFixtureIDs ( SiteConfig :: class );
if ( $ids ) {
foreach ( SiteConfig :: get () -> exclude ( 'ID' , $ids ) as $config ) {
$config -> delete ();
}
}
}
public function testSiteTreeHints ()
{
2017-02-22 02:34:07 +01:00
$cache = Injector :: inst () -> get ( CacheInterface :: class . '.CMSMain_SiteTreeHints' );
2017-01-25 21:59:25 +01:00
// Login as user with root creation privileges
2017-08-09 04:53:38 +02:00
$user = $this -> objFromFixture ( Member :: class , 'rootedituser' );
2017-05-21 05:15:00 +02:00
Security :: setCurrentUser ( $user );
2017-02-22 02:34:07 +01:00
$cache -> clear ();
2017-01-25 21:59:25 +01:00
2017-08-09 04:53:38 +02:00
$rawHints = singleton ( CMSMain :: class ) -> SiteTreeHints ();
2017-01-25 21:59:25 +01:00
$this -> assertNotNull ( $rawHints );
2022-04-13 07:07:59 +02:00
$rawHints = preg_replace ( '/^"(.*)"$/' , '$1' , Convert :: xml2raw ( $rawHints ) ? ? '' );
$hints = json_decode ( $rawHints ? ? '' , true );
2017-01-25 21:59:25 +01:00
$this -> assertArrayHasKey ( 'Root' , $hints );
$this -> assertArrayHasKey ( 'Page' , $hints );
$this -> assertArrayHasKey ( 'All' , $hints );
$this -> assertArrayHasKey (
2017-08-09 04:53:38 +02:00
CMSMainTest_ClassA :: class ,
2017-01-25 21:59:25 +01:00
$hints [ 'All' ],
'Global list shows allowed classes'
);
$this -> assertArrayNotHasKey (
2017-08-09 04:53:38 +02:00
CMSMainTest_HiddenClass :: class ,
2017-01-25 21:59:25 +01:00
$hints [ 'All' ],
'Global list does not list hidden classes'
);
$this -> assertNotContains (
2017-08-09 04:53:38 +02:00
CMSMainTest_ClassA :: class ,
2017-01-25 21:59:25 +01:00
$hints [ 'Root' ][ 'disallowedChildren' ],
'Limits root classes'
);
$this -> assertContains (
2017-08-09 04:53:38 +02:00
CMSMainTest_NotRoot :: class ,
2017-01-25 21:59:25 +01:00
$hints [ 'Root' ][ 'disallowedChildren' ],
'Limits root classes'
);
}
public function testChildFilter ()
{
$this -> logInWithPermission ( 'ADMIN' );
// Check page A
$pageA = new CMSMainTest_ClassA ();
$pageA -> write ();
$pageB = new CMSMainTest_ClassB ();
$pageB -> write ();
// Check query
$response = $this -> get ( 'admin/pages/childfilter?ParentID=' . $pageA -> ID );
2022-04-13 07:07:59 +02:00
$children = json_decode ( $response -> getBody () ? ? '' );
2017-01-25 21:59:25 +01:00
$this -> assertFalse ( $response -> isError ());
// Page A can't have unrelated children
$this -> assertContains (
'Page' ,
$children ,
'Limited parent lists disallowed classes'
);
// But it can create a ClassB
$this -> assertNotContains (
2017-08-09 04:53:38 +02:00
CMSMainTest_ClassB :: class ,
2017-01-25 21:59:25 +01:00
$children ,
'Limited parent omits explicitly allowed classes in disallowedChildren'
);
}
/**
* @ todo Test the results of a publication better
*/
public function testPublish ()
{
2018-05-04 03:04:18 +02:00
$page1 = $this -> objFromFixture ( SiteTree :: class , 'page1' );
$page2 = $this -> objFromFixture ( SiteTree :: class , 'page2' );
2021-08-18 02:35:36 +02:00
$this -> logInAs ( 'admin' );
2017-01-25 21:59:25 +01:00
$response = $this -> get ( 'admin/pages/publishall?confirm=1' );
2021-10-27 23:40:52 +02:00
$this -> assertStringContainsString (
2017-01-25 21:59:25 +01:00
'Done: Published 30 pages' ,
$response -> getBody ()
);
// Some modules (e.g., cmsworkflow) will remove this action
$actions = CMSBatchActionHandler :: config () -> batch_actions ;
if ( isset ( $actions [ 'publish' ])) {
2018-05-04 03:04:18 +02:00
$response = $this -> get (
'admin/pages/batchactions/publish?ajax=1&csvIDs=' . implode ( ',' , [ $page1 -> ID , $page2 -> ID ])
);
2022-04-13 07:07:59 +02:00
$responseData = json_decode ( $response -> getBody () ? ? '' , true );
2017-01-25 21:59:25 +01:00
$this -> assertArrayHasKey ( $page1 -> ID , $responseData [ 'modified' ]);
$this -> assertArrayHasKey ( $page2 -> ID , $responseData [ 'modified' ]);
}
// Get the latest version of the redirector page
2018-03-21 05:44:24 +01:00
$pageID = $this -> idFromFixture ( RedirectorPage :: class , 'page5' );
2018-05-04 03:04:18 +02:00
$latestID = DB :: prepared_query ( 'SELECT MAX("Version") FROM "RedirectorPage_Versions" WHERE "RecordID" = ?' , [ $pageID ]) -> value ();
$dsCount = DB :: prepared_query ( 'SELECT COUNT("Version") FROM "RedirectorPage_Versions" WHERE "RecordID" = ? AND "Version"= ?' , [ $pageID , $latestID ]) -> value ();
2017-01-25 21:59:25 +01:00
$this -> assertEquals ( 1 , $dsCount , " Published page has no duplicate version records: it has " . $dsCount . " for version " . $latestID );
}
/**
* Test that getCMSFields works on each page type .
* Mostly , this is just checking that the method doesn ' t return an error
*/
public function testThatGetCMSFieldsWorksOnEveryPageType ()
{
2017-07-17 07:19:00 +02:00
$classes = ClassInfo :: subclassesFor ( SiteTree :: class );
2017-01-25 21:59:25 +01:00
array_shift ( $classes );
foreach ( $classes as $class ) {
$page = new $class ();
if ( $page instanceof TestOnly ) {
continue ;
}
2017-08-22 23:46:46 +02:00
if ( ! $page -> config () -> get ( 'can_be_root' )) {
2017-01-25 21:59:25 +01:00
continue ;
}
$page -> Title = " Test $class page " ;
$page -> write ();
$page -> flushCache ();
2017-07-17 07:19:00 +02:00
$page = DataObject :: get_by_id ( SiteTree :: class , $page -> ID );
2017-01-25 21:59:25 +01:00
$this -> assertTrue ( $page -> getCMSFields () instanceof FieldList );
}
}
public function testCanPublishPageWithUnpublishedParentWithStrictHierarchyOff ()
{
$this -> logInWithPermission ( 'ADMIN' );
2017-07-17 07:19:00 +02:00
Config :: modify () -> set ( SiteTree :: class , 'enforce_strict_hierarchy' , true );
2018-05-04 03:04:18 +02:00
$parentPage = $this -> objFromFixture ( SiteTree :: class , 'page3' );
$childPage = $this -> objFromFixture ( SiteTree :: class , 'page1' );
2017-01-25 21:59:25 +01:00
$parentPage -> doUnpublish ();
$childPage -> doUnpublish ();
$actions = $childPage -> getCMSActions () -> dataFields ();
$this -> assertArrayHasKey (
'action_publish' ,
$actions ,
'Can publish a page with an unpublished parent with strict hierarchy off'
);
2017-07-17 07:19:00 +02:00
Config :: modify () -> set ( SiteTree :: class , 'enforce_strict_hierarchy' , false );
2017-01-25 21:59:25 +01:00
}
/**
* Test that a draft - deleted page can still be opened in the CMS
*/
public function testDraftDeletedPageCanBeOpenedInCMS ()
{
2017-05-21 05:15:00 +02:00
$this -> logInWithPermission ( 'ADMIN' );
2017-01-25 21:59:25 +01:00
// Set up a page that is delete from live
2018-05-04 03:04:18 +02:00
$page = $this -> objFromFixture ( SiteTree :: class , 'page1' );
2017-01-25 21:59:25 +01:00
$pageID = $page -> ID ;
$page -> publishRecursive ();
$page -> delete ();
$response = $this -> get ( 'admin/pages/edit/show/' . $pageID );
2020-04-19 06:18:01 +02:00
$livePage = Versioned :: get_one_by_stage ( SiteTree :: class , Versioned :: LIVE , [
2018-05-04 03:04:18 +02:00
'"SiteTree"."ID"' => $pageID ,
2020-04-19 06:18:01 +02:00
]);
2017-07-17 07:19:00 +02:00
$this -> assertInstanceOf ( SiteTree :: class , $livePage );
2017-01-25 21:59:25 +01:00
$this -> assertTrue ( $livePage -> canDelete ());
// Check that the 'restore' button exists as a simple way of checking that the correct page is returned.
2021-10-27 23:40:52 +02:00
$this -> assertMatchesRegularExpression ( '/<button type="submit"[^>]+name="action_(restore|revert)"/i' , $response -> getBody ());
2017-01-25 21:59:25 +01:00
}
/**
* Test CMSMain :: getRecord ()
*/
public function testGetRecord ()
{
$this -> logInWithPermission ( 'ADMIN' );
// Set up a page that is delete from live
2018-05-04 03:04:18 +02:00
$page1 = $this -> objFromFixture ( SiteTree :: class , 'page1' );
2017-01-25 21:59:25 +01:00
$page1ID = $page1 -> ID ;
$page1 -> publishRecursive ();
$page1 -> delete ();
2017-07-18 00:56:13 +02:00
$cmsMain = CMSMain :: create ();
2017-06-21 06:29:40 +02:00
$cmsMain -> setRequest ( Controller :: curr () -> getRequest ());
2017-01-25 21:59:25 +01:00
// Bad calls
$this -> assertNull ( $cmsMain -> getRecord ( '0' ));
$this -> assertNull ( $cmsMain -> getRecord ( 'asdf' ));
// Pages that are on draft and aren't on draft should both work
2018-05-04 03:04:18 +02:00
$this -> assertInstanceOf ( SiteTree :: class , $cmsMain -> getRecord ( $page1ID ));
$this -> assertInstanceOf ( SiteTree :: class , $cmsMain -> getRecord ( $this -> idFromFixture ( SiteTree :: class , 'page2' )));
2017-01-25 21:59:25 +01:00
// This functionality isn't actually used any more.
$newPage = $cmsMain -> getRecord ( 'new-Page-5' );
2018-05-04 03:04:18 +02:00
$this -> assertInstanceOf ( SiteTree :: class , $newPage );
2017-01-25 21:59:25 +01:00
$this -> assertEquals ( '5' , $newPage -> ParentID );
}
public function testDeletedPagesSiteTreeFilter ()
{
2018-05-04 03:04:18 +02:00
$id = $this -> idFromFixture ( SiteTree :: class , 'page3' );
2017-01-25 21:59:25 +01:00
$this -> logInWithPermission ( 'ADMIN' );
$result = $this -> get ( 'admin/pages/getsubtree?filter=CMSSiteTreeFilter_DeletedPages&ajax=1&ID=' . $id );
$this -> assertEquals ( 200 , $result -> getStatusCode ());
}
public function testCreationOfTopLevelPage ()
{
$origFollow = $this -> autoFollowRedirection ;
$this -> autoFollowRedirection = false ;
2017-08-09 04:53:38 +02:00
$cmsUser = $this -> objFromFixture ( Member :: class , 'allcmssectionsuser' );
$rootEditUser = $this -> objFromFixture ( Member :: class , 'rootedituser' );
2017-01-25 21:59:25 +01:00
// with insufficient permissions
2017-05-21 05:15:00 +02:00
Security :: setCurrentUser ( $cmsUser );
2017-01-25 21:59:25 +01:00
$this -> get ( 'admin/pages/add' );
$response = $this -> post (
'admin/pages/add/AddForm' ,
2020-04-19 06:18:01 +02:00
[
2017-01-25 21:59:25 +01:00
'ParentID' => '0' ,
2018-05-04 03:04:18 +02:00
'PageType' => RedirectorPage :: class ,
2017-01-25 21:59:25 +01:00
'Locale' => 'en_US' ,
'action_doAdd' => 1 ,
'ajax' => 1 ,
2020-04-19 06:18:01 +02:00
],
[
2017-01-25 21:59:25 +01:00
'X-Pjax' => 'CurrentForm,Breadcrumbs' ,
2020-04-19 06:18:01 +02:00
]
2017-01-25 21:59:25 +01:00
);
// should redirect, which is a permission error
$this -> assertEquals ( 403 , $response -> getStatusCode (), 'Add TopLevel page must fail for normal user' );
// with correct permissions
2021-08-18 07:18:01 +02:00
$this -> logInAs ( $rootEditUser );
2017-01-25 21:59:25 +01:00
$response = $this -> get ( 'admin/pages/add' );
$response = $this -> post (
'admin/pages/add/AddForm' ,
2020-04-19 06:18:01 +02:00
[
2017-01-25 21:59:25 +01:00
'ParentID' => '0' ,
2018-05-04 03:04:18 +02:00
'PageType' => RedirectorPage :: class ,
2017-01-25 21:59:25 +01:00
'Locale' => 'en_US' ,
'action_doAdd' => 1 ,
'ajax' => 1 ,
2020-04-19 06:18:01 +02:00
],
[
2017-01-25 21:59:25 +01:00
'X-Pjax' => 'CurrentForm,Breadcrumbs' ,
2020-04-19 06:18:01 +02:00
]
2017-01-25 21:59:25 +01:00
);
$location = $response -> getHeader ( 'X-ControllerURL' );
$this -> assertNotEmpty ( $location , 'Must be a redirect on success' );
2021-10-27 23:40:52 +02:00
$this -> assertStringContainsString ( '/show/' , $location , 'Must redirect to /show/ the new page' );
2021-08-18 07:18:01 +02:00
$this -> logOut ();
2017-01-25 21:59:25 +01:00
$this -> autoFollowRedirection = $origFollow ;
}
public function testCreationOfRestrictedPage ()
{
$origFollow = $this -> autoFollowRedirection ;
$this -> autoFollowRedirection = false ;
2021-08-18 07:18:01 +02:00
$this -> logInAs ( 'admin' );
2017-01-25 21:59:25 +01:00
// Create toplevel page
$this -> get ( 'admin/pages/add' );
$response = $this -> post (
'admin/pages/add/AddForm' ,
2020-04-19 06:18:01 +02:00
[
2017-01-25 21:59:25 +01:00
'ParentID' => '0' ,
2017-08-09 04:53:38 +02:00
'PageType' => CMSMainTest_ClassA :: class ,
2017-01-25 21:59:25 +01:00
'Locale' => 'en_US' ,
'action_doAdd' => 1 ,
2018-05-04 03:04:18 +02:00
'ajax' => 1 ,
2020-04-19 06:18:01 +02:00
],
[
2017-01-25 21:59:25 +01:00
'X-Pjax' => 'CurrentForm,Breadcrumbs' ,
2020-04-19 06:18:01 +02:00
]
2017-01-25 21:59:25 +01:00
);
$this -> assertFalse ( $response -> isError ());
2022-04-13 07:07:59 +02:00
$ok = preg_match ( '/edit\/show\/(\d*)/' , $response -> getHeader ( 'X-ControllerURL' ) ? ? '' , $matches );
2017-01-25 21:59:25 +01:00
$this -> assertNotEmpty ( $ok );
$newPageId = $matches [ 1 ];
// Create allowed child
$this -> get ( 'admin/pages/add' );
$response = $this -> post (
'admin/pages/add/AddForm' ,
2020-04-19 06:18:01 +02:00
[
2017-01-25 21:59:25 +01:00
'ParentID' => $newPageId ,
2017-08-09 04:53:38 +02:00
'PageType' => CMSMainTest_ClassB :: class ,
2017-01-25 21:59:25 +01:00
'Locale' => 'en_US' ,
'action_doAdd' => 1 ,
2018-05-04 03:04:18 +02:00
'ajax' => 1 ,
2020-04-19 06:18:01 +02:00
],
[
2017-01-25 21:59:25 +01:00
'X-Pjax' => 'CurrentForm,Breadcrumbs' ,
2020-04-19 06:18:01 +02:00
]
2017-01-25 21:59:25 +01:00
);
$this -> assertFalse ( $response -> isError ());
$this -> assertEmpty ( $response -> getBody ());
// Verify that the page was created and redirected to accurately
$newerPage = SiteTree :: get () -> byID ( $newPageId ) -> AllChildren () -> first ();
$this -> assertNotEmpty ( $newerPage );
2022-04-13 07:07:59 +02:00
$ok = preg_match ( '/edit\/show\/(\d*)/' , $response -> getHeader ( 'X-ControllerURL' ) ? ? '' , $matches );
2017-01-25 21:59:25 +01:00
$this -> assertNotEmpty ( $ok );
$newerPageID = $matches [ 1 ];
$this -> assertEquals ( $newerPage -> ID , $newerPageID );
// Create disallowed child
$this -> get ( 'admin/pages/add' );
$response = $this -> post (
'admin/pages/add/AddForm' ,
2020-04-19 06:18:01 +02:00
[
2017-01-25 21:59:25 +01:00
'ParentID' => $newPageId ,
2018-05-04 03:04:18 +02:00
'PageType' => RedirectorPage :: class ,
2017-01-25 21:59:25 +01:00
'Locale' => 'en_US' ,
'action_doAdd' => 1 ,
2018-05-04 03:04:18 +02:00
'ajax' => 1 ,
2020-04-19 06:18:01 +02:00
],
[
2017-01-25 21:59:25 +01:00
'X-Pjax' => 'CurrentForm,Breadcrumbs' ,
2020-04-19 06:18:01 +02:00
]
2017-01-25 21:59:25 +01:00
);
$this -> assertEquals ( 403 , $response -> getStatusCode (), 'Add disallowed child should fail' );
2017-05-21 05:15:00 +02:00
Security :: setCurrentUser ( null );
2017-01-25 21:59:25 +01:00
$this -> autoFollowRedirection = $origFollow ;
}
public function testBreadcrumbs ()
{
2018-05-04 03:04:18 +02:00
$page3 = $this -> objFromFixture ( SiteTree :: class , 'page3' );
$page31 = $this -> objFromFixture ( SiteTree :: class , 'page31' );
2021-08-18 07:18:01 +02:00
$this -> logInAs ( 'admin' );
2017-01-25 21:59:25 +01:00
$response = $this -> get ( 'admin/pages/edit/show/' . $page31 -> ID );
$parser = new CSSContentParser ( $response -> getBody ());
$crumbs = $parser -> getBySelector ( '.breadcrumbs-wrapper .crumb' );
$this -> assertNotNull ( $crumbs );
2022-04-13 07:07:59 +02:00
$this -> assertEquals ( 2 , count ( $crumbs ? ? []));
2017-01-25 21:59:25 +01:00
$this -> assertEquals ( 'Page 3' , ( string ) $crumbs [ 0 ]);
$this -> assertEquals ( 'Page 3.1' , ( string ) $crumbs [ 1 ]);
2017-05-21 05:15:00 +02:00
Security :: setCurrentUser ( null );
2017-01-25 21:59:25 +01:00
}
public function testGetNewItem ()
{
2017-07-18 00:56:13 +02:00
$controller = CMSMain :: create ();
2017-06-21 06:29:40 +02:00
$controller -> setRequest ( Controller :: curr () -> getRequest ());
2017-01-25 21:59:25 +01:00
$id = 'new-Page-0' ;
// Test success
$page = $controller -> getNewItem ( $id , false );
$this -> assertEquals ( $page -> Title , 'New Page' );
$this -> assertNotEquals ( $page -> Sort , 0 );
2018-05-04 03:04:18 +02:00
$this -> assertInstanceOf ( SiteTree :: class , $page );
2017-01-25 21:59:25 +01:00
// Test failure
try {
$id = 'new-Member-0' ;
$member = $controller -> getNewItem ( $id , false );
$this -> fail ( 'Should not be able to create a Member object' );
} catch ( HTTPResponse_Exception $e ) {
$this -> assertEquals ( $controller -> getResponse () -> getStatusCode (), 302 );
}
}
/**
* Tests filtering in { @ see CMSMain :: getList ()}
*/
public function testGetList ()
{
2017-07-18 00:56:13 +02:00
$controller = CMSMain :: create ();
2017-06-21 06:29:40 +02:00
$controller -> setRequest ( Controller :: curr () -> getRequest ());
2017-01-25 21:59:25 +01:00
// Test all pages (stage)
$pages = $controller -> getList () -> sort ( 'Title' );
$this -> assertEquals ( 28 , $pages -> count ());
$this -> assertEquals (
2020-04-19 06:18:01 +02:00
[ 'Home' , 'Page 1' , 'Page 10' , 'Page 11' , 'Page 12' ],
2017-01-25 21:59:25 +01:00
$pages -> Limit ( 5 ) -> column ( 'Title' )
);
// Change state of tree
2018-05-04 03:04:18 +02:00
$page1 = $this -> objFromFixture ( SiteTree :: class , 'page1' );
$page3 = $this -> objFromFixture ( SiteTree :: class , 'page3' );
$page11 = $this -> objFromFixture ( SiteTree :: class , 'page11' );
$page12 = $this -> objFromFixture ( SiteTree :: class , 'page12' );
2017-01-25 21:59:25 +01:00
// Deleted
$page1 -> doUnpublish ();
$page1 -> delete ();
// Live and draft
$page11 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
// Live only
$page12 -> copyVersionToStage ( Versioned :: DRAFT , Versioned :: LIVE );
$page12 -> delete ();
// Re-test all pages (stage)
$pages = $controller -> getList () -> sort ( 'Title' );
$this -> assertEquals ( 26 , $pages -> count ());
$this -> assertEquals (
2020-04-19 06:18:01 +02:00
[ 'Home' , 'Page 10' , 'Page 11' , 'Page 13' , 'Page 14' ],
2017-01-25 21:59:25 +01:00
$pages -> Limit ( 5 ) -> column ( 'Title' )
);
// Test deleted page filter
2020-04-19 06:18:01 +02:00
$params = [
2018-05-04 03:04:18 +02:00
'FilterClass' => 'SilverStripe\\CMS\\Controllers\\CMSSiteTreeFilter_StatusDeletedPages' ,
2020-04-19 06:18:01 +02:00
];
2017-01-25 21:59:25 +01:00
$pages = $controller -> getList ( $params );
$this -> assertEquals ( 1 , $pages -> count ());
$this -> assertEquals (
2020-04-19 06:18:01 +02:00
[ 'Page 1' ],
2017-01-25 21:59:25 +01:00
$pages -> column ( 'Title' )
);
// Test live, but not on draft filter
2020-04-19 06:18:01 +02:00
$params = [
2018-05-04 03:04:18 +02:00
'FilterClass' => 'SilverStripe\\CMS\\Controllers\\CMSSiteTreeFilter_StatusRemovedFromDraftPages' ,
2020-04-19 06:18:01 +02:00
];
2017-01-25 21:59:25 +01:00
$pages = $controller -> getList ( $params );
$this -> assertEquals ( 1 , $pages -> count ());
$this -> assertEquals (
2020-04-19 06:18:01 +02:00
[ 'Page 12' ],
2017-01-25 21:59:25 +01:00
$pages -> column ( 'Title' )
);
// Test live pages filter
2020-04-19 06:18:01 +02:00
$params = [
2018-05-04 03:04:18 +02:00
'FilterClass' => 'SilverStripe\\CMS\\Controllers\\CMSSiteTreeFilter_PublishedPages' ,
2020-04-19 06:18:01 +02:00
];
2017-01-25 21:59:25 +01:00
$pages = $controller -> getList ( $params );
$this -> assertEquals ( 2 , $pages -> count ());
$this -> assertEquals (
2020-04-19 06:18:01 +02:00
[ 'Page 11' , 'Page 12' ],
2017-01-25 21:59:25 +01:00
$pages -> column ( 'Title' )
);
// Test that parentID is ignored when filtering
$pages = $controller -> getList ( $params , $page3 -> ID );
$this -> assertEquals ( 2 , $pages -> count ());
$this -> assertEquals (
2020-04-19 06:18:01 +02:00
[ 'Page 11' , 'Page 12' ],
2017-01-25 21:59:25 +01:00
$pages -> column ( 'Title' )
);
// Test that parentID is respected when not filtering
2020-04-19 06:18:01 +02:00
$pages = $controller -> getList ([], $page3 -> ID );
2017-01-25 21:59:25 +01:00
$this -> assertEquals ( 2 , $pages -> count ());
$this -> assertEquals (
2020-04-19 06:18:01 +02:00
[ 'Page 3.1' , 'Page 3.2' ],
2017-01-25 21:59:25 +01:00
$pages -> column ( 'Title' )
);
}
/**
* Testing retrieval and type of CMS edit form .
*/
public function testGetEditForm ()
{
// Login is required prior to accessing a CMS form.
$this -> loginWithPermission ( 'ADMIN' );
// Get a associated with a fixture page.
2018-05-04 03:04:18 +02:00
$page = $this -> objFromFixture ( SiteTree :: class , 'page1' );
2017-07-18 00:56:13 +02:00
$controller = CMSMain :: create ();
2017-06-21 06:29:40 +02:00
$controller -> setRequest ( Controller :: curr () -> getRequest ());
2017-01-25 21:59:25 +01:00
$form = $controller -> getEditForm ( $page -> ID );
$this -> assertInstanceOf ( " SilverStripe \\ Forms \\ Form " , $form );
// Ensure that the form will not "validate" on delete or "unpublish" actions.
$exemptActions = $form -> getValidationExemptActions ();
$this -> assertContains ( " delete " , $exemptActions );
$this -> assertContains ( " unpublish " , $exemptActions );
}
/**
* Test that changed classes save with the correct class name
*/
public function testChangeClass ()
{
$this -> logInWithPermission ( 'ADMIN' );
2017-07-18 00:56:13 +02:00
$cms = CMSMain :: create ();
2017-06-21 06:29:40 +02:00
$cms -> setRequest ( Controller :: curr () -> getRequest ());
2017-01-25 21:59:25 +01:00
$page = new CMSMainTest_ClassA ();
$page -> Title = 'Class A' ;
$page -> write ();
$form = $cms -> getEditForm ( $page -> ID );
2017-08-09 04:53:38 +02:00
$form -> loadDataFrom ([ 'ClassName' => CMSMainTest_ClassB :: class ]);
2017-01-25 21:59:25 +01:00
$result = $cms -> save ([
'ID' => $page -> ID ,
2018-05-04 03:04:18 +02:00
'ClassName' => CMSMainTest_ClassB :: class ,
2017-01-25 21:59:25 +01:00
], $form );
$this -> assertEquals ( 200 , $result -> getStatusCode ());
$newPage = SiteTree :: get () -> byID ( $page -> ID );
2017-08-09 04:53:38 +02:00
$this -> assertInstanceOf ( CMSMainTest_ClassB :: class , $newPage );
$this -> assertEquals ( CMSMainTest_ClassB :: class , $newPage -> ClassName );
2017-01-25 21:59:25 +01:00
$this -> assertEquals ( 'Class A' , $newPage -> Title );
}
2017-11-30 03:56:16 +01:00
public function testSiteTreeHintsCache ()
{
$cms = CMSMain :: create ();
2017-12-13 03:36:35 +01:00
/** @var Member $user */
2017-11-30 03:56:16 +01:00
$user = $this -> objFromFixture ( Member :: class , 'rootedituser' );
Security :: setCurrentUser ( $user );
$pageClass = array_values ( SiteTree :: page_type_classes ())[ 0 ];
$mockPageMissesCache = $this -> getMockBuilder ( $pageClass )
-> setMethods ([ 'canCreate' ])
-> getMock ();
$mockPageMissesCache
-> expects ( $this -> exactly ( 3 ))
-> method ( 'canCreate' );
$mockPageHitsCache = $this -> getMockBuilder ( $pageClass )
-> setMethods ([ 'canCreate' ])
-> getMock ();
$mockPageHitsCache
-> expects ( $this -> never ())
-> method ( 'canCreate' );
// Initially, cache misses (1)
Injector :: inst () -> registerService ( $mockPageMissesCache , $pageClass );
$hints = $cms -> SiteTreeHints ();
$this -> assertNotNull ( $hints );
// Now it hits
Injector :: inst () -> registerService ( $mockPageHitsCache , $pageClass );
$hints = $cms -> SiteTreeHints ();
$this -> assertNotNull ( $hints );
// Mutating member record invalidates cache. Misses (2)
$user -> FirstName = 'changed' ;
$user -> write ();
Injector :: inst () -> registerService ( $mockPageMissesCache , $pageClass );
$hints = $cms -> SiteTreeHints ();
$this -> assertNotNull ( $hints );
// Now it hits again
Injector :: inst () -> registerService ( $mockPageHitsCache , $pageClass );
$hints = $cms -> SiteTreeHints ();
$this -> assertNotNull ( $hints );
// Different user. Misses. (3)
$user = $this -> objFromFixture ( Member :: class , 'allcmssectionsuser' );
Security :: setCurrentUser ( $user );
Injector :: inst () -> registerService ( $mockPageMissesCache , $pageClass );
$hints = $cms -> SiteTreeHints ();
$this -> assertNotNull ( $hints );
}
2018-09-04 03:13:33 +02:00
public function testSearchField ()
{
$cms = CMSMain :: create ();
$searchSchema = $cms -> getSearchFieldSchema ();
$this -> assertJsonStringEqualsJsonString (
json_encode ([
'formSchemaUrl' => 'admin/pages/schema/SearchForm' ,
'name' => 'Term' ,
'placeholder' => 'Search "Pages"' ,
'filters' => new \stdClass
]),
$searchSchema
);
$request = new HTTPRequest (
'GET' ,
'admin/pages/schema/SearchForm' ,
[ 'q' => [
'Term' => 'test' ,
'FilterClass' => 'SilverStripe\CMS\Controllers\CMSSiteTreeFilter_Search'
]]
);
$cms -> setRequest ( $request );
$searchSchema = $cms -> getSearchFieldSchema ();
$this -> assertJsonStringEqualsJsonString (
json_encode ([
'formSchemaUrl' => 'admin/pages/schema/SearchForm' ,
'name' => 'Term' ,
'placeholder' => 'Search "Pages"' ,
'filters' => [
2018-10-01 12:50:20 +02:00
'Search__Term' => 'test' ,
'Search__FilterClass' => 'SilverStripe\CMS\Controllers\CMSSiteTreeFilter_Search'
2018-09-04 03:13:33 +02:00
]
]),
$searchSchema
);
}
2019-01-11 11:56:40 +01:00
public function testCanOrganiseSitetree ()
{
$cms = CMSMain :: create ();
$this -> assertFalse ( $cms -> CanOrganiseSitetree ());
$this -> logInWithPermission ( 'CMS_ACCESS_CMSMain' );
$this -> assertFalse ( $cms -> CanOrganiseSitetree ());
$this -> logOut ();
$this -> logInWithPermission ( 'SITETREE_REORGANISE' );
$this -> assertTrue ( $cms -> CanOrganiseSitetree ());
$this -> logOut ();
$this -> logInWithPermission ( 'ADMIN' );
$this -> assertTrue ( $cms -> CanOrganiseSitetree ());
}
2010-12-14 02:29:38 +01:00
}