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

ImageBundleTest::testMediaBundleItemCreation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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