Completed
Push — 8.x-1.x ( 802e84...bea2b9 )
by Janez
02:44
created

testMediaBundleCreationFromModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 17
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 21
rs 9.3142
1
<?php
2
3
namespace Drupal\media\Tests;
4
5
use Drupal\simpletest\WebTestBase;
6
7
/**
8
 * Ensures that media bundle for tweets can be created.
9
 *
10
 * @group media
11
 */
12
class TweetBundleTest 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_twitter',
31
    'node',
32
    'link',
33
  ];
34
35
  /**
36
   * The test media bundle.
37
   *
38
   * @var \Drupal\media_entity\MediaBundleInterface
39
   */
40
  protected $testBundle;
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  protected function setUp() {
46
    parent::setUp();
47
    $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('tweet');
48
49
    $adminUser = $this->drupalCreateUser([
50
      'view media',
51
      'create media',
52
      'update media',
53
      'update any media',
54
      'delete media',
55
      'delete any media',
56
      'access media overview',
57
    ]);
58
    $this->drupalLogin($adminUser);
59
  }
60
61
  /**
62
   * Tests tweet media bundle creation from config files.
63
   */
64
  public function testMediaBundleCreationFromModule() {
65
    $type_configuration = [
66
      'use_twitter_api' => FALSE,
67
      'source_field' => 'field_tweet_url',
68
      'consumer_key' => '',
69
      'consumer_secret' => '',
70
      'oauth_access_token' => '',
71
      'oauth_access_token_secret' => '',
72
    ];
73
    $field_map = [
74
      'id' => 'field_tweet_id',
75
      'user' => 'field_tweet_author',
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'), 'Tweet', 'Correct label detected.');
80
    $this->assertEqual($this->testBundle->get('description'), 'Use this to embed Twitter content on your site.', 'Correct description detected.');
81
    $this->assertEqual($this->testBundle->get('type'), 'twitter', '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'), $field_map, 'Correct field map detected.');
84
  }
85
86
  /**
87
   * Tests item creation.
88
   */
89
  public function testMediaBundleItemCreation() {
90
    // Define the media item name.
91
    $name = $this->randomMachineName();
92
    $tweet_url = 'https://twitter.com/jack/status/20';
93
    $edit = [
94
      'name[0][value]' => $name,
95
      'field_tweet_url[0][uri]' => $tweet_url,
96
    ];
97
98
    // Save the tweet.
99
    $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
100
    // Let's retrieve the media id.
101
    $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'tweet')->sort('created', 'DESC')->execute();
102
    $media_id = reset($media_id);
103
    $media = $this->container->get('entity_type.manager')
104
      ->getStorage('media')
105
      ->loadUnchanged($media_id);
106
    $properties = $media->toArray();
0 ignored issues
show
Unused Code introduced by
$properties is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
    $this->assertEqual($media->get('field_tweet_author')[0]->getValue()['value'], "jack", "Correct tweet author stored.");
108
    $this->assertEqual($media->get('field_tweet_id')[0]->getValue()['value'], "20", "Correct tweet id stored.");
109
  }
110
111
}
112