1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DMSDocumentSetTest extends SapphireTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
protected static $fixture_file = 'dmstest.yml'; |
6
|
|
|
|
7
|
|
|
protected $extraDataObjects = array( |
8
|
|
|
'DMSDocument_Mock' |
9
|
|
|
); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Ensure that getDocuments is extensible |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
public function testGetDocumentsIsExtensible() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
DMSDocumentSet::add_extension('StubRelatedDocumentExtension'); |
17
|
|
|
|
18
|
|
|
$set = new DMSDocumentSet; |
19
|
|
|
$documents = $set->getDocuments(); |
20
|
|
|
|
21
|
|
|
$this->assertCount(1, $documents); |
|
|
|
|
22
|
|
|
$this->assertSame('Extended', $documents->first()->Filename); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test that the GridField for documents isn't shown until you've saved the set |
27
|
|
|
*/ |
28
|
|
|
public function testGridFieldShowsWhenSetIsSaved() |
29
|
|
|
{ |
30
|
|
|
$set = DMSDocumentSet::create(); |
31
|
|
|
|
32
|
|
|
// Not in database yet |
33
|
|
|
$fields = $set->getCMSFields(); |
34
|
|
|
$this->assertNull($fields->fieldByName('Root.Main.Documents')); |
|
|
|
|
35
|
|
|
$gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice'); |
36
|
|
|
$this->assertNotNull($gridFieldNotice); |
|
|
|
|
37
|
|
|
$this->assertContains('Managing documents will be available', $gridFieldNotice->getContent()); |
38
|
|
|
|
39
|
|
|
// In the database |
40
|
|
|
$set->Title = 'Testing'; |
41
|
|
|
$set->write(); |
42
|
|
|
$fields = $set->getCMSFields(); |
43
|
|
|
$this->assertNotNull($fields->fieldByName('Root.Main.Documents')); |
|
|
|
|
44
|
|
|
$this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice')); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testRelations() |
48
|
|
|
{ |
49
|
|
|
$s1 = $this->objFromFixture('SiteTree', 's1'); |
50
|
|
|
$s2 = $this->objFromFixture('SiteTree', 's2'); |
|
|
|
|
51
|
|
|
$s4 = $this->objFromFixture('SiteTree', 's4'); |
52
|
|
|
|
53
|
|
|
$ds1 = $this->objFromFixture('DMSDocumentSet', 'ds1'); |
54
|
|
|
$ds2 = $this->objFromFixture('DMSDocumentSet', 'ds2'); |
55
|
|
|
$ds3 = $this->objFromFixture('DMSDocumentSet', 'ds3'); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->assertCount(0, $s4->getDocumentSets(), 'Page 4 has no document sets associated'); |
|
|
|
|
58
|
|
|
$this->assertCount(2, $s1->getDocumentSets(), 'Page 1 has 2 document sets'); |
|
|
|
|
59
|
|
|
$this->assertEquals(array($ds1->ID, $ds2->ID), $s1->getDocumentSets()->column('ID')); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context. |
64
|
|
|
*/ |
65
|
|
|
public function testDocumentGridFieldConfig() |
66
|
|
|
{ |
67
|
|
|
$set = $this->objFromFixture('DMSDocumentSet', 'ds1'); |
68
|
|
|
$fields = $set->getCMSFields(); |
69
|
|
|
$gridField = $fields->fieldByName('Root.Main.Documents'); |
70
|
|
|
$this->assertTrue((bool) $gridField->hasClass('documents')); |
|
|
|
|
71
|
|
|
|
72
|
|
|
/** @var GridFieldConfig $config */ |
73
|
|
|
$config = $gridField->getConfig(); |
74
|
|
|
|
75
|
|
|
$this->assertNotNull($config->getComponentByType('DMSGridFieldDeleteAction')); |
|
|
|
|
76
|
|
|
$this->assertNotNull($addNew = $config->getComponentByType('DMSGridFieldAddNewButton')); |
|
|
|
|
77
|
|
|
$this->assertSame($set->ID, $addNew->getDocumentSetId()); |
|
|
|
|
78
|
|
|
|
79
|
|
|
if (class_exists('GridFieldPaginatorWithShowAll')) { |
80
|
|
|
$this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll')); |
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
$paginator = $config->getComponentByType('GridFieldPaginator'); |
83
|
|
|
$this->assertNotNull($paginator); |
|
|
|
|
84
|
|
|
$this->assertSame(15, $paginator->getItemsPerPage()); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$sortableAssertion = class_exists('GridFieldSortableRows') ? 'assertNotNull' : 'assertNull'; |
88
|
|
|
$this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test that query fields can be added to the gridfield |
93
|
|
|
*/ |
94
|
|
|
public function testAddQueryFields(){ |
95
|
|
|
|
96
|
|
|
/** @var DMSDocumentSet $set */ |
97
|
|
|
$set = $this->objFromFixture('DMSDocumentSet', 'ds6'); |
98
|
|
|
$doc = new DMSDocument_Mock(); |
99
|
|
|
$set->Documents()->add($doc); |
|
|
|
|
100
|
|
|
DMSDocumentSet::$linked_document_type = 'DMSDocument_Mock'; |
|
|
|
|
101
|
|
|
/** @var FieldList $fields */ |
102
|
|
|
$fields = new FieldList(new TabSet('Root')); |
103
|
|
|
/** @var FieldList $fields */ |
104
|
|
|
$set->addQueryFields($fields); |
105
|
|
|
$keyValuePairs = $fields->dataFieldByName('KeyValuePairs'); |
106
|
|
|
$this->assertNotNull($keyValuePairs, |
|
|
|
|
107
|
|
|
'addQueryFields() includes KeyValuePairs composite field'); |
108
|
|
|
$this->assertNotNull($keyValuePairs->fieldByName('KeyValuePairs[IsImported]'), |
|
|
|
|
109
|
|
|
'addQueryFields() includes KeyValuePairs composite field'); |
110
|
|
|
$this->assertNotNull($keyValuePairs->fieldByName('KeyValuePairs[ISBN]'), |
|
|
|
|
111
|
|
|
'addQueryFields() includes KeyValuePairs composite field'); |
112
|
|
|
// Reset the type |
113
|
|
|
DMSDocumentSet::$linked_document_type = 'DMSDocument'; |
|
|
|
|
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testAddQueryFieldsIsExtensible(){ |
118
|
|
|
DMSDocumentSet::add_extension('StubDocumentSetMockExtension'); |
119
|
|
|
|
120
|
|
|
$fields = new FieldList(new TabSet('Root')); |
121
|
|
|
$set = new DMSDocumentSet; |
122
|
|
|
$set->addQueryFields($fields); |
123
|
|
|
|
124
|
|
|
$this->assertNotNull($fields->dataFieldByName('ExtendedField'), |
|
|
|
|
125
|
|
|
'addQueryFields() is extendible as it included the field from the extension'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Test that extra documents are added after write |
130
|
|
|
*/ |
131
|
|
|
public function testSaveLinkedDocuments(){ |
132
|
|
|
/** @var DMSDocumentSet $set */ |
133
|
|
|
$set = $this->objFromFixture('DMSDocumentSet','ds6'); |
134
|
|
|
|
135
|
|
|
// Assert initially docs |
136
|
|
|
$this->assertEquals(1, $set->getDocuments()->count(), 'Set 6 has 1 document'); |
|
|
|
|
137
|
|
|
|
138
|
|
|
// Now apply the query and see if 2 extras were added with CreatedByID filter |
139
|
|
|
$set->KeyValuePairs = '{"CreatedByID":["1"]}'; |
140
|
|
|
$set->write(); |
141
|
|
|
$this->assertEquals(3, $set->getDocuments()->count(), 'Set 6 has 3 documents'); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
class DMSDocument_Mock extends DMSDocument implements TestOnly |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
private static $db = array( |
|
|
|
|
148
|
|
|
'IsImported' => 'Boolean', |
149
|
|
|
'ISBN' => 'Varchar', |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
private static $searchable_fields = array( |
|
|
|
|
153
|
|
|
'IsImported', |
154
|
|
|
'ISBN', |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
class StubDocumentSetMockExtension extends DataExtension implements TestOnly |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
/** |
161
|
|
|
* |
162
|
|
|
* For method {@link DMSDocumentSet::addQueryFields} |
163
|
|
|
* |
164
|
|
|
* @param FieldList $fields |
165
|
|
|
* |
166
|
|
|
* @return FieldList |
167
|
|
|
*/ |
168
|
|
|
public function updateQueryFields($fields) |
169
|
|
|
{ |
170
|
|
|
$fields->addFieldToTab('Root.QueryBuilder', new TextField('ExtendedField')); |
171
|
|
|
|
172
|
|
|
return $fields; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
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.