1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Tests\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Controller\AssetAdmin; |
6
|
|
|
use SilverStripe\AssetAdmin\Extensions\CampaignAdminExtension; |
7
|
|
|
use SilverStripe\AssetAdmin\Forms\FileFormFactory; |
8
|
|
|
use SilverStripe\AssetAdmin\Forms\FolderFormFactory; |
9
|
|
|
use SilverStripe\AssetAdmin\Forms\ImageFormFactory; |
10
|
|
|
use SilverStripe\AssetAdmin\Tests\Forms\FileFormBuilderTest\FileExtension; |
11
|
|
|
use Silverstripe\Assets\Dev\TestAssetStore; |
12
|
|
|
use SilverStripe\Assets\File; |
13
|
|
|
use SilverStripe\Assets\Folder; |
14
|
|
|
use SilverStripe\Assets\Image; |
15
|
|
|
use SilverStripe\Core\Config\Config; |
16
|
|
|
use SilverStripe\Dev\SapphireTest; |
17
|
|
|
use SilverStripe\Forms\LiteralField; |
18
|
|
|
|
19
|
|
|
class FileFormBuilderTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
protected $usesTransactions = false; |
22
|
|
|
|
23
|
|
|
protected static $fixture_file = 'FileFormBuilderTest.yml'; |
24
|
|
|
|
25
|
|
|
protected static $extra_dataobjects = [ |
26
|
|
|
FileFormBuilderTest\FileOwner::class, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
protected function setUp() : void |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
// Dynamically assign fileowner as owner (otherwise it pollutes other tests) |
34
|
|
|
FileFormBuilderTest\FileOwner::config()->set('owns', ['OwnedFile']); |
35
|
|
|
|
36
|
|
|
// Set backend and base url |
37
|
|
|
TestAssetStore::activate('FileFormBuilderTest'); |
38
|
|
|
|
39
|
|
|
/** @var File $testfile */ |
40
|
|
|
$testfile = $this->objFromFixture(File::class, 'file1'); |
41
|
|
|
$testfile->setFromLocalFile(__DIR__ . '/fixtures/testfile.txt', 'files/testfile.txt'); |
42
|
|
|
$testfile->write(); |
43
|
|
|
|
44
|
|
|
/** @var Image $testimage */ |
45
|
|
|
$testimage = $this->objFromFixture(Image::class, 'image1'); |
46
|
|
|
$testimage->setFromLocalFile(__DIR__ . '/fixtures/testimage.png', 'files/testimage.png'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function tearDown() : void |
50
|
|
|
{ |
51
|
|
|
TestAssetStore::reset(); |
52
|
|
|
parent::tearDown(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testEditFileForm() |
56
|
|
|
{ |
57
|
|
|
// Ensure campaign-admin extension is not applied! |
58
|
|
|
Config::modify()->remove(FileFormFactory::class, 'extensions'); |
59
|
|
|
|
60
|
|
|
$this->logInWithPermission('ADMIN'); |
61
|
|
|
|
62
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
63
|
|
|
$controller = new AssetAdmin(); |
64
|
|
|
$builder = new FileFormFactory(); |
65
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]); |
66
|
|
|
|
67
|
|
|
// Verify file form is scaffolded correctly |
68
|
|
|
$this->assertEquals('EditForm', $form->getName()); |
69
|
|
|
|
70
|
|
|
// Test fields exist |
71
|
|
|
/** @var LiteralField $fileSpecsField */ |
72
|
|
|
$fileSpecsField = $form->Fields()->fieldByName('FileSpecs'); |
73
|
|
|
$fileSpecs = $fileSpecsField->getContent(); |
74
|
|
|
$this->assertEquals( |
75
|
|
|
'<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>', |
76
|
|
|
$fileSpecs |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
/** @var LiteralField $iconFullField */ |
80
|
|
|
$iconFullField = $form->Fields()->fieldByName('PreviewImage'); |
81
|
|
|
$state = $iconFullField->getSchemaStateDefaults(); |
82
|
|
|
$this->assertEquals($file->Parent()->ID, $state['data']['parentid']); |
83
|
|
|
$this->assertStringContainsString('testfile.txt', $state['data']['url']); |
84
|
|
|
$this->assertTrue($state['data']['exists']); |
85
|
|
|
$this->assertStringContainsString('document_92.png', $state['data']['preview']); |
86
|
|
|
$this->assertEquals('document', $state['data']['category']); |
87
|
|
|
|
88
|
|
|
$uploaded = $form->Fields()->fieldByName('Editor.Details.Created'); |
89
|
|
|
$this->assertEquals( |
90
|
|
|
$file->Created, |
91
|
|
|
$uploaded->dataValue() |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
// Test actions exist |
95
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('Actions.action_save')); |
96
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('Actions.action_publish')); |
97
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_replacefile')); |
98
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
99
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
100
|
|
|
|
101
|
|
|
// Add to campaign should not be there by default |
102
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
103
|
|
|
|
104
|
|
|
// Add extension for campaign-admin |
105
|
|
|
Config::modify()->merge( |
106
|
|
|
FileFormFactory::class, |
107
|
|
|
'extensions', |
108
|
|
|
[CampaignAdminExtension::class] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$builder = new FileFormFactory(); |
112
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]); |
113
|
|
|
|
114
|
|
|
// Add to campaign should now be available |
115
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testEditFileFormWithPermissions() |
119
|
|
|
{ |
120
|
|
|
// Add extension for campaign-admin |
121
|
|
|
FileFormFactory::add_extension(CampaignAdminExtension::class); |
122
|
|
|
// Add extension to simulate different permissions |
123
|
|
|
File::add_extension(FileExtension::class); |
124
|
|
|
|
125
|
|
|
$this->logInWithPermission('ADMIN'); |
126
|
|
|
|
127
|
|
|
/** @var File $file */ |
128
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
129
|
|
|
$controller = new AssetAdmin(); |
130
|
|
|
$builder = new FileFormFactory(); |
131
|
|
|
|
132
|
|
|
FileExtension::$canDelete = false; |
133
|
|
|
FileExtension::$canPublish = false; |
134
|
|
|
FileExtension::$canEdit = false; |
135
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]); |
136
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions')); |
137
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
138
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile')); |
139
|
|
|
|
140
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
141
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
142
|
|
|
|
143
|
|
|
FileExtension::$canDelete = false; |
144
|
|
|
FileExtension::$canPublish = true; |
145
|
|
|
FileExtension::$canEdit = false; |
146
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]); |
147
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
148
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile')); |
149
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
150
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
151
|
|
|
|
152
|
|
|
FileExtension::$canDelete = true; |
153
|
|
|
FileExtension::$canPublish = false; |
154
|
|
|
FileExtension::$canEdit = false; |
155
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
156
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
157
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile')); |
158
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
159
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
160
|
|
|
|
161
|
|
|
FileExtension::$canDelete = false; |
162
|
|
|
FileExtension::$canPublish = false; |
163
|
|
|
FileExtension::$canEdit = true; |
164
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
165
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
166
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_replacefile')); |
167
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
168
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
169
|
|
|
|
170
|
|
|
FileExtension::$canDelete = true; |
171
|
|
|
FileExtension::$canPublish = true; |
172
|
|
|
FileExtension::$canUnpublish = true; |
173
|
|
|
FileExtension::$canEdit = true; |
174
|
|
|
$file->publishSingle(); |
175
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]); |
176
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
177
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_replacefile')); |
178
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
179
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
180
|
|
|
|
181
|
|
|
FileFormFactory::remove_extension(CampaignAdminExtension::class); |
182
|
|
|
File::remove_extension(FileExtension::class); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Ensure unpublish has owner count |
187
|
|
|
*/ |
188
|
|
|
public function testUnpublishOwners() |
189
|
|
|
{ |
190
|
|
|
$this->logInWithPermission('ADMIN'); |
191
|
|
|
|
192
|
|
|
// Publish one of the owners |
193
|
|
|
/** @var FileFormBuilderTest\FileOwner $owner1 */ |
194
|
|
|
$owner1 = $this->objFromFixture(FileFormBuilderTest\FileOwner::class, 'owner1'); |
195
|
|
|
$owner1->publishSingle(); |
|
|
|
|
196
|
|
|
|
197
|
|
|
/** @var File $file */ |
198
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
199
|
|
|
$file->publishSingle(); |
200
|
|
|
|
201
|
|
|
// Build new form |
202
|
|
|
$controller = new AssetAdmin(); |
203
|
|
|
$builder = new FileFormFactory(); |
204
|
|
|
|
205
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]); |
206
|
|
|
$unpublishAction = $form->Actions()->fieldByName('PopoverActions.action_unpublish'); |
207
|
|
|
$this->assertNotNull($unpublishAction); |
208
|
|
|
$this->assertEquals(1, $unpublishAction->getSchemaData()['data']['owners']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testCreateFileForm() |
212
|
|
|
{ |
213
|
|
|
$this->logInWithPermission('ADMIN'); |
214
|
|
|
|
215
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
216
|
|
|
$controller = new AssetAdmin(); |
217
|
|
|
$builder = new FileFormFactory(); |
218
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]); |
219
|
|
|
|
220
|
|
|
// Test fields |
221
|
|
|
/** @var LiteralField $fileSpecsField */ |
222
|
|
|
$fileSpecsField = $form->Fields()->fieldByName('FileSpecs'); |
223
|
|
|
$this->assertEquals( |
224
|
|
|
'<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>', |
225
|
|
|
$fileSpecsField->getContent() |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
// Test actions |
229
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('Actions.action_save')); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testEditImageForm() |
233
|
|
|
{ |
234
|
|
|
$this->logInWithPermission('ADMIN'); |
235
|
|
|
|
236
|
|
|
$image = $this->objFromFixture(Image::class, 'image1'); |
237
|
|
|
// write so that PreviewImageField could load this later on |
238
|
|
|
$image->write(); |
239
|
|
|
$controller = new AssetAdmin(); |
240
|
|
|
$builder = new ImageFormFactory(); |
241
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $image]); |
242
|
|
|
|
243
|
|
|
// Check thumbnail |
244
|
|
|
// Note: force_resample is turned off for testing |
245
|
|
|
/** @var LiteralField $iconFullField */ |
246
|
|
|
$iconFullField = $form->Fields()->fieldByName('PreviewImage'); |
247
|
|
|
$state = $iconFullField->getSchemaStateDefaults(); |
248
|
|
|
$this->assertEquals($image->Parent()->ID, $state['data']['parentid']); |
249
|
|
|
$this->assertStringContainsString('testimage.png', $state['data']['url']); |
250
|
|
|
$this->assertTrue($state['data']['exists']); |
251
|
|
|
$this->assertRegexp('#testimage__.*\.png$#', $state['data']['preview']); |
252
|
|
|
$this->assertEquals('image', $state['data']['category']); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testInsertImageForm() |
256
|
|
|
{ |
257
|
|
|
$this->logInWithPermission('CMS_ACCESS'); |
258
|
|
|
|
259
|
|
|
$image = $this->objFromFixture(Image::class, 'image1'); |
260
|
|
|
$controller = new AssetAdmin(); |
261
|
|
|
$builder = new ImageFormFactory(); |
262
|
|
|
$form = $builder->getForm( |
263
|
|
|
$controller, |
264
|
|
|
'EditForm', |
265
|
|
|
['Record' => $image, 'Type' => FileFormFactory::TYPE_INSERT_MEDIA] |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
// Check thumbnail |
269
|
|
|
// Note: force_resample is turned off for testing |
270
|
|
|
$altTextField = $form->Fields()->dataFieldByName('AltText'); |
271
|
|
|
$this->assertNotNull($altTextField); |
272
|
|
|
|
273
|
|
|
// Ensure "insert" button exists |
274
|
|
|
$this->assertTrue($image->canView()); |
275
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_insert')); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testInsertImageFormDenied() |
279
|
|
|
{ |
280
|
|
|
$this->logInWithPermission('SOME_PERMISSION'); |
281
|
|
|
|
282
|
|
|
$image = $this->objFromFixture(Image::class, 'image1'); |
283
|
|
|
$controller = new AssetAdmin(); |
284
|
|
|
$builder = new ImageFormFactory(); |
285
|
|
|
$form = $builder->getForm( |
286
|
|
|
$controller, |
287
|
|
|
'EditForm', |
288
|
|
|
['Record' => $image, 'Type' => FileFormFactory::TYPE_INSERT_MEDIA] |
289
|
|
|
); |
290
|
|
|
|
291
|
|
|
// Ensure "insert" button does not exist |
292
|
|
|
$this->assertFalse($image->canView()); |
293
|
|
|
$this->assertNull($form->Actions()->fieldByName('action_insert')); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function testInsertFileForm() |
297
|
|
|
{ |
298
|
|
|
$this->logInWithPermission('CMS_ACCESS'); |
299
|
|
|
|
300
|
|
|
$image = $this->objFromFixture(Image::class, 'image1'); |
301
|
|
|
$controller = new AssetAdmin(); |
302
|
|
|
$builder = new ImageFormFactory(); |
303
|
|
|
$form = $builder->getForm( |
304
|
|
|
$controller, |
305
|
|
|
'EditForm', |
306
|
|
|
['Record' => $image, 'Type' => FileFormFactory::TYPE_INSERT_LINK, 'RequireLinkText' => false] |
307
|
|
|
); |
308
|
|
|
|
309
|
|
|
// Ensure form contains correct fields |
310
|
|
|
$this->assertNotNull($form->Fields()->dataFieldByName('Description')); |
311
|
|
|
$this->assertNotNull($form->Fields()->dataFieldByName('TargetBlank')); |
312
|
|
|
|
313
|
|
|
// Don't show dimension tags |
314
|
|
|
$this->assertNull($form->Fields()->dataFieldByName('InsertWidth')); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function testFolderForm() |
318
|
|
|
{ |
319
|
|
|
$this->logInWithPermission('ADMIN'); |
320
|
|
|
|
321
|
|
|
$folder = $this->objFromFixture(Folder::class, 'parent'); |
322
|
|
|
$controller = new AssetAdmin(); |
323
|
|
|
$builder = new FolderFormFactory(); |
324
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $folder]); |
325
|
|
|
|
326
|
|
|
// Check fields |
327
|
|
|
$this->assertNull($form->Fields()->fieldByName('FileSpecs')); |
328
|
|
|
$this->assertNull($form->Fields()->fieldByName('Editor.Details.ClickableURL')); |
329
|
|
|
$this->assertNull($form->Fields()->fieldByName('Editor.Usage')); |
330
|
|
|
|
331
|
|
|
/** @var LiteralField $iconFullField */ |
332
|
|
|
$iconFullField = $form->Fields()->fieldByName('PreviewImage'); |
333
|
|
|
$state = $iconFullField->getSchemaStateDefaults(); |
334
|
|
|
$this->assertEquals($folder->Parent()->ID, $state['data']['parentid']); |
335
|
|
|
$this->assertTrue($state['data']['exists']); |
336
|
|
|
$this->assertStringContainsString('folder_icon_large.png', $state['data']['preview']); |
337
|
|
|
$this->assertEquals('folder', $state['data']['category']); |
338
|
|
|
|
339
|
|
|
// Check actions |
340
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_save')); |
341
|
|
|
$this->assertNotNull($form->Actions()->dataFieldByName('action_delete')); |
342
|
|
|
$this->assertNull($form->Actions()->fieldByName('action_publish')); |
343
|
|
|
$this->assertNull($form->Actions()->dataFieldByName('action_publish')); |
344
|
|
|
$this->assertNull($form->Actions()->dataFieldByName('action_unpublish')); |
345
|
|
|
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile')); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function testScaffolderFactory() |
349
|
|
|
{ |
350
|
|
|
$controller = new AssetAdmin(); |
351
|
|
|
$this->assertInstanceOf(FileFormFactory::class, $controller->getFormFactory(File::singleton())); |
352
|
|
|
$this->assertInstanceOf(ImageFormFactory::class, $controller->getFormFactory(Image::singleton())); |
353
|
|
|
$this->assertInstanceOf(FolderFormFactory::class, $controller->getFormFactory(Folder::singleton())); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|