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