1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\media\Tests; |
4
|
|
|
|
5
|
|
|
use Drupal\simpletest\WebTestBase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Ensures that media bundle for instagram can be created. |
9
|
|
|
* |
10
|
|
|
* @group media |
11
|
|
|
*/ |
12
|
|
|
class InstagramBundleTest 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_instagram', |
31
|
|
|
'link', |
32
|
|
|
'node', |
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('instagram'); |
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 instagram media bundle creation from config files. |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function testMediaBundleCreationFromModule() { |
|
|
|
|
65
|
|
|
$type_configuration = [ |
66
|
|
|
'use_instagram_api' => FALSE, |
67
|
|
|
'source_field' => 'field_instagram_url', |
68
|
|
|
'client_id' => '', |
69
|
|
|
]; |
70
|
|
|
$field_map = [ |
71
|
|
|
'shortcode' => 'field_instagram_shortcode', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
$this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.'); |
75
|
|
|
$this->assertEqual($this->testBundle->get('label'), 'Instagram Post', 'Correct label detected.'); |
76
|
|
|
$this->assertEqual($this->testBundle->get('description'), 'Use this to attach Instagram posts to your content.', 'Correct description detected.'); |
77
|
|
|
$this->assertEqual($this->testBundle->get('type'), 'instagram', 'Correct plugin ID detected.'); |
78
|
|
|
$this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.'); |
79
|
|
|
$this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Tests item creation and thumbnail. |
84
|
|
|
*/ |
85
|
|
|
public function testMediaBundleItemCreation() { |
86
|
|
|
// Define the media item name. |
87
|
|
|
$name = $this->randomMachineName(); |
88
|
|
|
$instagram_url = 'https://www.instagram.com/p/C/'; |
89
|
|
|
$edit = [ |
90
|
|
|
'name[0][value]' => $name, |
91
|
|
|
'field_instagram_url[0][uri]' => $instagram_url, |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
// Save the Instagram post. |
95
|
|
|
$this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish')); |
96
|
|
|
|
97
|
|
|
// Assert that the formatter exists on this page. |
98
|
|
|
$this->assertFieldByXPath('//iframe'); |
99
|
|
|
|
100
|
|
|
// Let's retrieve the media id and corresponding media entity object. |
101
|
|
|
$media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'instagram')->sort('created', 'DESC')->execute(); |
102
|
|
|
$media_id = reset($media_id); |
103
|
|
|
/** @var \Drupal\media_entity\MediaInterface $media */ |
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_instagram_shortcode')[0]->getValue()['value'], "C", "Correct shortcode stored."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
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.