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

ImageBundleTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
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\simpletest\WebTestBase;
12
13
/**
14
 * Ensures that media bundle for images can be created.
15
 *
16
 * @group media
17
 */
18
class ImageBundleTest extends WebTestBase {
19
20
  /**
21
   * Modules to enable.
22
   *
23
   * @var array
24
   */
25
  public static $modules = ['media_entity', 'media_entity_image', 'image', 'node'];
26
27
  /**
28
   * The test media bundle.
29
   *
30
   * @var \Drupal\media_entity\MediaBundleInterface
31
   */
32
  protected $testBundle;
33
  /**
34
   * Contains the media bundle storage
35
   *
36
   * @var \Drupal\media_entity\MediaBundleInterface
37
   */
38
  protected $bundleStorage;
39
40
  /**
41
   * The test user.
42
   *
43
   * @var \Drupal\User\UserInterface
44
   */
45
  protected $adminUser;
46
47
  /**
48
   * {@inheritdoc}
49
   */
50
  protected function setUp() {
51
    parent::setUp();
52
    $this->container->get('module_installer')->install(['image_bundle_test']);
53
    $entityManager = $this->container->get('entity.manager');
54
    $this->bundleStorage = $entityManager->getStorage('media_bundle');
55
    $this->testBundle = $this->bundleStorage->load('images_media_bundle');
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'), 'Images media bundle', 'Correct label detected.');
80
    $this->assertEqual($this->testBundle->get('description'), 'Media bundle for 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
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 it and verify the title.
102
    $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
103
    $this->assertTitle($edit['name[0][value]'] . ' | Drupal');
104
    $media_id = \Drupal::entityQuery('media')->execute();
105
    $media_id = reset($media_id);
106
    $edit['id'] = $media_id;
107
108
    // Go to media item list.
109
    $this->drupalGet('admin/content/media');
110
    $this->assertResponse(200);
111
    $this->assertText($name);
112
113
    // Check if the thumbnail exists or not.
114
    $xpath = "//table[@class='views-table views-view-table cols-7']/tbody/tr[1]/td[1]/img";
115
    $this->assertFieldByXPath($xpath, NULL, "Thumbnail for the media exists.");
116
  }
117
}
118