3 namespace Drupal\media\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Ensures that media bundle for tweets can be created.
12 class TweetBundleTest extends WebTestBase {
14 * Exempt from strict schema checking.
16 * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
20 protected $strictConfigSchema = FALSE;
27 public static $modules = [
30 'media_entity_twitter',
37 * The test media bundle.
39 * @var \Drupal\media_entity\MediaBundleInterface
41 protected $testBundle;
46 protected function setUp() {
48 $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('tweet');
50 $adminUser = $this->drupalCreateUser([
57 'access media overview',
59 $this->drupalLogin($adminUser);
63 * Tests tweet media bundle creation from config files.
65 public function testMediaBundleCreationFromModule() {
66 $type_configuration = [
67 'use_twitter_api' => FALSE,
68 'source_field' => 'field_tweet_url',
70 'consumer_secret' => '',
71 'oauth_access_token' => '',
72 'oauth_access_token_secret' => '',
75 'id' => 'field_tweet_id',
76 'user' => 'field_tweet_author',
79 $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
80 $this->assertEqual($this->testBundle->get('label'), 'Tweet', 'Correct label detected.');
81 $this->assertEqual($this->testBundle->get('description'), 'Use this to embed Twitter content on your site.', 'Correct description detected.');
82 $this->assertEqual($this->testBundle->get('type'), 'twitter', 'Correct plugin ID detected.');
83 $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
84 $this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.');
88 * Tests item creation.
90 public function testMediaBundleItemCreation() {
91 // Define the media item name.
92 $name = $this->randomMachineName();
93 $tweet_url = 'https://twitter.com/jack/status/20';
95 'name[0][value]' => $name,
96 'field_tweet_url[0][uri]' => $tweet_url,
100 $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
101 // Let's retrieve the media id.
102 $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'tweet')->sort('created', 'DESC')->execute();
103 $media_id = reset($media_id);
104 $media = $this->container->get('entity_type.manager')
105 ->getStorage('media')
106 ->loadUnchanged($media_id);
107 $properties = $media->toArray();
108 $this->assertEqual($media->get('field_tweet_author')[0]->getValue()['value'], "jack", "Correct tweet author stored.");
109 $this->assertEqual($media->get('field_tweet_id')[0]->getValue()['value'], "20", "Correct tweet id stored.");