Completed
Pull Request — master (#458)
by Roman
06:58 queued 04:35
created

FileFormBuilderTest::testEditFileForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 64
rs 9.3956
cc 1
eloc 38
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Assets\File;
11
use SilverStripe\Assets\Folder;
12
use SilverStripe\Assets\Image;
13
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
14
use SilverStripe\Core\Config\Config;
15
use SilverStripe\Dev\SapphireTest;
16
use SilverStripe\Forms\LiteralField;
17
18
class FileFormBuilderTest extends SapphireTest
19
{
20
    protected static $fixture_file = 'FileFormBuilderTest.yml';
21
22 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...
23
    {
24
        parent::setUp();
25
26
        // Set backend and base url
27
        TestAssetStore::activate('FileFormBuilderTest');
28
29
        /** @var File $testfile */
30
        $testfile = $this->objFromFixture(File::class, 'file1');
31
        $testfile->setFromLocalFile(__DIR__ .'/fixtures/testfile.txt', 'files/testfile.txt');
32
        $testfile->write();
33
34
        /** @var Image $testimage */
35
        $testimage = $this->objFromFixture(Image::class, 'image1');
36
        $testimage->setFromLocalFile(__DIR__.'/fixtures/testimage.png', 'files/testimage.png');
37
    }
38
39
    public function tearDown()
40
    {
41
        TestAssetStore::reset();
42
        parent::tearDown();
43
    }
44
45
    public function testEditFileForm()
46
    {
47
        // Ensure campaign-admin extension is not applied!
48
        Config::modify()->remove(FileFormFactory::class, 'extensions');
49
50
        $this->logInWithPermission('ADMIN');
51
52
        $file = $this->objFromFixture(File::class, 'file1');
53
        $controller = new AssetAdmin();
54
        $builder = new FileFormFactory();
55
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
56
57
        // Verify file form is scaffolded correctly
58
        $this->assertEquals('EditForm', $form->getName());
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
59
60
        // Test fields exist
61
        /** @var LiteralField $fileSpecsField */
62
        $fileSpecsField = $form->Fields()->fieldByName('FileSpecs');
63
        $fileSpecs = $fileSpecsField->getContent();
64
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
65
            '<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>',
66
            $fileSpecs
67
        );
68
        $filePath = $form->Fields()->fieldByName('Editor.Details.Path')->Value();
69
        $this->assertEquals('files/', $filePath);
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
70
71
        /** @var LiteralField $iconFullField */
72
        $iconFullField = $form->Fields()->fieldByName('PreviewImage');
73
        $state = $iconFullField->getSchemaStateDefaults();
74
        $this->assertEquals($file->Parent()->ID, $state['data']['parentid']);
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
The method assertEquals() 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...
75
        $this->assertContains('testfile.txt', $state['data']['url']);
76
        $this->assertTrue($state['data']['exists']);
0 ignored issues
show
Bug introduced by
The method assertTrue() 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...
77
        $this->assertContains('document_92.png', $state['data']['preview']);
78
        $this->assertEquals('document', $state['data']['category']);
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
79
80
        // Usage tab
81
        $uploaded = $form->Fields()->fieldByName('Editor.Usage.Created');
82
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
83
            $file->Created,
84
            $uploaded->dataValue()
85
        );
86
87
        // Test actions exist
88
        $this->assertNotNull($form->Actions()->fieldByName('Actions.action_save'));
0 ignored issues
show
Bug introduced by
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...
89
        $this->assertNotNull($form->Actions()->fieldByName('Actions.action_publish'));
0 ignored issues
show
Bug introduced by
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...
90
        $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete'));
0 ignored issues
show
Bug introduced by
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...
91
        $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish'));
0 ignored issues
show
Bug introduced by
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...
92
93
        // Add to campaign should not be there by default
94
        $this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign'));
0 ignored issues
show
Bug introduced by
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...
95
96
        // Add extension for campaign-admin
97
        Config::modify()->merge(
98
            FileFormFactory::class,
99
            'extensions',
100
            [ CampaignAdminExtension::class ]
101
        );
102
103
        $builder = new FileFormFactory();
104
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
105
106
        // Add to campaign should now be available
107
        $this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign'));
0 ignored issues
show
Bug introduced by
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...
108
    }
109
110
    public function testCreateFileForm()
111
    {
112
        $this->logInWithPermission('ADMIN');
113
114
        $file = $this->objFromFixture(File::class, 'file1');
115
        $controller = new AssetAdmin();
116
        $builder = new FileFormFactory();
117
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
118
119
        // Test fields
120
        /** @var LiteralField $fileSpecsField */
121
        $fileSpecsField = $form->Fields()->fieldByName('FileSpecs');
122
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
123
            '<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>',
124
            $fileSpecsField->getContent()
125
        );
126
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
127
            'files/',
128
            $form->Fields()->fieldByName('Editor.Details.Path')->dataValue()
129
        );
130
131
        // Test actions
132
        $this->assertNotNull($form->Actions()->fieldByName('Actions.action_save'));
0 ignored issues
show
Bug introduced by
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...
133
    }
134
135
    public function testEditImageForm()
136
    {
137
        $this->logInWithPermission('ADMIN');
138
139
        $image = $this->objFromFixture(Image::class, 'image1');
140
        // write so that PreviewImageField could load this later on
141
        $image->write();
142
        $controller = new AssetAdmin();
143
        $builder = new ImageFormFactory();
144
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $image]);
145
146
        // Check thumbnail
147
        // Note: force_resample is turned off for testing
148
        /** @var LiteralField $iconFullField */
149
        $iconFullField = $form->Fields()->fieldByName('PreviewImage');
150
        $state = $iconFullField->getSchemaStateDefaults();
151
        $this->assertEquals($image->Parent()->ID, $state['data']['parentid']);
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
The method assertEquals() 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...
152
        $this->assertContains('testimage.png', $state['data']['url']);
153
        $this->assertTrue($state['data']['exists']);
0 ignored issues
show
Bug introduced by
The method assertTrue() 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...
154
        $this->assertContains('testimage.png', $state['data']['preview']);
155
        $this->assertEquals('image', $state['data']['category']);
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
156
    }
157
158
    public function testInsertImageForm()
159
    {
160
        $this->logInWithPermission('ADMIN');
161
162
        $image = $this->objFromFixture(Image::class, 'image1');
163
        $controller = new AssetAdmin();
164
        $builder = new ImageFormFactory();
165
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $image, 'Type' => 'insert']);
166
167
        // Check thumbnail
168
        // Note: force_resample is turned off for testing
169
        $altTextField = $form->Fields()->dataFieldByName('AltText');
170
        $this->assertNotNull($altTextField);
0 ignored issues
show
Bug introduced by
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...
171
    }
172
173
    public function testFolderForm()
174
    {
175
        $this->logInWithPermission('ADMIN');
176
177
        $folder = $this->objFromFixture(Folder::class, 'parent');
178
        $controller = new AssetAdmin();
179
        $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...
180
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $folder]);
181
182
        // Check fields
183
        $this->assertNull($form->Fields()->fieldByName('FileSpecs'));
0 ignored issues
show
Bug introduced by
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...
184
        $this->assertNull($form->Fields()->fieldByName('Editor.Details.ClickableURL'));
0 ignored issues
show
Bug introduced by
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...
185
        $this->assertNull($form->Fields()->fieldByName('Editor.Usage'));
0 ignored issues
show
Bug introduced by
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...
186
187
        /** @var LiteralField $iconFullField */
188
        $iconFullField = $form->Fields()->fieldByName('PreviewImage');
189
        $state = $iconFullField->getSchemaStateDefaults();
190
        $this->assertEquals($folder->Parent()->ID, $state['data']['parentid']);
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SilverStripe\ORM\DataObject. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
The method assertEquals() 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...
191
        $this->assertTrue($state['data']['exists']);
0 ignored issues
show
Bug introduced by
The method assertTrue() 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...
192
        $this->assertContains('folder_icon_large.png', $state['data']['preview']);
193
        $this->assertEquals('folder', $state['data']['category']);
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
194
195
        // Check actions
196
        $this->assertNotNull($form->Actions()->fieldByName('action_save'));
0 ignored issues
show
Bug introduced by
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...
197
        $this->assertNotNull($form->Actions()->dataFieldByName('action_delete'));
0 ignored issues
show
Bug introduced by
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...
198
        $this->assertNull($form->Actions()->fieldByName('action_publish'));
0 ignored issues
show
Bug introduced by
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...
199
        $this->assertNull($form->Actions()->dataFieldByName('action_publish'));
0 ignored issues
show
Bug introduced by
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...
200
        $this->assertNull($form->Actions()->dataFieldByName('action_unpublish'));
0 ignored issues
show
Bug introduced by
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...
201
    }
202
203
    public function testScaffolderFactory()
204
    {
205
        $controller = new AssetAdmin();
206
        $this->assertInstanceOf(FileFormFactory::class, $controller->getFormFactory(File::singleton()));
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() 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...
207
        $this->assertInstanceOf(ImageFormFactory::class, $controller->getFormFactory(Image::singleton()));
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() 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...
208
        $this->assertInstanceOf(FolderFormFactory::class, $controller->getFormFactory(Folder::singleton()));
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() 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...
209
    }
210
}
211