API Implement namespacing, add upgrade mapping. Remove deprecated methods and support for Widget_Controller (underscored)

This commit is contained in:
Robbie Averill 2017-01-18 11:43:11 +13:00
parent defb4bf134
commit 8eee397b9a
26 changed files with 574 additions and 375 deletions

16
.upgrade.yml Normal file
View File

@ -0,0 +1,16 @@
mappings:
WidgetContentControllerExtension: SilverStripe\Widgets\Controllers\WidgetContentControllerExtension
WidgetController: SilverStripe\Widgets\Controllers\WidgetController
Widget_Controller: SilverStripe\Widgets\Controllers\Widget_Controller
WidgetPageExtension: SilverStripe\Widgets\Extensions\WidgetPageExtension
WidgetAreaEditor: SilverStripe\Widgets\Forms\WidgetAreaEditor
Widget: SilverStripe\Widgets\Model\Widget
WidgetArea: SilverStripe\Widgets\Model\WidgetArea
WidgetAreaEditorTest: SilverStripe\Widgets\Tests\WidgetAreaEditorTest
WidgetAreaEditorTest_FakePage: SilverStripe\Widgets\Tests\WidgetAreaEditorTest\FakePage
WidgetAreaEditorTest_TestWidget: SilverStripe\Widgets\Tests\WidgetAreaEditorTest\TestWidget
WidgetControllerTest: SilverStripe\Widgets\Tests\WidgetControllerTest
WidgetControllerTest_Widget: SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget
WidgetControllerTest_WidgetController: SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidgetController
WidgetControllerTestPage: SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage
WidgetControllerTestPage_Controller: SilverStripe\Widgets\Tests\WidgetControllerTest\TestPageController

View File

@ -1,8 +1,8 @@
--- ---
Name: contentcontrollerurlhandler Name: contentcontrollerurlhandler
--- ---
ContentController: SilverStripe\CMS\Controllers\ContentController:
extensions: extensions:
- WidgetContentControllerExtension - SilverStripe\Widgets\Controllers\WidgetContentControllerExtension
url_handlers: url_handlers:
'widget/$ID!': 'handleWidget' 'widget/$ID!': 'handleWidget'

View File

@ -1,3 +1,3 @@
Director: SilverStripe\Control\Director:
rules: rules:
'WidgetController//$Action/$ID/$OtherID': 'WidgetController' 'WidgetController//$Action/$ID/$OtherID': 'SilverStripe\Widgets\Controllers\WidgetController'

View File

@ -1,71 +0,0 @@
<?php
/**
* Add this to ContentController to enable widgets
*
* @package widgets
*/
class WidgetContentControllerExtension extends Extension
{
/**
*
* @var array
*/
private static $allowed_actions = array(
'handleWidget'
);
/**
* Handles widgets attached to a page through one or more {@link WidgetArea}
* elements.
*
* Iterated through each $has_one relation with a {@link WidgetArea} and
* looks for connected widgets by their database identifier.
*
* Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
*
* @return RequestHandler
*/
public function handleWidget()
{$SQL_id = $this->owner->getRequest()->param('ID');
if(!$SQL_id) {return false;
}
/** @var SiteTree $widgetOwner */
$widgetOwner = $this->owner->data();
while($widgetOwner->InheritSideBar && $widgetOwner->Parent()->exists()){
$widgetOwner = $widgetOwner->Parent();
}
// find WidgetArea relations
$widgetAreaRelations = array();
$hasOnes = $widgetOwner->hasOne();
if(!$hasOnes) {
return false;
}
foreach ($hasOnes as $hasOneName => $hasOneClass) {
if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
$widgetAreaRelations[] = $hasOneName;
}
}
// find widget
$widget = null;
foreach ($widgetAreaRelations as $widgetAreaRelation) {
if ($widget) {
break;
}
$widget = $widgetOwner->$widgetAreaRelation()->Widgets()
->filter('ID', $SQL_id)
->First();
}
if (!$widget) {
user_error('No widget found', E_USER_ERROR);
}
return $widget->getController();
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace SilverStripe\Widgets\Controllers;
use SilverStripe\Core\Extension;
use SilverStripe\Widgets\Model\WidgetArea;
/**
* Add this to ContentController to enable widgets
*
* @package widgets
*/
class WidgetContentControllerExtension extends Extension
{
/**
*
* @var array
*/
private static $allowed_actions = array(
'handleWidget'
);
/**
* Handles widgets attached to a page through one or more {@link WidgetArea}
* elements.
*
* Iterated through each $has_one relation with a {@link WidgetArea} and
* looks for connected widgets by their database identifier.
*
* Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
*
* @return RequestHandler
*/
public function handleWidget()
{
$SQL_id = $this->owner->getRequest()->param('ID');
if (!$SQL_id) {
return false;
}
/** @var SiteTree $widgetOwner */
$widgetOwner = $this->owner->data();
while ($widgetOwner->InheritSideBar && $widgetOwner->Parent()->exists()) {
$widgetOwner = $widgetOwner->Parent();
}
// find WidgetArea relations
$widgetAreaRelations = array();
$hasOnes = $widgetOwner->hasOne();
if (!$hasOnes) {
return false;
}
foreach ($hasOnes as $hasOneName => $hasOneClass) {
if ($hasOneClass == WidgetArea::class || is_subclass_of($hasOneClass, WidgetArea::class)) {
$widgetAreaRelations[] = $hasOneName;
}
}
// find widget
$widget = null;
foreach ($widgetAreaRelations as $widgetAreaRelation) {
if ($widget) {
break;
}
$widget = $widgetOwner->$widgetAreaRelation()->Widgets()
->filter('ID', $SQL_id)
->First();
}
if (!$widget) {
user_error('No widget found', E_USER_ERROR);
}
return $widget->getController();
}
}

View File

@ -1,5 +1,15 @@
<?php <?php
namespace SilverStripe\Widgets\Controllers;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\ClassInfo;
use SilverStripe\i18n\i18n;
use SilverStripe\Security\Member;
use SilverStripe\Widgets\Model\Widget;
/** /**
* 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.
* *
@ -52,13 +62,15 @@ 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);
$page = Director::get_current_page(); $page = Director::get_current_page();
if($page && !($page instanceof WidgetController)) {return $page->Link($segment); if ($page && !($page instanceof WidgetController)) {
} return $page->Link($segment);
}
if ($controller = $this->getParentController()) {return $controller->Link($segment); if ($controller = $this->getParentController()) {
return $controller->Link($segment);
} }
return $segment; return $segment;
@ -73,19 +85,50 @@ class WidgetController extends Controller
*/ */
public function getParentController() public function getParentController()
{ {
foreach(Controller::$controller_stack as $controller) { foreach (Controller::$controller_stack as $controller) {
if (!($controller instanceof WidgetController)) { if (!($controller instanceof WidgetController)) {
return $controller; return $controller;
} }
} }
return false; return false;
} }
/**
* @return Widget
*/
public function getWidget()
{
return $this->widget;
}
/**
* Overloaded from {@link Widget->Content()} to allow for controller / form
* linking.
*
* @return string HTML
*/
public function Content()
{
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
}
/**
* Overloaded from {@link Widget->WidgetHolder()} to allow for controller/
* form linking.
*
* @return string HTML
*/
public function WidgetHolder()
{
return $this->renderWith("WidgetHolder");
}
/** /**
* @return Widget * @return Widget
*/ */
public function getWidget() public function getWidget()
{return $this->widget; {
return $this->widget;
} }
/** /**
@ -95,7 +138,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)));
} }
/** /**
@ -105,7 +149,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");
} }
/** /**
@ -116,30 +161,24 @@ class WidgetController extends Controller
* *
* @return string HTML * @return string HTML
*/ */
public function editablesegment() { public function editablesegment()
{
// use left and main to set the html config // use left and main to set the html config
$leftandmain = LeftAndMain::create(); $leftandmain = LeftAndMain::create();
$leftandmain->init(); $leftandmain->doInit();
$className = $this->urlParams['ID'];
if (class_exists('Translatable') && Member::currentUserID()) {
// set current locale based on logged in user's locale
$locale = Member::currentUser()->Locale;
i18n::set_locale($locale);
}
if(class_exists($className) && is_subclass_of($className, 'Widget')) {
$obj = new $className();
return $obj->EditableSegment();
} else {
user_error("Bad widget class: $className", E_USER_WARNING);
return "Bad widget class name given";
}
}
}
/** $className = $this->urlParams['ID'];
* @deprecated Use WidgetController if (class_exists('Translatable') && Member::currentUserID()) {
* @package widgets // set current locale based on logged in user's locale
*/ $locale = Member::currentUser()->Locale;
class Widget_Controller extends WidgetController i18n::set_locale($locale);
{ }
if (class_exists($className) && is_subclass_of($className, Widget::class)) {
$obj = new $className();
return $obj->EditableSegment();
} else {
user_error("Bad widget class: $className", E_USER_WARNING);
return "Bad widget class name given";
}
}
} }

View File

@ -1,4 +1,13 @@
<?php <?php
namespace SilverStripe\Widgets\Extensions;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
use SilverStripe\Widgets\Model\WidgetArea;
/** /**
* Adds a single {@link WidgetArea} called "SideBar" to {@link Page} classes. * Adds a single {@link WidgetArea} called "SideBar" to {@link Page} classes.
* Adjust your templates to render the resulting * Adjust your templates to render the resulting
@ -19,7 +28,7 @@ class WidgetPageExtension extends DataExtension
); );
private static $has_one = array( private static $has_one = array(
'SideBar' => 'WidgetArea' 'SideBar' => WidgetArea::class
); );
public function updateCMSFields(FieldList $fields) public function updateCMSFields(FieldList $fields)
@ -39,8 +48,7 @@ class WidgetPageExtension extends DataExtension
*/ */
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')
) { ) {
@ -49,7 +57,7 @@ class WidgetPageExtension extends DataExtension
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')) {

View File

@ -1,5 +1,18 @@
<?php <?php
namespace SilverStripe\Widgets\Forms;
use Exception;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FormField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
use SilverStripe\Widgets\Model\Widget;
use SilverStripe\View\Requirements;
/** /**
* Special field type for selecting and configuring widgets on a page. * Special field type for selecting and configuring widgets on a page.
* *
@ -12,11 +25,11 @@ class WidgetAreaEditor extends FormField
* @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::class), $maxWidgets = 0)
{ {
$this->MaxWidgets = $maxWidgets; $this->MaxWidgets = $maxWidgets;
$this->widgetClasses = $widgetClasses; $this->widgetClasses = $widgetClasses;
parent::__construct($name); parent::__construct($name);
} }
@ -30,7 +43,7 @@ class WidgetAreaEditor extends FormField
Requirements::css('widgets/css/WidgetAreaEditor.css'); Requirements::css('widgets/css/WidgetAreaEditor.css');
Requirements::javascript('widgets/javascript/WidgetAreaEditor.js'); Requirements::javascript('widgets/javascript/WidgetAreaEditor.js');
return $this->renderWith("WidgetAreaEditor"); return $this->renderWith(WidgetAreaEditor::class);
} }
/** /**
@ -44,15 +57,15 @@ class WidgetAreaEditor extends FormField
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::class])) {
unset($classes['Widget']); unset($classes[Widget::class]);
} elseif (isset($classes[0]) && $classes[0] == 'Widget') { } elseif (isset($classes[0]) && $classes[0] == Widget::class) {
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));
@ -62,7 +75,7 @@ class WidgetAreaEditor extends FormField
} }
} }
} }
return $widgets; return $widgets;
} }
@ -72,8 +85,8 @@ 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::class);
$relationName = $this->name; $relationName = $this->name;
$widgets = $this->form->getRecord()->getComponent($relationName)->Items(); $widgets = $this->form->getRecord()->getComponent($relationName)->Items();
@ -101,6 +114,7 @@ class WidgetAreaEditor extends FormField
/** /**
* @param DataObjectInterface $record * @param DataObjectInterface $record
* @throws Exception if no form could be retrieved
*/ */
public function saveInto(DataObjectInterface $record) public function saveInto(DataObjectInterface $record)
{ {
@ -109,15 +123,15 @@ 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;
@ -128,12 +142,11 @@ class WidgetAreaEditor extends FormField
throw new Exception("no form"); throw new Exception("no form");
} }
$widgetData = $this->getForm()->getRequest()->requestVar('Widget'); $widgetData = $this->getForm()->getRequest()->requestVar(Widget::class);
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;
@ -156,7 +169,7 @@ class WidgetAreaEditor extends FormField
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::class)
) { ) {
$widget = Injector::inst()->create($newWidgetData['Type']); $widget = Injector::inst()->create($newWidgetData['Type']);
$widget->ID = 0; $widget->ID = 0;
@ -172,7 +185,7 @@ 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) {

View File

@ -1,12 +1,24 @@
<?php <?php
namespace SilverStripe\Widgets\Model;
use Exception;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataObject;
use SilverStripe\Widgets\Model\WidgetArea;
/** /**
* 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 WidgetController} for more information.
* *
* @package widgets * @package widgets
*/ */
@ -42,7 +54,7 @@ class Widget extends DataObject
* @var array * @var array
*/ */
private static $has_one = array( private static $has_one = array(
"Parent" => "WidgetArea", "Parent" => WidgetArea::class,
); );
/** /**
@ -72,6 +84,11 @@ class Widget extends DataObject
'CMSTitle' => 'Title' 'CMSTitle' => 'Title'
); );
/**
* @var string
*/
private static $table_name = 'Widget';
/** /**
* @var WidgetController * @var WidgetController
*/ */
@ -122,15 +139,6 @@ class Widget extends DataObject
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class))); return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
} }
/**
* @return string
* @deprecated
*/
public function Title()
{
return $this->getTitle();
}
/** /**
* Get the frontend title for this widget * Get the frontend title for this widget
* *
@ -139,16 +147,7 @@ class Widget extends DataObject
public function getTitle() public function getTitle()
{ {
return $this->getField('Title') return $this->getField('Title')
?: _t($this->class . '.TITLE', $this->config()->title); ?: _t($this->ClassName() . '.TITLE', $this->config()->title);
}
/**
* @return string
* @deprecated
*/
public function CMSTitle()
{
return $this->getCMSTitle();
} }
/** /**
@ -156,16 +155,7 @@ class Widget extends DataObject
*/ */
public function getCMSTitle() public function getCMSTitle()
{ {
return _t($this->class . '.CMSTITLE', $this->config()->cmsTitle); return _t($this->ClassName() . '.CMSTITLE', $this->config()->cmsTitle);
}
/**
* @return string
* @deprecated
*/
public function Description()
{
return $this->getDescription();
} }
/** /**
@ -173,7 +163,7 @@ class Widget extends DataObject
*/ */
public function getDescription() public function getDescription()
{ {
return _t($this->class . '.DESCRIPTION', $this->config()->description); return _t($this->ClassName() . '.DESCRIPTION', $this->config()->description);
} }
/** /**
@ -217,8 +207,13 @@ class Widget extends DataObject
$outputFields = new FieldList(); $outputFields = new FieldList();
$this->FormID = $this->ID ?: uniqid(); $this->FormID = $this->ID ?: uniqid();
$outputFields->push(HiddenField::create('Widget[' . $this->FormID . '][FormID]', 'FormID', $outputFields->push(
$this->FormID)->addExtraClass('formid')); HiddenField::create(
'Widget[' . $this->FormID . '][FormID]',
'FormID',
$this->FormID
)->addExtraClass('formid')
);
foreach ($fields as $field) { foreach ($fields as $field) {
$name = $field->getName(); $name = $field->getName();
@ -263,14 +258,7 @@ class Widget extends DataObject
} }
foreach (array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) { foreach (array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
$controllerClass = "{$widgetClass}_Controller";
if (class_exists($controllerClass)) {
break;
}
$controllerClass = "{$widgetClass}Controller"; $controllerClass = "{$widgetClass}Controller";
if (class_exists($controllerClass)) { if (class_exists($controllerClass)) {
break; break;
} }

View File

@ -1,5 +1,11 @@
<?php <?php
namespace SilverStripe\Widgets\Model;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Widgets\Model\Widget;
/** /**
* Represents a set of widgets shown on a page. * Represents a set of widgets shown on a page.
* *
@ -11,31 +17,36 @@ class WidgetArea extends DataObject
* @var array * @var array
*/ */
private static $has_many = array( private static $has_many = array(
"Widgets" => "Widget" "Widgets" => Widget::class
); );
/**
* @var string
*/
private static $table_name = 'WidgetArea';
/** /**
* *
* @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();
$items = $this->ItemsToRender(); $items = $this->ItemsToRender();
if (!is_null($items)){ if (!is_null($items)) {
foreach ($items as $widget) { foreach ($items as $widget) {
$controller = $widget->getController(); $controller = $widget->getController();
$controller->init(); $controller->doInit();
$controllers->push($controller); $controllers->push($controller);
} }
} }

View File

@ -1,4 +1,21 @@
<?php <?php
namespace SilverStripe\Widgets\Tests;
use Page;
use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Widgets\Extensions\WidgetPageExtension;
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
use SilverStripe\Widgets\Model\Widget;
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\FakePage;
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\TestWidget;
/** /**
* @package cms * @package cms
* @subpackage tests * @subpackage tests
@ -8,25 +25,23 @@ 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.
*/ */
protected $widgetToTest = 'WidgetAreaEditorTest_TestWidget'; protected $widgetToTest = TestWidget::class;
protected $extraDataObjects = array( protected $extraDataObjects = array(
'WidgetAreaEditorTest_FakePage', FakePage::class,
'WidgetAreaEditorTest_TestWidget', TestWidget::class,
); );
protected $usesDatabase = true; protected $usesDatabase = true;
protected $requiredExtensions = array( protected $requiredExtensions = array(
"SiteTree" => array( SiteTree::class => array(WidgetPageExtension::class)
"WidgetPageExtension"
)
); );
public function testFillingOneArea() public function testFillingOneArea()
{ {
$data = array( $data = array(
'Widget' => array( Widget::class => array(
'BottomBar' => array( 'BottomBar' => array(
'new-1' => array( 'new-1' => array(
'Title' => 'MyTestWidget', 'Title' => 'MyTestWidget',
@ -36,14 +51,19 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
@ -58,7 +78,7 @@ class WidgetAreaEditorTest extends SapphireTest
public function testFillingTwoAreas() public function testFillingTwoAreas()
{ {
$data = array( $data = array(
'Widget' => array( Widget::class => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
'Title' => 'MyTestWidgetSide', 'Title' => 'MyTestWidgetSide',
@ -75,35 +95,40 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new FakePage();
$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();
// 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]->getTitle(), 'MyTestWidgetSide');
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom'); $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
} }
public function testDeletingOneWidgetFromOneArea() public function testDeletingOneWidgetFromOneArea()
{ {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( Widget::class => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
'Title' => 'MyTestWidgetSide', 'Title' => 'MyTestWidgetSide',
@ -120,13 +145,18 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
@ -135,10 +165,10 @@ 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::class => array(
'SideBar' => array( 'SideBar' => array(
), ),
'BottomBar' => array( 'BottomBar' => array(
@ -150,7 +180,7 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
@ -160,9 +190,9 @@ 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]->getTitle(), 'MyTestWidgetBottom');
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0); $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
} }
@ -170,7 +200,7 @@ class WidgetAreaEditorTest extends SapphireTest
{ {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( Widget::class => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
'Title' => 'MyTestWidgetSide', 'Title' => 'MyTestWidgetSide',
@ -187,13 +217,18 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
@ -202,36 +237,36 @@ 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::class => array(
'SideBar' => array( 'SideBar' => array(
), ),
'BottomBar' => array( 'BottomBar' => array(
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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);
} }
public function testEditingOneWidget() public function testEditingOneWidget()
{ {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( Widget::class => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
'Title' => 'MyTestWidgetSide', 'Title' => 'MyTestWidgetSide',
@ -248,13 +283,18 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
@ -263,10 +303,10 @@ 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::class => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited', 'Title' => 'MyTestWidgetSide-edited',
@ -283,7 +323,7 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
@ -293,18 +333,18 @@ 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]->getTitle(), 'MyTestWidgetBottom');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
} }
public function testEditingAWidgetFromEachArea() public function testEditingAWidgetFromEachArea()
{ {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( Widget::class => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
'Title' => 'MyTestWidgetSide', 'Title' => 'MyTestWidgetSide',
@ -321,13 +361,18 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new FakePage();
$form->saveInto($page); $form->saveInto($page);
$page->write(); $page->write();
@ -336,10 +381,10 @@ 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::class => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited', 'Title' => 'MyTestWidgetSide-edited',
@ -356,7 +401,7 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
@ -366,18 +411,18 @@ 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]->getTitle(), 'MyTestWidgetBottom-edited');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited'); $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
} }
public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea()
{ {
// First get some widgets in there // First get some widgets in there
$data = array( $data = array(
'Widget' => array( Widget::class => array(
'SideBar' => array( 'SideBar' => array(
'new-1' => array( 'new-1' => array(
'Title' => 'MyTestWidgetSide', 'Title' => 'MyTestWidgetSide',
@ -394,13 +439,18 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new 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::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$form->setRequest($request); $form->setRequest($request);
$page = new WidgetAreaEditorTest_FakePage(); $page = new FakePage();
$editorSide->saveInto($page); $editorSide->saveInto($page);
$editorBott->saveInto($page); $editorBott->saveInto($page);
@ -410,10 +460,10 @@ 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::class => array(
'SideBar' => array( 'SideBar' => array(
$sideWidgets[0]->ID => array( $sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited', 'Title' => 'MyTestWidgetSide-edited',
@ -425,7 +475,7 @@ class WidgetAreaEditorTest extends SapphireTest
) )
) )
); );
$request = new SS_HTTPRequest('get', 'post', array(), $data); $request = new HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request); $form->setRequest($request);
$form->saveInto($page); $form->saveInto($page);
@ -435,23 +485,9 @@ 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]->getTitle(), 'MyTestWidgetSide-edited');
} }
} }
class WidgetAreaEditorTest_FakePage extends Page implements TestOnly
{
private static $has_one = array(
"BottomBar" => "WidgetArea",
);
}
class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly
{
private static $cmsTitle = "Test widget";
private static $title = "Test widget";
private static $description = "Test widget";
}

View File

@ -0,0 +1,16 @@
<?php
namespace SilverStripe\Widgets\Tests\WidgetAreaEditorTest;
use Page;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Widgets\Model\WidgetArea;
class FakePage extends Page implements TestOnly
{
private static $table_name = 'FakePage';
private static $has_one = array(
"BottomBar" => WidgetArea::class
);
}

View File

@ -0,0 +1,14 @@
<?php
namespace SilverStripe\Widgets\Tests\WidgetAreaEditorTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Widgets\Model\Widget;
class TestWidget extends Widget implements TestOnly
{
private static $table_name = 'TestWidget';
private static $cmsTitle = "Test widget";
private static $title = "Test widget";
private static $description = "Test widget";
}

View File

@ -1,4 +1,18 @@
<?php <?php
namespace SilverStripe\Widgets\Tests;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Widgets\Model\Widget;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\Form;
use SilverStripe\Widgets\Controllers\WidgetController;
use SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage;
use SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget;
/** /**
* @package widgets * @package widgets
* @subpackage tests * @subpackage tests
@ -8,19 +22,19 @@ class WidgetControllerTest extends FunctionalTest
protected static $fixture_file = 'WidgetControllerTest.yml'; protected static $fixture_file = 'WidgetControllerTest.yml';
protected $extraDataObjects = array( protected $extraDataObjects = array(
'WidgetControllerTestPage', TestPage::class,
'WidgetControllerTest_Widget', TestWidget::class,
); );
public function testWidgetFormRendering() public function testWidgetFormRendering()
{ {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1'); $page = $this->objFromFixture(TestPage::class, 'page1');
$page->publish('Stage', 'Live'); $page->copyVersionToStage('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1'); $widget = $this->objFromFixture(TestWidget::class, '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,
@ -28,16 +42,16 @@ class WidgetControllerTest extends FunctionalTest
"Widget forms are rendered through WidgetArea templates" "Widget forms are rendered through WidgetArea templates"
); );
} }
public function testWidgetFormSubmission() public function testWidgetFormSubmission()
{ {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1'); $page = $this->objFromFixture(TestPage::class, 'page1');
$page->publish('Stage', 'Live'); $page->copyVersionToStage('Stage', 'Live');
$widget = $this->objFromFixture('WidgetControllerTest_Widget', 'widget1'); $widget = $this->objFromFixture(TestWidget::class, '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',
@ -51,49 +65,3 @@ class WidgetControllerTest extends FunctionalTest
); );
} }
} }
/**
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest_Widget extends Widget implements TestOnly
{
private static $db = array(
'TestValue' => 'Text'
);
}
/**
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest_WidgetController extends WidgetController implements TestOnly
{
private static $allowed_actions = array(
'Form'
);
public function Form()
{
$widgetform = new Form(
$this,
'Form',
new FieldList(
new TextField('TestValue')
),
new FieldList(
new FormAction('doAction')
)
);
return $widgetform;
}
public function doAction($data, $form)
{
return sprintf('TestValue: %s\nWidget ID: %d',
$data['TestValue'],
$this->widget->ID
);
}
}

View File

@ -1,10 +1,10 @@
WidgetControllerTest_Widget: SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget:
widget1: widget1:
Title: Widget 1 Title: Widget 1
WidgetArea: SilverStripe\Widgets\Model\WidgetArea:
area1: area1:
Widgets: =>WidgetControllerTest_Widget.widget1 Widgets: =>SilverStripe\Widgets\Tests\WidgetControllerTest\TestWidget.widget1
WidgetControllerTestPage: SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage:
page1: page1:
Title: Page1 Title: Page1
WidgetControllerTestSidebar: =>WidgetArea.area1 WidgetControllerTestSidebar: =>SilverStripe\Widgets\Model\WidgetArea.area1

View File

@ -0,0 +1,21 @@
<?php
namespace SilverStripe\Widgets\Tests\WidgetControllerTest;
use Page;
use SilverStripe\Dev\TestOnly;
use SilverStripe\View\SSViewer;
use SilverStripe\Widgets\Model\WidgetArea;
/**
* @package cms
* @subpackage tests
*/
class TestPage extends Page implements TestOnly
{
private static $table_name = 'TestPage';
private static $has_one = array(
'WidgetControllerTestSidebar' => WidgetArea::class
);
}

View File

@ -0,0 +1,25 @@
<?php
namespace SilverStripe\Widgets\Tests\WidgetControllerTest;
use PageController;
use ReflectionClass;
use SilverStripe\Dev\TestOnly;
use SilverStripe\View\SSViewer;
use SilverStripe\Widgets\Tests\WidgetControllerTest\TestPage;
/**
* @package cms
* @subpackage tests
*/
class TestPageController extends PageController implements TestOnly
{
/**
* Template selection doesnt work in test folders, so we add a test theme a template name.
*/
public function getViewer($action)
{
SSViewer::add_themes(["silverstripe/widgets:widgets/tests/WidgetControllerTest"]);
return new SSViewer(TestPage::class);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace SilverStripe\Widgets\Tests\WidgetControllerTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Widgets\Model\Widget;
/**
* @package widgets
* @subpackage tests
*/
class TestWidget extends Widget implements TestOnly
{
private static $table_name = 'TestWidgetB';
private static $db = array(
'TestValue' => 'Text'
);
}

View File

@ -0,0 +1,46 @@
<?php
namespace SilverStripe\Widgets\Tests\WidgetControllerTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\Widgets\Controllers\WidgetController;
/**
* @package widgets
* @subpackage tests
*/
class TestWidgetController extends WidgetController implements TestOnly
{
private static $allowed_actions = array(
'Form'
);
public function Form()
{
$widgetform = new Form(
$this,
'Form',
new FieldList(
new TextField('TestValue')
),
new FieldList(
new FormAction('doAction')
)
);
return $widgetform;
}
public function doAction($data, $form)
{
return sprintf(
'TestValue: %s\nWidget ID: %d',
$data['TestValue'],
$this->widget->ID
);
}
}

View File

@ -0,0 +1 @@
$WidgetControllerTestSidebar

View File

@ -1,29 +0,0 @@
<?php
/**
* @package cms
* @subpackage tests
*/
class WidgetControllerTestPage extends Page implements TestOnly
{
private static $has_one = array(
'WidgetControllerTestSidebar' => 'WidgetArea'
);
}
/**
* @package cms
* @subpackage tests
*/
class WidgetControllerTestPage_Controller extends Page_Controller implements TestOnly
{
/**
* Template selection doesnt work in test folders,
* so we enforce a template name.
*/
public function getViewer($action)
{
$templates = array('WidgetControllerTestPage');
return new SSViewer($templates);
}
}

View File

@ -1 +0,0 @@
$WidgetControllerTestSidebar

View File

@ -1 +0,0 @@
$Form