3 namespace Drupal\media_entity_twitter\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\media_entity\Tests\MediaTestTrait;
9 * Tests for Twitter embed formatter.
11 * @group media_entity_twitter
13 class TweetEmbedFormatterTest extends WebTestBase {
22 public static $modules = array(
23 'media_entity_twitter',
35 * @var \Drupal\User\UserInterface
40 * Media entity machine id.
44 protected $mediaId = 'twitter';
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, 'twitter');
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 a twitter embed formatter.
84 public function testManageEmbedFormatter() {
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->assertFieldByName('label', $bundle->label());
91 $this->assertFieldByName('type', 'twitter');
93 // Add and save link field type settings (Embed code).
94 $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
96 'new_storage_type' => 'link',
97 'label' => 'Link URL',
98 'field_name' => 'link_url',
100 $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
101 $this->assertText('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->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
109 // Set the new link field type as required.
112 'settings[link_type]' => '16',
113 'settings[title]' => '0',
115 $this->drupalPostForm(NULL, $edit, t('Save settings'));
116 $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
118 // Add and save string_long field type settings (Embed code).
119 $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
121 'new_storage_type' => 'string_long',
122 'label' => 'Embed code',
123 'field_name' => 'embed_code',
125 $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
126 $this->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
128 'cardinality' => 'number',
129 'cardinality_number' => '1',
131 $this->drupalPostForm(NULL, $edit, t('Save field settings'));
132 $this->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
134 // Set the new string_long field type as required.
138 $this->drupalPostForm(NULL, $edit, t('Save settings'));
139 $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
141 // Assert that the new field types configurations have been successfully
143 $xpath = $this->xpath('//*[@id="field-link-url"]');
144 $this->assertEqual((string) $xpath[0]->td[0], 'Link URL');
145 $this->assertEqual((string) $xpath[0]->td[1], 'field_link_url');
146 $this->assertEqual((string) $xpath[0]->td[2]->a, 'Link');
148 $xpath = $this->xpath('//*[@id="field-embed-code"]');
149 $this->assertEqual((string) $xpath[0]->td[0], 'Embed code');
150 $this->assertEqual((string) $xpath[0]->td[1], 'field_embed_code');
151 $this->assertEqual((string) $xpath[0]->td[2]->a, 'Text (plain, long)');
153 // Test if edit worked and if new fields values have been saved as
155 $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
156 $this->assertFieldByName('label', $bundle->label());
157 $this->assertFieldByName('type', 'twitter');
158 $this->assertFieldByName('type_configuration[twitter][source_field]', 'field_embed_code');
159 $this->drupalPostForm(NULL, NULL, t('Save media bundle'));
160 $this->assertText('The media bundle ' . $bundle->label() . ' has been updated.');
161 $this->assertText($bundle->label());
163 $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/display');
165 // Set and save the settings of the new field types.
167 'fields[field_link_url][label]' => 'above',
168 'fields[field_link_url][type]' => 'twitter_embed',
169 'fields[field_embed_code][label]' => 'above',
170 'fields[field_embed_code][type]' => 'twitter_embed',
172 $this->drupalPostForm(NULL, $edit, t('Save'));
173 $this->assertText('Your settings have been saved.');
175 // Create and save the media with a twitter media code.
176 $this->drupalGet('media/add/' . $bundle->id());
178 // Random image url from twitter.
179 $tweet_url = 'https://twitter.com/RamzyStinson/status/670650348319576064';
181 // Random image from twitter.
182 $tweet = '<blockquote class="twitter-tweet" lang="it"><p lang="en" dir="ltr">' .
183 'Midnight project. I ain't got no oven. So I improvise making this milo crunchy kek batik. hahahaha ' .
184 '<a href="https://twitter.com/hashtag/itssomething?src=hash">#itssomething</a> ' .
185 '<a href="https://t.co/Nvn4Q1v2ae">pic.twitter.com/Nvn4Q1v2ae</a></p>— Zi (@RamzyStinson) ' .
186 '<a href="https://twitter.com/RamzyStinson/status/670650348319576064">' .
187 '28 Novembre 2015</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>';
190 'name[0][value]' => 'Title',
191 'field_link_url[0][uri]' => $tweet_url,
192 'field_embed_code[0][value]' => $tweet,
194 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
196 // Assert that the media has been successfully saved.
197 $this->assertText('Title');
199 // Assert that the link url formatter exists on this page.
200 $this->assertText('Link URL');
201 $this->assertRaw('<a href="https://twitter.com/RamzyStinson/statuses/670650348319576064">', 'Link in embedded Tweet found.');
203 // Assert that the string_long code formatter exists on this page.
204 $this->assertText('Embed code');
205 $this->assertRaw('<blockquote class="twitter-tweet', 'Embedded Tweet found.');