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
|
|
|
'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
|
|
|
protected function setUp() { |
47
|
|
|
parent::setUp(); |
48
|
|
|
$this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('tweet'); |
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 tweet media bundle creation from config files. |
64
|
|
|
*/ |
65
|
|
|
public function testMediaBundleCreationFromModule() { |
66
|
|
|
$type_configuration = [ |
67
|
|
|
'use_twitter_api' => FALSE, |
68
|
|
|
'source_field' => 'field_tweet_url', |
69
|
|
|
'consumer_key' => '', |
70
|
|
|
'consumer_secret' => '', |
71
|
|
|
'oauth_access_token' => '', |
72
|
|
|
'oauth_access_token_secret' => '', |
73
|
|
|
]; |
74
|
|
|
$field_map = [ |
75
|
|
|
'id' => 'field_tweet_id', |
76
|
|
|
'user' => 'field_tweet_author', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
$this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.'); |
80
|
|
|
$this->assertEqual($this->testBundle->get('label'), 'Tweet', 'Correct label detected.'); |
81
|
|
|
$this->assertEqual($this->testBundle->get('description'), 'Use this to embed Twitter content on your site.', 'Correct description detected.'); |
82
|
|
|
$this->assertEqual($this->testBundle->get('type'), 'twitter', 'Correct plugin ID detected.'); |
83
|
|
|
$this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.'); |
84
|
|
|
$this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tests item creation. |
89
|
|
|
*/ |
90
|
|
|
public function testMediaBundleItemCreation() { |
91
|
|
|
// Define the media item name. |
92
|
|
|
$name = $this->randomMachineName(); |
93
|
|
|
$tweet_url = 'https://twitter.com/jack/status/20'; |
94
|
|
|
$edit = [ |
95
|
|
|
'name[0][value]' => $name, |
96
|
|
|
'field_tweet_url[0][uri]' => $tweet_url, |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
// Save the tweet. |
100
|
|
|
$this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish')); |
101
|
|
|
// Let's retrieve the media id. |
102
|
|
|
$media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'tweet')->sort('created', 'DESC')->execute(); |
103
|
|
|
$media_id = reset($media_id); |
104
|
|
|
$media = $this->container->get('entity_type.manager') |
105
|
|
|
->getStorage('media') |
106
|
|
|
->loadUnchanged($media_id); |
107
|
|
|
$properties = $media->toArray(); |
|
|
|
|
108
|
|
|
$this->assertEqual($media->get('field_tweet_author')[0]->getValue()['value'], "jack", "Correct tweet author stored."); |
109
|
|
|
$this->assertEqual($media->get('field_tweet_id')[0]->getValue()['value'], "20", "Correct tweet id stored."); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.