Completed
Pull Request — master (#331)
by
unknown
01:55
created

FileFormBuilderTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 174
Duplicated Lines 27.01 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 12
dl 47
loc 174
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 16 16 1
A tearDown() 0 5 1
A testEditFileForm() 0 47 1
B testCreateFileForm() 0 24 1
A testEditImageForm() 18 19 1
A testInsertImageForm() 13 14 1
B testFolderForm() 0 30 1
A testScaffolderFactory() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\Forms;
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 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 tearDown()
38
    {
39
        TestAssetStore::reset();
40
        parent::tearDown();
41
    }
42
43
    public function testEditFileForm()
44
    {
45
        $this->logInWithPermission('ADMIN');
46
47
        $file = $this->objFromFixture(File::class, 'file1');
48
        $controller = new AssetAdmin();
49
        $builder = new FileFormFactory();
50
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
51
52
        // Verify file form is scaffolded correctly
53
        $this->assertEquals('EditForm', $form->getName());
54
55
        // Test fields exist
56
        /** @var LiteralField $fileSpecsField */
57
        $fileSpecsField = $form->Fields()->fieldByName('FileSpecs');
58
        $fileSpecs = $fileSpecsField->getContent();
59
        $this->assertEquals(
60
            '<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>',
61
            $fileSpecs
62
        );
63
        $filePath = $form->Fields()->fieldByName('Editor.Details.Path')->Value();
64
        $this->assertEquals('files/', $filePath);
65
66
        /** @var LiteralField $iconFullField */
67
        $iconFullField = $form->Fields()->fieldByName('IconFull');
68
        $fileThumbnail = $iconFullField->getContent();
69
        $this->assertEquals(
70
            '<a class="editor__file-preview-link" href="/assets/files/6adf67caca/testfile.txt" target="_blank">'.
71
                '<img src="framework/client/dist/images/app_icons/document_92.png" class="editor__thumbnail" />'.
72
            '</a>',
73
            $fileThumbnail
74
        );
75
76
        // Usage tab
77
        $uploaded = $form->Fields()->fieldByName('Editor.Usage.Created');
78
        $this->assertEquals(
79
            $file->Created,
80
            $uploaded->dataValue()
81
        );
82
83
        // Test actions exist
84
        $this->assertNotNull($form->Actions()->fieldByName('action_save'));
85
        $this->assertNotNull($form->Actions()->fieldByName('action_publish'));
86
        $this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_addtocampaign'));
87
        $this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_delete'));
88
        $this->assertNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_unpublish'));
89
    }
90
91
    public function testCreateFileForm()
92
    {
93
        $this->logInWithPermission('ADMIN');
94
95
        $file = $this->objFromFixture(File::class, 'file1');
96
        $controller = new AssetAdmin();
97
        $builder = new FileFormFactory();
98
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
99
100
        // Test fields
101
        /** @var LiteralField $fileSpecsField */
102
        $fileSpecsField = $form->Fields()->fieldByName('FileSpecs');
103
        $this->assertEquals(
104
            '<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>',
105
            $fileSpecsField->getContent()
106
        );
107
        $this->assertEquals(
108
            'files/',
109
            $form->Fields()->fieldByName('Editor.Details.Path')->dataValue()
110
        );
111
112
        // Test actions
113
        $this->assertNotNull($form->Actions()->fieldByName('action_save'));
114
    }
115
116 View Code Duplication
    public function testEditImageForm()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
117
    {
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]);
124
125
        // Check thumbnail
126
        // Note: force_resample is turned off for testing
127
        /** @var LiteralField $iconFullField */
128
        $iconFullField = $form->Fields()->fieldByName('IconFull');
129
        $fileThumbnail = $iconFullField->getContent();
130
        $this->assertContains(
131
            '/FileFormBuilderTest/files/906835357d/testimage.png',
132
            $fileThumbnail
133
        );
134
    }
135
136 View Code Duplication
    public function testInsertImageForm()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
137
    {
138
        $this->logInWithPermission('ADMIN');
139
140
        $image = $this->objFromFixture(Image::class, 'image1');
141
        $controller = new AssetAdmin();
142
        $builder = new ImageFormFactory();
143
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $image, 'Type' => 'insert']);
144
145
        // Check thumbnail
146
        // Note: force_resample is turned off for testing
147
        $altTextField = $form->Fields()->dataFieldByName('AltText');
148
        $this->assertNotNull($altTextField);
149
    }
150
151
    public function testFolderForm()
152
    {
153
        $this->logInWithPermission('ADMIN');
154
155
        $folder = $this->objFromFixture(Folder::class, 'parent');
156
        $controller = new AssetAdmin();
157
        $builder = new FolderFormFactory($controller, $folder);
0 ignored issues
show
Unused Code introduced by
The call to FolderFormFactory::__construct() has too many arguments starting with $controller.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
158
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $folder]);
159
160
        // Check fields
161
        $this->assertNull($form->Fields()->fieldByName('FileSpecs'));
162
        $this->assertNull($form->Fields()->fieldByName('Editor.Details.ClickableURL'));
163
        $this->assertNull($form->Fields()->fieldByName('Editor.Usage'));
164
165
        /** @var LiteralField $iconField */
166
        $iconField = $form->Fields()->fieldByName('IconFull');
167
        $fileThumbnail = $iconField->getContent();
168
        $this->assertEquals(
169
            '<img src="framework/client/dist/images/app_icons/folder_icon_large.png" class="editor__thumbnail" />',
170
            $fileThumbnail
171
        );
172
173
174
        // Check actions
175
        $this->assertNotNull($form->Actions()->fieldByName('action_save'));
176
        $this->assertNotNull($form->Actions()->fieldByName('action_delete')); // At top level, not in popup
177
        $this->assertNull($form->Actions()->fieldByName('action_publish'));
178
        $this->assertNull($form->Actions()->dataFieldByName('action_publish'));
179
        $this->assertNull($form->Actions()->dataFieldByName('action_unpublish'));
180
    }
181
182
    public function testScaffolderFactory()
183
    {
184
        $controller = new AssetAdmin();
185
        $this->assertInstanceOf(FileFormFactory::class, $controller->getFormFactory(File::singleton()));
186
        $this->assertInstanceOf(ImageFormFactory::class, $controller->getFormFactory(Image::singleton()));
187
        $this->assertInstanceOf(FolderFormFactory::class, $controller->getFormFactory(Folder::singleton()));
188
    }
189
}
190