Source of file CmsActionsTest.php
Size: 6,549 Bytes - Last Modified: 2021-12-23T10:00:19+00:00
/var/www/docs.ssmods.com/process/src/tests/CmsActionsTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 | <?php namespace LeKoala\CmsActions\Test; use SilverStripe\Forms\Form; use SilverStripe\Security\Member; use LeKoala\CmsActions\CustomLink; use SilverStripe\Dev\SapphireTest; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Config; use SilverStripe\Versioned\Versioned; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\GridField\GridField; use LeKoala\CmsActions\ActionsGridFieldItemRequest; use SilverStripe\Forms\GridField\GridFieldDetailForm; use SilverStripe\Versioned\VersionedGridFieldItemRequest; use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest; /** * Tests for Cms Actions module */ class CmsActionsTest extends SapphireTest { /** * Defines the fixture file to use for this test class * @var string */ protected static $fixture_file = 'CmsActionsTest.yml'; protected static $extra_dataobjects = array( Test_CmsActionsModel::class, Test_ActionsPage::class, ); public function setUp() { parent::setUp(); $controller = Controller::curr(); $controller->config()->set('url_segment', 'test_controller'); } public function tearDown() { parent::tearDown(); } /** * @return Test_ActionsPage */ public function getTestPage() { return $this->objFromFixture(Test_ActionsPage::class, 'demo'); } /** * @return Test_CmsActionsModel */ public function getTestModel() { return $this->objFromFixture(Test_CmsActionsModel::class, 'demo'); } /** * @return Member */ public function getAdminMember() { return $this->objFromFixture(Member::class, 'admin'); } /** * @return Form */ public function getMemberForm() { $controller = Controller::curr(); $form = new Form($controller); $record = $this->getAdminMember(); $list = Member::get(); $gridField = new GridField('testGridfield', null, $list); $detailForm = new GridFieldDetailForm('testDetailForm'); $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); $form = $GridFieldDetailForm->ItemEditForm(); $form->loadDataFrom($record); return $form; } /** * @param Controller $controller * @param DataObject $record * @return Form */ public function getTestForm($controller = null, $record = null) { if (!$controller) { $controller = Controller::curr(); } if (!$record) { $record = $this->getTestModel(); } $list = Test_CmsActionsModel::get(); $gridField = new GridField('testGridfield', null, $list); $detailForm = new GridFieldDetailForm('testDetailForm'); if ($record->hasExtension(Versioned::class)) { $GridFieldDetailForm = new VersionedGridFieldItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); } else { $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); } $form = $GridFieldDetailForm->ItemEditForm(); $form->loadDataFrom($record); return $form; } public function testCustomDeleteTitle() { $form = $this->getTestForm(); $record = $form->getRecord(); $delete = $form->Actions()->fieldByName("action_doDelete"); $this->assertEquals($delete->Title(), $record->getDeleteButtonTitle()); } public function testHasSaveAndClose() { $form = $this->getTestForm(); $doSaveAndClose = $form->Actions()->fieldByName("action_doSaveAndClose"); // It can be nested in MajorActions, then we need to use dot notation if (!$doSaveAndClose) { $doSaveAndClose = $form->Actions()->fieldByName("MajorActions.action_doSaveAndClose"); } $this->assertNotEmpty($doSaveAndClose); } public function testHasDefaultTitle() { $customLink = new CustomLink('doTest'); $this->assertEquals('Do test', $customLink->getTitle()); } public function testConfirmationMessage() { $customLink = new CustomLink('doTest'); $customLink->setConfirmation(true); $this->assertContains('sure', $customLink->getConfirmation()); } public function testGridFieldAction() { $form = $this->getTestForm(); $action = new Test_GridFieldAction; $record = $this->getTestModel(); $list = Test_CmsActionsModel::get(); $gridField = new GridField('testGridfield', null, $list); $actionName = 'test'; $arguments = ['ID' => $record->ID]; $data = []; $result = $action->doHandle($gridField, $actionName, $arguments, $data); $this->assertEquals($actionName, $action->performedActionName); $this->assertEquals($arguments, $action->performedArguments); $this->assertEquals($data, $action->performedData); } public function testLeftAndMain() { $page = $this->getTestPage(); $leftAndMain = LeftAndMain::create(); $form = $this->getTestForm($leftAndMain, $page); // otherwise getRecord complains $leftAndMain->record = $page; $result = $leftAndMain->doCustomAction( [ 'action_doCustomAction' => [ 'testAction' => 1 ], 'ID' => $page->ID, 'ClassName' => $page->ClassName ], $form ); $this->assertEquals($page->testAction(), $form->getMessage()); $list = []; $simpleList = []; foreach ($form->Actions() as $action) { if ($action instanceof CompositeField) { $arr = []; foreach ($action->getChildren() as $subAction) { $arr[] = $subAction->getName() . ' (' . get_class($subAction) . ')'; $simpleList[] = $subAction->getName(); } $list[] = $arr; } else { $list[] = $action->getName() . ' (' . get_class($action) . ')'; $simpleList[] = $action->getName(); } } $filteredSimpleList = array_unique($simpleList); // We should not have duplicated actions $this->assertEquals($filteredSimpleList, $simpleList); } } |