silverstripe-widgets/tests/WidgetAreaEditorTest.php

504 lines
17 KiB
PHP
Raw Normal View History

2012-04-18 23:15:45 +02:00
<?php
namespace SilverStripe\Widgets\Tests;
use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
2017-12-18 04:30:49 +01:00
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Widgets\Extensions\WidgetPageExtension;
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\FakePage;
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\TestWidget;
2012-04-18 23:15:45 +02:00
/**
* @package cms
* @subpackage tests
*/
2015-11-18 05:08:21 +01:00
class WidgetAreaEditorTest extends SapphireTest
{
/**
* This is the widget you want to use for your unit tests.
*/
protected $widgetToTest = TestWidget::class;
2012-04-18 23:15:45 +02:00
protected static $extra_dataobjects = [
FakePage::class,
TestWidget::class,
];
2015-11-18 05:08:21 +01:00
protected $usesDatabase = true;
protected static $required_extensions = [
SiteTree::class => [WidgetPageExtension::class]
];
2015-11-18 05:08:21 +01:00
public function testFillingOneArea()
{
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidget',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
2015-11-18 05:08:21 +01:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(
$controller = new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$controller->setRequest($request);
$controller->setRequest($request);
$form->setController($controller);
$page = new FakePage();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
public function testFillingTwoAreas()
{
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
2015-11-18 05:08:21 +01:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(
$controller = new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$controller->setRequest($request);
$form->setController($controller);
$page = new FakePage();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
2015-11-18 05:08:21 +01:00
// Make sure they both got saved
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
2015-11-18 05:08:21 +01:00
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide');
$this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
2015-11-18 05:08:21 +01:00
}
2015-11-18 05:08:21 +01:00
public function testDeletingOneWidgetFromOneArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
2015-11-18 05:08:21 +01:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(
$controller = new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$controller->setRequest($request);
$form->setController($controller);
$page = new FakePage();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
$controller->setRequest($request);
$form->setController($controller);
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
2015-11-18 05:08:21 +01:00
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
public function testDeletingAWidgetFromEachArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
2015-11-18 05:08:21 +01:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(
$controller = new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$controller->setRequest($request);
$form->setController($controller);
$page = new FakePage();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
),
'BottomBar' => array(
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
$controller->setRequest($request);
$form->setController($controller);
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
2015-11-18 05:08:21 +01:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
2015-11-18 05:08:21 +01:00
public function testEditingOneWidget()
{
// First get some widgets in there
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
2015-11-18 05:08:21 +01:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(
$controller = new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$controller->setRequest($request);
$form->setController($controller);
$page = new FakePage();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
$controller->setRequest($request);
$form->setController($controller);
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
$this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
2015-11-18 05:08:21 +01:00
}
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
public function testEditingAWidgetFromEachArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
2015-11-18 05:08:21 +01:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(
$controller = new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$controller->setRequest($request);
$form->setController($controller);
$page = new FakePage();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
$controller->setRequest($request);
$form->setController($controller);
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom-edited');
$this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
2015-11-18 05:08:21 +01:00
}
2015-11-18 05:08:21 +01:00
public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea()
{
// First get some widgets in there
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetSide',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
2015-11-18 05:08:21 +01:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(
$controller = new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
$controller->setRequest($request);
$form->setController($controller);
$page = new FakePage();
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$editorSide->saveInto($page);
$editorBott->saveInto($page);
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
// Save again (after removing the SideBar's widget)
$data = array(
'Widget' => array(
2015-11-18 05:08:21 +01:00
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
)
)
);
$request = new HTTPRequest('get', 'post', array(), $data);
$controller->setRequest($request);
$form->setController($controller);
2015-11-18 05:08:21 +01:00
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
2015-11-18 05:08:21 +01:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
2015-11-18 05:08:21 +01:00
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
2015-11-18 05:08:21 +01:00
}
2012-04-18 23:15:45 +02:00
}