View source
<?php
namespace Drupal\Tests\media_entity_twitter\Functional;
use Drupal\Tests\media\Functional\MediaFunctionalTestBase;
class TweetEmbedFormatterTest extends MediaFunctionalTestBase {
public static $modules = [
'media_entity_twitter',
'link',
];
protected $defaultTheme = 'stark';
public function testManageEmbedFormatter() {
$bundle = $this
->createMediaType('twitter', [
'id' => 'twitter',
]);
$source = $bundle
->getSource();
$source_field = $source
->getSourceFieldDefinition($bundle);
$component = \Drupal::service('plugin.manager.field.widget')
->prepareConfiguration('string', []);
\Drupal::configFactory()
->getEditable('media.settings')
->set('standalone_url', TRUE)
->save(TRUE);
$this->container
->get('router.builder')
->rebuild();
\Drupal::service('entity_display.repository')
->getFormDisplay('media', $bundle
->id(), 'default')
->setComponent($source_field
->getName(), $component)
->save();
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id());
$this
->assertSession()
->fieldValueEquals('label', $bundle
->label());
$this
->assertSession()
->fieldValueEquals('source', 'twitter');
$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
->assertSession()
->responseContains('These settings apply to the <em class="placeholder">' . $edit_conf['label'] . '</em> field everywhere it is used.');
$edit = [
'cardinality' => 'number',
'cardinality_number' => '1',
];
$this
->drupalPostForm(NULL, $edit, t('Save field settings'));
$this
->assertSession()
->responseContains('Updated field <em class="placeholder">' . $edit_conf['label'] . '</em> field settings.');
$edit = [
'required' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->assertSession()
->responseContains('Saved <em class="placeholder">' . $edit_conf['label'] . '</em> configuration.');
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id() . '/fields');
$xpath = $this
->xpath('//*[@id=:id]/td', [
':id' => 'field-media-twitter',
]);
$this
->assertEquals((string) $xpath[0]
->getText(), 'Tweet URL');
$this
->assertEquals((string) $xpath[1]
->getText(), 'field_media_twitter');
$this
->assertEquals((string) $xpath[2]
->find('css', 'a')
->getText(), 'Text (plain)');
$xpath = $this
->xpath('//*[@id=:id]/td', [
':id' => 'field-embed-code',
]);
$this
->assertEquals((string) $xpath[0]
->getText(), 'Embed code');
$this
->assertEquals((string) $xpath[1]
->getText(), 'field_embed_code');
$this
->assertEquals((string) $xpath[2]
->find('css', 'a')
->getText(), 'Text (plain, long)');
$this
->drupalGet('admin/structure/media/manage/' . $bundle
->id() . '/display');
$edit = [
'fields[field_media_twitter][parent]' => 'content',
'fields[field_media_twitter][region]' => 'content',
'fields[field_media_twitter][label]' => 'above',
'fields[field_media_twitter][type]' => 'twitter_embed',
'fields[field_embed_code][label]' => 'above',
'fields[field_embed_code][type]' => 'twitter_embed',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->responseContains('Your settings have been saved.');
$this
->drupalGet('media/add/' . $bundle
->id());
$tweet_url = 'https://twitter.com/DrupalConEur/status/1176518741208817664';
$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_media_twitter[0][value]' => $tweet_url,
'field_embed_code[0][value]' => $tweet,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->drupalGet('media/1');
$this
->assertSession()
->pageTextContains('Title');
$this
->assertSession()
->pageTextContains('Tweet URL');
$this
->assertSession()
->responseContains('<a href="https://twitter.com/RamzyStinson/statuses/670650348319576064">', 'Link in embedded Tweet found.');
$this
->assertSession()
->pageTextContains('Embed code');
$this
->assertSession()
->responseContains('<blockquote class="twitter-tweet', 'Embedded Tweet found.');
}
}