OSS-905 PSR2 compliance

This commit is contained in:
Shoaib Ali 2015-11-07 15:42:22 +13:00
parent eee39bca4e
commit 0a19119114
9 changed files with 185 additions and 197 deletions

View File

@ -5,7 +5,6 @@
* @package widgets * @package widgets
*/ */
class WidgetContentControllerExtension extends Extension { class WidgetContentControllerExtension extends Extension {
/** /**
* *
* @var array * @var array
@ -13,32 +12,34 @@ class WidgetContentControllerExtension extends Extension {
private static $allowed_actions = array( private static $allowed_actions = array(
'handleWidget' 'handleWidget'
); );
/** /**
* Handles widgets attached to a page through one or more {@link WidgetArea} * Handles widgets attached to a page through one or more {@link WidgetArea}
* elements. * elements.
* *
* Iterated through each $has_one relation with a {@link WidgetArea} and * Iterated through each $has_one relation with a {@link WidgetArea} and
* looks for connected widgets by their database identifier. * looks for connected widgets by their database identifier.
* *
* Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>. * Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
* *
* @return RequestHandler * @return RequestHandler
*/ */
public function handleWidget() { public function handleWidget() {
$SQL_id = $this->owner->getRequest()->param('ID'); $SQL_id = $this->owner->getRequest()->param('ID');
if(!$SQL_id) return false; if (!$SQL_id) {
// find WidgetArea relations
$widgetAreaRelations = array();
$hasOnes = $this->owner->data()->hasOne();
if(!$hasOnes) {
return false; return false;
} }
foreach($hasOnes as $hasOneName => $hasOneClass) { // find WidgetArea relations
if($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) { $widgetAreaRelations = array();
$hasOnes = $this->owner->data()->hasOne();
if (!$hasOnes) {
return false;
}
foreach ($hasOnes as $hasOneName => $hasOneClass) {
if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
$widgetAreaRelations[] = $hasOneName; $widgetAreaRelations[] = $hasOneName;
} }
} }
@ -46,8 +47,8 @@ class WidgetContentControllerExtension extends Extension {
// find widget // find widget
$widget = null; $widget = null;
foreach($widgetAreaRelations as $widgetAreaRelation) { foreach ($widgetAreaRelations as $widgetAreaRelation) {
if($widget) { if ($widget) {
break; break;
} }
@ -56,10 +57,10 @@ class WidgetContentControllerExtension extends Extension {
->First(); ->First();
} }
if(!$widget) { if (!$widget) {
user_error('No widget found', E_USER_ERROR); user_error('No widget found', E_USER_ERROR);
} }
return $widget->getController(); return $widget->getController();
} }
} }

View File

@ -1,25 +1,24 @@
<?php <?php
/** /**
* Optional controller for every widget which has its own logic, e.g. in forms. * Optional controller for every widget which has its own logic, e.g. in forms.
* *
* It always handles a single widget, usually passed in as a database * It always handles a single widget, usually passed in as a database
* identifier through the controller URL. Needs to be constructed as a nested * identifier through the controller URL. Needs to be constructed as a nested
* controller within a {@link ContentController}. * controller within a {@link ContentController}.
* *
* ## Forms * ## Forms
* You can add forms like in any other SilverStripe controller. If you need * You can add forms like in any other SilverStripe controller. If you need
* access to the widget from within a form, you can use * access to the widget from within a form, you can use
* `$this->controller->getWidget()` inside the form logic. * `$this->controller->getWidget()` inside the form logic.
* *
* Note: Widget controllers currently only work on {@link Page} objects, * Note: Widget controllers currently only work on {@link Page} objects,
* because the logic is implemented in {@link ContentController->handleWidget()}. * because the logic is implemented in {@link ContentController->handleWidget()}.
* Copy this logic and the URL rules to enable it for other controllers. * Copy this logic and the URL rules to enable it for other controllers.
* *
* @package widgets * @package widgets
*/ */
class WidgetController extends Controller { class WidgetController extends Controller {
/** /**
* @var Widget * @var Widget
*/ */
@ -36,11 +35,11 @@ class WidgetController extends Controller {
* @param Widget $widget * @param Widget $widget
*/ */
public function __construct($widget = null) { public function __construct($widget = null) {
if($widget) { if ($widget) {
$this->widget = $widget; $this->widget = $widget;
$this->failover = $widget; $this->failover = $widget;
} }
parent::__construct(); parent::__construct();
} }
@ -51,47 +50,47 @@ class WidgetController extends Controller {
public function Link($action = null) { public function Link($action = null) {
$id = ($this->widget) ? $this->widget->ID : null; $id = ($this->widget) ? $this->widget->ID : null;
$segment = Controller::join_links('widget', $id, $action); $segment = Controller::join_links('widget', $id, $action);
if($page = Director::get_current_page()) { if ($page = Director::get_current_page()) {
return $page->Link($segment); return $page->Link($segment);
} }
return Controller::curr()->Link($segment); return Controller::curr()->Link($segment);
} }
/** /**
* @return Widget * @return Widget
*/ */
public function getWidget() { public function getWidget() {
return $this->widget; return $this->widget;
} }
/** /**
* Overloaded from {@link Widget->Content()} to allow for controller / form * Overloaded from {@link Widget->Content()} to allow for controller / form
* linking. * linking.
* *
* @return string HTML * @return string HTML
*/ */
public function Content() { public function Content() {
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class))); return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
} }
/** /**
* Overloaded from {@link Widget->WidgetHolder()} to allow for controller/ * Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
* form linking. * form linking.
* *
* @return string HTML * @return string HTML
*/ */
public function WidgetHolder() { public function WidgetHolder() {
return $this->renderWith("WidgetHolder"); return $this->renderWith("WidgetHolder");
} }
/** /**
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()} * Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
* to render a administrator-view of the widget. It is assumed that this * to render a administrator-view of the widget. It is assumed that this
* view contains form elements which are submitted and saved through * view contains form elements which are submitted and saved through
* {@link WidgetAreaEditor} within the CMS interface. * {@link WidgetAreaEditor} within the CMS interface.
* *
* @return string HTML * @return string HTML
*/ */
public function editablesegment() { public function editablesegment() {
@ -101,14 +100,14 @@ class WidgetController extends Controller {
$locale = Member::currentUser()->Locale; $locale = Member::currentUser()->Locale;
i18n::set_locale($locale); i18n::set_locale($locale);
} }
if(class_exists($className) && is_subclass_of($className, 'Widget')) { if (class_exists($className) && is_subclass_of($className, 'Widget')) {
$obj = new $className(); $obj = new $className();
return $obj->EditableSegment(); return $obj->EditableSegment();
} else { } else {
user_error("Bad widget class: $className", E_USER_WARNING); user_error("Bad widget class: $className", E_USER_WARNING);
return "Bad widget class name given"; return "Bad widget class name given";
} }
} }
} }
/** /**
@ -116,5 +115,4 @@ class WidgetController extends Controller {
* @package widgets * @package widgets
*/ */
class Widget_Controller extends WidgetController { class Widget_Controller extends WidgetController {
} }

View File

@ -9,7 +9,6 @@
* without using this class. * without using this class.
*/ */
class WidgetPageExtension extends DataExtension { class WidgetPageExtension extends DataExtension {
private static $db = array( private static $db = array(
'InheritSideBar' => 'Boolean', 'InheritSideBar' => 'Boolean',
); );
@ -24,11 +23,11 @@ class WidgetPageExtension extends DataExtension {
public function updateCMSFields(FieldList $fields) { public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab( $fields->addFieldToTab(
"Root.Widgets", "Root.Widgets",
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent') new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent')
); );
$fields->addFieldToTab( $fields->addFieldToTab(
"Root.Widgets", "Root.Widgets",
new WidgetAreaEditor("SideBar") new WidgetAreaEditor("SideBar")
); );
} }
@ -37,30 +36,29 @@ class WidgetPageExtension extends DataExtension {
* @return WidgetArea * @return WidgetArea
*/ */
public function SideBarView() { public function SideBarView() {
if( if (
$this->owner->InheritSideBar $this->owner->InheritSideBar
&& ($parent = $this->owner->getParent()) && ($parent = $this->owner->getParent())
&& $parent->hasMethod('SideBarView') && $parent->hasMethod('SideBarView')
) { ) {
return $parent->SideBarView(); return $parent->SideBarView();
} elseif($this->owner->SideBar()->exists()){ } elseif ($this->owner->SideBar()->exists()) {
return $this->owner->SideBar(); return $this->owner->SideBar();
} }
} }
public function onBeforeDuplicate($duplicatePage) { public function onBeforeDuplicate($duplicatePage) {
if($this->owner->hasField('SideBarID')) { if ($this->owner->hasField('SideBarID')) {
$sideBar = $this->owner->getComponent('SideBar'); $sideBar = $this->owner->getComponent('SideBar');
$duplicateWidgetArea = $sideBar->duplicate(); $duplicateWidgetArea = $sideBar->duplicate();
foreach($sideBar->Items() as $originalWidget) { foreach ($sideBar->Items() as $originalWidget) {
$widget = $originalWidget->duplicate(false); $widget = $originalWidget->duplicate(false);
$widget->ParentID = $duplicateWidgetArea->ID; $widget->ParentID = $duplicateWidgetArea->ID;
$widget->write(); $widget->write();
} }
$duplicatePage->SideBarID = $duplicateWidgetArea->ID; $duplicatePage->SideBarID = $duplicateWidgetArea->ID;
} }
return $duplicatePage; return $duplicatePage;
@ -73,5 +71,4 @@ class WidgetPageExtension extends DataExtension {
//reset the sidebar ID //reset the sidebar ID
$this->owner->SideBarID = 0; $this->owner->SideBarID = 0;
} }
} }

View File

@ -6,7 +6,6 @@
* @package widgets * @package widgets
*/ */
class WidgetAreaEditor extends FormField { class WidgetAreaEditor extends FormField {
/** /**
* @param string $name * @param string $name
* @param array $widgetClasses * @param array $widgetClasses
@ -15,7 +14,7 @@ class WidgetAreaEditor extends FormField {
public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) { public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) {
$this->MaxWidgets = $maxWidgets; $this->MaxWidgets = $maxWidgets;
$this->widgetClasses = $widgetClasses; $this->widgetClasses = $widgetClasses;
parent::__construct($name); parent::__construct($name);
} }
@ -38,30 +37,28 @@ class WidgetAreaEditor extends FormField {
public function AvailableWidgets() { public function AvailableWidgets() {
$widgets= new ArrayList(); $widgets= new ArrayList();
foreach($this->widgetClasses as $widgetClass) { foreach ($this->widgetClasses as $widgetClass) {
$classes = ClassInfo::subclassesFor($widgetClass); $classes = ClassInfo::subclassesFor($widgetClass);
if (isset($classes['Widget'])) { if (isset($classes['Widget'])) {
unset($classes['Widget']); unset($classes['Widget']);
} } elseif (isset($classes[0]) && $classes[0] == 'Widget') {
else if (isset($classes[0]) && $classes[0] == 'Widget') { unset($classes[0]);
unset($classes[0]);
} }
foreach($classes as $class) { foreach ($classes as $class) {
$available = Config::inst()->get($class, 'only_available_in'); $available = Config::inst()->get($class, 'only_available_in');
if (!empty($available) && is_array($available)) { if (!empty($available) && is_array($available)) {
if(in_array($this->Name, $available)) { if (in_array($this->Name, $available)) {
$widgets->push(singleton($class)); $widgets->push(singleton($class));
} }
}else { } else {
$widgets->push(singleton($class)); $widgets->push(singleton($class));
} }
} }
} }
return $widgets; return $widgets;
} }
@ -71,7 +68,7 @@ class WidgetAreaEditor extends FormField {
public function UsedWidgets() { public function UsedWidgets() {
// Call class_exists() to load Widget.php earlier and avoid a segfault // Call class_exists() to load Widget.php earlier and avoid a segfault
class_exists('Widget'); class_exists('Widget');
$relationName = $this->name; $relationName = $this->name;
$widgets = $this->form->getRecord()->getComponent($relationName)->Items(); $widgets = $this->form->getRecord()->getComponent($relationName)->Items();
@ -104,49 +101,51 @@ class WidgetAreaEditor extends FormField {
$widgetarea = $record->getComponent($name); $widgetarea = $record->getComponent($name);
$widgetarea->write(); $widgetarea->write();
$record->$idName = $widgetarea->ID; $record->$idName = $widgetarea->ID;
$widgets = $widgetarea->Items(); $widgets = $widgetarea->Items();
// store the field IDs and delete the missing fields // store the field IDs and delete the missing fields
// alternatively, we could delete all the fields and re add them // alternatively, we could delete all the fields and re add them
$missingWidgets = array(); $missingWidgets = array();
if($widgets) { if ($widgets) {
foreach($widgets as $existingWidget) { foreach ($widgets as $existingWidget) {
$missingWidgets[$existingWidget->ID] = $existingWidget; $missingWidgets[$existingWidget->ID] = $existingWidget;
} }
} }
if(!$this->getForm()) throw new Exception("no form"); if (!$this->getForm()) {
throw new Exception("no form");
}
$widgetData = $this->getForm()->getRequest()->requestVar('Widget'); $widgetData = $this->getForm()->getRequest()->requestVar('Widget');
if($widgetData && isset($widgetData[$this->getName()])) { if ($widgetData && isset($widgetData[$this->getName()])) {
$widgetAreaData = $widgetData[$this->getName()]; $widgetAreaData = $widgetData[$this->getName()];
foreach($widgetAreaData as $newWidgetID => $newWidgetData) { foreach ($widgetAreaData as $newWidgetID => $newWidgetData) {
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query // Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if(!is_numeric($newWidgetID)) { if (!is_numeric($newWidgetID)) {
$newWidgetID = 0; $newWidgetID = 0;
} }
$widget = null; $widget = null;
if($newWidgetID) { if ($newWidgetID) {
// \"ParentID\" = '0' is for the new page // \"ParentID\" = '0' is for the new page
$widget = Widget::get() $widget = Widget::get()
->filter('ParentID', array(0, $record->$name()->ID)) ->filter('ParentID', array(0, $record->$name()->ID))
->byID($newWidgetID); ->byID($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 if (!$widget
&& !empty($newWidgetData['Type']) && !empty($newWidgetData['Type'])
&& class_exists($newWidgetData['Type']) && class_exists($newWidgetData['Type'])
&& is_subclass_of($newWidgetData['Type'], 'Widget') && is_subclass_of($newWidgetData['Type'], 'Widget')
@ -156,8 +155,8 @@ class WidgetAreaEditor extends FormField {
$widget->ParentID = $record->$name()->ID; $widget->ParentID = $record->$name()->ID;
} }
if($widget) { if ($widget) {
if($widget->ParentID == 0) { if ($widget->ParentID == 0) {
$widget->ParentID = $record->$name()->ID; $widget->ParentID = $record->$name()->ID;
} }
@ -165,11 +164,11 @@ class WidgetAreaEditor extends FormField {
} }
} }
} }
// remove the fields not saved // remove the fields not saved
if($missingWidgets) { if ($missingWidgets) {
foreach($missingWidgets as $removedWidget) { foreach ($missingWidgets as $removedWidget) {
if(isset($removedWidget) && is_numeric($removedWidget->ID)) { if (isset($removedWidget) && is_numeric($removedWidget->ID)) {
$removedWidget->delete(); $removedWidget->delete();
} }
} }

View File

@ -1,17 +1,16 @@
<?php <?php
/** /**
* Widgets let CMS authors drag and drop small pieces of functionality into * Widgets let CMS authors drag and drop small pieces of functionality into
* defined areas of their websites. * defined areas of their websites.
* *
* You can use forms in widgets by implementing a {@link WidgetController}. * You can use forms in widgets by implementing a {@link WidgetController}.
* *
* See {@link Widget_Controller} for more information. * See {@link Widget_Controller} for more information.
* *
* @package widgets * @package widgets
*/ */
class Widget extends DataObject { class Widget extends DataObject {
/** /**
* @var array * @var array
*/ */
@ -35,7 +34,7 @@ class Widget extends DataObject {
'CMSTitle' => 'Text', 'CMSTitle' => 'Text',
'Description' => 'Text', 'Description' => 'Text',
); );
private static $only_available_in = array(); private static $only_available_in = array();
/** /**
@ -81,10 +80,10 @@ class Widget extends DataObject {
parent::populateDefaults(); parent::populateDefaults();
$this->setField('Title', $this->getTitle()); $this->setField('Title', $this->getTitle());
} }
/** /**
* Note: Overloaded in {@link WidgetController}. * Note: Overloaded in {@link WidgetController}.
* *
* @return string HTML * @return string HTML
*/ */
public function WidgetHolder() { public function WidgetHolder() {
@ -95,22 +94,22 @@ class Widget extends DataObject {
* Default way to render widget in templates. * Default way to render widget in templates.
* @return string HTML * @return string HTML
*/ */
public function forTemplate($holder = true){ public function forTemplate($holder = true) {
if($holder){ if ($holder) {
return $this->WidgetHolder(); return $this->WidgetHolder();
} }
return $this->Content(); return $this->Content();
} }
/** /**
* Renders the widget content in a custom template with the same name as the * Renders the widget content in a custom template with the same name as the
* current class. This should be the main point of output customization. * current class. This should be the main point of output customization.
* *
* Invoked from within WidgetHolder.ss, which contains the "framing" around * Invoked from within WidgetHolder.ss, which contains the "framing" around
* the custom content, like a title. * the custom content, like a title.
* *
* Note: Overloaded in {@link WidgetController}. * Note: Overloaded in {@link WidgetController}.
* *
* @return string HTML * @return string HTML
*/ */
public function Content() { public function Content() {
@ -169,16 +168,16 @@ class Widget extends DataObject {
* @return string - HTML * @return string - HTML
*/ */
public function DescriptionSegment() { public function DescriptionSegment() {
return $this->renderWith('WidgetDescription'); return $this->renderWith('WidgetDescription');
} }
/** /**
* @see WidgetController::editablesegment() * @see WidgetController::editablesegment()
* *
* @return string - HTML * @return string - HTML
*/ */
public function EditableSegment() { public function EditableSegment() {
return $this->renderWith('WidgetEditor'); return $this->renderWith('WidgetEditor');
} }
/** /**
@ -192,7 +191,7 @@ class Widget extends DataObject {
$this->extend('updateCMSFields', $fields); $this->extend('updateCMSFields', $fields);
return $fields; return $fields;
} }
/** /**
* @return FieldList * @return FieldList
*/ */
@ -200,7 +199,7 @@ class Widget extends DataObject {
$fields = $this->getCMSFields(); $fields = $this->getCMSFields();
$outputFields = new FieldList(); $outputFields = new FieldList();
foreach($fields as $field) { foreach ($fields as $field) {
$name = $field->getName(); $name = $field->getName();
$value = $this->getField($name); $value = $this->getField($name);
if ($value) { if ($value) {
@ -234,25 +233,25 @@ class Widget extends DataObject {
* @return WidgetController * @return WidgetController
*/ */
public function getController() { public function getController() {
if($this->controller) { if ($this->controller) {
return $this->controller; return $this->controller;
} }
foreach(array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) { foreach (array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
$controllerClass = "{$widgetClass}_Controller"; $controllerClass = "{$widgetClass}_Controller";
if(class_exists($controllerClass)) { if (class_exists($controllerClass)) {
break; break;
} }
$controllerClass = "{$widgetClass}Controller"; $controllerClass = "{$widgetClass}Controller";
if(class_exists($controllerClass)) { if (class_exists($controllerClass)) {
break; break;
} }
} }
if(!class_exists($controllerClass)) { if (!class_exists($controllerClass)) {
throw new Exception("Could not find controller class for $this->classname"); throw new Exception("Could not find controller class for $this->classname");
} }
@ -260,37 +259,35 @@ class Widget extends DataObject {
return $this->controller; return $this->controller;
} }
/** /**
* @param array $data * @param array $data
*/ */
public function populateFromPostData($data) { public function populateFromPostData($data) {
$fields = $this->getCMSFields(); $fields = $this->getCMSFields();
foreach($data as $name => $value) { foreach ($data as $name => $value) {
if($name != "Type") { if ($name != "Type") {
if ($field = $fields->dataFieldByName($name)) { if ($field = $fields->dataFieldByName($name)) {
$field->setValue($value); $field->setValue($value);
$field->saveInto($this); $field->saveInto($this);
} } else {
else {
$this->setField($name, $value); $this->setField($name, $value);
} }
} }
} }
//Look for checkbox fields not present in the data //Look for checkbox fields not present in the data
foreach($fields as $field) { foreach ($fields as $field) {
if($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) { if ($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
$field->setValue(false); $field->setValue(false);
$field->saveInto($this); $field->saveInto($this);
} }
} }
$this->write(); $this->write();
// The field must be written to ensure a unique ID. // The field must be written to ensure a unique ID.
$this->Name = $this->class.$this->ID; $this->Name = $this->class.$this->ID;
$this->write(); $this->write();
} }
} }

View File

@ -6,7 +6,6 @@
* @package widgets * @package widgets
*/ */
class WidgetArea extends DataObject { class WidgetArea extends DataObject {
/** /**
* @var array * @var array
*/ */
@ -19,18 +18,18 @@ class WidgetArea extends DataObject {
* @var string * @var string
*/ */
public $template = __CLASS__; public $template = __CLASS__;
/** /**
* Used in template instead of {@link Widgets()} to wrap each widget in its * Used in template instead of {@link Widgets()} to wrap each widget in its
* controller, making it easier to access and process form logic and * controller, making it easier to access and process form logic and
* actions stored in {@link WidgetController}. * actions stored in {@link WidgetController}.
* *
* @return SS_List - Collection of {@link WidgetController} instances. * @return SS_List - Collection of {@link WidgetController} instances.
*/ */
public function WidgetControllers() { public function WidgetControllers() {
$controllers = new ArrayList(); $controllers = new ArrayList();
foreach($this->ItemsToRender() as $widget) { foreach ($this->ItemsToRender() as $widget) {
$controller = $widget->getController(); $controller = $widget->getController();
$controller->init(); $controller->init();
@ -59,7 +58,7 @@ class WidgetArea extends DataObject {
* @return string - HTML * @return string - HTML
*/ */
public function forTemplate() { public function forTemplate() {
return $this->renderWith($this->template); return $this->renderWith($this->template);
} }
/** /**
@ -75,9 +74,8 @@ class WidgetArea extends DataObject {
*/ */
public function onBeforeDelete() { public function onBeforeDelete() {
parent::onBeforeDelete(); parent::onBeforeDelete();
foreach($this->Widgets() as $widget) { foreach ($this->Widgets() as $widget) {
$widget->delete(); $widget->delete();
} }
} }
} }

View File

@ -13,7 +13,7 @@ class WidgetAreaEditorTest extends SapphireTest {
'WidgetAreaEditorTest_FakePage', 'WidgetAreaEditorTest_FakePage',
'WidgetAreaEditorTest_TestWidget', 'WidgetAreaEditorTest_TestWidget',
); );
protected $usesDatabase = true; protected $usesDatabase = true;
protected $requiredExtensions = array( protected $requiredExtensions = array(
@ -21,8 +21,8 @@ class WidgetAreaEditorTest extends SapphireTest {
"WidgetPageExtension" "WidgetPageExtension"
) )
); );
function testFillingOneArea() { public function testFillingOneArea() {
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'BottomBar' => array( 'BottomBar' => array(
@ -35,12 +35,12 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $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 = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page); $form->saveInto($page);
@ -53,7 +53,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
} }
function testFillingTwoAreas() { public function testFillingTwoAreas() {
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'SideBar' => array( 'SideBar' => array(
@ -73,7 +73,7 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $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 = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
@ -85,18 +85,18 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
// Make sure they both got saved // Make sure they both got saved
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1); $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$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');
} }
function testDeletingOneWidgetFromOneArea() { public function testDeletingOneWidgetFromOneArea() {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -117,7 +117,7 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $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 = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
@ -131,7 +131,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$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)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -156,13 +156,13 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$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);
} }
function testDeletingAWidgetFromEachArea() { public function testDeletingAWidgetFromEachArea() {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -183,7 +183,7 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $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 = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
@ -197,7 +197,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$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)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -210,19 +210,19 @@ class WidgetAreaEditorTest extends SapphireTest {
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
$page->flushCache(); $page->flushCache();
$page->BottomBar()->flushCache(); $page->BottomBar()->flushCache();
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$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);
} }
function testEditingOneWidget() { public function testEditingOneWidget() {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -243,7 +243,7 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $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 = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
@ -257,7 +257,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$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)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -287,14 +287,14 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$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');
} }
function testEditingAWidgetFromEachArea() { public function testEditingAWidgetFromEachArea() {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -315,7 +315,7 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $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 = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
@ -329,7 +329,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$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)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -359,14 +359,14 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1); $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$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');
} }
function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() { public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -387,7 +387,7 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $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 = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
@ -402,7 +402,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$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)
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
@ -427,7 +427,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$page->SideBar()->flushCache(); $page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray(); $sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray(); $bottWidgets = $page->BottomBar()->Widgets()->toArray();
$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');

View File

@ -4,46 +4,45 @@
* @subpackage tests * @subpackage tests
*/ */
class WidgetControllerTest extends FunctionalTest { class WidgetControllerTest extends FunctionalTest {
protected static $fixture_file = 'WidgetControllerTest.yml'; protected static $fixture_file = 'WidgetControllerTest.yml';
protected $extraDataObjects = array( protected $extraDataObjects = array(
'WidgetControllerTestPage', 'WidgetControllerTestPage',
'WidgetControllerTest_Widget', 'WidgetControllerTest_Widget',
); );
function testWidgetFormRendering() { public function testWidgetFormRendering() {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1'); $page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live'); $page->publish('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1'); $widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1');
$response = $this->get($page->URLSegment); $response = $this->get($page->URLSegment);
$formAction = sprintf('%s/widget/%d/Form', $page->URLSegment, $widget->ID); $formAction = sprintf('%s/widget/%d/Form', $page->URLSegment, $widget->ID);
$this->assertContains( $this->assertContains(
$formAction, $formAction,
$response->getBody(), $response->getBody(),
"Widget forms are rendered through WidgetArea templates" "Widget forms are rendered through WidgetArea templates"
); );
} }
function testWidgetFormSubmission() { public function testWidgetFormSubmission() {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1'); $page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live'); $page->publish('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1'); $widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1');
$response = $this->get($page->URLSegment); $response = $this->get($page->URLSegment);
$response = $this->submitForm('Form_Form', null, array('TestValue'=>'Updated')); $response = $this->submitForm('Form_Form', null, array('TestValue'=>'Updated'));
$this->assertContains( $this->assertContains(
'TestValue: Updated', 'TestValue: Updated',
$response->getBody(), $response->getBody(),
"Form values are submitted to correct widget form" "Form values are submitted to correct widget form"
); );
$this->assertContains( $this->assertContains(
sprintf('Widget ID: %d', $widget->ID), sprintf('Widget ID: %d', $widget->ID),
$response->getBody(), $response->getBody(),
"Widget form acts on correct widget, as identified in the URL" "Widget form acts on correct widget, as identified in the URL"
); );
@ -64,19 +63,19 @@ class WidgetControllerTest_Widget extends Widget implements TestOnly {
* @package widgets * @package widgets
* @subpackage tests * @subpackage tests
*/ */
class WidgetControllerTest_WidgetController extends WidgetController implements TestOnly { class WidgetControllerTest_WidgetController extends WidgetController implements TestOnly
{
private static $allowed_actions = array( private static $allowed_actions = array(
'Form' 'Form'
); );
function Form() { public function Form() {
$widgetform = new Form( $widgetform = new Form(
$this, $this,
'Form', 'Form',
new FieldList( new FieldList(
new TextField('TestValue') new TextField('TestValue')
), ),
new FieldList( new FieldList(
new FormAction('doAction') new FormAction('doAction')
) )
@ -84,8 +83,8 @@ class WidgetControllerTest_WidgetController extends WidgetController implements
return $widgetform; return $widgetform;
} }
function doAction($data, $form) { public function doAction($data, $form) {
return sprintf('TestValue: %s\nWidget ID: %d', return sprintf('TestValue: %s\nWidget ID: %d',
$data['TestValue'], $data['TestValue'],
$this->widget->ID $this->widget->ID

View File

@ -14,14 +14,13 @@ class WidgetControllerTestPage extends Page implements TestOnly {
* @subpackage tests * @subpackage tests
*/ */
class WidgetControllerTestPage_Controller extends Page_Controller implements TestOnly { class WidgetControllerTestPage_Controller extends Page_Controller implements TestOnly {
/** /**
* Template selection doesnt work in test folders, * Template selection doesnt work in test folders,
* so we enforce a template name. * so we enforce a template name.
*/ */
function getViewer($action) { public function getViewer($action) {
$templates = array('WidgetControllerTestPage'); $templates = array('WidgetControllerTestPage');
return new SSViewer($templates); return new SSViewer($templates);
} }
} }