diff --git a/src/Cms/DMSDocumentAddExistingField.php b/src/Cms/DMSDocumentAddExistingField.php index 2403ea1..a6e2460 100644 --- a/src/Cms/DMSDocumentAddExistingField.php +++ b/src/Cms/DMSDocumentAddExistingField.php @@ -12,6 +12,12 @@ class DMSDocumentAddExistingField extends CompositeField { public $useFieldContext = true; + /** + * @var DataObject + */ + protected $record; + + public function __construct($name, $title = null) { $this->name = $name; @@ -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() diff --git a/src/Cms/DMSUploadField.php b/src/Cms/DMSUploadField.php index 8956e87..d3cd835 100644 --- a/src/Cms/DMSUploadField.php +++ b/src/Cms/DMSUploadField.php @@ -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; diff --git a/src/Cms/DMSUploadField_ItemHandler.php b/src/Cms/DMSUploadField_ItemHandler.php index 7f6f6c6..25b81da 100644 --- a/src/Cms/DMSUploadField_ItemHandler.php +++ b/src/Cms/DMSUploadField_ItemHandler.php @@ -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; @@ -16,10 +14,6 @@ class DMSUploadField_ItemHandler use Extensible; use Injectable; - /** - * @var array - * @config - */ private static array $allowed_actions = [ 'delete', 'edit', diff --git a/tests/DMSDocumentControllerTest.php b/tests/DMSDocumentControllerTest.php index 9c18a52..2ceee71 100644 --- a/tests/DMSDocumentControllerTest.php +++ b/tests/DMSDocumentControllerTest.php @@ -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[] diff --git a/tests/DMSDocumentSetTest.php b/tests/DMSDocumentSetTest.php index ba6455c..0a68616 100644 --- a/tests/DMSDocumentSetTest.php +++ b/tests/DMSDocumentSetTest.php @@ -4,23 +4,20 @@ 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 */ @@ -28,11 +25,11 @@ 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); } /** @@ -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() @@ -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 */ @@ -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 @@ -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')); } /**