testMediaBundleCreationFromModule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\media\Tests;
4
5
use Drupal\simpletest\WebTestBase;
6
7
/**
8
 * Ensures that media bundle for videos can be created.
9
 *
10
 * @group media
11
 */
12
class VideoBundleTest 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
    'video_embed_field',
31
    'video_embed_media',
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('video');
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 video 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_video',
68
    ];
69
70
    $field_map = [
71
      'id' => 'field_id',
72
      'source_name' => 'field_source',
73
    ];
74
75
    $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
76
    $this->assertEqual($this->testBundle->get('label'), 'Video', 'Correct label detected.');
77
    $this->assertEqual($this->testBundle->get('description'), 'Use Video for embedding videos hosted by YouTube, Vimeo, or some other provider.', 'Correct description detected.');
78
    $this->assertEqual($this->testBundle->get('type'), 'video_embed_field', 'Correct plugin ID detected.');
79
    $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
80
    $this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.');
81
  }
82
83
  /**
84
   * Tests video media bundle field maps.
85
   */
86
  public function testBundleFieldMap() {
87
    $edit = [
88
      'name[0][value]' => 'Drupal video!',
89
      'field_video[0][value]' => 'https://www.youtube.com/watch?v=XgYu7-DQjDQ',
90
    ];
91
    $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
92
93
    // Let's retrieve the media id and corresponding media entity object.
94
    $media_id = $this->container->get('entity.query')->get('media')->execute();
95
    $media_id = reset($media_id);
96
    /** @var \Drupal\media_entity\MediaInterface $media */
97
    $media = $this->container->get('entity_type.manager')
98
      ->getStorage('media')
99
      ->loadUnchanged($media_id);
100
    $properties = $media->toArray();
101
    $this->assertEqual($properties['field_id'][0]['value'], 'XgYu7-DQjDQ', 'Correct video ID detected.');
102
    $this->assertEqual($properties['field_source'][0]['value'], 'youtube', 'Correct video source detected.');
103
  }
104
105
}
106