MINOR Add more tests for classes in the "cms" code folder

This commit is contained in:
Robbie Averill 2017-05-05 14:51:52 +12:00
parent e4bc553521
commit b80aa645f8
9 changed files with 214 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -106,7 +106,7 @@ class DMSGridFieldDeleteAction extends GridFieldDeleteAction implements
}
$delete = false;
if ($item->Pages()->Count() <= 1) {
if ($item->getRelatedPages()->count() <= 1) {
$delete = true;
}

View File

@ -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();

View File

@ -0,0 +1,38 @@
<?php
class DMSDocumentAddControllerTest extends FunctionalTest
{
protected static $fixture_file = 'dms/tests/dmstest.yml';
public function setUp()
{
parent::setUp();
$this->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());
}
}

View File

@ -0,0 +1,22 @@
<?php
class DMSDocumentAddExistingFieldTest extends SapphireTest
{
/**
* The constructor should create a tree dropdown field
*/
public function testFieldContainsTreeDropdownField()
{
$field = new DMSDocumentAddExistingField('Test', 'Test');
$this->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());
}
}

View File

@ -0,0 +1,40 @@
<?php
class DMSUploadFieldTest extends SapphireTest
{
/**
* @var DMSUploadField
*/
protected $field;
public function setUp()
{
parent::setUp();
$this->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());
}
}

View File

@ -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

View File

@ -0,0 +1,86 @@
<?php
class DMSSiteTreeExtensionTest extends SapphireTest
{
protected static $fixture_file = 'dms/tests/dmstest.yml';
protected $requiredExtensions = array(
'SiteTree' => 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));
}
}