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