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); |
|
|
|
|
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
|
|
|
|