Merge pull request #314 from sminnee/pulls/312-fix-editable-readonly

FIX: Fixed handling of uneditable records in GridFieldEditableColumns
This commit is contained in:
Robbie Averill 2020-09-10 14:08:29 -07:00 committed by GitHub
commit 3a9fd3c928
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 125 additions and 4 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
vendor/
resources/
composer.lock
assets/

View File

@ -56,10 +56,6 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
public function getColumnContent($grid, $record, $col)
{
if (!$record->canEdit()) {
return parent::getColumnContent($grid, $record, $col);
}
$fields = $this->getForm($grid, $record)->Fields();
if (!$this->displayFields) {
@ -104,6 +100,10 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
$field->setName($this->getFieldName($field->getName(), $grid, $record));
$field->setValue($value);
if ($grid->isReadonly() || !$record->canEdit()) {
$field = $field->performReadonlyTransformation();
}
if ($field instanceof HtmlEditorField) {
return $field->FieldHolder();
}

View File

@ -0,0 +1,94 @@
<?php
namespace Symbiote\GridFieldExtensions\Tests;
use Symbiote\GridFieldExtensions\Tests\Stub\TestController;
use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Dev\SapphireTest;
class GridFieldEditableColumnsTest extends SapphireTest
{
private function getMockGrid()
{
$controller = new TestController('Test');
$form = new Form($controller, 'TestForm', new FieldList(
$grid = new GridField('TestGridField')
), new FieldList());
$grid->setModelClass(StubUnorderable::class);
$grid->setList(StubUnorderable::get());
return $grid;
}
private function getMockRecord($id, $title)
{
$record = new StubUnorderable();
$record->ID = $id;
$record->Title = $title;
return $record;
}
public function testProvidesEditableFieldsInColumns()
{
$grid = $this->getMockGrid();
$component = new GridFieldEditableColumns();
$record = $this->getMockRecord(100, "foo");
$this->assertEquals(
[ 'Title' ],
$component->getColumnsHandled($grid)
);
$record->setCanEdit(true);
$column = $component->getColumnContent($grid, $record, 'Title');
$this->assertInstanceOf(DBHTMLText::class, $column);
$this->assertRegExp(
'/<input type="text" name="TestGridField\[GridFieldEditableColumns\]\[100\]\[Title\]" value="foo"[^>]*>/',
$column->getValue()
);
}
public function testProvidesReadonlyColumnsForNoneditableRecords()
{
$grid = $this->getMockGrid();
$component = new GridFieldEditableColumns();
$record = $this->getMockRecord(100, "testval");
$record->setCanEdit(false);
$column = $component->getColumnContent($grid, $record, 'Title');
$this->assertInstanceOf(DBHTMLText::class, $column);
$this->assertRegExp(
'/<span[^>]*>\s*testval\s*<\/span>/',
$column->getValue()
);
}
public function testProvidesReadonlyColumnsForReadonlyGrids()
{
$grid = $this->getMockGrid();
$component = new GridFieldEditableColumns();
$record = $this->getMockRecord(100, "testval");
$record->setCanEdit(true);
$grid = $grid->performReadonlyTransformation();
if (!$grid instanceof GridField) {
$this->markTestSkipped('silverstripe/framework <4.2.2 doesn\'t support readonly GridFields');
}
$column = $component->getColumnContent($grid, $record, 'Title');
$this->assertInstanceOf(DBHTMLText::class, $column);
$this->assertRegExp(
'/<span[^>]*>\s*testval\s*<\/span>/',
$column->getValue()
);
}
}

View File

@ -12,4 +12,16 @@ class StubUnorderable extends DataObject implements TestOnly
];
private static $table_name = 'StubUnorderable';
private $canEdit = false;
public function setCanEdit($canEdit)
{
$this->canEdit = $canEdit;
}
public function canEdit($member = null)
{
return $this->canEdit;
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Symbiote\GridFieldExtensions\Tests\Stub;
use SilverStripe\Control\Controller;
class TestController extends Controller
{
private static $url_segment = 'test';
}