diff --git a/code/cms/DMSDocumentAddExistingField.php b/code/cms/DMSDocumentAddExistingField.php index 7e45953..83d9bb1 100644 --- a/code/cms/DMSDocumentAddExistingField.php +++ b/code/cms/DMSDocumentAddExistingField.php @@ -63,9 +63,12 @@ class DMSDocumentAddExistingField extends CompositeField /** * Sets or unsets the use of the "field" class in the template. The "field" class adds Javascript behaviour * that causes unwelcome hiding side-effects when this Field is used within the link editor pop-up + * + * @return $this */ public function setUseFieldClass($use = false) { $this->useFieldContext = $use; + return $this; } } diff --git a/code/cms/DMSGridFieldDeleteAction.php b/code/cms/DMSGridFieldDeleteAction.php index 7907f35..edbba46 100644 --- a/code/cms/DMSGridFieldDeleteAction.php +++ b/code/cms/DMSGridFieldDeleteAction.php @@ -106,7 +106,7 @@ class DMSGridFieldDeleteAction extends GridFieldDeleteAction implements } $delete = false; - if ($item->Pages()->Count() <= 1) { + if ($item->getRelatedPages()->count() <= 1) { $delete = true; } diff --git a/code/extensions/DMSSiteTreeExtension.php b/code/extensions/DMSSiteTreeExtension.php index 6de0554..4db8d36 100644 --- a/code/extensions/DMSSiteTreeExtension.php +++ b/code/extensions/DMSSiteTreeExtension.php @@ -25,7 +25,7 @@ class DMSSiteTreeExtension extends DataExtension $gridField->addExtraClass('documentsets'); $fields->addFieldToTab( - 'Root.Document Sets (' . $this->owner->DocumentSets()->Count() . ')', + 'Root.Document Sets (' . $this->owner->DocumentSets()->count() . ')', $gridField ); } @@ -53,6 +53,7 @@ class DMSSiteTreeExtension extends DataExtension /** @var DocumentSet $documentSet */ $documents->merge($documentSet->getDocuments()); } + $documents->removeDuplicates(); return $documents; } @@ -67,11 +68,11 @@ class DMSSiteTreeExtension extends DataExtension // Only remove if record doesn't still exist on live stage. if (!$existsOnOtherStage) { - $dmsDocuments = $this->owner->Documents(); + $dmsDocuments = $this->owner->getAllDocuments(); foreach ($dmsDocuments as $document) { - //if the document is only associated with one page, i.e. only associated with this page - if ($document->Pages()->Count() <= 1) { - //delete the document before deleting this page + // If the document is only associated with one page, i.e. only associated with this page + if ($document->getRelatedPages()->count() <= 1) { + // Delete the document before deleting this page $document->delete(); } } @@ -81,7 +82,7 @@ class DMSSiteTreeExtension extends DataExtension public function onBeforePublish() { $embargoedDocuments = $this->owner->getAllDocuments()->filter('EmbargoedUntilPublished', true); - if ($embargoedDocuments->Count() > 0) { + if ($embargoedDocuments->count() > 0) { foreach ($embargoedDocuments as $doc) { $doc->EmbargoedUntilPublished = false; $doc->write(); diff --git a/tests/cms/DMSDocumentAddControllerTest.php b/tests/cms/DMSDocumentAddControllerTest.php new file mode 100644 index 0000000..fadee27 --- /dev/null +++ b/tests/cms/DMSDocumentAddControllerTest.php @@ -0,0 +1,38 @@ +logInWithPermission(); + } + + public function testCurrentPageReturnsSiteTree() + { + $controller = new DMSDocumentAddController; + $this->assertInstanceOf('SiteTree', $controller->currentPage()); + } + + public function testGetCurrentDocumentSetReturnsDocumentSet() + { + $controller = new DMSDocumentAddController; + $this->assertInstanceOf('DMSDocumentSet', $controller->getCurrentDocumentSet()); + } + + /** + * Test that extra allowed extensions are merged into the default upload field allowed extensions + */ + public function testGetAllowedExtensions() + { + $controller = new DMSDocumentAddController; + Config::inst()->remove('File', 'allowed_extensions'); + Config::inst()->update('File', 'allowed_extensions', array('jpg', 'gif')); + $this->assertSame(array('jpg', 'gif'), $controller->getAllowedExtensions()); + + Config::inst()->update('DMSDocumentAddController', 'allowed_extensions', array('php', 'php5')); + $this->assertSame(array('jpg', 'gif', 'php', 'php5'), $controller->getAllowedExtensions()); + } +} diff --git a/tests/cms/DMSDocumentAddExistingFieldTest.php b/tests/cms/DMSDocumentAddExistingFieldTest.php new file mode 100644 index 0000000..af30f1d --- /dev/null +++ b/tests/cms/DMSDocumentAddExistingFieldTest.php @@ -0,0 +1,22 @@ +assertContainsOnlyInstancesOf('TreeDropdownField', $field->getChildren()); + $this->assertSame('PageSelector', $field->getChildren()->first()->getName()); + } + + public function testSetAndGetRecord() + { + $record = new DMSDocumentSet; + $field = new DMSDocumentAddExistingField('Test'); + $field->setRecord($record); + $this->assertSame($record, $field->getRecord()); + } +} diff --git a/tests/DMSDocumentAdminTest.php b/tests/cms/DMSDocumentAdminTest.php similarity index 100% rename from tests/DMSDocumentAdminTest.php rename to tests/cms/DMSDocumentAdminTest.php diff --git a/tests/cms/DMSUploadFieldTest.php b/tests/cms/DMSUploadFieldTest.php new file mode 100644 index 0000000..6048f86 --- /dev/null +++ b/tests/cms/DMSUploadFieldTest.php @@ -0,0 +1,40 @@ +field = new DMSUploadField('StubUploadField'); + } + + /** + * The validator is coded to always return true. Replace this test if this behaviour changes in future. + */ + public function testValidatorAlwaysReturnsTrue() + { + $this->assertTrue($this->field->validate('foo')); + } + + public function testGetItemHandler() + { + $this->assertInstanceOf('DMSUploadField_ItemHandler', $this->field->getItemHandler(123)); + } + + /** + * Ensure that the folder name can be get/set and that the value set is casted to a string + */ + public function testCanGetAndSetFolderName() + { + $this->field->setFolderName('qwerty'); + $this->assertSame('qwerty', $this->field->getFolderName()); + $this->field->setFolderName(123); + $this->assertSame('123', $this->field->getFolderName()); + } +} diff --git a/tests/dmstest.yml b/tests/dmstest.yml index 058236c..437f09d 100644 --- a/tests/dmstest.yml +++ b/tests/dmstest.yml @@ -3,7 +3,7 @@ SiteTree: Title: testPage1 has document sets URLSegment: s1 s2: - Title: testPage2 has document sets + Title: testPage2 a document set URLSegment: s2 s3: Title: testPage3 has document sets with embargoed docs @@ -17,6 +17,9 @@ SiteTree: s6: Title: testPage6 has no sets URLSegment: s6 + s7: + Title: testPage7 has documents embargoed until publish + URLSegment: s7 DMSTag: t1: Category: tag1 @@ -56,6 +59,9 @@ DMSDocumentSet: ds4: Title: Set containing embargoed documents Page: =>SiteTree.s3 + ds5: + Title: Set containing embargoed until publish documents + Page: =>SiteTree.s7 DMSDocument: d1: Filename: test-file-file-doesnt-exist-1 @@ -98,6 +104,16 @@ DMSDocument: EmbargoedIndefinitely: true Folder: 5 Sets: =>DMSDocumentSet.ds4 + embargo_until_publish1: + Filename: embargo-until-publish1 + EmbargoUntilPublish: true + Folder: 5 + Sets: =>DMSDocumentSet.ds5 + embargo_until_publish2: + Filename: embargo-until-publish2 + EmbargoUntilPublish: true + Folder: 5 + Sets: =>DMSDocumentSet.ds5 Member: editor: Name: editor diff --git a/tests/extensions/DMSSiteTreeExtensionTest.php b/tests/extensions/DMSSiteTreeExtensionTest.php new file mode 100644 index 0000000..a902cd7 --- /dev/null +++ b/tests/extensions/DMSSiteTreeExtensionTest.php @@ -0,0 +1,86 @@ + array('DMSSiteTreeExtension') + ); + + /** + * Ensure that setting the configuration property "documents_enabled" to false for a page type will prevent the + * CMS fields from being modified. + * + * Also ensures that a correctly named Document Sets GridField is added to the fields in the right place. + * + * Note: the (1) is the number of sets defined for this SiteTree in the fixture + * + * @dataProvider documentSetEnabledConfigProvider + */ + public function testCanDisableDocumentSetsTab($configSetting, $assertionMethod) + { + Config::inst()->update('SiteTree', 'documents_enabled', $configSetting); + $siteTree = $this->objFromFixture('SiteTree', 's2'); + $this->$assertionMethod($siteTree->getCMSFields()->fieldByName('Root.Document Sets (1).Document Sets')); + } + + /** + * @return array[] + */ + public function documentSetEnabledConfigProvider() + { + return array( + array(true, 'assertNotNull'), + array(false, 'assertNull') + ); + } + + /** + * Tests for the Document Sets GridField. + * + * Note: the (1) is the number of sets defined for this SiteTree in the fixture + */ + public function testDocumentSetsGridFieldIsCorrectlyConfigured() + { + Config::inst()->update('SiteTree', 'documents_enabled', true); + $siteTree = $this->objFromFixture('SiteTree', 's2'); + $gridField = $siteTree->getCMSFields()->fieldByName('Root.Document Sets (1).Document Sets'); + + $this->assertInstanceOf('GridField', $gridField); + $this->assertTrue((bool) $gridField->hasClass('documentsets')); + } + + /** + * Ensure that a page title can be retrieved with the number of related documents it has (across all document sets). + * + * Note that the fixture has the same two documents attached to two different document sets, attached to this + * page, but we're expecting only two since they should be returned as unique only (rather than four). + */ + public function testGetTitleWithNumberOfDocuments() + { + $siteTree = $this->objFromFixture('SiteTree', 's1'); + $this->assertSame('testPage1 has document sets (2)', $siteTree->getTitleWithNumberOfDocuments()); + } + + /** + * Ensure that documents marked as "embargo until publish" are unmarked as such when a page containing them is + * published + */ + public function testOnBeforePublishUnEmbargoesDocumentsSetAsEmbargoedUntilPublish() + { + $siteTree = $this->objFromFixture('SiteTree', 's7'); + $siteTree->doPublish(); + + // Fixture defines this page as having two documents via one set + foreach (array('embargo-until-publish1', 'embargo-until-publish2') as $filename) { + $this->assertFalse( + (bool) $siteTree->getAllDocuments() + ->filter('Filename', 'embargo-until-publish1') + ->first() + ->EmbargoedUntilPublished + ); + } + $this->assertCount(0, $siteTree->getAllDocuments()->filter('EmbargoedUntilPublished', true)); + } +}