1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests\GridField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionMenu; |
6
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
7
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldFilterHeader; |
15
|
|
|
use SilverStripe\Forms\GridField\GridFieldPageCount; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator; |
17
|
|
|
use SilverStripe\Forms\GridField\GridFieldSortableHeader; |
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
19
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader; |
20
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team; |
21
|
|
|
use SilverStripe\Dev\SapphireTest; |
22
|
|
|
use SilverStripe\Forms\GridField\GridField; |
23
|
|
|
use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState; |
24
|
|
|
|
25
|
|
|
class GridFieldReadonlyTest extends SapphireTest |
26
|
|
|
{ |
27
|
|
|
protected static $fixture_file = 'GridFieldReadonlyTest.yml'; |
28
|
|
|
|
29
|
|
|
protected static $extra_dataobjects = array( |
30
|
|
|
Team::class, |
31
|
|
|
Cheerleader::class, |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The CMS can set the value of a GridField to be a hasMany relation, which needs a readonly state. |
36
|
|
|
* This test ensures GridField has a readonly transformation. |
37
|
|
|
*/ |
38
|
|
|
public function testReadOnlyTransformation() |
39
|
|
|
{ |
40
|
|
|
// Build a hasMany Relation via getComponents like ModelAdmin does. |
41
|
|
|
$components = Team::get_one(Team::class) |
42
|
|
|
->getComponents('Cheerleaders'); |
43
|
|
|
|
44
|
|
|
$gridConfig = GridFieldConfig_RelationEditor::create(); |
45
|
|
|
|
46
|
|
|
// Build some commonly used components to make sure we're only allowing the correct components |
47
|
|
|
$gridConfig->addComponent(new GridFieldButtonRow('before')); |
48
|
|
|
$gridConfig->addComponent(new GridFieldAddNewButton('buttons-before-left')); |
49
|
|
|
$gridConfig->addComponent(new GridFieldAddExistingAutocompleter('buttons-before-right')); |
50
|
|
|
$gridConfig->addComponent(new GridFieldToolbarHeader()); |
51
|
|
|
$gridConfig->addComponent($sort = new GridFieldSortableHeader()); |
52
|
|
|
$gridConfig->addComponent($filter = new GridFieldFilterHeader()); |
53
|
|
|
$gridConfig->addComponent(new GridFieldDataColumns()); |
54
|
|
|
$gridConfig->addComponent(new GridFieldEditButton()); |
55
|
|
|
$gridConfig->addComponent(new GridFieldDeleteAction(true)); |
56
|
|
|
$gridConfig->addComponent(new GridField_ActionMenu()); |
57
|
|
|
$gridConfig->addComponent(new GridFieldPageCount('toolbar-header-right')); |
58
|
|
|
$gridConfig->addComponent($pagination = new GridFieldPaginator(2)); |
59
|
|
|
$gridConfig->addComponent(new GridFieldDetailForm()); |
60
|
|
|
$gridConfig->addComponent(new GridFieldDeleteAction()); |
61
|
|
|
$gridConfig->addComponent(new VersionedGridFieldState()); |
62
|
|
|
|
63
|
|
|
$gridField = GridField::create( |
64
|
|
|
'Cheerleaders', |
65
|
|
|
'Cheerleaders', |
66
|
|
|
$components, |
67
|
|
|
$gridConfig |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
// Model Admin sets the value of the GridField directly to the relation, which doesn't have a forTemplate() |
71
|
|
|
// function, if we rely on FormField to render into a ReadonlyField we'll get an error as HasManyRelation |
72
|
|
|
// doesn't have a forTemplate() function. |
73
|
|
|
$gridField->setValue($components); |
74
|
|
|
$gridField->setModelClass(Cheerleader::class); |
75
|
|
|
|
76
|
|
|
// This function is called by $form->makeReadonly(). |
77
|
|
|
$readonlyGridField = $gridField->performReadonlyTransformation(); |
78
|
|
|
|
79
|
|
|
// if we've made it this far, then the GridField is at least transforming correctly. |
80
|
|
|
$readonlyComponents = $readonlyGridField->getReadonlyComponents(); |
81
|
|
|
|
82
|
|
|
// assert that all the components in the readonly version are present in the whitelist. |
83
|
|
|
foreach ($readonlyGridField->getConfig()->getComponents() as $component) { |
84
|
|
|
$this->assertTrue(in_array(get_class($component), $readonlyComponents)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|