ImageBundleTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 15
loc 15
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
   * Exempt from strict schema checking.
15
   *
16
   * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
17
   *
18
   * @var bool
19
   */
20
  protected $strictConfigSchema = FALSE;
21
22
  /**
23
   * Modules to enable.
24
   *
25
   * @var array
26
   */
27
  public static $modules = [
28
    'media',
29
    'media_entity',
30
    'media_entity_image',
31
    'image',
32
    'node',
33
    'editor',
34
  ];
35
36
  /**
37
   * The test media bundle.
38
   *
39
   * @var \Drupal\media_entity\MediaBundleInterface
40
   */
41
  protected $testBundle;
42
43
  /**
44
   * {@inheritdoc}
45
   */
46 View Code Duplication
  protected function setUp() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    parent::setUp();
48
    $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('image');
49
50
    $adminUser = $this->drupalCreateUser([
51
      'view media',
52
      'create media',
53
      'update media',
54
      'update any media',
55
      'delete media',
56
      'delete any media',
57
      'access media overview',
58
    ]);
59
    $this->drupalLogin($adminUser);
60
  }
61
62
  /**
63
   * Tests image media bundle creation from config files.
64
   */
65 View Code Duplication
  public function testMediaBundleCreationFromModule() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    $type_configuration = [
67
      'source_field' => 'field_image',
68
      'gather_exif' => FALSE,
69
    ];
70
71
    $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
72
    $this->assertEqual($this->testBundle->get('label'), 'Image', 'Correct label detected.');
73
    $this->assertEqual($this->testBundle->get('description'), 'Use Image for uploading locally hosted images.', 'Correct description detected.');
74
    $this->assertEqual($this->testBundle->get('type'), 'image', 'Correct plugin ID detected.');
75
    $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
76
    $this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.');
77
  }
78
79
  /**
80
   * Tests item creation and thumbnail.
81
   */
82
  public function testMediaBundleItemCreation() {
83
    // Define the media item name.
84
    $name = $this->randomMachineName();
85
    $image_files = $this->drupalGetTestFiles('image');
86
    $testImage = current($image_files);
87
    $file_path = $this->container->get('file_system')->realpath($testImage->uri);
88
    $edit = [
89
      'name[0][value]' => $name,
90
      'files[field_image_0]' => $file_path,
91
    ];
92
93
    // Save the image.
94
    $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
95
    $this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish'));
96
97
    // Let's retrieve the media id and corresponding media entity object.
98
    $media_id = $this->container->get('entity.query')->get('media')->execute();
99
    $media_id = reset($media_id);
100
    /** @var \Drupal\media_entity\MediaInterface $media */
101
    $media = $this->container->get('entity_type.manager')
102
        ->getStorage('media')
103
        ->loadUnchanged($media_id);
104
    $this->assertEqual($media->get('name')[0]->getValue()['value'], $name, "Correct name stored.");
105
    $image = $media->getType();
106
    $thumbnail = $image->thumbnail($media);
107
    $default_thumbnail = $image->getDefaultThumbnail();
108
    $this->assertNotEqual($thumbnail, $default_thumbnail, "The thumbnail generated is different from the default thumbnail.");
109
  }
110
111
}
112