Completed
Pull Request — 8.x-1.x (#4)
by Vijay
02:48
created

testMediaBundleCreationFromModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.4285
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
    // Verify the title
82
    $this->assertTitle($edit['name[0][value]'] . ' | Drupal');
83
84
    // Let's retrieve the media id and corresponding media entity object
85
    $media_id = $this->container->get('entity.query')->get('media')->execute();
86
    $media_id = reset($media_id);
87
    /** @var \Drupal\media_entity\MediaInterface $media */
88
    $media = $this->container->get('entity_type.manager')
89
        ->getStorage('media')
90
        ->loadUnchanged($media_id);
91
    $image = $media->getType();
92
    $thumbnail = $image->thumbnail($media);
93
    $default_thumbnail = $image->getDefaultThumbnail();
94
    $this->assertNotEqual($thumbnail, $default_thumbnail, "The thumbnail generated is different from the default thumbnail.");
95
  }
96
}
97