ThumbnailGeneratorTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 58
c 4
b 0
f 0
dl 0
loc 105
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 4 1
A testGenerateThumbnail() 0 27 1
A testGenerateLink() 0 58 1
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\Model;
4
5
use SilverStripe\AssetAdmin\Model\ThumbnailGenerator;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Assets\Storage\AssetStore;
9
use Silverstripe\Assets\Dev\TestAssetStore;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Dev\SapphireTest;
12
13
class ThumbnailGeneratorTest extends SapphireTest
14
{
15
16
    protected $usesDatabase = true;
17
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
        $this->logInWithPermission('ADMIN');
22
        TestAssetStore::activate('ThumbnailGeneratorTest');
23
    }
24
25
    protected function tearDown() : void
26
    {
27
        TestAssetStore::reset();
28
        parent::tearDown();
29
    }
30
31
    public function testGenerateThumbnail()
32
    {
33
        $generator = new ThumbnailGenerator();
34
        // Build image
35
        $image = new Image();
36
        $image->setFromLocalFile(__DIR__ . '/../Forms/fixtures/testimage.png', 'TestImage.png');
37
        $image->write();
38
39
        // protected image should have inline thumbnail
40
        $thumbnail = $generator->generateThumbnail($image, 100, 200);
41
        $this->assertEquals(100, $thumbnail->getWidth());
42
        $this->assertEquals(50, $thumbnail->getHeight()); // Note: Aspect ratio of original image retained
43
44
        // Build non-image
45
        $file = new File();
46
        $file->setFromLocalFile(__DIR__ . '/../Forms/fixtures/testfile.txt', 'testfile.txt');
47
        $file->write();
48
        $thumbnail = $generator->generateThumbnail($file, 100, 200);
49
        $this->assertNull($thumbnail);
50
51
        // Broken image
52
        $image = new Image();
53
        $image->Filename = 'somefile.jpg';
54
        $image->Hash = sha1('somefile.jpg');
55
        $image->write();
56
        $thumbnail = $generator->generateThumbnail($image, 100, 200);
57
        $this->assertNull($thumbnail);
58
    }
59
60
    public function testGenerateLink()
61
    {
62
        $generator = new ThumbnailGenerator();
63
64
        ThumbnailGenerator::config()->set('thumbnail_links', [
65
            AssetStore::VISIBILITY_PROTECTED => ThumbnailGenerator::INLINE,
66
            AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::URL,
67
        ]);
68
69
        // Build image
70
        $image = new Image();
71
        $image->setFromLocalFile(__DIR__.'/../Forms/fixtures/testimage.png', 'TestImage.png');
72
        $image->write();
73
74
        // Non-images are ignored
75
        $file = new File();
76
        $link = $generator->generateLink($file);
77
        $this->assertNull($link);
78
79
        // original image
80
        $thumbnail = $generator->generateLink($image);
81
        $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACW', $thumbnail);
82
83
        // protected image should have inline thumbnail
84
        $thumbnail = $generator->generateThumbnailLink($image, 100, 200);
85
        $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAy', $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

85
        $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAy', /** @scrutinizer ignore-type */ $thumbnail);
Loading history...
86
87
        // but not if it's too big
88
        Config::nest();
89
        Config::modify()->set(ThumbnailGenerator::class, 'max_thumbnail_bytes', 1);
90
        // Without graceful thumbnails, it should come back null
91
        $thumbnail = $generator->generateThumbnailLink($image, 100, 200);
92
        $this->assertNull($thumbnail);
93
        // With graceful thumbnails, it should come back as a URL
94
        $thumbnail = $generator->generateThumbnailLink($image, 100, 200, true);
95
        $this->assertRegExp('#/assets/[A-Za-z0-9]+/TestImage__FitMaxWzEwMCwyMDBd\.png$#', $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::assertRegExp() 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

95
        $this->assertRegExp('#/assets/[A-Za-z0-9]+/TestImage__FitMaxWzEwMCwyMDBd\.png$#', /** @scrutinizer ignore-type */ $thumbnail);
Loading history...
96
        Config::unnest();
97
98
        // public image should have url
99
        $image->publishSingle();
100
        $thumbnail = $generator->generateThumbnailLink($image, 100, 200);
101
        $this->assertEquals('/assets/ThumbnailGeneratorTest/TestImage__FitMaxWzEwMCwyMDBd.png', $thumbnail);
102
103
        // Public assets can be set to inline
104
        ThumbnailGenerator::config()->merge('thumbnail_links', [
105
            AssetStore::VISIBILITY_PUBLIC => ThumbnailGenerator::INLINE,
106
        ]);
107
        $thumbnail = $generator->generateThumbnailLink($image, 100, 200);
108
        $this->assertStringStartsWith('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAy', $thumbnail);
109
110
        // Protected assets can be set to url
111
        // This uses protected asset adapter, so not direct asset link
112
        ThumbnailGenerator::config()->merge('thumbnail_links', [
113
            AssetStore::VISIBILITY_PROTECTED => ThumbnailGenerator::URL,
114
        ]);
115
        $image->doUnpublish();
116
        $thumbnail = $generator->generateThumbnailLink($image, 100, 200);
117
        $this->assertEquals('/assets/906835357d/TestImage__FitMaxWzEwMCwyMDBd.png', $thumbnail);
118
    }
119
}
120