These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\File; |
||
12 | use SilverStripe\Assets\Folder; |
||
13 | use SilverStripe\Assets\Image; |
||
14 | use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore; |
||
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 static $fixture_file = 'FileFormBuilderTest.yml'; |
||
22 | |||
23 | View Code Duplication | public function setUp() |
|
24 | { |
||
25 | parent::setUp(); |
||
26 | |||
27 | // Set backend and base url |
||
28 | TestAssetStore::activate('FileFormBuilderTest'); |
||
29 | |||
30 | /** @var File $testfile */ |
||
31 | $testfile = $this->objFromFixture(File::class, 'file1'); |
||
32 | $testfile->setFromLocalFile(__DIR__ .'/fixtures/testfile.txt', 'files/testfile.txt'); |
||
33 | $testfile->write(); |
||
34 | |||
35 | /** @var Image $testimage */ |
||
36 | $testimage = $this->objFromFixture(Image::class, 'image1'); |
||
37 | $testimage->setFromLocalFile(__DIR__.'/fixtures/testimage.png', 'files/testimage.png'); |
||
38 | } |
||
39 | |||
40 | public function tearDown() |
||
41 | { |
||
42 | TestAssetStore::reset(); |
||
43 | parent::tearDown(); |
||
44 | } |
||
45 | |||
46 | public function testEditFileForm() |
||
47 | { |
||
48 | // Ensure campaign-admin extension is not applied! |
||
49 | Config::modify()->remove(FileFormFactory::class, 'extensions'); |
||
50 | |||
51 | $this->logInWithPermission('ADMIN'); |
||
52 | |||
53 | $file = $this->objFromFixture(File::class, 'file1'); |
||
54 | $controller = new AssetAdmin(); |
||
55 | $builder = new FileFormFactory(); |
||
56 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
||
57 | |||
58 | // Verify file form is scaffolded correctly |
||
59 | $this->assertEquals('EditForm', $form->getName()); |
||
60 | |||
61 | // Test fields exist |
||
62 | /** @var LiteralField $fileSpecsField */ |
||
63 | $fileSpecsField = $form->Fields()->fieldByName('FileSpecs'); |
||
64 | $fileSpecs = $fileSpecsField->getContent(); |
||
65 | $this->assertEquals( |
||
66 | '<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>', |
||
67 | $fileSpecs |
||
68 | ); |
||
69 | $filePath = $form->Fields()->fieldByName('Editor.Details.Path')->Value(); |
||
70 | $this->assertEquals('files/', $filePath); |
||
71 | |||
72 | /** @var LiteralField $iconFullField */ |
||
73 | $iconFullField = $form->Fields()->fieldByName('PreviewImage'); |
||
74 | $state = $iconFullField->getSchemaStateDefaults(); |
||
75 | $this->assertEquals($file->Parent()->ID, $state['data']['parentid']); |
||
76 | $this->assertContains('testfile.txt', $state['data']['url']); |
||
77 | $this->assertTrue($state['data']['exists']); |
||
78 | $this->assertContains('document_92.png', $state['data']['preview']); |
||
79 | $this->assertEquals('document', $state['data']['category']); |
||
80 | |||
81 | // Usage tab |
||
82 | $uploaded = $form->Fields()->fieldByName('Editor.Usage.Created'); |
||
83 | $this->assertEquals( |
||
84 | $file->Created, |
||
85 | $uploaded->dataValue() |
||
86 | ); |
||
87 | |||
88 | // Test actions exist |
||
89 | $this->assertNotNull($form->Actions()->fieldByName('Actions.action_save')); |
||
90 | $this->assertNotNull($form->Actions()->fieldByName('Actions.action_publish')); |
||
91 | $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
||
92 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
||
93 | |||
94 | // Add to campaign should not be there by default |
||
95 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
||
96 | |||
97 | // Add extension for campaign-admin |
||
98 | Config::modify()->merge( |
||
99 | FileFormFactory::class, |
||
100 | 'extensions', |
||
101 | [ CampaignAdminExtension::class ] |
||
102 | ); |
||
103 | |||
104 | $builder = new FileFormFactory(); |
||
105 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
||
106 | |||
107 | // Add to campaign should now be available |
||
108 | $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
||
109 | } |
||
110 | |||
111 | public function testEditFileFormWithPermissions() |
||
112 | { |
||
113 | // Add extension for campaign-admin |
||
114 | FileFormFactory::add_extension(CampaignAdminExtension::class); |
||
115 | // Add extension to simulate different permissions |
||
116 | File::add_extension(FileExtension::class); |
||
117 | |||
118 | $this->logInWithPermission('ADMIN'); |
||
119 | |||
120 | /** @var File $file */ |
||
121 | $file = $this->objFromFixture(File::class, 'file1'); |
||
122 | $controller = new AssetAdmin(); |
||
123 | $builder = new FileFormFactory(); |
||
124 | |||
125 | FileExtension::$canDelete = false; |
||
126 | FileExtension::$canPublish = false; |
||
127 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
||
128 | $this->assertNull($form->Actions()->fieldByName('PopoverActions')); |
||
129 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
||
130 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
||
131 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
||
132 | |||
133 | FileExtension::$canDelete = false; |
||
134 | FileExtension::$canPublish = true; |
||
135 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
||
136 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
||
137 | $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
||
138 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
||
139 | |||
140 | FileExtension::$canDelete = true; |
||
141 | FileExtension::$canPublish = false; |
||
142 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
||
143 | $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
||
144 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
||
145 | $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
||
146 | |||
147 | FileExtension::$canDelete = true; |
||
148 | FileExtension::$canPublish = true; |
||
149 | FileExtension::$canUnpublish = true; |
||
150 | $file->publishSingle(); |
||
151 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
||
152 | $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete')); |
||
153 | $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign')); |
||
154 | $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_unpublish')); |
||
155 | |||
156 | FileFormFactory::remove_extension(CampaignAdminExtension::class); |
||
157 | File::remove_extension(FileExtension::class); |
||
158 | } |
||
159 | |||
160 | public function testCreateFileForm() |
||
161 | { |
||
162 | $this->logInWithPermission('ADMIN'); |
||
163 | |||
164 | $file = $this->objFromFixture(File::class, 'file1'); |
||
165 | $controller = new AssetAdmin(); |
||
166 | $builder = new FileFormFactory(); |
||
167 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]); |
||
168 | |||
169 | // Test fields |
||
170 | /** @var LiteralField $fileSpecsField */ |
||
171 | $fileSpecsField = $form->Fields()->fieldByName('FileSpecs'); |
||
172 | $this->assertEquals( |
||
173 | '<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>', |
||
174 | $fileSpecsField->getContent() |
||
175 | ); |
||
176 | $this->assertEquals( |
||
177 | 'files/', |
||
178 | $form->Fields()->fieldByName('Editor.Details.Path')->dataValue() |
||
179 | ); |
||
180 | |||
181 | // Test actions |
||
182 | $this->assertNotNull($form->Actions()->fieldByName('Actions.action_save')); |
||
183 | } |
||
184 | |||
185 | public function testEditImageForm() |
||
186 | { |
||
187 | $this->logInWithPermission('ADMIN'); |
||
188 | |||
189 | $image = $this->objFromFixture(Image::class, 'image1'); |
||
190 | // write so that PreviewImageField could load this later on |
||
191 | $image->write(); |
||
192 | $controller = new AssetAdmin(); |
||
193 | $builder = new ImageFormFactory(); |
||
194 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $image]); |
||
195 | |||
196 | // Check thumbnail |
||
197 | // Note: force_resample is turned off for testing |
||
198 | /** @var LiteralField $iconFullField */ |
||
199 | $iconFullField = $form->Fields()->fieldByName('PreviewImage'); |
||
200 | $state = $iconFullField->getSchemaStateDefaults(); |
||
201 | $this->assertEquals($image->Parent()->ID, $state['data']['parentid']); |
||
202 | $this->assertContains('testimage.png', $state['data']['url']); |
||
203 | $this->assertTrue($state['data']['exists']); |
||
204 | $this->assertContains('testimage.png', $state['data']['preview']); |
||
205 | $this->assertEquals('image', $state['data']['category']); |
||
206 | } |
||
207 | |||
208 | public function testInsertImageForm() |
||
209 | { |
||
210 | $this->logInWithPermission('ADMIN'); |
||
211 | |||
212 | $image = $this->objFromFixture(Image::class, 'image1'); |
||
213 | $controller = new AssetAdmin(); |
||
214 | $builder = new ImageFormFactory(); |
||
215 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $image, 'Type' => FileFormFactory::TYPE_INSERT_MEDIA]); |
||
216 | |||
217 | // Check thumbnail |
||
218 | // Note: force_resample is turned off for testing |
||
219 | $altTextField = $form->Fields()->dataFieldByName('AltText'); |
||
220 | $this->assertNotNull($altTextField); |
||
221 | } |
||
222 | |||
223 | public function testInsertFileForm() |
||
224 | { |
||
225 | $this->logInWithPermission('ADMIN'); |
||
226 | |||
227 | $image = $this->objFromFixture(Image::class, 'image1'); |
||
228 | $controller = new AssetAdmin(); |
||
229 | $builder = new ImageFormFactory(); |
||
230 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $image, 'Type' => FileFormFactory::TYPE_INSERT_LINK]); |
||
231 | |||
232 | // Ensure form contains correct fields |
||
233 | $this->assertNotNull($form->Fields()->dataFieldByName('Description')); |
||
0 ignored issues
–
show
|
|||
234 | $this->assertNotNull($form->Fields()->dataFieldByName('TargetBlank')); |
||
0 ignored issues
–
show
The method
assertNotNull() does not seem to exist on object<SilverStripe\Asse...ms\FileFormBuilderTest> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
235 | |||
236 | // Don't show dimension tags |
||
237 | $this->assertNull($form->Fields()->dataFieldByName('InsertWidth')); |
||
0 ignored issues
–
show
The method
assertNull() does not seem to exist on object<SilverStripe\Asse...ms\FileFormBuilderTest> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
238 | } |
||
239 | |||
240 | public function testFolderForm() |
||
241 | { |
||
242 | $this->logInWithPermission('ADMIN'); |
||
243 | |||
244 | $folder = $this->objFromFixture(Folder::class, 'parent'); |
||
245 | $controller = new AssetAdmin(); |
||
246 | $builder = new FolderFormFactory(); |
||
247 | $form = $builder->getForm($controller, 'EditForm', ['Record' => $folder]); |
||
248 | |||
249 | // Check fields |
||
250 | $this->assertNull($form->Fields()->fieldByName('FileSpecs')); |
||
251 | $this->assertNull($form->Fields()->fieldByName('Editor.Details.ClickableURL')); |
||
252 | $this->assertNull($form->Fields()->fieldByName('Editor.Usage')); |
||
253 | |||
254 | /** @var LiteralField $iconFullField */ |
||
255 | $iconFullField = $form->Fields()->fieldByName('PreviewImage'); |
||
256 | $state = $iconFullField->getSchemaStateDefaults(); |
||
257 | $this->assertEquals($folder->Parent()->ID, $state['data']['parentid']); |
||
258 | $this->assertTrue($state['data']['exists']); |
||
259 | $this->assertContains('folder_icon_large.png', $state['data']['preview']); |
||
260 | $this->assertEquals('folder', $state['data']['category']); |
||
261 | |||
262 | // Check actions |
||
263 | $this->assertNotNull($form->Actions()->fieldByName('action_save')); |
||
264 | $this->assertNotNull($form->Actions()->dataFieldByName('action_delete')); |
||
265 | $this->assertNull($form->Actions()->fieldByName('action_publish')); |
||
266 | $this->assertNull($form->Actions()->dataFieldByName('action_publish')); |
||
267 | $this->assertNull($form->Actions()->dataFieldByName('action_unpublish')); |
||
268 | } |
||
269 | |||
270 | public function testScaffolderFactory() |
||
271 | { |
||
272 | $controller = new AssetAdmin(); |
||
273 | $this->assertInstanceOf(FileFormFactory::class, $controller->getFormFactory(File::singleton())); |
||
274 | $this->assertInstanceOf(ImageFormFactory::class, $controller->getFormFactory(Image::singleton())); |
||
275 | $this->assertInstanceOf(FolderFormFactory::class, $controller->getFormFactory(Folder::singleton())); |
||
276 | } |
||
277 | } |
||
278 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.