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