|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class DMSDocumentSetTest extends SapphireTest |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
protected static $fixture_file = 'dmstest.yml'; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Ensure that getDocuments is extensible |
|
9
|
|
|
*/ |
|
10
|
|
View Code Duplication |
public function testGetDocumentsIsExtensible() |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
DMSDocumentSet::add_extension('StubRelatedDocumentExtension'); |
|
13
|
|
|
|
|
14
|
|
|
$set = new DMSDocumentSet; |
|
15
|
|
|
$documents = $set->getDocuments(); |
|
16
|
|
|
|
|
17
|
|
|
$this->assertCount(1, $documents); |
|
|
|
|
|
|
18
|
|
|
$this->assertSame('Extended', $documents->first()->Filename); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Test that the GridField for documents isn't shown until you've saved the set |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testGridFieldShowsWhenSetIsSaved() |
|
25
|
|
|
{ |
|
26
|
|
|
$set = DMSDocumentSet::create(); |
|
27
|
|
|
|
|
28
|
|
|
// Not in database yet |
|
29
|
|
|
$fields = $set->getCMSFields(); |
|
30
|
|
|
$this->assertNull($fields->fieldByName('Root.Main.Documents')); |
|
|
|
|
|
|
31
|
|
|
$gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice'); |
|
32
|
|
|
$this->assertNotNull($gridFieldNotice); |
|
|
|
|
|
|
33
|
|
|
$this->assertContains('Managing documents will be available', $gridFieldNotice->getContent()); |
|
34
|
|
|
|
|
35
|
|
|
// In the database |
|
36
|
|
|
$set->Title = 'Testing'; |
|
37
|
|
|
$set->write(); |
|
38
|
|
|
$fields = $set->getCMSFields(); |
|
39
|
|
|
$this->assertNotNull($fields->fieldByName('Root.Main.Documents')); |
|
|
|
|
|
|
40
|
|
|
$this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice')); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testRelations() |
|
44
|
|
|
{ |
|
45
|
|
|
$s1 = $this->objFromFixture('SiteTree', 's1'); |
|
46
|
|
|
$s2 = $this->objFromFixture('SiteTree', 's2'); |
|
|
|
|
|
|
47
|
|
|
$s4 = $this->objFromFixture('SiteTree', 's4'); |
|
48
|
|
|
|
|
49
|
|
|
$ds1 = $this->objFromFixture('DMSDocumentSet', 'ds1'); |
|
50
|
|
|
$ds2 = $this->objFromFixture('DMSDocumentSet', 'ds2'); |
|
51
|
|
|
$ds3 = $this->objFromFixture('DMSDocumentSet', 'ds3'); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$this->assertCount(0, $s4->getDocumentSets(), 'Page 4 has no document sets associated'); |
|
|
|
|
|
|
54
|
|
|
$this->assertCount(2, $s1->getDocumentSets(), 'Page 1 has 2 document sets'); |
|
|
|
|
|
|
55
|
|
|
$this->assertEquals(array($ds1->ID, $ds2->ID), $s1->getDocumentSets()->column('ID')); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testDocumentGridFieldConfig() |
|
62
|
|
|
{ |
|
63
|
|
|
$set = $this->objFromFixture('DMSDocumentSet', 'ds1'); |
|
64
|
|
|
$fields = $set->getCMSFields(); |
|
65
|
|
|
$gridField = $fields->fieldByName('Root.Main.Documents'); |
|
66
|
|
|
$this->assertTrue((bool) $gridField->hasClass('documents')); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** @var GridFieldConfig $config */ |
|
69
|
|
|
$config = $gridField->getConfig(); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertNotNull($config->getComponentByType('DMSGridFieldDeleteAction')); |
|
|
|
|
|
|
72
|
|
|
$this->assertNotNull($addNew = $config->getComponentByType('DMSGridFieldAddNewButton')); |
|
|
|
|
|
|
73
|
|
|
$this->assertSame($set->ID, $addNew->getDocumentSetId()); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
if (class_exists('GridFieldPaginatorWithShowAll')) { |
|
76
|
|
|
$this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll')); |
|
|
|
|
|
|
77
|
|
|
} else { |
|
78
|
|
|
$paginator = $config->getComponentByType('GridFieldPaginator'); |
|
79
|
|
|
$this->assertNotNull($paginator); |
|
|
|
|
|
|
80
|
|
|
$this->assertSame(15, $paginator->getItemsPerPage()); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$sortableAssertion = class_exists('GridFieldSortableRows') ? 'assertNotNull' : 'assertNull'; |
|
84
|
|
|
$this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Test that query fields can be added to the gridfield |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testAddQueryFields() |
|
91
|
|
|
{ |
|
92
|
|
|
|
|
93
|
|
|
/** @var DMSDocumentSet $set */ |
|
94
|
|
|
$set = $this->objFromFixture('DMSDocumentSet', 'ds6'); |
|
95
|
|
|
/** @var FieldList $fields */ |
|
96
|
|
|
$fields = new FieldList(new TabSet('Root')); |
|
97
|
|
|
/** @var FieldList $fields */ |
|
98
|
|
|
$set->addQueryFields($fields); |
|
99
|
|
|
$keyValuePairs = $fields->dataFieldByName('KeyValuePairs'); |
|
100
|
|
|
$this->assertNotNull( |
|
|
|
|
|
|
101
|
|
|
$keyValuePairs, |
|
102
|
|
|
'addQueryFields() includes KeyValuePairs composite field' |
|
103
|
|
|
); |
|
104
|
|
|
$this->assertNotNull( |
|
|
|
|
|
|
105
|
|
|
$keyValuePairs->fieldByName('KeyValuePairs[Title]'), |
|
106
|
|
|
'addQueryFields() includes KeyValuePairs composite field' |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testAddQueryFieldsIsExtensible() |
|
111
|
|
|
{ |
|
112
|
|
|
|
|
113
|
|
|
DMSDocumentSet::add_extension('StubDocumentSetMockExtension'); |
|
114
|
|
|
|
|
115
|
|
|
$fields = new FieldList(new TabSet('Root')); |
|
116
|
|
|
$set = new DMSDocumentSet; |
|
117
|
|
|
$set->addQueryFields($fields); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertNotNull( |
|
|
|
|
|
|
120
|
|
|
$fields->dataFieldByName('ExtendedField'), |
|
121
|
|
|
'addQueryFields() is extendible as it included the field from the extension' |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Ensure that the configurable shortcode handler key is a hidden field in the CMS |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testShortcodeHandlerKeyFieldExists() |
|
129
|
|
|
{ |
|
130
|
|
|
Config::inst()->update('DMS', 'shortcode_handler_key', 'unit-test'); |
|
131
|
|
|
|
|
132
|
|
|
$set = DMSDocumentSet::create(); |
|
133
|
|
|
$set->write(); |
|
134
|
|
|
|
|
135
|
|
|
$fields = $set->getCMSFields(); |
|
136
|
|
|
$field = $fields->fieldByName('Root.Main.DMSShortcodeHandlerKey'); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertInstanceOf('HiddenField', $field); |
|
|
|
|
|
|
139
|
|
|
$this->assertSame('unit-test', $field->Value()); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Test that extra documents are added after write |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testSaveLinkedDocuments() |
|
146
|
|
|
{ |
|
147
|
|
|
/** @var DMSDocumentSet $set */ |
|
148
|
|
|
$set = $this->objFromFixture('DMSDocumentSet', 'dsSaveLinkedDocuments'); |
|
149
|
|
|
// Assert initially docs |
|
150
|
|
|
$this->assertEquals(1, $set->getDocuments()->count(), 'Set has 1 document'); |
|
|
|
|
|
|
151
|
|
|
// Now apply the query and see if 2 extras were added with CreatedByID filter |
|
152
|
|
|
$set->KeyValuePairs = '{"Filename":"extradoc3"}'; |
|
153
|
|
|
$set->saveLinkedDocuments(); |
|
154
|
|
|
$this->assertEquals(2, $set->getDocuments()->count(), 'Set has 2 documents'); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.