mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
commit
ee717c7f66
23
_config/dev.yml
Normal file
23
_config/dev.yml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
Name: DevelopmentAdmin
|
||||||
|
---
|
||||||
|
DevelopmentAdmin:
|
||||||
|
registered_controllers:
|
||||||
|
build:
|
||||||
|
controller: 'DevBuildController'
|
||||||
|
links:
|
||||||
|
build: 'Build/rebuild this environment. Call this whenever you have updated your project sources'
|
||||||
|
tests:
|
||||||
|
controller: 'TestRunner'
|
||||||
|
links:
|
||||||
|
tests: 'See a list of unit tests to run'
|
||||||
|
'tests/all': 'Run all tests'
|
||||||
|
jstests:
|
||||||
|
controller: 'JSTestRunner'
|
||||||
|
links:
|
||||||
|
jstests: 'See a list of JavaScript tests to run'
|
||||||
|
'jstests/all': 'Run all JavaScript tests'
|
||||||
|
tasks:
|
||||||
|
controller: 'TaskRunner'
|
||||||
|
links:
|
||||||
|
tasks: 'See a list of build tasks to run'
|
32
dev/DevBuildController.php
Normal file
32
dev/DevBuildController.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class DevBuildController extends Controller {
|
||||||
|
|
||||||
|
private static $url_handlers = array(
|
||||||
|
'' => 'build'
|
||||||
|
);
|
||||||
|
|
||||||
|
private static $allowed_actions = array(
|
||||||
|
'build'
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
public function build($request) {
|
||||||
|
if(Director::is_cli()) {
|
||||||
|
$da = DatabaseAdmin::create();
|
||||||
|
return $da->handleRequest($request, $this->model);
|
||||||
|
} else {
|
||||||
|
$renderer = DebugView::create();
|
||||||
|
$renderer->writeHeader();
|
||||||
|
$renderer->writeInfo("Environment Builder", Director::absoluteBaseURL());
|
||||||
|
echo "<div class=\"build\">";
|
||||||
|
|
||||||
|
$da = DatabaseAdmin::create();
|
||||||
|
return $da->handleRequest($request, $this->model);
|
||||||
|
|
||||||
|
echo "</div>";
|
||||||
|
$renderer->writeFooter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,10 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for URL access to development tools. Currently supports the
|
* Base class for development tools.
|
||||||
* ; and TaskRunner.
|
*
|
||||||
|
* Configured in framework/_config/dev.yml, with the config key registeredControllers being
|
||||||
|
* used to generate the list of links for /dev.
|
||||||
*
|
*
|
||||||
* @todo documentation for how to add new unit tests and tasks
|
* @todo documentation for how to add new unit tests and tasks
|
||||||
|
* @todo do we need buildDefaults and generatesecuretoken? if so, register in the list
|
||||||
|
* @todo cleanup errors() it's not even an allowed action, so can go
|
||||||
|
* @todo cleanup index() html building
|
||||||
* @package framework
|
* @package framework
|
||||||
* @subpackage dev
|
* @subpackage dev
|
||||||
*/
|
*/
|
||||||
@ -13,23 +18,18 @@ class DevelopmentAdmin extends Controller {
|
|||||||
private static $url_handlers = array(
|
private static $url_handlers = array(
|
||||||
'' => 'index',
|
'' => 'index',
|
||||||
'build/defaults' => 'buildDefaults',
|
'build/defaults' => 'buildDefaults',
|
||||||
'$Action' => '$Action',
|
'generatesecuretoken' => 'generatesecuretoken',
|
||||||
'$Action//$Action/$ID' => 'handleAction',
|
'$Action' => 'runRegisteredController',
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $allowed_actions = array(
|
private static $allowed_actions = array(
|
||||||
'index',
|
'index',
|
||||||
'tests',
|
|
||||||
'jstests',
|
|
||||||
'tasks',
|
|
||||||
'viewmodel',
|
|
||||||
'build',
|
|
||||||
'reset',
|
|
||||||
'viewcode',
|
|
||||||
'generatesecuretoken',
|
|
||||||
'buildDefaults',
|
'buildDefaults',
|
||||||
|
'runRegisteredController',
|
||||||
|
'generatesecuretoken',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
parent::init();
|
parent::init();
|
||||||
|
|
||||||
@ -80,20 +80,8 @@ class DevelopmentAdmin extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function index() {
|
public function index() {
|
||||||
$actions = array(
|
|
||||||
"build" => "Build/rebuild this environment. Call this whenever you have updated your project sources",
|
|
||||||
"tests" => "See a list of unit tests to run",
|
|
||||||
"tests/all" => "Run all tests",
|
|
||||||
"jstests" => "See a list of JavaScript tests to run",
|
|
||||||
"jstests/all" => "Run all JavaScript tests",
|
|
||||||
"tasks" => "See a list of build tasks to run"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Web mode
|
// Web mode
|
||||||
if(!Director::is_cli()) {
|
if(!Director::is_cli()) {
|
||||||
// This action is sake-only right now.
|
|
||||||
unset($actions["modules/add"]);
|
|
||||||
|
|
||||||
$renderer = DebugView::create();
|
$renderer = DebugView::create();
|
||||||
$renderer->writeHeader();
|
$renderer->writeHeader();
|
||||||
$renderer->writeInfo("SilverStripe Development Tools", Director::absoluteBaseURL());
|
$renderer->writeInfo("SilverStripe Development Tools", Director::absoluteBaseURL());
|
||||||
@ -101,7 +89,7 @@ class DevelopmentAdmin extends Controller {
|
|||||||
|
|
||||||
echo '<div class="options"><ul>';
|
echo '<div class="options"><ul>';
|
||||||
$evenOdd = "odd";
|
$evenOdd = "odd";
|
||||||
foreach($actions as $action => $description) {
|
foreach(self::get_links() as $action => $description) {
|
||||||
echo "<li class=\"$evenOdd\"><a href=\"{$base}dev/$action\"><b>/dev/$action:</b>"
|
echo "<li class=\"$evenOdd\"><a href=\"{$base}dev/$action\"><b>/dev/$action:</b>"
|
||||||
. " $description</a></li>\n";
|
. " $description</a></li>\n";
|
||||||
$evenOdd = ($evenOdd == "odd") ? "even" : "odd";
|
$evenOdd = ($evenOdd == "odd") ? "even" : "odd";
|
||||||
@ -113,42 +101,74 @@ class DevelopmentAdmin extends Controller {
|
|||||||
} else {
|
} else {
|
||||||
echo "SILVERSTRIPE DEVELOPMENT TOOLS\n--------------------------\n\n";
|
echo "SILVERSTRIPE DEVELOPMENT TOOLS\n--------------------------\n\n";
|
||||||
echo "You can execute any of the following commands:\n\n";
|
echo "You can execute any of the following commands:\n\n";
|
||||||
foreach($actions as $action => $description) {
|
foreach(self::get_links() as $action => $description) {
|
||||||
echo " sake dev/$action: $description\n";
|
echo " sake dev/$action: $description\n";
|
||||||
}
|
}
|
||||||
echo "\n\n";
|
echo "\n\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tests($request) {
|
public function runRegisteredController(SS_HTTPRequest $request){
|
||||||
return TestRunner::create();
|
$controllerClass = null;
|
||||||
}
|
|
||||||
|
$baseUrlPart = $request->param('Action');
|
||||||
public function jstests($request) {
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
||||||
return JSTestRunner::create();
|
if(isset($reg[$baseUrlPart])){
|
||||||
}
|
$controllerClass = $reg[$baseUrlPart]['controller'];
|
||||||
|
}
|
||||||
public function tasks() {
|
|
||||||
return TaskRunner::create();
|
if($controllerClass && class_exists($controllerClass)){
|
||||||
}
|
return $controllerClass::create();
|
||||||
|
}
|
||||||
public function build($request) {
|
|
||||||
if(Director::is_cli()) {
|
$msg = 'Error: no controller registered in '.__CLASS__.' for: '.$request->param('Action');
|
||||||
$da = DatabaseAdmin::create();
|
if(Director::is_cli()){
|
||||||
return $da->handleRequest($request, $this->model);
|
// in CLI we cant use httpError because of a bug with stuff being in the output already, see DevAdminControllerTest
|
||||||
} else {
|
throw new Exception($msg);
|
||||||
$renderer = DebugView::create();
|
}else{
|
||||||
$renderer->writeHeader();
|
$this->httpError(500, $msg);
|
||||||
$renderer->writeInfo("Environment Builder", Director::absoluteBaseURL());
|
|
||||||
echo "<div class=\"build\">";
|
|
||||||
|
|
||||||
$da = DatabaseAdmin::create();
|
|
||||||
return $da->handleRequest($request, $this->model);
|
|
||||||
|
|
||||||
echo "</div>";
|
|
||||||
$renderer->writeFooter();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Internal methods
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array of url => description
|
||||||
|
*/
|
||||||
|
protected static function get_links(){
|
||||||
|
$links = array();
|
||||||
|
|
||||||
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
||||||
|
foreach($reg as $registeredController){
|
||||||
|
foreach($registeredController['links'] as $url => $desc){
|
||||||
|
$links[$url] = $desc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $links;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRegisteredController($baseUrlPart){
|
||||||
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
||||||
|
|
||||||
|
if(isset($reg[$baseUrlPart])){
|
||||||
|
$controllerClass = $reg[$baseUrlPart]['controller'];
|
||||||
|
return $controllerClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Unregistered (hidden) actions
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the default data, calling requireDefaultRecords on all
|
* Build the default data, calling requireDefaultRecords on all
|
||||||
|
116
tests/dev/DevAdminControllerTest.php
Normal file
116
tests/dev/DevAdminControllerTest.php
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Note: the running of this test is handled by the thing it's testing (DevelopmentAdmin controller).
|
||||||
|
*
|
||||||
|
* @package framework
|
||||||
|
* @package tests
|
||||||
|
*/
|
||||||
|
class DevAdminControllerTest extends FunctionalTest {
|
||||||
|
|
||||||
|
public function setUp(){
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Config::nest()->update('DevelopmentAdmin', 'registered_controllers', array(
|
||||||
|
'x1' => array(
|
||||||
|
'controller' => 'DevAdminControllerTest_Controller1',
|
||||||
|
'links' => array(
|
||||||
|
'x1' => 'x1 link description',
|
||||||
|
'x1/y1' => 'x1/y1 link description'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'x2' => array(
|
||||||
|
'controller' => 'DevAdminControllerTest_Controller2', // intentionally not a class that exists
|
||||||
|
'links' => array(
|
||||||
|
'x2' => 'x2 link description'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown(){
|
||||||
|
parent::tearDown();
|
||||||
|
Config::unnest();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testGoodRegisteredControllerOutput(){
|
||||||
|
// Check for the controller running from the registered url above
|
||||||
|
// (we use contains rather than equals because sometimes you get Warning: You probably want to define an entry in $_FILE_TO_URL_MAPPING)
|
||||||
|
$this->assertContains(DevAdminControllerTest_Controller1::OK_MSG, $this->getCapture('/dev/x1'));
|
||||||
|
$this->assertContains(DevAdminControllerTest_Controller1::OK_MSG, $this->getCapture('/dev/x1/y1'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGoodRegisteredControllerStatus(){
|
||||||
|
// Check response code is 200/OK
|
||||||
|
$this->assertEquals(false, $this->getAndCheckForError('/dev/x1'));
|
||||||
|
$this->assertEquals(false, $this->getAndCheckForError('/dev/x1/y1'));
|
||||||
|
|
||||||
|
// Check response code is 500/ some sort of error
|
||||||
|
$this->assertEquals(true, $this->getAndCheckForError('/dev/x2'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected function getCapture($url){
|
||||||
|
$this->logInWithPermission('ADMIN');
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
$this->get($url);
|
||||||
|
$r = ob_get_contents();
|
||||||
|
ob_end_clean();
|
||||||
|
|
||||||
|
return $r;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getAndCheckForError($url){
|
||||||
|
$this->logInWithPermission('ADMIN');
|
||||||
|
|
||||||
|
if(Director::is_cli()){
|
||||||
|
// when in CLI the admin controller throws exceptions
|
||||||
|
ob_start();
|
||||||
|
try{
|
||||||
|
$this->get($url);
|
||||||
|
}catch(Exception $e){
|
||||||
|
ob_end_clean();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ob_end_clean();
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
// when in http the admin controller sets a response header
|
||||||
|
ob_start();
|
||||||
|
$resp = $this->get($url);
|
||||||
|
ob_end_clean();
|
||||||
|
return $resp->isError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class DevAdminControllerTest_Controller1 extends Controller {
|
||||||
|
|
||||||
|
const OK_MSG = 'DevAdminControllerTest_Controller1 TEST OK';
|
||||||
|
|
||||||
|
private static $url_handlers = array(
|
||||||
|
'' => 'index',
|
||||||
|
'y1' => 'y1Action'
|
||||||
|
);
|
||||||
|
|
||||||
|
private static $allowed_actions = array(
|
||||||
|
'index',
|
||||||
|
'y1Action',
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
public function index(){
|
||||||
|
echo self::OK_MSG;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function y1Action(){
|
||||||
|
echo self::OK_MSG;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user