Further modules included.
[yaffs-website] / web / modules / contrib / media / src / Tests / InstagramBundleTest.php
1 <?php
2
3 namespace Drupal\media\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Ensures that media bundle for instagram can be created.
9  *
10  * @group media
11  */
12 class InstagramBundleTest extends WebTestBase {
13   /**
14    * Exempt from strict schema checking.
15    *
16    * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
17    *
18    * @var bool
19    */
20   protected $strictConfigSchema = FALSE;
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = [
28     'media',
29     'media_entity',
30     'media_entity_instagram',
31     'link',
32     'node',
33     'editor',
34   ];
35
36   /**
37    * The test media bundle.
38    *
39    * @var \Drupal\media_entity\MediaBundleInterface
40    */
41   protected $testBundle;
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function setUp() {
47     parent::setUp();
48     $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('instagram');
49
50     $adminUser = $this->drupalCreateUser([
51       'view media',
52       'create media',
53       'update media',
54       'update any media',
55       'delete media',
56       'delete any media',
57       'access media overview',
58     ]);
59     $this->drupalLogin($adminUser);
60   }
61
62   /**
63    * Tests instagram media bundle creation from config files.
64    */
65   public function testMediaBundleCreationFromModule() {
66     $type_configuration = [
67       'use_instagram_api' => FALSE,
68       'source_field' => 'field_instagram_url',
69       'client_id'  => '',
70     ];
71     $field_map = [
72       'shortcode' => 'field_instagram_shortcode',
73     ];
74
75     $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
76     $this->assertEqual($this->testBundle->get('label'), 'Instagram Post', 'Correct label detected.');
77     $this->assertEqual($this->testBundle->get('description'), 'Use this to attach Instagram posts to your content.', 'Correct description detected.');
78     $this->assertEqual($this->testBundle->get('type'), 'instagram', 'Correct plugin ID detected.');
79     $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
80     $this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.');
81   }
82
83   /**
84    * Tests item creation and thumbnail.
85    */
86   public function testMediaBundleItemCreation() {
87     // Define the media item name.
88     $name = $this->randomMachineName();
89     $instagram_url = 'https://www.instagram.com/p/C/';
90     $edit = [
91       'name[0][value]' => $name,
92       'field_instagram_url[0][uri]' => $instagram_url,
93     ];
94
95     // Save the Instagram post.
96     $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
97
98     // Assert that the formatter exists on this page.
99     $this->assertFieldByXPath('//iframe');
100
101     // Let's retrieve the media id and corresponding media entity object.
102     $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'instagram')->sort('created', 'DESC')->execute();
103     $media_id = reset($media_id);
104     /** @var \Drupal\media_entity\MediaInterface $media */
105     $media = $this->container->get('entity_type.manager')
106       ->getStorage('media')
107       ->loadUnchanged($media_id);
108     $properties = $media->toArray();
109     $this->assertEqual($media->get('field_instagram_shortcode')[0]->getValue()['value'], "C", "Correct shortcode stored.");
110   }
111
112 }