View source
<?php
namespace Drupal\media_entity_twitter\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\media_entity\Tests\MediaTestTrait;
class TweetEmbedFormatterTest extends WebTestBase {
use MediaTestTrait;
public static $modules = array(
'media_entity_twitter',
'media_entity',
'node',
'field_ui',
'views_ui',
'block',
'link',
);
protected $adminUser;
protected $mediaId = 'twitter';
protected $testBundle;
protected function setUp() {
parent::setUp();
$bundle['bundle'] = $this->mediaId;
$this->testBundle = $this
->drupalCreateMediaBundle($bundle, 'twitter');
$this
->drupalPlaceBlock('local_actions_block');
$this->adminUser = $this
->drupalCreateUser([
'administer media',
'administer media bundles',
'administer media fields',
'administer media form display',
'administer media display',
'view media',
'create media',
'update media',
'update any media',
'delete media',
'delete any media',
'administer views',
]);
$this
->drupalLogin($this->adminUser);
}
public function testManageEmbedFormatter() {
$bundle = $this->testBundle;
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id());
$this
->assertFieldByName('label', $bundle
->label());
$this
->assertFieldByName('type', 'twitter');
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id() . '/fields/add-field');
$edit_conf = [
'new_storage_type' => 'link',
'label' => 'Link URL',
'field_name' => 'link_url',
];
$this
->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
$this
->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
$edit = [
'cardinality' => 'number',
'cardinality_number' => '1',
];
$this
->drupalPostForm(NULL, $edit, t('Save field settings'));
$this
->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
$edit = [
'required' => TRUE,
'settings[link_type]' => '16',
'settings[title]' => '0',
];
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id() . '/fields/add-field');
$edit_conf = [
'new_storage_type' => 'string_long',
'label' => 'Embed code',
'field_name' => 'embed_code',
];
$this
->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
$this
->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
$edit = [
'cardinality' => 'number',
'cardinality_number' => '1',
];
$this
->drupalPostForm(NULL, $edit, t('Save field settings'));
$this
->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
$edit = [
'required' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
$xpath = $this
->xpath('//*[@id="field-link-url"]');
$this
->assertEqual((string) $xpath[0]->td[0], 'Link URL');
$this
->assertEqual((string) $xpath[0]->td[1], 'field_link_url');
$this
->assertEqual((string) $xpath[0]->td[2]->a, 'Link');
$xpath = $this
->xpath('//*[@id="field-embed-code"]');
$this
->assertEqual((string) $xpath[0]->td[0], 'Embed code');
$this
->assertEqual((string) $xpath[0]->td[1], 'field_embed_code');
$this
->assertEqual((string) $xpath[0]->td[2]->a, 'Text (plain, long)');
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id());
$this
->assertFieldByName('label', $bundle
->label());
$this
->assertFieldByName('type', 'twitter');
$this
->assertFieldByName('type_configuration[twitter][source_field]', 'field_embed_code');
$this
->drupalPostForm(NULL, NULL, t('Save media bundle'));
$this
->assertText('The media bundle ' . $bundle
->label() . ' has been updated.');
$this
->assertText($bundle
->label());
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id() . '/display');
$edit = [
'fields[field_link_url][label]' => 'above',
'fields[field_link_url][type]' => 'twitter_embed',
'fields[field_embed_code][label]' => 'above',
'fields[field_embed_code][type]' => 'twitter_embed',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText('Your settings have been saved.');
$this
->drupalGet('media/add/' . $bundle
->id());
$tweet_url = 'https://twitter.com/RamzyStinson/status/670650348319576064';
$tweet = '<blockquote class="twitter-tweet" lang="it"><p lang="en" dir="ltr">' . 'Midnight project. I ain't got no oven. So I improvise making this milo crunchy kek batik. hahahaha ' . '<a href="https://twitter.com/hashtag/itssomething?src=hash">#itssomething</a> ' . '<a href="https://t.co/Nvn4Q1v2ae">pic.twitter.com/Nvn4Q1v2ae</a></p>— Zi (@RamzyStinson) ' . '<a href="https://twitter.com/RamzyStinson/status/670650348319576064">' . '28 Novembre 2015</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>';
$edit = [
'name[0][value]' => 'Title',
'field_link_url[0][uri]' => $tweet_url,
'field_embed_code[0][value]' => $tweet,
];
$this
->drupalPostForm(NULL, $edit, t('Save and publish'));
$this
->assertText('Title');
$this
->assertText('Link URL');
$this
->assertRaw('<a href="https://twitter.com/RamzyStinson/statuses/670650348319576064">', 'Link in embedded Tweet found.');
$this
->assertText('Embed code');
$this
->assertRaw('<blockquote class="twitter-tweet', 'Embedded Tweet found.');
}
}