| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LeKoala\CmsActions\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Security\Member; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use LeKoala\CmsActions\ActionsGridFieldItemRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use LeKoala\CmsActions\CustomLink; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Admin\LeftAndMain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Forms\GridField\GridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\Forms\GridField\GridFieldDetailForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Tests for Cms Actions module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class CmsActionsTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Defines the fixture file to use for this test class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected static $fixture_file = 'CmsActionsTest.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected static $extra_dataobjects = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         Test_CmsActionsModel::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         Test_ActionsPage::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $controller = Controller::curr(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $controller->config()->set('url_segment', 'test_controller'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return Test_ActionsPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function getTestPage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $this->objFromFixture(Test_ActionsPage::class, 'demo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return Test_CmsActionsModel | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     public function getTestModel() | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         return $this->objFromFixture(Test_CmsActionsModel::class, 'demo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return Member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function getAdminMember() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $this->objFromFixture(Member::class, 'admin'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function getMemberForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $controller = Controller::curr(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $form = new Form($controller); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $record = $this->getAdminMember(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $list = Member::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $gridField = new GridField('testGridfield', null, $list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $detailForm = new GridFieldDetailForm('testDetailForm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $form = $GridFieldDetailForm->ItemEditForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $form->loadDataFrom($record); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $form; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function getTestForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $controller = Controller::curr(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $record = $this->getTestModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $list = Test_CmsActionsModel::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $gridField = new GridField('testGridfield', null, $list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $detailForm = new GridFieldDetailForm('testDetailForm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $form = $GridFieldDetailForm->ItemEditForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $form->loadDataFrom($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function testCustomDeleteTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $form = $this->getTestForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $record = $form->getRecord(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $delete = $form->Actions()->fieldByName("action_doDelete"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->assertEquals($delete->Title(), $record->getDeleteButtonTitle()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function testHasSaveAndClose() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $form = $this->getTestForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $doSaveAndClose = $form->Actions()->fieldByName("action_doSaveAndClose"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         // It can be nested in MajorActions, then we need to use dot notation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if (!$doSaveAndClose) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $doSaveAndClose = $form->Actions()->fieldByName("MajorActions.action_doSaveAndClose"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->assertNotEmpty($doSaveAndClose); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function testHasDefaultTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $customLink = new CustomLink('doTest'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->assertEquals('Do test', $customLink->getTitle()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function testConfirmationMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $customLink = new CustomLink('doTest'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $customLink->setConfirmation(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->assertContains('sure', $customLink->getConfirmation()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function testGridFieldAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $form = $this->getTestForm(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $action = new Test_GridFieldAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $record = $this->getTestModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $list = Test_CmsActionsModel::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $gridField = new GridField('testGridfield', null, $list); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $actionName = 'test'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $arguments = ['ID' => $record->ID]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $result = $action->doHandle($gridField, $actionName, $arguments, $data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->assertEquals($actionName, $action->performedActionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->assertEquals($arguments, $action->performedArguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->assertEquals($data, $action->performedData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     public function testLeftAndMain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $leftAndMain = LeftAndMain::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $form = $this->getTestForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $page = $this->getTestPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         // otherwise getRecord complains | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $leftAndMain->record = $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $result = $leftAndMain->doCustomAction(['action_doCustomAction' => [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             'testAction' => 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         ], 'ID' => $page->ID, 'ClassName' => $page->ClassName], $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $this->assertEquals($page->testAction(), $form->getMessage()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |  |