1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Controller\AssetAdmin; |
6
|
|
|
use SilverStripe\AssetAdmin\Forms\FileFormFactory; |
7
|
|
|
use SilverStripe\AssetAdmin\Forms\FolderFormFactory; |
8
|
|
|
use SilverStripe\AssetAdmin\Forms\ImageFormFactory; |
9
|
|
|
use SilverStripe\Assets\File; |
10
|
|
|
use SilverStripe\Assets\Folder; |
11
|
|
|
use SilverStripe\Assets\Image; |
12
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore; |
13
|
|
|
use SilverStripe\Dev\SapphireTest; |
14
|
|
|
use SilverStripe\Forms\LiteralField; |
15
|
|
|
|
16
|
|
|
class FileFormBuilderTest extends SapphireTest |
17
|
|
|
{ |
18
|
|
|
protected static $fixture_file = 'FileFormBuilderTest.yml'; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
// Set backend and base url |
25
|
|
|
TestAssetStore::activate('FileFormBuilderTest'); |
26
|
|
|
|
27
|
|
|
/** @var File $testfile */ |
28
|
|
|
$testfile = $this->objFromFixture(File::class, 'file1'); |
29
|
|
|
$testfile->setFromLocalFile(__DIR__ .'/fixtures/testfile.txt', 'files/testfile.txt'); |
30
|
|
|
$testfile->write(); |
31
|
|
|
|
32
|
|
|
/** @var Image $testimage */ |
33
|
|
|
$testimage = $this->objFromFixture(Image::class, 'image1'); |
34
|
|
|
$testimage->setFromLocalFile(__DIR__.'/fixtures/testimage.png', 'files/testimage.png'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testEditFileForm() |
38
|
|
|
{ |
39
|
|
|
$this->logInWithPermission('ADMIN'); |
40
|
|
|
|
41
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
42
|
|
|
$controller = new AssetAdmin(); |
43
|
|
|
$builder = new FileFormFactory(); |
44
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
45
|
|
|
|
46
|
|
|
// Verify file form is scaffolded correctly |
47
|
|
|
$this->assertEquals('EditForm', $form->getName()); |
48
|
|
|
|
49
|
|
|
// Test fields exist |
50
|
|
|
/** @var LiteralField $fileSpecsField */ |
51
|
|
|
$fileSpecsField = $form->Fields()->fieldByName('FileSpecs'); |
52
|
|
|
$fileSpecs = $fileSpecsField->getContent(); |
53
|
|
|
$this->assertEquals( |
54
|
|
|
'<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>', |
55
|
|
|
$fileSpecs |
56
|
|
|
); |
57
|
|
|
$filePath = $form->Fields()->fieldByName('Editor.Details.Path')->Value(); |
58
|
|
|
$this->assertEquals('files/', $filePath); |
59
|
|
|
|
60
|
|
|
/** @var LiteralField $iconFullField */ |
61
|
|
|
$iconFullField = $form->Fields()->fieldByName('IconFull'); |
62
|
|
|
$fileThumbnail = $iconFullField->getContent(); |
63
|
|
|
$this->assertEquals( |
64
|
|
|
'<a class="editor__file-preview-link" href="/assets/files/6adf67caca/testfile.txt" target="_blank">'. |
65
|
|
|
'<img src="framework/client/dist/images/app_icons/document_92.png" class="editor__thumbnail" />'. |
66
|
|
|
'</a>', |
67
|
|
|
$fileThumbnail |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
// Usage tab |
71
|
|
|
$uploaded = $form->Fields()->fieldByName('Editor.Usage.Created'); |
72
|
|
|
$this->assertEquals( |
73
|
|
|
$file->Created, |
74
|
|
|
$uploaded->dataValue() |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
// Test actions exist |
78
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_save')); |
79
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_publish')); |
80
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_addtocampaign')); |
81
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_delete')); |
82
|
|
|
$this->assertNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_unpublish')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testCreateFileForm() |
86
|
|
|
{ |
87
|
|
|
$this->logInWithPermission('ADMIN'); |
88
|
|
|
|
89
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
90
|
|
|
$controller = new AssetAdmin(); |
91
|
|
|
$builder = new FileFormFactory(); |
92
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
93
|
|
|
|
94
|
|
|
// Test fields |
95
|
|
|
/** @var LiteralField $fileSpecsField */ |
96
|
|
|
$fileSpecsField = $form->Fields()->fieldByName('FileSpecs'); |
97
|
|
|
$this->assertEquals( |
98
|
|
|
'<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>', |
99
|
|
|
$fileSpecsField->getContent() |
100
|
|
|
); |
101
|
|
|
$this->assertEquals( |
102
|
|
|
'files/', |
103
|
|
|
$form->Fields()->fieldByName('Editor.Details.Path')->dataValue() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
// Test actions |
107
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_save')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function testEditImageForm() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$this->logInWithPermission('ADMIN'); |
113
|
|
|
|
114
|
|
|
$image = $this->objFromFixture(Image::class, 'image1'); |
115
|
|
|
$controller = new AssetAdmin(); |
116
|
|
|
$builder = new ImageFormFactory(); |
117
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $image]); |
118
|
|
|
|
119
|
|
|
// Check thumbnail |
120
|
|
|
// Note: force_resample is turned off for testing |
121
|
|
|
/** @var LiteralField $iconFullField */ |
122
|
|
|
$iconFullField = $form->Fields()->fieldByName('IconFull'); |
123
|
|
|
$fileThumbnail = $iconFullField->getContent(); |
124
|
|
|
$this->assertContains( |
125
|
|
|
'/FileFormBuilderTest/files/906835357d/testimage.png', |
126
|
|
|
$fileThumbnail |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function testInsertImageForm() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$this->logInWithPermission('ADMIN'); |
133
|
|
|
|
134
|
|
|
$image = $this->objFromFixture(Image::class, 'image1'); |
135
|
|
|
$controller = new AssetAdmin(); |
136
|
|
|
$builder = new ImageFormFactory(); |
137
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $image, 'Type' => 'insert']); |
138
|
|
|
|
139
|
|
|
// Check thumbnail |
140
|
|
|
// Note: force_resample is turned off for testing |
141
|
|
|
$altTextField = $form->Fields()->dataFieldByName('AltText'); |
142
|
|
|
$this->assertNotNull($altTextField); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testFolderForm() |
146
|
|
|
{ |
147
|
|
|
$this->logInWithPermission('ADMIN'); |
148
|
|
|
|
149
|
|
|
$folder = $this->objFromFixture(Folder::class, 'parent'); |
150
|
|
|
$controller = new AssetAdmin(); |
151
|
|
|
$builder = new FolderFormFactory($controller, $folder); |
|
|
|
|
152
|
|
|
$form = $builder->getForm($controller, 'EditForm', ['Record' => $folder]); |
153
|
|
|
|
154
|
|
|
// Check fields |
155
|
|
|
$this->assertNull($form->Fields()->fieldByName('FileSpecs')); |
156
|
|
|
$this->assertNull($form->Fields()->fieldByName('Editor.Details.ClickableURL')); |
157
|
|
|
$this->assertNull($form->Fields()->fieldByName('Editor.Usage')); |
158
|
|
|
|
159
|
|
|
/** @var LiteralField $iconField */ |
160
|
|
|
$iconField = $form->Fields()->fieldByName('IconFull'); |
161
|
|
|
$fileThumbnail = $iconField->getContent(); |
162
|
|
|
$this->assertEquals( |
163
|
|
|
'<img src="framework/client/dist/images/app_icons/folder_icon_large.png" class="editor__thumbnail" />', |
164
|
|
|
$fileThumbnail |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
// Check actions |
169
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_save')); |
170
|
|
|
$this->assertNotNull($form->Actions()->fieldByName('action_delete')); // At top level, not in popup |
171
|
|
|
$this->assertNull($form->Actions()->fieldByName('action_publish')); |
172
|
|
|
$this->assertNull($form->Actions()->dataFieldByName('action_publish')); |
173
|
|
|
$this->assertNull($form->Actions()->dataFieldByName('action_unpublish')); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testScaffolderFactory() |
177
|
|
|
{ |
178
|
|
|
$controller = new AssetAdmin(); |
179
|
|
|
$this->assertInstanceOf(FileFormFactory::class, $controller->getFormFactory(File::singleton())); |
180
|
|
|
$this->assertInstanceOf(ImageFormFactory::class, $controller->getFormFactory(Image::singleton())); |
181
|
|
|
$this->assertInstanceOf(FolderFormFactory::class, $controller->getFormFactory(Folder::singleton())); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.