2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
2013-06-02 03:37:34 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Admin\Tests;
|
|
|
|
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-11 01:14:02 +02:00
|
|
|
use SilverStripe\Admin\CMSMenu;
|
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
use SilverStripe\View\Requirements;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2013-06-02 03:37:34 +02:00
|
|
|
* @package framework
|
2011-03-23 10:51:00 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class LeftAndMainTest extends FunctionalTest {
|
2011-03-29 06:54:08 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'LeftAndMainTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-29 06:54:08 +02:00
|
|
|
protected $extraDataObjects = array('LeftAndMainTest_Object');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
protected $backupCombined;
|
2013-06-02 03:37:34 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2011-03-23 10:51:00 +01:00
|
|
|
parent::setUp();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// @todo fix controller stack problems and re-activate
|
|
|
|
//$this->autoFollowRedirection = false;
|
2015-09-07 07:38:29 +02:00
|
|
|
$this->resetMenu();
|
2013-06-02 03:37:34 +02:00
|
|
|
$this->backupCombined = Requirements::get_combined_files_enabled();
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
LeftAndMain::config()
|
|
|
|
->update('extra_requirements_css', array(
|
|
|
|
FRAMEWORK_DIR . '/tests/assets/LeftAndMainTest.css'
|
|
|
|
))
|
|
|
|
->update('extra_requirements_javascript', array(
|
|
|
|
FRAMEWORK_DIR . '/tests/assets/LeftAndMainTest.js'
|
|
|
|
));
|
2013-06-02 03:37:34 +02:00
|
|
|
|
|
|
|
Requirements::set_combined_files_enabled(false);
|
|
|
|
}
|
|
|
|
|
2015-09-07 07:38:29 +02:00
|
|
|
/**
|
|
|
|
* Clear menu to default state as per LeftAndMain::init()
|
|
|
|
*/
|
|
|
|
protected function resetMenu() {
|
|
|
|
CMSMenu::clear_menu();
|
|
|
|
CMSMenu::populate_menu();
|
|
|
|
CMSMenu::add_link(
|
|
|
|
'Help',
|
|
|
|
_t('LeftAndMain.HELP', 'Help', 'Menu title'),
|
|
|
|
LeftAndMain::config()->help_link,
|
|
|
|
-2,
|
|
|
|
array(
|
|
|
|
'target' => '_blank'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-02 03:37:34 +02:00
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
Requirements::set_combined_files_enabled($this->backupCombined);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-02 03:37:34 +02:00
|
|
|
|
|
|
|
public function testExtraCssAndJavascript() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
2013-06-02 03:37:34 +02:00
|
|
|
$this->session()->inst_set('loggedInAs', $admin->ID);
|
|
|
|
$response = $this->get('LeftAndMainTest_Controller');
|
|
|
|
|
2013-08-21 08:54:05 +02:00
|
|
|
$this->assertRegExp('/tests\/assets\/LeftAndMainTest.css/i', $response->getBody(),
|
|
|
|
"body should contain custom css");
|
|
|
|
$this->assertRegExp('/tests\/assets\/LeftAndMainTest.js/i', $response->getBody(),
|
|
|
|
"body should contain custom js");
|
2013-06-02 03:37:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-29 06:54:08 +02:00
|
|
|
/**
|
|
|
|
* Note: This test would typically rely on SiteTree and CMSMain, but is mocked by
|
|
|
|
* LeftAndMain_Controller and LeftAndMain_Object here to remove this dependency.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveTreeNodeSorting() {
|
2016-08-11 01:40:23 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// forcing sorting for non-MySQL
|
2013-06-21 00:32:08 +02:00
|
|
|
$rootPages = LeftAndMainTest_Object::get()
|
|
|
|
->filter("ParentID", 0)
|
|
|
|
->sort('"ID"');
|
2011-03-23 10:51:00 +01:00
|
|
|
$siblingIDs = $rootPages->column('ID');
|
|
|
|
$page1 = $rootPages->offsetGet(0);
|
|
|
|
$page2 = $rootPages->offsetGet(1);
|
|
|
|
$page3 = $rootPages->offsetGet(2);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Move page2 before page1
|
|
|
|
$siblingIDs[0] = $page2->ID;
|
|
|
|
$siblingIDs[1] = $page1->ID;
|
|
|
|
$data = array(
|
|
|
|
'SiblingIDs' => $siblingIDs,
|
|
|
|
'ID' => $page2->ID,
|
|
|
|
'ParentID' => 0
|
|
|
|
);
|
|
|
|
|
2011-03-29 06:54:08 +02:00
|
|
|
$response = $this->post('LeftAndMainTest_Controller/savetreenode', $data);
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2011-03-29 06:54:08 +02:00
|
|
|
$page1 = DataObject::get_by_id('LeftAndMainTest_Object', $page1->ID, false);
|
|
|
|
$page2 = DataObject::get_by_id('LeftAndMainTest_Object', $page2->ID, false);
|
|
|
|
$page3 = DataObject::get_by_id('LeftAndMainTest_Object', $page3->ID, false);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals(2, $page1->Sort, 'Page1 is sorted after Page2');
|
|
|
|
$this->assertEquals(1, $page2->Sort, 'Page2 is sorted before Page1');
|
|
|
|
$this->assertEquals(3, $page3->Sort, 'Sort order for other pages is unaffected');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
public function testSaveTreeNodeParentID() {
|
2016-08-11 01:40:23 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2011-03-29 06:54:08 +02:00
|
|
|
$page2 = $this->objFromFixture('LeftAndMainTest_Object', 'page2');
|
|
|
|
$page3 = $this->objFromFixture('LeftAndMainTest_Object', 'page3');
|
|
|
|
$page31 = $this->objFromFixture('LeftAndMainTest_Object', 'page31');
|
|
|
|
$page32 = $this->objFromFixture('LeftAndMainTest_Object', 'page32');
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
// Move page2 into page3, between page3.1 and page 3.2
|
|
|
|
$siblingIDs = array(
|
|
|
|
$page31->ID,
|
|
|
|
$page2->ID,
|
|
|
|
$page32->ID
|
|
|
|
);
|
|
|
|
$data = array(
|
|
|
|
'SiblingIDs' => $siblingIDs,
|
|
|
|
'ID' => $page2->ID,
|
|
|
|
'ParentID' => $page3->ID
|
|
|
|
);
|
2011-03-29 06:54:08 +02:00
|
|
|
$response = $this->post('LeftAndMainTest_Controller/savetreenode', $data);
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2011-03-29 06:54:08 +02:00
|
|
|
$page2 = DataObject::get_by_id('LeftAndMainTest_Object', $page2->ID, false);
|
|
|
|
$page31 = DataObject::get_by_id('LeftAndMainTest_Object', $page31->ID, false);
|
|
|
|
$page32 = DataObject::get_by_id('LeftAndMainTest_Object', $page32->ID, false);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
$this->assertEquals($page3->ID, $page2->ParentID, 'Moved page gets new parent');
|
|
|
|
$this->assertEquals(1, $page31->Sort, 'Children pages before insertaion are unaffected');
|
|
|
|
$this->assertEquals(2, $page2->Sort, 'Moved page is correctly sorted');
|
|
|
|
$this->assertEquals(3, $page32->Sort, 'Children pages after insertion are resorted');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Check that all subclasses of leftandmain can be accessed
|
|
|
|
*/
|
|
|
|
public function testLeftAndMainSubclasses() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$adminuser = $this->objFromFixture('SilverStripe\\Security\\Member','admin');
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->session()->inst_set('loggedInAs', $adminuser->ID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-07 07:38:29 +02:00
|
|
|
$this->resetMenu();
|
2016-08-11 01:40:23 +02:00
|
|
|
$menuItems = LeftAndMain::singleton()->MainMenu(false);
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($menuItems as $menuItem) {
|
|
|
|
$link = $menuItem->Link;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// don't test external links
|
2015-07-31 01:38:18 +02:00
|
|
|
if(preg_match('/^(https?:)?\/\//',$link)) continue;
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
$response = $this->get($link);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\Control\\HTTPResponse', $response, "$link should return a response object");
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode(), "$link should return 200 status code");
|
|
|
|
// Check that a HTML page has been returned
|
|
|
|
$this->assertRegExp('/<html[^>]*>/i', $response->getBody(), "$link should contain <html> tag");
|
|
|
|
$this->assertRegExp('/<head[^>]*>/i', $response->getBody(), "$link should contain <head> tag");
|
|
|
|
$this->assertRegExp('/<body[^>]*>/i', $response->getBody(), "$link should contain <body> tag");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCanView() {
|
2016-06-23 01:37:22 +02:00
|
|
|
$adminuser = $this->objFromFixture('SilverStripe\\Security\\Member', 'admin');
|
|
|
|
$securityonlyuser = $this->objFromFixture('SilverStripe\\Security\\Member', 'securityonlyuser');
|
|
|
|
$allcmssectionsuser = $this->objFromFixture('SilverStripe\\Security\\Member', 'allcmssectionsuser');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// anonymous user
|
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
2015-09-07 07:38:29 +02:00
|
|
|
$this->resetMenu();
|
2016-08-11 01:40:23 +02:00
|
|
|
$menuItems = LeftAndMain::singleton()->MainMenu(false);
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals(
|
2016-08-11 01:40:23 +02:00
|
|
|
$menuItems->column('Code'),
|
2011-03-23 10:51:00 +01:00
|
|
|
array(),
|
|
|
|
'Without valid login, members cant access any menu entries'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// restricted cms user
|
2015-09-07 07:38:29 +02:00
|
|
|
$this->logInAs($securityonlyuser);
|
|
|
|
$this->resetMenu();
|
2016-08-11 01:40:23 +02:00
|
|
|
$menuItems = LeftAndMain::singleton()->MainMenu(false);
|
|
|
|
$menuItems = $menuItems->column('Code');
|
2013-06-21 00:32:08 +02:00
|
|
|
sort($menuItems);
|
2016-08-11 01:40:23 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertEquals(
|
2016-08-11 01:40:23 +02:00
|
|
|
array(
|
|
|
|
'Help',
|
|
|
|
'SilverStripe-Admin-CMSProfileController',
|
|
|
|
'SilverStripe-Admin-SecurityAdmin'
|
|
|
|
),
|
2013-06-21 00:32:08 +02:00
|
|
|
$menuItems,
|
2011-03-23 10:51:00 +01:00
|
|
|
'Groups with limited access can only access the interfaces they have permissions for'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// all cms sections user
|
2015-09-07 07:38:29 +02:00
|
|
|
$this->logInAs($allcmssectionsuser);
|
|
|
|
$this->resetMenu();
|
2016-08-11 01:40:23 +02:00
|
|
|
$menuItems = LeftAndMain::singleton()->MainMenu(false);
|
|
|
|
$this->assertContains(
|
|
|
|
'SilverStripe-Admin-CMSProfileController',
|
|
|
|
$menuItems->column('Code'),
|
|
|
|
'Group with CMS_ACCESS_SilverStripe\\Admin\\LeftAndMain permission can edit own profile'
|
2015-09-07 07:38:29 +02:00
|
|
|
);
|
2016-08-11 01:40:23 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'SilverStripe-Admin-SecurityAdmin',
|
|
|
|
$menuItems->column('Code'),
|
|
|
|
'Group with CMS_ACCESS_SilverStripe\\Admin\\LeftAndMain permission can access all sections'
|
2011-03-29 06:54:08 +02:00
|
|
|
);
|
2016-08-11 01:40:23 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'Help',
|
|
|
|
$menuItems->column('Code'),
|
|
|
|
'Group with CMS_ACCESS_SilverStripe\\Admin\\LeftAndMain permission can access all sections'
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// admin
|
2015-09-07 07:38:29 +02:00
|
|
|
$this->logInAs($adminuser);
|
|
|
|
$this->resetMenu();
|
2016-08-11 01:40:23 +02:00
|
|
|
$menuItems = LeftAndMain::singleton()->MainMenu(false);
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->assertContains(
|
2016-08-11 01:40:23 +02:00
|
|
|
'SilverStripe-Admin-SecurityAdmin',
|
|
|
|
$menuItems->column('Code'),
|
2011-03-29 06:54:08 +02:00
|
|
|
'Administrators can access Security Admin'
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
|
|
|
}
|
2014-08-08 04:37:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test {@see LeftAndMain::updatetreenodes}
|
|
|
|
*/
|
|
|
|
public function testUpdateTreeNodes() {
|
|
|
|
$page1 = $this->objFromFixture('LeftAndMainTest_Object', 'page1');
|
|
|
|
$page2 = $this->objFromFixture('LeftAndMainTest_Object', 'page2');
|
|
|
|
$page3 = $this->objFromFixture('LeftAndMainTest_Object', 'page3');
|
|
|
|
$page31 = $this->objFromFixture('LeftAndMainTest_Object', 'page31');
|
|
|
|
$page32 = $this->objFromFixture('LeftAndMainTest_Object', 'page32');
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
// Check page
|
|
|
|
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids='.$page1->ID);
|
|
|
|
$this->assertEquals(200, $result->getStatusCode());
|
|
|
|
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
|
|
|
|
$data = json_decode($result->getBody(), true);
|
|
|
|
$pageData = $data[$page1->ID];
|
|
|
|
$this->assertEquals(0, $pageData['ParentID']);
|
|
|
|
$this->assertEquals($page2->ID, $pageData['NextID']);
|
|
|
|
$this->assertEmpty($pageData['PrevID']);
|
|
|
|
|
|
|
|
// check subpage
|
|
|
|
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids='.$page31->ID);
|
|
|
|
$this->assertEquals(200, $result->getStatusCode());
|
|
|
|
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
|
|
|
|
$data = json_decode($result->getBody(), true);
|
|
|
|
$pageData = $data[$page31->ID];
|
|
|
|
$this->assertEquals($page3->ID, $pageData['ParentID']);
|
|
|
|
$this->assertEquals($page32->ID, $pageData['NextID']);
|
|
|
|
$this->assertEmpty($pageData['PrevID']);
|
|
|
|
|
|
|
|
// Multiple pages
|
|
|
|
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids='.$page1->ID.','.$page2->ID);
|
|
|
|
$this->assertEquals(200, $result->getStatusCode());
|
|
|
|
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
|
|
|
|
$data = json_decode($result->getBody(), true);
|
|
|
|
$this->assertEquals(2, count($data));
|
|
|
|
|
|
|
|
// Invalid IDs
|
|
|
|
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids=-3');
|
|
|
|
$this->assertEquals(200, $result->getStatusCode());
|
|
|
|
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
|
|
|
|
$data = json_decode($result->getBody(), true);
|
|
|
|
$this->assertEquals(0, count($data));
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
2013-06-02 03:37:34 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2011-03-29 06:54:08 +02:00
|
|
|
class LeftAndMainTest_Controller extends LeftAndMain implements TestOnly {
|
|
|
|
protected $template = 'BlankPage';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $tree_class = 'LeftAndMainTest_Object';
|
2011-03-29 06:54:08 +02:00
|
|
|
}
|
|
|
|
|
2013-06-02 03:37:34 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2011-03-29 06:54:08 +02:00
|
|
|
class LeftAndMainTest_Object extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2011-03-29 06:54:08 +02:00
|
|
|
'Title' => 'Varchar',
|
|
|
|
'URLSegment' => 'Varchar',
|
|
|
|
'Sort' => 'Int',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-07 07:38:29 +02:00
|
|
|
private static $default_sort = '"Sort"';
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
'SilverStripe\\ORM\\Hierarchy\\Hierarchy'
|
2011-03-29 06:54:08 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-08 04:37:57 +02:00
|
|
|
public function CMSTreeClasses() {}
|
|
|
|
|
2011-10-29 03:45:44 +02:00
|
|
|
}
|