diff --git a/code/cms/DMSDocumentAdmin.php b/code/cms/DMSDocumentAdmin.php index b52eb68..b56d8bf 100644 --- a/code/cms/DMSDocumentAdmin.php +++ b/code/cms/DMSDocumentAdmin.php @@ -3,7 +3,8 @@ class DMSDocumentAdmin extends ModelAdmin { private static $managed_models = array( - 'DMSDocument' + 'DMSDocument', + 'DMSDocumentSet' ); private static $url_segment = 'documents'; @@ -21,13 +22,34 @@ class DMSDocumentAdmin extends ModelAdmin { /** @var CMSForm $form */ $form = parent::getEditForm($id, $fields); + $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass)); + return $this->modifyGridField($form, $gridField); + } - // See parent class - $gridFieldName = $this->sanitiseClassName($this->modelClass); + /** + * If the GridField is for DMSDocument then add a custom "add" button. If it's for DMSDocumentSet then + * update the display fields to include some extra columns that are only for this ModelAdmin, so cannot + * be added directly to the model's display fields. + * + * @param CMSForm $form + * @param GridField $gridField + * @return CMSForm + */ + protected function modifyGridField(CMSForm $form, GridField $gridField) + { + $gridFieldConfig = $gridField->getConfig(); - $gridFieldConfig = $form->Fields()->fieldByName($gridFieldName)->getConfig(); - $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton'); - $gridFieldConfig->addComponent(new DMSGridFieldAddNewButton('buttons-before-left'), 'GridFieldExportButton'); + if ($this->modelClass === 'DMSDocument') { + $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton'); + $gridFieldConfig->addComponent(new DMSGridFieldAddNewButton('buttons-before-left'), 'GridFieldExportButton'); + } elseif ($this->modelClass === 'DMSDocumentSet') { + $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton'); + + $dataColumns = $gridFieldConfig->getComponentByType('GridFieldDataColumns'); + $fields = $dataColumns->getDisplayFields($gridField); + $fields = array('Title' => 'Title', 'Page.Title' => 'Page') + $fields; + $dataColumns->setDisplayFields($fields); + } return $form; } diff --git a/tests/cms/DMSDocumentAdminTest.php b/tests/cms/DMSDocumentAdminTest.php index d1c6c7e..680aa3c 100644 --- a/tests/cms/DMSDocumentAdminTest.php +++ b/tests/cms/DMSDocumentAdminTest.php @@ -2,14 +2,21 @@ class DMSDocumentAdminTest extends FunctionalTest { + protected static $fixture_file = 'DMSDocumentAdminTest.yml'; + + public function setUp() + { + parent::setUp(); + + $this->logInWithPermission('ADMIN'); + } + /** * Check that the default "add new" button is gone, and replaced with our customised version of it */ public function testGridFieldHasCustomisedAddNewButton() { $modelAdmin = new DMSDocumentAdmin; - // SS < 3.3 doesn't have a response setter, this initialises it - $modelAdmin->handleRequest(new SS_HTTPRequest('GET', '/'), DataModel::inst()); $modelAdmin->init(); $form = $modelAdmin->getEditForm(); @@ -31,4 +38,32 @@ class DMSDocumentAdminTest extends FunctionalTest 'Model admin for documents contains customised DMS add new button' ); } + + /** + * Quick check to ensure that the ModelAdmin endpoint is working + */ + public function testModelAdminEndpointWorks() + { + $this->assertEquals(200, $this->get('admin/documents')->getStatusCode()); + } + + /** + * Check that the document sets GridField has had its "add new" button removed + */ + public function testDocumentSetsGridFieldHasNoAddButton() + { + $result = (string) $this->get('admin/documents/DMSDocumentSet')->getBody(); + $this->assertNotContains('Add Document Set', $result); + } + + /** + * Check that the document sets GridField has a data column for the parent page title. Here we check for the + * Page title existing in the DOM, since "Page" is guaranteed to exist somewhere else. + */ + public function testDocumentSetsGridFieldHasParentPageColumn() + { + $result = (string) $this->get('admin/documents/DMSDocumentSet')->getBody(); + $this->assertContains('Home Test Page', $result); + $this->assertContains('About Us Test Page', $result); + } } diff --git a/tests/cms/DMSDocumentAdminTest.yml b/tests/cms/DMSDocumentAdminTest.yml new file mode 100644 index 0000000..b57ac41 --- /dev/null +++ b/tests/cms/DMSDocumentAdminTest.yml @@ -0,0 +1,32 @@ +Page: + home: + Title: Home Test Page + URLSegment: home + about_us: + Title: About Us Test Page + URLSegment: About Us + +DMSDocumentSet: + home_default: + Title: Home Default + Page: =>Page.home + home_alt: + Title: Home Alternative + Page: =>Page.home + about_default: + Title: About Default + Page: =>Page.about_us + +DMSDocument: + h_d_1: + Title: Home Default 1 + Filename: home-default-1 + Sets: =>DMSDocumentSet.home_default + h_a_2: + Title: Home Default 2 + Filename: home-default-2 + Sets: =>DMSDocumentSet.home_alt + a_d_1: + Title: About Default 1 + Filename: about-default-1 + Sets: =>DMSDocumentSet.about_default