Merge pull request #1749 from open-sausages/pulls/4.0/show-diff

Fix history comparison fields
This commit is contained in:
Damian Mooyman 2017-02-28 17:01:53 +13:00 committed by GitHub
commit 20f6ec47c6
2 changed files with 58 additions and 9 deletions

View File

@ -7,10 +7,12 @@ use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\HTMLReadonlyField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
@ -450,21 +452,16 @@ class CMSPageHistoryController extends CMSMain
$form->setActions(new FieldList());
$form->addExtraClass('compare');
// Comparison views shouldn't be editable.
// Its important to convert fields *before* loading data,
// as the comparison output is HTML and not valid values for the various field types
$readonlyFields = $form->Fields()->makeReadonly();
$form->setFields($readonlyFields);
$form->loadDataFrom($record);
$form->loadDataFrom(array(
"ID" => $id,
"Version" => $fromVersion,
));
foreach ($form->Fields()->dataFields() as $field) {
$field->dontEscape = true;
}
// Comparison views shouldn't be editable.
// As the comparison output is HTML and not valid values for the various field types
$readonlyFields = $this->transformReadonly($form->Fields());
$form->setFields($readonlyFields);
return $form;
}
@ -475,4 +472,22 @@ class CMSPageHistoryController extends CMSMain
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages');
return $crumbs;
}
/**
* Replace all data fields with HTML readonly fields to display diff
*
* @param FieldList $fields
* @return FieldList
*/
public function transformReadonly(FieldList $fields)
{
foreach ($fields->dataFields() as $field) {
if ($field instanceof HiddenField) {
continue;
}
$newField = $field->castedCopy(HTMLReadonlyField::class);
$fields->replaceField($field->getName(), $newField);
}
return $fields;
}
}

View File

@ -1,5 +1,10 @@
<?php
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\HTMLReadonlyField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\CMS\Controllers\CMSPageHistoryController;
use SilverStripe\Dev\FunctionalTest;
@ -152,4 +157,33 @@ class CMSPageHistoryControllerTest extends FunctionalTest
$this->assertThat($checkbox[0], $this->logicalNot($this->isNull()));
$this->assertEquals('checked', (string) $checkbox[0]->attributes()->checked);
}
public function testTransformReadonly()
{
/** @var CMSPageHistoryController $history */
$history = singleton(CMSPageHistoryController::class);
$fieldList = FieldList::create([
FieldGroup::create('group', [
TextField::create('childField', 'child field'),
]),
TextField::create('field', 'field', 'My <del>value</del><ins>change</ins>'),
HiddenField::create('hiddenField', 'hidden field'),
]);
$newList = $history->transformReadonly($fieldList);
$field = $newList->dataFieldByName('field');
$this->assertTrue($field instanceof HTMLReadonlyField);
$this->assertContains('<ins>', $field->forTemplate());
$groupField = $newList->fieldByName('group');
$this->assertTrue($groupField instanceof FieldGroup);
$childField = $newList->dataFieldByName('childField');
$this->assertTrue($childField instanceof HTMLReadonlyField);
$hiddenField = $newList->dataFieldByName('hiddenField');
$this->assertTrue($hiddenField instanceof HiddenField);
}
}