Merge pull request #107 from helpfulrobot/convert-to-psr-2

Converted to PSR-2
This commit is contained in:
Daniel Hensby 2015-11-19 17:34:49 +00:00
commit dfe1236eef
9 changed files with 1233 additions and 1177 deletions

View File

@ -4,8 +4,8 @@
* *
* @package widgets * @package widgets
*/ */
class WidgetContentControllerExtension extends Extension { class WidgetContentControllerExtension extends Extension
{
/** /**
* *
* @var array * @var array
@ -25,20 +25,23 @@ class WidgetContentControllerExtension extends Extension {
* *
* @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) {
return false;
}
// find WidgetArea relations // find WidgetArea relations
$widgetAreaRelations = array(); $widgetAreaRelations = array();
$hasOnes = $this->owner->data()->hasOne(); $hasOnes = $this->owner->data()->hasOne();
if(!$hasOnes) { if (!$hasOnes) {
return false; return false;
} }
foreach($hasOnes as $hasOneName => $hasOneClass) { foreach ($hasOnes as $hasOneName => $hasOneClass) {
if($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) { if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
$widgetAreaRelations[] = $hasOneName; $widgetAreaRelations[] = $hasOneName;
} }
} }
@ -46,8 +49,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,7 +59,7 @@ 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);
} }

View File

@ -18,8 +18,8 @@
* *
* @package widgets * @package widgets
*/ */
class WidgetController extends Controller { class WidgetController extends Controller
{
/** /**
* @var Widget * @var Widget
*/ */
@ -35,8 +35,9 @@ 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;
} }
@ -48,11 +49,12 @@ class WidgetController extends Controller {
* @param string $action * @param string $action
* @return string * @return string
*/ */
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);
} }
@ -62,7 +64,8 @@ class WidgetController extends Controller {
/** /**
* @return Widget * @return Widget
*/ */
public function getWidget() { public function getWidget()
{
return $this->widget; return $this->widget;
} }
@ -72,7 +75,8 @@ class WidgetController extends Controller {
* *
* @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)));
} }
@ -82,7 +86,8 @@ class WidgetController extends Controller {
* *
* @return string HTML * @return string HTML
*/ */
public function WidgetHolder() { public function WidgetHolder()
{
return $this->renderWith("WidgetHolder"); return $this->renderWith("WidgetHolder");
} }
@ -94,14 +99,15 @@ class WidgetController extends Controller {
* *
* @return string HTML * @return string HTML
*/ */
public function editablesegment() { public function editablesegment()
{
$className = $this->urlParams['ID']; $className = $this->urlParams['ID'];
if (class_exists('Translatable') && Member::currentUserID()) { if (class_exists('Translatable') && Member::currentUserID()) {
// set current locale based on logged in user's locale // set current locale based on logged in user's locale
$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 {
@ -115,6 +121,6 @@ class WidgetController extends Controller {
* @deprecated Use WidgetController * @deprecated Use WidgetController
* @package widgets * @package widgets
*/ */
class Widget_Controller extends WidgetController { class Widget_Controller extends WidgetController
{
} }

View File

@ -8,8 +8,8 @@
* feel free to create your own relationships, naming conventions, etc. * feel free to create your own relationships, naming conventions, etc.
* 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',
); );
@ -22,7 +22,8 @@ class WidgetPageExtension extends DataExtension {
'SideBar' => 'WidgetArea' 'SideBar' => 'WidgetArea'
); );
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')
@ -36,31 +37,32 @@ 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;
@ -69,9 +71,9 @@ class WidgetPageExtension extends DataExtension {
/** /**
* Support Translatable so that we don't link WidgetAreas across translations * Support Translatable so that we don't link WidgetAreas across translations
*/ */
public function onTranslatableCreate() { public function onTranslatableCreate()
{
//reset the sidebar ID //reset the sidebar ID
$this->owner->SideBarID = 0; $this->owner->SideBarID = 0;
} }
} }

View File

@ -5,14 +5,15 @@
* *
* @package widgets * @package widgets
*/ */
class WidgetAreaEditor extends FormField { class WidgetAreaEditor extends FormField
{
/** /**
* @param string $name * @param string $name
* @param array $widgetClasses * @param array $widgetClasses
* @param int $maxWidgets * @param int $maxWidgets
*/ */
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;
@ -24,7 +25,8 @@ class WidgetAreaEditor extends FormField {
* *
* @return string - HTML * @return string - HTML
*/ */
public function FieldHolder($properties = array()) { public function FieldHolder($properties = array())
{
Requirements::css('widgets/css/WidgetAreaEditor.css'); Requirements::css('widgets/css/WidgetAreaEditor.css');
Requirements::javascript('widgets/javascript/WidgetAreaEditor.js'); Requirements::javascript('widgets/javascript/WidgetAreaEditor.js');
@ -35,28 +37,27 @@ class WidgetAreaEditor extends FormField {
* *
* @return ArrayList * @return ArrayList
*/ */
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));
} }
} }
@ -68,7 +69,8 @@ class WidgetAreaEditor extends FormField {
/** /**
* @return HasManyList * @return HasManyList
*/ */
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');
@ -81,7 +83,8 @@ class WidgetAreaEditor extends FormField {
/** /**
* @return string * @return string
*/ */
public function IdxField() { public function IdxField()
{
return $this->id() . 'ID'; return $this->id() . 'ID';
} }
@ -89,7 +92,8 @@ class WidgetAreaEditor extends FormField {
* *
* @return int * @return int
*/ */
public function Value() { public function Value()
{
$relationName = $this->name; $relationName = $this->name;
return $this->form->getRecord()->getComponent($relationName)->ID; return $this->form->getRecord()->getComponent($relationName)->ID;
@ -98,7 +102,8 @@ class WidgetAreaEditor extends FormField {
/** /**
* @param DataObjectInterface $record * @param DataObjectInterface $record
*/ */
public function saveInto(DataObjectInterface $record) { public function saveInto(DataObjectInterface $record)
{
$name = $this->name; $name = $this->name;
$idName = $name . "ID"; $idName = $name . "ID";
@ -113,40 +118,42 @@ class WidgetAreaEditor extends FormField {
// 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 +163,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;
} }
@ -167,9 +174,9 @@ 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

@ -10,8 +10,8 @@
* *
* @package widgets * @package widgets
*/ */
class Widget extends DataObject { class Widget extends DataObject
{
/** /**
* @var array * @var array
*/ */
@ -77,7 +77,8 @@ class Widget extends DataObject {
*/ */
protected $controller; protected $controller;
public function populateDefaults() { public function populateDefaults()
{
parent::populateDefaults(); parent::populateDefaults();
$this->setField('Title', $this->getTitle()); $this->setField('Title', $this->getTitle());
} }
@ -87,7 +88,8 @@ class Widget extends DataObject {
* *
* @return string HTML * @return string HTML
*/ */
public function WidgetHolder() { public function WidgetHolder()
{
return $this->renderWith("WidgetHolder"); return $this->renderWith("WidgetHolder");
} }
@ -95,8 +97,9 @@ 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();
@ -113,7 +116,8 @@ class Widget extends DataObject {
* *
* @return string HTML * @return string HTML
*/ */
public function Content() { public function Content()
{
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class))); return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
} }
@ -121,7 +125,8 @@ class Widget extends DataObject {
* @return string * @return string
* @deprecated * @deprecated
*/ */
public function Title() { public function Title()
{
return $this->getTitle(); return $this->getTitle();
} }
@ -130,7 +135,8 @@ class Widget extends DataObject {
* *
* @return string * @return string
*/ */
public function getTitle() { public function getTitle()
{
return $this->getField('Title') return $this->getField('Title')
?: _t($this->class.'.TITLE', $this->config()->title); ?: _t($this->class.'.TITLE', $this->config()->title);
} }
@ -139,14 +145,16 @@ class Widget extends DataObject {
* @return string * @return string
* @deprecated * @deprecated
*/ */
public function CMSTitle() { public function CMSTitle()
{
return $this->getCMSTitle(); return $this->getCMSTitle();
} }
/** /**
* @return string * @return string
*/ */
public function getCMSTitle() { public function getCMSTitle()
{
return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle); return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle);
} }
@ -154,21 +162,24 @@ class Widget extends DataObject {
* @return string * @return string
* @deprecated * @deprecated
*/ */
public function Description() { public function Description()
{
return $this->getDescription(); return $this->getDescription();
} }
/** /**
* @return string * @return string
*/ */
public function getDescription() { public function getDescription()
{
return _t($this->class.'.DESCRIPTION', $this->config()->description); return _t($this->class.'.DESCRIPTION', $this->config()->description);
} }
/** /**
* @return string - HTML * @return string - HTML
*/ */
public function DescriptionSegment() { public function DescriptionSegment()
{
return $this->renderWith('WidgetDescription'); return $this->renderWith('WidgetDescription');
} }
@ -177,14 +188,16 @@ class Widget extends DataObject {
* *
* @return string - HTML * @return string - HTML
*/ */
public function EditableSegment() { public function EditableSegment()
{
return $this->renderWith('WidgetEditor'); return $this->renderWith('WidgetEditor');
} }
/** /**
* @return FieldList * @return FieldList
*/ */
public function getCMSFields() { public function getCMSFields()
{
$fields = new FieldList( $fields = new FieldList(
new TextField('Title', $this->fieldLabel('Title'), null, 255), new TextField('Title', $this->fieldLabel('Title'), null, 255),
new CheckboxField('Enabled', $this->fieldLabel('Enabled')) new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
@ -196,11 +209,12 @@ class Widget extends DataObject {
/** /**
* @return FieldList * @return FieldList
*/ */
public function CMSEditor() { public function CMSEditor()
{
$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) {
@ -217,14 +231,16 @@ class Widget extends DataObject {
/** /**
* @return string * @return string
*/ */
public function ClassName() { public function ClassName()
{
return $this->class; return $this->class;
} }
/** /**
* @return string * @return string
*/ */
public function Name() { public function Name()
{
return "Widget[".$this->ID."]"; return "Widget[".$this->ID."]";
} }
@ -233,26 +249,27 @@ 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");
} }
@ -264,23 +281,23 @@ class Widget extends DataObject {
/** /**
* @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);
} }
@ -293,4 +310,3 @@ class Widget extends DataObject {
$this->write(); $this->write();
} }
} }

View File

@ -5,8 +5,8 @@
* *
* @package widgets * @package widgets
*/ */
class WidgetArea extends DataObject { class WidgetArea extends DataObject
{
/** /**
* @var array * @var array
*/ */
@ -27,10 +27,11 @@ class WidgetArea extends DataObject {
* *
* @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();
@ -43,14 +44,16 @@ class WidgetArea extends DataObject {
/** /**
* @return HasManyList * @return HasManyList
*/ */
public function Items() { public function Items()
{
return $this->getComponents('Widgets'); return $this->getComponents('Widgets');
} }
/** /**
* @return HasManyList * @return HasManyList
*/ */
public function ItemsToRender() { public function ItemsToRender()
{
return $this->getComponents('Widgets') return $this->getComponents('Widgets')
->filter("Enabled", 1); ->filter("Enabled", 1);
} }
@ -58,7 +61,8 @@ 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);
} }
@ -66,18 +70,19 @@ class WidgetArea extends DataObject {
* *
* @param string $template * @param string $template
*/ */
public function setTemplate($template) { public function setTemplate($template)
{
$this->template = $template; $this->template = $template;
} }
/** /**
* Delete all connected Widgets when this WidgetArea gets deleted * Delete all connected Widgets when this WidgetArea gets deleted
*/ */
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

@ -3,7 +3,8 @@
* @package cms * @package cms
* @subpackage tests * @subpackage tests
*/ */
class WidgetAreaEditorTest extends SapphireTest { class WidgetAreaEditorTest extends SapphireTest
{
/** /**
* This is the widget you want to use for your unit tests. * This is the widget you want to use for your unit tests.
*/ */
@ -22,7 +23,8 @@ class WidgetAreaEditorTest extends SapphireTest {
) )
); );
function testFillingOneArea() { public function testFillingOneArea()
{
$data = array( $data = array(
'Widget' => array( 'Widget' => array(
'BottomBar' => array( 'BottomBar' => array(
@ -53,7 +55,8 @@ 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(
@ -96,7 +99,8 @@ class WidgetAreaEditorTest extends SapphireTest {
$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(
@ -162,7 +166,8 @@ class WidgetAreaEditorTest extends SapphireTest {
$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(
@ -222,7 +227,8 @@ class WidgetAreaEditorTest extends SapphireTest {
$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(
@ -294,7 +300,8 @@ class WidgetAreaEditorTest extends SapphireTest {
$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(
@ -366,7 +373,8 @@ class WidgetAreaEditorTest extends SapphireTest {
$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(
@ -434,13 +442,15 @@ class WidgetAreaEditorTest extends SapphireTest {
} }
} }
class WidgetAreaEditorTest_FakePage extends Page implements TestOnly { class WidgetAreaEditorTest_FakePage extends Page implements TestOnly
{
private static $has_one = array( private static $has_one = array(
"BottomBar" => "WidgetArea", "BottomBar" => "WidgetArea",
); );
} }
class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly { class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly
{
private static $cmsTitle = "Test widget"; private static $cmsTitle = "Test widget";
private static $title = "Test widget"; private static $title = "Test widget";
private static $description = "Test widget"; private static $description = "Test widget";

View File

@ -3,8 +3,8 @@
* @package widgets * @package widgets
* @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(
@ -12,7 +12,8 @@ class WidgetControllerTest extends FunctionalTest {
'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');
@ -28,7 +29,8 @@ class WidgetControllerTest extends FunctionalTest {
); );
} }
function testWidgetFormSubmission() { public function testWidgetFormSubmission()
{
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1'); $page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live'); $page->publish('Stage', 'Live');
@ -54,7 +56,8 @@ class WidgetControllerTest extends FunctionalTest {
* @package widgets * @package widgets
* @subpackage tests * @subpackage tests
*/ */
class WidgetControllerTest_Widget extends Widget implements TestOnly { class WidgetControllerTest_Widget extends Widget implements TestOnly
{
private static $db = array( private static $db = array(
'TestValue' => 'Text' 'TestValue' => 'Text'
); );
@ -64,13 +67,14 @@ 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',
@ -85,7 +89,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

@ -3,7 +3,8 @@
* @package cms * @package cms
* @subpackage tests * @subpackage tests
*/ */
class WidgetControllerTestPage extends Page implements TestOnly { class WidgetControllerTestPage extends Page implements TestOnly
{
private static $has_one = array( private static $has_one = array(
'WidgetControllerTestSidebar' => 'WidgetArea' 'WidgetControllerTestSidebar' => 'WidgetArea'
); );
@ -13,13 +14,14 @@ class WidgetControllerTestPage extends Page implements TestOnly {
* @package cms * @package cms
* @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);