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

src/Tests/ImageBundleTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 = [
20
    'media',
21
    'media_entity',
22
    'media_entity_image',
23
    'image',
24
    'node',
25
  ];
26
27
  /**
28
   * The test media bundle.
29
   *
30
   * @var \Drupal\media_entity\MediaBundleInterface
31
   */
32
  protected $testBundle;
33
34
  /**
35
   * {@inheritdoc}
36
   */
37 View Code Duplication
  protected function setUp() {
0 ignored issues
show
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...
38
    parent::setUp();
39
    $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('image');
40
41
    $adminUser = $this->drupalCreateUser([
42
      'view media',
43
      'create media',
44
      'update media',
45
      'update any media',
46
      'delete media',
47
      'delete any media',
48
      'access media overview',
49
    ]);
50
    $this->drupalLogin($adminUser);
51
  }
52
53
  /**
54
   * Tests image media bundle creation from config files.
55
   */
56
  public function testMediaBundleCreationFromModule() {
57
    $type_configuration = [
58
      'source_field' => 'field_image',
59
      'gather_exif' => FALSE,
60
    ];
61
62
    $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
63
    $this->assertEqual($this->testBundle->get('label'), 'Image', 'Correct label detected.');
64
    $this->assertEqual($this->testBundle->get('description'), 'Use Image for uploading locally hosted images.', 'Correct description detected.');
65
    $this->assertEqual($this->testBundle->get('type'), 'image', 'Correct plugin ID detected.');
66
    $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
67
    $this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.');
68
  }
69
70
  /**
71
   * Tests item creation and thumbnail.
72
   */
73
  public function testMediaBundleItemCreation() {
74
    // Define the media item name.
75
    $name = $this->randomMachineName();
76
    $image_files = $this->drupalGetTestFiles('image');
77
    $testImage = current($image_files);
78
    $file_path = $this->container->get('file_system')->realpath($testImage->uri);
79
    $edit = [
80
      'name[0][value]' => $name,
81
      'files[field_image_0]' => $file_path,
82
    ];
83
84
    // Save the image.
85
    $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
86
    $this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish'));
87
88
    // Let's retrieve the media id and corresponding media entity object.
89
    $media_id = $this->container->get('entity.query')->get('media')->execute();
90
    $media_id = reset($media_id);
91
    /** @var \Drupal\media_entity\MediaInterface $media */
92
    $media = $this->container->get('entity_type.manager')
93
        ->getStorage('media')
94
        ->loadUnchanged($media_id);
95
    $this->assertEqual($media->get('name')[0]->getValue()['value'], $name, "Correct name stored.");
96
    $image = $media->getType();
97
    $thumbnail = $image->thumbnail($media);
98
    $default_thumbnail = $image->getDefaultThumbnail();
99
    $this->assertNotEqual($thumbnail, $default_thumbnail, "The thumbnail generated is different from the default thumbnail.");
100
  }
101
102
}
103