FileBlockTest::testImageIsAddedToSchemaData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace SilverStripe\ElementalFileBlock\Tests\Block;
4
5
use SilverStripe\AssetAdmin\Forms\UploadField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\Forms\UploadField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Folder;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Assets\Test...toreTest\TestAssetStore was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\ElementalFileBlock\Block\FileBlock;
12
13
class FileBlockTest extends SapphireTest
14
{
15
    protected static $fixture_file = 'FileBlockTest.yml';
16
17
    protected function setUp(): void
18
    {
19
        parent::setUp();
20
        TestAssetStore::activate('FileBlockTest');
21
22
        // Copy test images for each of the fixture references
23
        /** @var File $image */
24
        $files = File::get()->exclude('ClassName', Folder::class);
25
        foreach ($files as $image) {
26
            $sourcePath = __DIR__ . '/FileBlockTest/' . $image->Name;
27
            $image->setFromLocalFile($sourcePath, $image->Filename);
28
            $image->write();
29
        }
30
    }
31
32
    protected function tearDown(): void
33
    {
34
        TestAssetStore::reset();
35
        parent::tearDown();
36
    }
37
38
    public function testGetSummaryReturnsStringWithoutAssociatedFile()
39
    {
40
        $block = new FileBlock;
41
        $this->assertSame('', $block->getSummary());
42
    }
43
44
    public function testGetSummaryReturnsThumbnailAndFileTitle()
45
    {
46
        /** @var FileBlock $block */
47
        $block = $this->objFromFixture(FileBlock::class, 'with_image');
48
49
        $summary = $block->getSummary();
50
51
        $this->assertStringContainsString('elemental-preview__thumbnail-image', $summary);
52
        $this->assertStringContainsString('Some image', $summary);
53
    }
54
55
    public function testGetSummaryReturnsFileTitleWhenLinkedToFile()
56
    {
57
        /** @var FileBlock $block */
58
        $block = $this->objFromFixture(FileBlock::class, 'with_file');
59
60
        $summary = $block->getSummary();
61
62
        $this->assertStringContainsString('elemental-preview__thumbnail-image', $summary);
63
        $this->assertStringContainsString('elemental-preview__thumbnail-image--placeholder', $summary);
64
        $this->assertStringContainsString('Some file', $summary);
65
    }
66
67
    public function testImageIsAddedToSchemaData()
68
    {
69
        /** @var FileBlock $block */
70
        $block = $this->objFromFixture(FileBlock::class, 'with_image');
71
72
        $schemaData = $block->getBlockSchema();
73
74
        $this->assertNotEmpty($schemaData['fileURL'], 'File URL is added to schema');
75
        $this->assertNotEmpty($schemaData['fileTitle'], 'File title is added to schema');
76
    }
77
78
    public function testUploadFieldShouldNotAllowMultipleFiles()
79
    {
80
        /** @var FileBlock $block */
81
        $block = $this->objFromFixture(FileBlock::class, 'with_image');
82
83
        /** @var UploadField $field */
84
        $field = $block->getCMSFields()->fieldByName('Root.Main.File');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $field is correct as $block->getCMSFields()->...yName('Root.Main.File') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
86
        $this->assertInstanceOf(UploadField::class, $field, 'Field should be an UploadField');
87
        $this->assertFalse($field->getIsMultiUpload(), 'Field should not allow multiple uploads');
88
    }
89
}
90