Completed
Pull Request — master (#339)
by
unknown
01:50
created

FileFormBuilderTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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('PreviewImage');
68
        $state = $iconFullField->getSchemaStateDefaults();
69
        $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...
70
        $this->assertContains('testfile.txt', $state['data']['url']);
71
        $this->assertTrue($state['data']['exists']);
72
        $this->assertContains('document_92.png', $state['data']['preview']);
73
        $this->assertEquals('document', $state['data']['category']);
74
75
        // Usage tab
76
        $uploaded = $form->Fields()->fieldByName('Editor.Usage.Created');
77
        $this->assertEquals(
78
            $file->Created,
79
            $uploaded->dataValue()
80
        );
81
82
        // Test actions exist
83
        $this->assertNotNull($form->Actions()->fieldByName('action_save'));
84
        $this->assertNotNull($form->Actions()->fieldByName('action_publish'));
85
        $this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_addtocampaign'));
86
        $this->assertNotNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_delete'));
87
        $this->assertNull($form->Actions()->fieldByName('actionaddtocampaignactiondelete.action_unpublish'));
88
    }
89
90
    public function testCreateFileForm()
91
    {
92
        $this->logInWithPermission('ADMIN');
93
94
        $file = $this->objFromFixture(File::class, 'file1');
95
        $controller = new AssetAdmin();
96
        $builder = new FileFormFactory();
97
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
98
99
        // Test fields
100
        /** @var LiteralField $fileSpecsField */
101
        $fileSpecsField = $form->Fields()->fieldByName('FileSpecs');
102
        $this->assertEquals(
103
            '<div class="editor__specs">11 bytes <span class="editor__status-flag">Draft</span></div>',
104
            $fileSpecsField->getContent()
105
        );
106
        $this->assertEquals(
107
            'files/',
108
            $form->Fields()->fieldByName('Editor.Details.Path')->dataValue()
109
        );
110
111
        // Test actions
112
        $this->assertNotNull($form->Actions()->fieldByName('action_save'));
113
    }
114
115
    public function testEditImageForm()
116
    {
117
        $this->logInWithPermission('ADMIN');
118
119
        $image = $this->objFromFixture(Image::class, 'image1');
120
        // write so that PreviewImageField could load this later on
121
        $image->write();
122
        $controller = new AssetAdmin();
123
        $builder = new ImageFormFactory();
124
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $image]);
125
126
        // Check thumbnail
127
        // Note: force_resample is turned off for testing
128
        /** @var LiteralField $iconFullField */
129
        $iconFullField = $form->Fields()->fieldByName('PreviewImage');
130
        $state = $iconFullField->getSchemaStateDefaults();
131
        $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...
132
        $this->assertContains('testimage.png', $state['data']['url']);
133
        $this->assertTrue($state['data']['exists']);
134
        $this->assertContains('testimage.png', $state['data']['preview']);
135
        $this->assertEquals('image', $state['data']['category']);
136
    }
137
138
    public function testInsertImageForm()
139
    {
140
        $this->logInWithPermission('ADMIN');
141
142
        $image = $this->objFromFixture(Image::class, 'image1');
143
        $controller = new AssetAdmin();
144
        $builder = new ImageFormFactory();
145
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $image, 'Type' => 'insert']);
146
147
        // Check thumbnail
148
        // Note: force_resample is turned off for testing
149
        $altTextField = $form->Fields()->dataFieldByName('AltText');
150
        $this->assertNotNull($altTextField);
151
    }
152
153
    public function testFolderForm()
154
    {
155
        $this->logInWithPermission('ADMIN');
156
157
        $folder = $this->objFromFixture(Folder::class, 'parent');
158
        $controller = new AssetAdmin();
159
        $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...
160
        $form = $builder->getForm($controller, 'EditForm', ['Record' => $folder]);
161
162
        // Check fields
163
        $this->assertNull($form->Fields()->fieldByName('FileSpecs'));
164
        $this->assertNull($form->Fields()->fieldByName('Editor.Details.ClickableURL'));
165
        $this->assertNull($form->Fields()->fieldByName('Editor.Usage'));
166
167
        /** @var LiteralField $iconFullField */
168
        $iconFullField = $form->Fields()->fieldByName('PreviewImage');
169
        $state = $iconFullField->getSchemaStateDefaults();
170
        $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...
171
        $this->assertTrue($state['data']['exists']);
172
        $this->assertContains('folder_icon_large.png', $state['data']['preview']);
173
        $this->assertEquals('folder', $state['data']['category']);
174
175
        // Check actions
176
        $this->assertNotNull($form->Actions()->fieldByName('action_save'));
177
        $this->assertNotNull($form->Actions()->fieldByName('action_delete')); // At top level, not in popup
178
        $this->assertNull($form->Actions()->fieldByName('action_publish'));
179
        $this->assertNull($form->Actions()->dataFieldByName('action_publish'));
180
        $this->assertNull($form->Actions()->dataFieldByName('action_unpublish'));
181
    }
182
183
    public function testScaffolderFactory()
184
    {
185
        $controller = new AssetAdmin();
186
        $this->assertInstanceOf(FileFormFactory::class, $controller->getFormFactory(File::singleton()));
187
        $this->assertInstanceOf(ImageFormFactory::class, $controller->getFormFactory(Image::singleton()));
188
        $this->assertInstanceOf(FolderFormFactory::class, $controller->getFormFactory(Folder::singleton()));
189
    }
190
}
191