silverstripe-widgets/tests/WidgetAreaEditorTest.php

494 lines
17 KiB
PHP
Raw Normal View History

2012-04-18 23:15:45 +02:00
<?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;
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
2015-11-18 05:08:21 +01:00
protected $extraDataObjects = array(
FakePage::class,
TestWidget::class,
2015-11-18 05:08:21 +01:00
);
2015-11-18 05:08:21 +01:00
protected $usesDatabase = true;
2015-11-18 05:08:21 +01:00
protected $requiredExtensions = array(
SiteTree::class => array(WidgetPageExtension::class)
2015-11-18 05:08:21 +01:00
);
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(
new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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(
new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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(
new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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(
new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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(
new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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(
new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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(
new ContentController(),
Form::class,
new FieldList($editorSide, $editorBott),
new FieldList()
);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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);
2015-11-18 05:08:21 +01:00
$form->setRequest($request);
$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
}