Skip to content

Commit

Permalink
chore: Updated test class methods - removed old tests that are no lon…
Browse files Browse the repository at this point in the history
…ger required or just plain overkill
  • Loading branch information
jack-nzl committed Jan 10, 2025
1 parent a67288c commit 0423735
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 170 deletions.
7 changes: 7 additions & 0 deletions src/Cms/DMSDocumentAddExistingField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class DMSDocumentAddExistingField extends CompositeField
{
public $useFieldContext = true;

/**
* @var DataObject
*/
protected $record;


public function __construct($name, $title = null)
{
$this->name = $name;
Expand All @@ -37,6 +43,7 @@ public function setRecord($record)
$this->record = $record;
return $this;
}

/**
* Get the record to use as "Parent" for uploaded Files (eg a Page with a has_one to File) If none is set, it
* will use Form->getRecord() or Form->Controller()->data()
Expand Down
1 change: 0 additions & 1 deletion src/Cms/DMSUploadField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Sunnysideup\DMS\Model\DMSDocument;
use Sunnysideup\DMS\Model\DMSDocumentSet;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Convert;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\Requirements;
Expand Down
6 changes: 0 additions & 6 deletions src/Cms/DMSUploadField_ItemHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Sunnysideup\DMS\Cms;

// use UploadField_ItemHandler;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
Expand All @@ -16,10 +14,6 @@ class DMSUploadField_ItemHandler
use Extensible;
use Injectable;

/**
* @var array<string>
* @config
*/
private static array $allowed_actions = [
'delete',
'edit',
Expand Down
28 changes: 0 additions & 28 deletions tests/DMSDocumentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,6 @@ public function tearDown(): void
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
parent::tearDown();
}
//
// /**
// * Test that the download behaviour is either "open" or "download"
// *
// * @param string $behaviour
// * @param string $expectedDisposition
// * @dataProvider behaviourProvider
// */
// public function testDownloadBehaviourOpen($behaviour, $expectedDisposition)
// {
// $self = $this;
// $this->controller->expects($this->once())
// ->method('sendFile')
// ->will(
// $this->returnCallback(function ($path, $mime, $name, $disposition) use ($self, $expectedDisposition) {
// $self->assertEquals($expectedDisposition, $disposition);
// })
// );
//
// $openDoc = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
// $openDoc->DownloadBehavior = $behaviour;
// $openDoc->clearEmbargo(false);
// $openDoc->write();
//
// $request = new HTTPRequest('GET', $openDoc->Link());
// $request->match('dmsdocument/$ID');
// $this->controller->index($request);
// }

/**
* @return array[]
Expand Down
146 changes: 11 additions & 135 deletions tests/DMSDocumentSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,32 @@

use Sunnysideup\DMS\Model\DMSDocumentSet;
use SilverStripe\CMS\Model\SiteTree;
use Sunnysideup\DMS\Cms\DMSGridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Core\Config\Config;
use Sunnysideup\DMS\Model\DMSDocument;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\DropdownField;
use Sunnysideup\DMS\DMS;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Dev\SapphireTest;

class DMSDocumentSetTest extends SapphireTest
{
protected static $fixture_file = 'dmstest.yml';

protected function setUp(): void
{
parent::setUp();
}

/**
* Ensure that getDocuments is extensible
*/
public function testGetDocumentsIsExtensible()
{
DMSDocumentSet::add_extension('StubRelatedDocumentExtension');

$set = new DMSDocumentSet;
$set = DMSDocumentSet::create();
$documents = $set->getDocuments();

$this->assertCount(1, $documents);
$this->assertSame('Extended', $documents->first()->Filename);
$this->assertSame('Extended', $documents->first()->Name);
}

/**
Expand All @@ -44,17 +41,14 @@ public function testGridFieldShowsWhenSetIsSaved()

// Not in database yet
$fields = $set->getCMSFields();
$this->assertNull($fields->fieldByName('Root.Main.Documents'));
$gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice');
$this->assertNotNull($gridFieldNotice);
$this->assertContains('Managing documents will be available', $gridFieldNotice->getContent());
$this->assertNull($fields->fieldByName('Documents'));

// In the database
$set->Title = 'Testing';
$set->write();
$fields = $set->getCMSFields();
$this->assertNotNull($fields->fieldByName('Root.Main.Documents'));
$this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice'));
$gridField = $fields->dataFieldByName('Documents');
$this->assertNotNull($gridField);
}

public function testRelations()
Expand All @@ -72,34 +66,6 @@ public function testRelations()
$this->assertEquals([$ds1->ID, $ds2->ID], $s1->DocumentSets()->column('ID'));
}

/**
* Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context.
*/
public function testDocumentGridFieldConfig()
{
$set = $this->objFromFixture(DMSDocumentSet::class, 'ds1');
$fields = $set->getCMSFields();
$gridField = $fields->fieldByName('Root.Main.Documents');
$this->assertTrue((bool) $gridField->hasClass('documents'));

/** @var GridFieldConfig $config */
$config = $gridField->getConfig();

$this->assertNotNull($addNew = $config->getComponentByType(DMSGridFieldAddNewButton::class));
$this->assertSame($set->ID, $addNew->getDocumentSetId());

if (class_exists('GridFieldPaginatorWithShowAll')) {
$this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll'));
} else {
$paginator = $config->getComponentByType(GridFieldPaginator::class);
$this->assertNotNull($paginator);
$this->assertSame(15, $paginator->getItemsPerPage());
}

$sortableAssertion = class_exists('GridFieldSortableRows') ? 'assertNotNull' : 'assertNull';
$this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows'));
}

/**
* Ensure that the display fields for the documents GridField can be returned
*/
Expand All @@ -112,93 +78,9 @@ public function testGetDocumentDisplayFields()
$displayFields = $document->getDocumentDisplayFields();
$this->assertContains('Apple', $displayFields);
$this->assertContains('Orange', $displayFields);
$this->assertArrayHasKey('ManuallyAdded', $displayFields);
$this->assertContains('Added', $displayFields);
}

/**
* Tests to ensure that the callback for formatting ManuallyAdded will return a nice label for the user
*/
public function testNiceFormattingForManuallyAddedInGridField()
{
$fieldFormatting = $this->objFromFixture(DMSDocumentSet::class, 'ds1')
->getCMSFields()
->fieldByName('Root.Main.Documents')
->getConfig()
->getComponentByType(GridFieldDataColumns::class)
->getFieldFormatting();

$this->assertArrayHasKey('ManuallyAdded', $fieldFormatting);
$this->assertTrue(is_callable($fieldFormatting['ManuallyAdded']));

$this->assertSame('Manually', $fieldFormatting['ManuallyAdded'](1));
$this->assertSame('Query Builder', $fieldFormatting['ManuallyAdded'](0));
}

/**
* Ensure that the "direction" dropdown field has user friendly field labels
*/
public function testQueryBuilderDirectionFieldHasFriendlyLabels()
{
$fields = $this->objFromFixture(DMSDocumentSet::class, 'ds1')->getCMSFields();

$dropdown = $fields->fieldByName('Root.QueryBuilder')->FieldList()->filterByCallback(function ($field) {
return $field instanceof FieldGroup;
})->first()->fieldByName('SortByDirection');

$this->assertInstanceOf(DropdownField::class, $dropdown);
$source = $dropdown->getSource();
$this->assertContains('Ascending', $source);
$this->assertContains('Descending', $source);
}

/**
* Ensure that the configurable shortcode handler key is a hidden field in the CMS
*/
public function testShortcodeHandlerKeyFieldExists()
{
Config::modify()->set(DMS::class, 'shortcode_handler_key', 'unit-test');

$set = DMSDocumentSet::create(['Title' => 'TestSet']);
$set->write();

$fields = $set->getCMSFields();
$field = $fields->fieldByName('Root.Main.DMSShortcodeHandlerKey');

$this->assertInstanceOf(HiddenField::class, $field);
$this->assertSame('unit-test', $field->Value());
}

/**
* Ensure that if the module is available, the orderable rows GridField component is added
*/
public function testDocumentsAreOrderable()
{
if (!class_exists('GridFieldSortableRows')) {
$this->markTestSkipped('Test requires undefinedoffset/sortablegridfield installed.');
}

$fields = $this->objFromFixture(DMSDocumentSet::class, 'ds1')->getCMSFields();

$gridField = $fields->fieldByName('Root.Main.Documents');
$this->assertInstanceOf(GridField::class, $gridField);

$this->assertInstanceOf(
'GridFieldSortableRows',
$gridField->getConfig()->getComponentByType('GridFieldSortableRows')
);
}

/**
* Tests that an exception is thrown if no title entered for a DMSDocumentSet.
*
* @expectedException \SilverStripe\ORM\ValidationException
*/
public function testExceptionOnNoTitleGiven()
{
DMSDocumentSet::create(['Title' => ''])->write();
}

/**
* Ensure that when editing in a page context that the "page" field is removed, or is labelled "Show on page"
* otherwise
Expand All @@ -208,13 +90,7 @@ public function testPageFieldRemovedWhenEditingInPageContext()
$set = $this->objFromFixture(DMSDocumentSet::class, 'ds1');

$fields = $set->getCMSFields();
$this->assertInstanceOf(DropdownField::class, $fields->fieldByName('Root.Main.PageID'));

$pageController = new CMSPageEditController;
$pageController->pushCurrent();

$fields = $set->getCMSFields();
$this->assertNull($fields->fieldByName('Root.Main.PageID'));
$this->assertInstanceOf(DropdownField::class, $fields->dataFieldByName('PageID'));
}

/**
Expand Down

0 comments on commit 0423735

Please sign in to comment.