|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\media\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\simpletest\WebTestBase; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Ensures that media bundle for images can be created. |
|
9
|
|
|
* |
|
10
|
|
|
* @group media |
|
11
|
|
|
*/ |
|
12
|
|
|
class ImageBundleTest extends WebTestBase { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Modules to enable. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
public static $modules = ['media', 'media_entity', 'media_entity_image', 'image', 'node']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The test media bundle. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Drupal\media_entity\MediaBundleInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $testBundle; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function setUp() { |
|
32
|
|
|
parent::setUp(); |
|
33
|
|
|
$this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('image'); |
|
34
|
|
|
|
|
35
|
|
|
$adminUser = $this->drupalCreateUser([ |
|
36
|
|
|
'view media', |
|
37
|
|
|
'create media', |
|
38
|
|
|
'update media', |
|
39
|
|
|
'update any media', |
|
40
|
|
|
'delete media', |
|
41
|
|
|
'delete any media', |
|
42
|
|
|
'access media overview', |
|
43
|
|
|
]); |
|
44
|
|
|
$this->drupalLogin($adminUser); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Tests image media bundle creation from config files. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testMediaBundleCreationFromModule() { |
|
51
|
|
|
$type_configuration = [ |
|
52
|
|
|
'source_field' => 'field_image', |
|
53
|
|
|
'gather_exif' => false |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.'); |
|
57
|
|
|
$this->assertEqual($this->testBundle->get('label'), 'Image', 'Correct label detected.'); |
|
58
|
|
|
$this->assertEqual($this->testBundle->get('description'), 'Use Image for uploading locally hosted images.', 'Correct description detected.'); |
|
59
|
|
|
$this->assertEqual($this->testBundle->get('type'), 'image', 'Correct plugin ID detected.'); |
|
60
|
|
|
$this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.'); |
|
61
|
|
|
$this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Tests item creation and thumbnail |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testMediaBundleItemCreation() { |
|
68
|
|
|
// Define the media item name. |
|
69
|
|
|
$name = $this->randomMachineName(); |
|
70
|
|
|
$image_files = $this->drupalGetTestFiles('image'); |
|
71
|
|
|
$testImage = current($image_files); |
|
72
|
|
|
$file_path = $this->container->get('file_system')->realpath($testImage->uri); |
|
73
|
|
|
$edit = [ |
|
74
|
|
|
'name[0][value]' => $name, |
|
75
|
|
|
'files[field_image_0]' => $file_path, |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
// Save the image |
|
79
|
|
|
$this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish')); |
|
80
|
|
|
$this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish')); |
|
81
|
|
|
|
|
82
|
|
|
// Let's retrieve the media id and corresponding media entity object |
|
83
|
|
|
$media_id = $this->container->get('entity.query')->get('media')->execute(); |
|
84
|
|
|
$media_id = reset($media_id); |
|
85
|
|
|
/** @var \Drupal\media_entity\MediaInterface $media */ |
|
86
|
|
|
$media = $this->container->get('entity_type.manager') |
|
87
|
|
|
->getStorage('media') |
|
88
|
|
|
->loadUnchanged($media_id); |
|
89
|
|
|
$this->assertEqual($media->get('name')[0]->getValue()['value'], $name, "Correct name stored."); |
|
90
|
|
|
$image = $media->getType(); |
|
91
|
|
|
$thumbnail = $image->thumbnail($media); |
|
92
|
|
|
$default_thumbnail = $image->getDefaultThumbnail(); |
|
93
|
|
|
$this->assertNotEqual($thumbnail, $default_thumbnail, "The thumbnail generated is different from the default thumbnail."); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|