0e1e167233ea9b30ed296e98084960106b020256
[yaffs-website] / web / modules / contrib / media_entity_instagram / src / Tests / InstagramEmbedFormatterTest.php
1 <?php
2
3 namespace Drupal\media_entity_instagram\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\media_entity\Tests\MediaTestTrait;
7
8 /**
9  * Tests for Instagram embed formatter.
10  *
11  * @group media_entity_instagram
12  */
13 class InstagramEmbedFormatterTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'media_entity_instagram',
22     'media_entity',
23     'node',
24     'field_ui',
25     'views_ui',
26     'block',
27   ];
28
29   use MediaTestTrait;
30
31   /**
32    * The test user.
33    *
34    * @var \Drupal\User\UserInterface
35    */
36   protected $adminUser;
37
38   /**
39    * Media entity machine id.
40    *
41    * @var string
42    */
43   protected $mediaId = 'instagram';
44
45   /**
46    * The test media bundle.
47    *
48    * @var \Drupal\media_entity\MediaBundleInterface
49    */
50   protected $testBundle;
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function setUp() {
56     parent::setUp();
57
58     $bundle['bundle'] = $this->mediaId;
59     $this->testBundle = $this->drupalCreateMediaBundle($bundle, 'instagram');
60     $this->drupalPlaceBlock('local_actions_block');
61     $this->adminUser = $this->drupalCreateUser([
62       'administer media',
63       'administer media bundles',
64       'administer media fields',
65       'administer media form display',
66       'administer media display',
67       // Media entity permissions.
68       'view media',
69       'create media',
70       'update media',
71       'update any media',
72       'delete media',
73       'delete any media',
74       // Other permissions.
75       'administer views',
76     ]);
77     $this->drupalLogin($this->adminUser);
78   }
79
80   /**
81    * Tests adding and editing an instagram embed formatter.
82    */
83   public function testManageFieldFormatter() {
84     // Test and create one media bundle.
85     $bundle = $this->testBundle;
86
87     // Assert that the media bundle has the expected values before proceeding.
88     $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
89     $this->assertFieldByName('label', $bundle->label());
90     $this->assertFieldByName('type', 'instagram');
91
92     // Add and save field settings (Embed code).
93     $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
94     $edit_conf = [
95       'new_storage_type' => 'string_long',
96       'label' => 'Embed code',
97       'field_name' => 'embed_code',
98     ];
99     $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
100     $this->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
101     $edit = [
102       'cardinality' => 'number',
103       'cardinality_number' => '1',
104     ];
105     $this->drupalPostForm(NULL, $edit, t('Save field settings'));
106     $this->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
107
108     // Set the new field as required.
109     $edit = [
110       'required' => TRUE,
111     ];
112     $this->drupalPostForm(NULL, $edit, t('Save settings'));
113     $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
114
115     // Assert that the new field configuration has been successfully saved.
116     $xpath = $this->xpath('//*[@id="field-embed-code"]');
117     $this->assertEqual((string) $xpath[0]->td[0], 'Embed code');
118     $this->assertEqual((string) $xpath[0]->td[1], 'field_embed_code');
119     $this->assertEqual((string) $xpath[0]->td[2]->a, 'Text (plain, long)');
120
121     // Test if edit worked and if new field values have been saved as
122     // expected.
123     $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
124     $this->assertFieldByName('label', $bundle->label());
125     $this->assertFieldByName('type', 'instagram');
126     $this->assertFieldByName('type_configuration[instagram][source_field]', 'field_embed_code');
127     $this->drupalPostForm(NULL, NULL, t('Save media bundle'));
128     $this->assertText('The media bundle ' . $bundle->label() . ' has been updated.');
129     $this->assertText($bundle->label());
130
131     $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/display');
132
133     // Set and save the settings of the new field.
134     $edit = [
135       'fields[field_embed_code][label]' => 'above',
136       'fields[field_embed_code][type]' => 'instagram_embed',
137     ];
138     $this->drupalPostForm(NULL, $edit, t('Save'));
139     $this->assertText('Your settings have been saved.');
140
141     // First set absolute size of the embed.
142     $this->drupalPostAjaxForm(NULL, [], 'field_embed_code_settings_edit');
143     $edit = [
144       'fields[field_embed_code][settings_edit_form][settings][hidecaption]' => FALSE,
145     ];
146     $this->drupalPostAjaxForm(NULL, $edit, 'field_embed_code_plugin_settings_update');
147     $this->drupalPostForm(NULL, [], t('Save'));
148     $this->assertText('Your settings have been saved.');
149     $this->assertText('Caption: Visible');
150
151     // Create and save the media with an instagram media code.
152     $this->drupalGet('media/add/' . $bundle->id());
153
154     // Example instagram from https://www.instagram.com/developer/embedding/
155     $instagram = 'https://www.instagram.com/p/bNd86MSFv6/';
156
157     $edit = [
158       'name[0][value]' => 'My test instagram',
159       'field_embed_code[0][value]' => $instagram,
160     ];
161     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
162
163     // Assert that the media has been successfully saved.
164     $this->assertText('My test instagram');
165     $this->assertText('Embed code');
166
167     // Assert that the formatter exists on this page and that it has absolute
168     // size.
169     $this->assertFieldByXPath('//blockquote');
170     $this->assertRaw('platform.instagram.com/en_US/embeds.js');
171   }
172
173 }