Remove reliance on $_REQUEST global

Update to support 4.0
Alias dev-master as 1.0
This commit is contained in:
Damian Mooyman 2015-03-06 12:02:46 +13:00
parent f35800541e
commit 9c678ca540
4 changed files with 107 additions and 117 deletions

View File

@ -6,10 +6,12 @@ php:
env: env:
- DB=MYSQL CORE_RELEASE=3.1 - DB=MYSQL CORE_RELEASE=3.1
- DB=PGSQL CORE_RELEASE=master - DB=PGSQL CORE_RELEASE=3.1
matrix: matrix:
include: include:
- php: 5.4
env: DB=MYSQL CORE_RELEASE=3
- php: 5.4 - php: 5.4
env: DB=MYSQL CORE_RELEASE=master env: DB=MYSQL CORE_RELEASE=master
@ -19,4 +21,4 @@ before_script:
- cd ~/builds/ss - cd ~/builds/ss
script: script:
- phpunit widgets/tests/ - vendor/bin/phpunit widgets/tests/

View File

@ -119,50 +119,49 @@ class WidgetAreaEditor extends FormField {
} }
} }
if(isset($_REQUEST['Widget'])) { if(!$this->getForm()) throw new Exception("no form");
foreach(array_keys($_REQUEST['Widget']) as $widgetAreaName) {
if ($widgetAreaName !== $this->name) { $widgetData = $this->getForm()->getRequest()->requestVar('Widget');
continue; if($widgetData && isset($widgetData[$this->getName()])) {
$widgetAreaData = $widgetData[$this->getName()];
foreach($widgetAreaData as $newWidgetID => $newWidgetData) {
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if(!is_numeric($newWidgetID)) {
$newWidgetID = 0;
} }
foreach(array_keys($_REQUEST['Widget'][$widgetAreaName]) as $newWidgetID) { $widget = null;
$newWidgetData = $_REQUEST['Widget'][$widgetAreaName][$newWidgetID]; if($newWidgetID) {
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if(!is_numeric($newWidgetID)) {
$newWidgetID = 0;
}
// \"ParentID\" = '0' is for the new page // \"ParentID\" = '0' is for the new page
$widget = DataObject::get_one( $widget = Widget::get()
'Widget', ->filter('ParentID', array(0, $record->$name()->ID))
"(\"ParentID\" = '{$record->$name()->ID}' OR ". ->byID($newWidgetID);
"\"ParentID\" = '0') AND \"Widget\".\"ID\" = '$newWidgetID'"
);
// check if we are updating an existing widget // check if we are updating an existing widget
if($widget && isset($missingWidgets[$widget->ID])) { if($widget && isset($missingWidgets[$widget->ID])) {
unset($missingWidgets[$widget->ID]); unset($missingWidgets[$widget->ID]);
} }
}
// create a new object // create a new object
if(!$widget && !empty($newWidgetData['Type']) && class_exists($newWidgetData['Type'])) { if(!$widget
$widget = new $newWidgetData['Type'](); && !empty($newWidgetData['Type'])
$widget->ID = 0; && class_exists($newWidgetData['Type'])
&& is_subclass_of($newWidgetData['Type'], 'Widget')
) {
$widget = Injector::inst()->create($newWidgetData['Type']);
$widget->ID = 0;
$widget->ParentID = $record->$name()->ID;
}
if($widget) {
if($widget->ParentID == 0) {
$widget->ParentID = $record->$name()->ID; $widget->ParentID = $record->$name()->ID;
if(!is_subclass_of($widget, 'Widget')) {
$widget = null;
}
} }
if($widget) { $widget->populateFromPostData($newWidgetData);
if($widget->ParentID == 0) {
$widget->ParentID = $record->$name()->ID;
}
$widget->populateFromPostData($newWidgetData);
}
} }
} }
} }

View File

@ -4,14 +4,21 @@
"type": "silverstripe-module", "type": "silverstripe-module",
"keywords": ["silverstripe", "widgets", "blog"], "keywords": ["silverstripe", "widgets", "blog"],
"authors": [ "authors": [
{ {
"name": "Ingo Schommer", "name": "Ingo Schommer",
"email": "ingo@silverstripe.com" "email": "ingo@silverstripe.com"
} }
], ],
"require": "require": {
{ "silverstripe/framework": ">=3.1",
"silverstripe/framework": "~3.1", "silverstripe/cms": ">=3.1"
"silverstripe/cms": "~3.1" },
"require-dev": {
"phpunit/PHPUnit": "~3.7@stable"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
} }
} }

View File

@ -17,9 +17,7 @@ class WidgetAreaEditorTest extends SapphireTest {
protected $usesDatabase = true; protected $usesDatabase = true;
function testFillingOneArea() { function testFillingOneArea() {
$oldRequest = $_REQUEST; $data = array(
$_REQUEST = array(
'Widget' => array( 'Widget' => array(
'BottomBar' => array( 'BottomBar' => array(
'new-1' => array( 'new-1' => array(
@ -30,13 +28,16 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
@ -44,14 +45,10 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
$_REQUEST = $oldRequest;
} }
function testFillingTwoAreas() { function testFillingTwoAreas() {
$oldRequest = $_REQUEST; $data = array(
$_REQUEST = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
@ -69,13 +66,15 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
@ -89,15 +88,11 @@ class WidgetAreaEditorTest extends SapphireTest {
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide');
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$_REQUEST = $oldRequest;
} }
function testDeletingOneWidgetFromOneArea() { function testDeletingOneWidgetFromOneArea() {
$oldRequest = $_REQUEST;
// First get some widgets in there // First get some widgets in there
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
@ -115,13 +110,15 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
@ -130,7 +127,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
), ),
@ -143,9 +140,9 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide->saveInto($page); $form->setRequest($request);
$editorBott->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
@ -157,15 +154,11 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
$_REQUEST = $oldRequest;
} }
function testDeletingAWidgetFromEachArea() { function testDeletingAWidgetFromEachArea() {
$oldRequest = $_REQUEST;
// First get some widgets in there // First get some widgets in there
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
@ -183,13 +176,15 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
@ -198,7 +193,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
), ),
@ -206,9 +201,9 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide->saveInto($page); $form->setRequest($request);
$editorBott->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
@ -219,15 +214,11 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
$_REQUEST = $oldRequest;
} }
function testEditingOneWidget() { function testEditingOneWidget() {
$oldRequest = $_REQUEST;
// First get some widgets in there // First get some widgets in there
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
@ -245,13 +236,15 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
@ -260,7 +253,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
@ -278,10 +271,9 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
@ -294,16 +286,11 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
$_REQUEST = $oldRequest;
} }
function testEditingAWidgetFromEachArea() { function testEditingAWidgetFromEachArea() {
$oldRequest = $_REQUEST;
// First get some widgets in there // First get some widgets in there
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
@ -321,13 +308,15 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
@ -336,7 +325,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
@ -354,10 +343,9 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
@ -370,16 +358,11 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom-edited'); $this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom-edited');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
$_REQUEST = $oldRequest;
} }
function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() { function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() {
$oldRequest = $_REQUEST;
// First get some widgets in there // First get some widgets in there
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
@ -397,9 +380,12 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$editorSide = new WidgetAreaEditor('SideBar'); $editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar'); $editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$editorSide->saveInto($page); $editorSide->saveInto($page);
@ -412,7 +398,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget) // Save again (after removing the SideBar's widget)
$_REQUEST = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
@ -425,10 +411,9 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$editorSide->saveInto($page); $form->saveInto($page);
$editorBott->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
@ -440,9 +425,6 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
$_REQUEST = $oldRequest;
} }
} }