3 namespace Drupal\media_entity_instagram\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\media_entity\Tests\MediaTestTrait;
9 * Tests for Instagram embed formatter.
11 * @group media_entity_instagram
13 class InstagramEmbedFormatterTest extends WebTestBase {
20 public static $modules = [
21 'media_entity_instagram',
34 * @var \Drupal\User\UserInterface
39 * Media entity machine id.
43 protected $mediaId = 'instagram';
46 * The test media bundle.
48 * @var \Drupal\media_entity\MediaBundleInterface
50 protected $testBundle;
55 protected function setUp() {
58 $bundle['bundle'] = $this->mediaId;
59 $this->testBundle = $this->drupalCreateMediaBundle($bundle, 'instagram');
60 $this->drupalPlaceBlock('local_actions_block');
61 $this->adminUser = $this->drupalCreateUser([
63 'administer media bundles',
64 'administer media fields',
65 'administer media form display',
66 'administer media display',
67 // Media entity permissions.
77 $this->drupalLogin($this->adminUser);
81 * Tests adding and editing an instagram embed formatter.
83 public function testManageFieldFormatter() {
84 // Test and create one media bundle.
85 $bundle = $this->testBundle;
87 // Assert that the media bundle has the expected values before proceeding.
88 $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
89 $this->assertFieldByName('label', $bundle->label());
90 $this->assertFieldByName('type', 'instagram');
92 // Add and save field settings (Embed code).
93 $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
95 'new_storage_type' => 'string_long',
96 'label' => 'Embed code',
97 'field_name' => 'embed_code',
99 $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
100 $this->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
102 'cardinality' => 'number',
103 'cardinality_number' => '1',
105 $this->drupalPostForm(NULL, $edit, t('Save field settings'));
106 $this->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
108 // Set the new field as required.
112 $this->drupalPostForm(NULL, $edit, t('Save settings'));
113 $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
115 // Assert that the new field configuration has been successfully saved.
116 $xpath = $this->xpath('//*[@id="field-embed-code"]');
117 $this->assertEqual((string) $xpath[0]->td[0], 'Embed code');
118 $this->assertEqual((string) $xpath[0]->td[1], 'field_embed_code');
119 $this->assertEqual((string) $xpath[0]->td[2]->a, 'Text (plain, long)');
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->assertFieldByName('label', $bundle->label());
125 $this->assertFieldByName('type', 'instagram');
126 $this->assertFieldByName('type_configuration[instagram][source_field]', 'field_embed_code');
127 $this->drupalPostForm(NULL, NULL, t('Save media bundle'));
128 $this->assertText('The media bundle ' . $bundle->label() . ' has been updated.');
129 $this->assertText($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->assertText('Your settings have been saved.');
141 // First set absolute size of the embed.
142 $this->drupalPostAjaxForm(NULL, [], 'field_embed_code_settings_edit');
144 'fields[field_embed_code][settings_edit_form][settings][hidecaption]' => FALSE,
146 $this->drupalPostAjaxForm(NULL, $edit, 'field_embed_code_plugin_settings_update');
147 $this->drupalPostForm(NULL, [], t('Save'));
148 $this->assertText('Your settings have been saved.');
149 $this->assertText('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->assertText('My test instagram');
165 $this->assertText('Embed code');
167 // Assert that the formatter exists on this page and that it has absolute
169 $this->assertFieldByXPath('//blockquote');
170 $this->assertRaw('platform.instagram.com/en_US/embeds.js');