3 namespace Drupal\Tests\media_entity_instagram\Functional;
5 use Drupal\media_entity\Entity\MediaBundle;
6 use Drupal\media_entity\Tests\MediaTestTrait;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests for Instagram embed formatter.
12 * @group media_entity_instagram
14 class InstagramEmbedFormatterTest extends BrowserTestBase {
21 public static $modules = [
22 'media_entity_instagram',
35 * @var \Drupal\User\UserInterface
40 * Media entity machine id.
44 protected $mediaId = 'instagram';
47 * The test media bundle.
49 * @var \Drupal\media_entity\MediaBundleInterface
51 protected $testBundle;
56 protected function setUp() {
59 $bundle['bundle'] = $this->mediaId;
60 $this->testBundle = $this->drupalCreateMediaBundle($bundle, 'instagram');
61 $this->drupalPlaceBlock('local_actions_block');
62 $this->adminUser = $this->drupalCreateUser([
64 'administer media bundles',
65 'administer media fields',
66 'administer media form display',
67 'administer media display',
68 // Media entity permissions.
78 $this->drupalLogin($this->adminUser);
82 * Tests adding and editing an instagram embed formatter.
84 public function testManageFieldFormatter() {
85 // Test and create one media bundle.
86 $bundle = $this->testBundle;
88 // Assert that the media bundle has the expected values before proceeding.
89 $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
90 $this->assertSession()->fieldValueEquals('label', $bundle->label());
91 $this->assertSession()->fieldValueEquals('type', 'instagram');
93 // Add and save field settings (Embed code).
94 $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
96 'new_storage_type' => 'string_long',
97 'label' => 'Embed code',
98 'field_name' => 'embed_code',
100 $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
101 $this->assertSession()->pageTextContains('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
103 'cardinality' => 'number',
104 'cardinality_number' => '1',
106 $this->drupalPostForm(NULL, $edit, t('Save field settings'));
107 $this->assertSession()->pageTextContains('Updated field ' . $edit_conf['label'] . ' field settings.');
109 // Set the new field as required.
113 $this->drupalPostForm(NULL, $edit, t('Save settings'));
114 $this->assertSession()->pageTextContains('Saved ' . $edit_conf['label'] . ' configuration.');
116 // Assert that the new field configuration has been successfully saved.
117 $this->assertEquals('Embed code', $this->xpath('//*[@id="field-embed-code"]/td[1]')[0]->getText());
118 $this->assertEquals('field_embed_code', $this->xpath('//*[@id="field-embed-code"]/td[2]')[0]->getText());
119 $this->assertEquals('Text (plain, long)', $this->xpath('//*[@id="field-embed-code"]/td[3]')[0]->getText());
121 // Test if edit worked and if new field values have been saved as
123 $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
124 $this->assertSession()->fieldValueEquals('label', $bundle->label());
125 $this->assertSession()->fieldValueEquals('type', 'instagram');
126 $this->assertSession()->fieldValueEquals('type_configuration[instagram][source_field]', 'field_embed_code');
127 $this->drupalPostForm(NULL, NULL, t('Save media bundle'));
128 $this->assertSession()->pageTextContains('The media bundle ' . $bundle->label() . ' has been updated.');
129 $this->assertSession()->pageTextContains($bundle->label());
131 $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/display');
133 // Set and save the settings of the new field.
135 'fields[field_embed_code][label]' => 'above',
136 'fields[field_embed_code][type]' => 'instagram_embed',
138 $this->drupalPostForm(NULL, $edit, t('Save'));
139 $this->assertSession()->pageTextContains('Your settings have been saved.');
141 // First set absolute size of the embed.
142 $this->submitForm([], 'field_embed_code_settings_edit');
144 'fields[field_embed_code][settings_edit_form][settings][hidecaption]' => FALSE,
146 $this->submitForm($edit, 'field_embed_code_plugin_settings_update');
147 $this->drupalPostForm(NULL, [], t('Save'));
148 $this->assertSession()->pageTextContains('Your settings have been saved.');
149 $this->assertSession()->pageTextContains('Caption: Visible');
151 // Create and save the media with an instagram media code.
152 $this->drupalGet('media/add/' . $bundle->id());
154 // Example instagram from https://www.instagram.com/developer/embedding/
155 $instagram = 'https://www.instagram.com/p/bNd86MSFv6/';
158 'name[0][value]' => 'My test instagram',
159 'field_embed_code[0][value]' => $instagram,
161 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
163 // Assert that the media has been successfully saved.
164 $this->assertSession()->pageTextContains('My test instagram');
165 $this->assertSession()->pageTextContains('Embed code');
167 // Assert that the formatter exists on this page and that it has absolute
169 $this->assertFieldByXPath('//blockquote');
170 $this->assertSession()->responseContains('platform.instagram.com/en_US/embeds.js');
174 * Creates media bundle.
176 * @param array $values
177 * The media bundle values.
178 * @param string $type_name
179 * (optional) The media type provider plugin that is responsible for
180 * additional logic related to this media).
182 * @return \Drupal\Core\Entity\EntityInterface
183 * Returns newly created media bundle.
185 protected function drupalCreateMediaBundle(array $values = [], $type_name = 'generic') {
186 if (!isset($values['bundle'])) {
187 $id = strtolower($this->randomMachineName());
190 $id = $values['bundle'];
195 'type' => $type_name,
196 'type_configuration' => [],
198 'new_revision' => FALSE,
201 $bundle = MediaBundle::create($values);
202 $status = $bundle->save();
204 $this->assertEquals(SAVED_NEW, $status, t('Created media bundle %bundle.', ['%bundle' => $bundle->id()]));