|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use AssetStoreTest_SpyStore; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use SilverStripe\AssetAdmin\Controller\AssetAdmin; |
|
8
|
|
|
use SilverStripe\AssetAdmin\Forms\FileFormBuilder; |
|
9
|
|
|
use SilverStripe\AssetAdmin\Forms\FolderFormBuilder; |
|
10
|
|
|
use SilverStripe\AssetAdmin\Forms\ImageFormBuilder; |
|
11
|
|
|
use SilverStripe\Assets\File; |
|
12
|
|
|
use SilverStripe\Assets\Folder; |
|
13
|
|
|
use SilverStripe\Assets\Image; |
|
14
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
15
|
|
|
|
|
16
|
|
|
class FileFormBuilderTest extends SapphireTest |
|
17
|
|
|
{ |
|
18
|
|
|
protected static $fixture_file = 'FileFormBuilderTest.yml'; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp() { |
|
21
|
|
|
parent::setUp(); |
|
22
|
|
|
|
|
23
|
|
|
// Set backend and base url |
|
24
|
|
|
AssetStoreTest_SpyStore::activate('FileFormBuilderTest'); |
|
25
|
|
|
|
|
26
|
|
|
/** @var File $testfile */ |
|
27
|
|
|
$testfile = $this->objFromFixture(File::class, 'file1'); |
|
28
|
|
|
$testfile->setFromLocalFile(__DIR__ .'/fixtures/testfile.txt', 'files/testfile.txt'); |
|
29
|
|
|
$testfile->write(); |
|
30
|
|
|
|
|
31
|
|
|
/** @var Image $testimage */ |
|
32
|
|
|
$testimage = $this->objFromFixture(Image::class, 'image1'); |
|
33
|
|
|
$testimage->setFromLocalFile(__DIR__.'/fixtures/testimage.png', 'files/testimage.png'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testEditFileForm() { |
|
37
|
|
|
$this->logInWithPermission('ADMIN'); |
|
38
|
|
|
|
|
39
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
|
40
|
|
|
$controller = new AssetAdmin(); |
|
41
|
|
|
$builder = new FileFormBuilder($controller, $file); |
|
42
|
|
|
$form = $builder->getForm('EditForm'); |
|
43
|
|
|
|
|
44
|
|
|
// Verify file form is scaffolded correctly |
|
45
|
|
|
$this->assertEquals('EditForm', $form->getName()); |
|
46
|
|
|
|
|
47
|
|
|
// Test fields exist |
|
48
|
|
|
$fileSpecs = $form->Fields()->fieldByName('FileSpecs')->getContent(); |
|
49
|
|
|
$this->assertEquals( |
|
50
|
|
|
'<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>', |
|
51
|
|
|
$fileSpecs |
|
52
|
|
|
); |
|
53
|
|
|
$fileURL = $form->Fields()->fieldByName('Editor.Details.ClickableURL')->Value(); |
|
54
|
|
|
$this->assertEquals( |
|
55
|
|
|
'<i class="font-icon-link btn--icon-large form-control-static__icon"></i>' |
|
56
|
|
|
. '<a href="/assets/files/6adf67caca/testfile.txt" target="_blank">/assets/files/6adf67caca/testfile.txt</a>', |
|
57
|
|
|
$fileURL |
|
58
|
|
|
); |
|
59
|
|
|
$filePath = $form->Fields()->fieldByName('Editor.Details.Path')->Value(); |
|
60
|
|
|
$this->assertEquals('files/', $filePath); |
|
61
|
|
|
|
|
62
|
|
|
$fileThumbnail = $form->Fields()->fieldByName('IconFull')->getContent(); |
|
63
|
|
|
$this->assertEquals( |
|
64
|
|
|
'<img src="framework/client/dist/images/app_icons/generic_32.png" class="editor__thumbnail" />', |
|
65
|
|
|
$fileThumbnail |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
// Usage tab |
|
69
|
|
|
$uploaded = $form->Fields()->fieldByName('Editor.Usage.Created'); |
|
70
|
|
|
$this->assertEquals( |
|
71
|
|
|
$file->Created, |
|
72
|
|
|
$uploaded->dataValue() |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
// Test actions exist |
|
76
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_save')); |
|
77
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_publish')); |
|
78
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_addtocampaign')); |
|
79
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_delete')); |
|
80
|
|
|
$this->assertNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_unpublish')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testCreateFileForm() { |
|
84
|
|
|
$this->logInWithPermission('ADMIN'); |
|
85
|
|
|
|
|
86
|
|
|
$file = File::singleton(); |
|
87
|
|
|
$controller = new AssetAdmin(); |
|
88
|
|
|
$builder = new FileFormBuilder($controller, $file); |
|
89
|
|
|
$form = $builder->getForm('EditForm'); |
|
90
|
|
|
|
|
91
|
|
|
// Test fields |
|
92
|
|
|
$this->assertEmpty($form->Fields()->fieldByName('FileSpecs')->getContent()); |
|
93
|
|
|
$this->assertEmpty($form->Fields()->fieldByName('Editor.Details.Path')->dataValue()); |
|
94
|
|
|
$this->assertEmpty($form->Fields()->fieldByName('Editor.Details.ClickableURL')->dataValue()); |
|
95
|
|
|
|
|
96
|
|
|
// Test actions |
|
97
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_save')); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testEditImageForm() { |
|
101
|
|
|
$this->logInWithPermission('ADMIN'); |
|
102
|
|
|
|
|
103
|
|
|
$image = $this->objFromFixture(Image::class, 'image1'); |
|
104
|
|
|
$controller = new AssetAdmin(); |
|
105
|
|
|
$builder = new FileFormBuilder($controller, $image); |
|
106
|
|
|
$form = $builder->getForm('EditForm'); |
|
107
|
|
|
|
|
108
|
|
|
// Check thumbnail |
|
109
|
|
|
// Note: force_resample is turned off for testing |
|
110
|
|
|
$fileThumbnail = $form->Fields()->fieldByName('IconFull')->getContent(); |
|
111
|
|
|
$this->assertContains( |
|
112
|
|
|
'/FileFormBuilderTest/files/906835357d/testimage.png', |
|
113
|
|
|
$fileThumbnail |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testFolderForm() { |
|
118
|
|
|
$this->logInWithPermission('ADMIN'); |
|
119
|
|
|
|
|
120
|
|
|
$folder = $this->objFromFixture(Folder::class, 'parent'); |
|
121
|
|
|
$controller = new AssetAdmin(); |
|
122
|
|
|
$builder = new FolderFormBuilder($controller, $folder); |
|
123
|
|
|
$form = $builder->getForm('EditForm'); |
|
124
|
|
|
|
|
125
|
|
|
// Check fields |
|
126
|
|
|
$this->assertNull($form->Fields()->fieldByName('FileSpecs')); |
|
127
|
|
|
$this->assertNull($form->Fields()->fieldByName('Editor.Details.ClickableURL')); |
|
128
|
|
|
$this->assertNull($form->Fields()->fieldByName('Editor.Usage')); |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$fileThumbnail = $form->Fields()->fieldByName('IconFull')->getContent(); |
|
132
|
|
|
$this->assertEquals( |
|
133
|
|
|
'<img src="framework/client/dist/images/app_icons/folder_icon_large.png" class="editor__thumbnail" />', |
|
134
|
|
|
$fileThumbnail |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
// Check actions |
|
139
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_save')); |
|
140
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_delete')); // At top level, not in popup |
|
141
|
|
|
$this->assertNull($form->Actions()->fieldByName('action_publish')); |
|
142
|
|
|
$this->assertNull($form->Actions()->dataFieldByName('action_publish')); |
|
143
|
|
|
$this->assertNull($form->Actions()->dataFieldByName('action_unpublish')); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testScaffolderFactory() { |
|
147
|
|
|
$controller = new AssetAdmin(); |
|
148
|
|
|
$this->assertInstanceOf(FileFormBuilder::class, $controller->getFormBuilder(File::singleton())); |
|
149
|
|
|
$this->assertInstanceOf(ImageFormBuilder::class, $controller->getFormBuilder(Image::singleton())); |
|
150
|
|
|
$this->assertInstanceOf(FolderFormBuilder::class, $controller->getFormBuilder(Folder::singleton())); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|