FileTypeCreatorTest::testThumbnail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 0
dl 0
loc 49
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\GraphQL;
4
5
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
6
use SilverStripe\AssetAdmin\GraphQL\FileTypeCreator;
7
use SilverStripe\AssetAdmin\Model\ThumbnailGenerator;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Assets\Storage\AssetStore;
10
use Silverstripe\Assets\Dev\TestAssetStore;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\SapphireTest;
13
14
class FileTypeCreatorTest extends SapphireTest
15
{
16
17
    protected $usesDatabase = true;
18
19
    protected function setUp() : void
20
    {
21
        parent::setUp();
22
        TestAssetStore::activate('FileTypeCreatorTest');
23
    }
24
25
    protected function tearDown() : void
26
    {
27
        TestAssetStore::reset();
28
        parent::tearDown();
29
    }
30
31
    public function testThumbnail()
32
    {
33
        $this->logInWithPermission('ADMIN');
34
        /** @var FileTypeCreator $type */
35
        $type = Injector::inst()->create(FileTypeCreator::class);
36
37
        ThumbnailGenerator::config()->set('thumbnail_links', [
38
            AssetStore::VISIBILITY_PROTECTED => ThumbnailGenerator::INLINE,
39
            AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::URL,
40
        ]);
41
42
        $assetAdmin = AssetAdmin::create();
43
44
        // Build image
45
        $image = new Image();
46
        $image->setFromLocalFile(__DIR__.'/../Forms/fixtures/largeimage.png', 'TestImage.png');
47
        $image->write();
48
49
        // Image original is unset
50
        $thumbnail = $type->resolveThumbnailField($image, [], [], null);
51
        $this->assertNull($thumbnail);
52
53
        // Generate thumbnails by viewing this file's data
54
        $assetAdmin->getObjectFromData($image, false);
55
56
        // protected image should have inline thumbnail
57
        $thumbnail = $type->resolveThumbnailField($image, [], [], null);
58
        $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAADr', $thumbnail);
0 ignored issues
show
Bug introduced by
It seems like $thumbnail can also be of type null; however, parameter $string of PHPUnit\Framework\Assert::assertStringStartsWith() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAADr', /** @scrutinizer ignore-type */ $thumbnail);
Loading history...
59
60
        // public image should have url
61
        $image->publishSingle();
62
        $thumbnail = $type->resolveThumbnailField($image, [], [], null);
63
        $this->assertEquals('/assets/FileTypeCreatorTest/TestImage__FitMaxWzM1MiwyNjRd.png', $thumbnail);
64
65
        // Public assets can be set to inline
66
        ThumbnailGenerator::config()->merge('thumbnail_links', [
67
            AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::INLINE,
68
        ]);
69
        $thumbnail = $type->resolveThumbnailField($image, [], [], null);
70
        $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWAAAADr', $thumbnail);
71
72
        // Protected assets can be set to url
73
        // This uses protected asset adapter, so not direct asset link
74
        ThumbnailGenerator::config()->merge('thumbnail_links', [
75
            AssetStore::VISIBILITY_PROTECTED => ThumbnailGenerator::URL,
76
        ]);
77
        $image->doUnpublish();
78
        $thumbnail = $type->resolveThumbnailField($image, [], [], null);
79
        $this->assertEquals('/assets/8cf6c65fa7/TestImage__FitMaxWzM1MiwyNjRd.png', $thumbnail);
80
    }
81
}
82