silverstripe-widgets/tests/WidgetAreaEditorTest.php

454 lines
13 KiB
PHP
Raw Normal View History

2012-04-18 23:15:45 +02:00
<?php
/**
* @package cms
* @subpackage tests
*/
class WidgetAreaEditorTest extends SapphireTest {
/**
* This is the widget you want to use for your unit tests.
*/
protected $widgetToTest = 'WidgetAreaEditorTest_TestWidget';
protected $extraDataObjects = array(
'WidgetAreaEditorTest_FakePage',
'WidgetAreaEditorTest_TestWidget',
);
protected $usesDatabase = true;
function testFillingOneArea() {
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'BottomBar' => array(
'new-1' => array(
'Title' => 'MyTestWidget',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
2012-04-18 23:15:45 +02:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
2012-04-18 23:15:45 +02:00
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testFillingTwoAreas() {
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
2012-04-18 23:15:45 +02:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
2012-04-18 23:15:45 +02:00
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
// Make sure they both got saved
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide');
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
}
function testDeletingOneWidgetFromOneArea() {
// First get some widgets in there
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
2012-04-18 23:15:45 +02:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
2012-04-18 23:15:45 +02:00
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'SideBar' => array(
),
'BottomBar' => array(
$bottWidgets[0]->ID => array(
'Title' => 'MyTestWidgetBottom',
'Type' => $this->widgetToTest,
'Sort' => 0
)
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testDeletingAWidgetFromEachArea() {
// First get some widgets in there
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
2012-04-18 23:15:45 +02:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
2012-04-18 23:15:45 +02:00
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'SideBar' => array(
),
'BottomBar' => array(
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testEditingOneWidget() {
// First get some widgets in there
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
2012-04-18 23:15:45 +02:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
2012-04-18 23:15:45 +02:00
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
function testEditingAWidgetFromEachArea() {
// First get some widgets in there
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
2012-04-18 23:15:45 +02:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
2012-04-18 23:15:45 +02:00
$page = new WidgetAreaEditorTest_FakePage();
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
// Save again (after removing the SideBar's widget)
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom-edited');
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() {
// First get some widgets in there
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'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 SS_HTTPRequest('get', 'post', array(), $data);
2012-04-18 23:15:45 +02:00
$editorSide = new WidgetAreaEditor('SideBar');
$editorBott = new WidgetAreaEditor('BottomBar');
$form = new Form(new ContentController(), 'Form', new FieldList($editorSide, $editorBott), new FieldList());
$form->setRequest($request);
2012-04-18 23:15:45 +02:00
$page = new WidgetAreaEditorTest_FakePage();
$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();
// Save again (after removing the SideBar's widget)
$data = array(
2012-04-18 23:15:45 +02:00
'Widget' => array(
'SideBar' => array(
$sideWidgets[0]->ID => array(
'Title' => 'MyTestWidgetSide-edited',
'Type' => $this->widgetToTest,
'Sort' => 0
)
),
'BottomBar' => array(
)
)
);
$request = new SS_HTTPRequest('get', 'post', array(), $data);
$form->setRequest($request);
$form->saveInto($page);
2012-04-18 23:15:45 +02:00
$page->write();
$page->flushCache();
$page->BottomBar()->flushCache();
$page->SideBar()->flushCache();
$sideWidgets = $page->SideBar()->Widgets()->toArray();
$bottWidgets = $page->BottomBar()->Widgets()->toArray();
$this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
$this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
}
class WidgetAreaEditorTest_FakePage extends Page implements TestOnly {
2013-04-01 21:37:44 +02:00
private static $has_one = array(
2012-04-18 23:15:45 +02:00
"SideBar" => "WidgetArea",
"BottomBar" => "WidgetArea",
);
}
class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly {
2013-04-01 21:37:44 +02:00
private static $cmsTitle = "Test widget";
private static $title = "Test widget";
private static $description = "Test widget";
private static $db = array(
2012-04-18 23:15:45 +02:00
'Title' => 'Varchar'
);
public function getCMSFields() {
$fields = new FieldList();
$fields->push(new TextField('Title'));
return $fields;
}
function Title() {
return $this->Title ? $this->Title : self::$title;
}
}