9e2d99b3d45bdebeb1a452cbb6bcaf0ff077c271
[yaffs-website] / web / modules / contrib / media_entity_instagram / tests / src / Functional / InstagramEmbedFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\media_entity_instagram\Functional;
4
5 use Drupal\media_entity\Entity\MediaBundle;
6 use Drupal\media_entity\Tests\MediaTestTrait;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests for Instagram embed formatter.
11  *
12  * @group media_entity_instagram
13  */
14 class InstagramEmbedFormatterTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'media_entity_instagram',
23     'media_entity',
24     'node',
25     'field_ui',
26     'views_ui',
27     'block',
28   ];
29
30   use MediaTestTrait;
31
32   /**
33    * The test user.
34    *
35    * @var \Drupal\User\UserInterface
36    */
37   protected $adminUser;
38
39   /**
40    * Media entity machine id.
41    *
42    * @var string
43    */
44   protected $mediaId = 'instagram';
45
46   /**
47    * The test media bundle.
48    *
49    * @var \Drupal\media_entity\MediaBundleInterface
50    */
51   protected $testBundle;
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function setUp() {
57     parent::setUp();
58
59     $bundle['bundle'] = $this->mediaId;
60     $this->testBundle = $this->drupalCreateMediaBundle($bundle, 'instagram');
61     $this->drupalPlaceBlock('local_actions_block');
62     $this->adminUser = $this->drupalCreateUser([
63       'administer media',
64       'administer media bundles',
65       'administer media fields',
66       'administer media form display',
67       'administer media display',
68       // Media entity permissions.
69       'view media',
70       'create media',
71       'update media',
72       'update any media',
73       'delete media',
74       'delete any media',
75       // Other permissions.
76       'administer views',
77     ]);
78     $this->drupalLogin($this->adminUser);
79   }
80
81   /**
82    * Tests adding and editing an instagram embed formatter.
83    */
84   public function testManageFieldFormatter() {
85     // Test and create one media bundle.
86     $bundle = $this->testBundle;
87
88     // Assert that the media bundle has the expected values before proceeding.
89     $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
90     $this->assertSession()->fieldValueEquals('label', $bundle->label());
91     $this->assertSession()->fieldValueEquals('type', 'instagram');
92
93     // Add and save field settings (Embed code).
94     $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
95     $edit_conf = [
96       'new_storage_type' => 'string_long',
97       'label' => 'Embed code',
98       'field_name' => 'embed_code',
99     ];
100     $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
101     $this->assertSession()->pageTextContains('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
102     $edit = [
103       'cardinality' => 'number',
104       'cardinality_number' => '1',
105     ];
106     $this->drupalPostForm(NULL, $edit, t('Save field settings'));
107     $this->assertSession()->pageTextContains('Updated field ' . $edit_conf['label'] . ' field settings.');
108
109     // Set the new field as required.
110     $edit = [
111       'required' => TRUE,
112     ];
113     $this->drupalPostForm(NULL, $edit, t('Save settings'));
114     $this->assertSession()->pageTextContains('Saved ' . $edit_conf['label'] . ' configuration.');
115
116     // Assert that the new field configuration has been successfully saved.
117     $this->assertEquals('Embed code', $this->xpath('//*[@id="field-embed-code"]/td[1]')[0]->getText());
118     $this->assertEquals('field_embed_code', $this->xpath('//*[@id="field-embed-code"]/td[2]')[0]->getText());
119     $this->assertEquals('Text (plain, long)', $this->xpath('//*[@id="field-embed-code"]/td[3]')[0]->getText());
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->assertSession()->fieldValueEquals('label', $bundle->label());
125     $this->assertSession()->fieldValueEquals('type', 'instagram');
126     $this->assertSession()->fieldValueEquals('type_configuration[instagram][source_field]', 'field_embed_code');
127     $this->drupalPostForm(NULL, NULL, t('Save media bundle'));
128     $this->assertSession()->pageTextContains('The media bundle ' . $bundle->label() . ' has been updated.');
129     $this->assertSession()->pageTextContains($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->assertSession()->pageTextContains('Your settings have been saved.');
140
141     // First set absolute size of the embed.
142     $this->submitForm([], 'field_embed_code_settings_edit');
143     $edit = [
144       'fields[field_embed_code][settings_edit_form][settings][hidecaption]' => FALSE,
145     ];
146     $this->submitForm($edit, 'field_embed_code_plugin_settings_update');
147     $this->drupalPostForm(NULL, [], t('Save'));
148     $this->assertSession()->pageTextContains('Your settings have been saved.');
149     $this->assertSession()->pageTextContains('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->assertSession()->pageTextContains('My test instagram');
165     $this->assertSession()->pageTextContains('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->assertSession()->responseContains('platform.instagram.com/en_US/embeds.js');
171   }
172
173   /**
174    * Creates media bundle.
175    *
176    * @param array $values
177    *   The media bundle values.
178    * @param string $type_name
179    *   (optional) The media type provider plugin that is responsible for
180    *   additional logic related to this media).
181    *
182    * @return \Drupal\Core\Entity\EntityInterface
183    *   Returns newly created media bundle.
184    */
185   protected function drupalCreateMediaBundle(array $values = [], $type_name = 'generic') {
186     if (!isset($values['bundle'])) {
187       $id = strtolower($this->randomMachineName());
188     }
189     else {
190       $id = $values['bundle'];
191     }
192     $values += [
193       'id' => $id,
194       'label' => $id,
195       'type' => $type_name,
196       'type_configuration' => [],
197       'field_map' => [],
198       'new_revision' => FALSE,
199     ];
200
201     $bundle = MediaBundle::create($values);
202     $status = $bundle->save();
203
204     $this->assertEquals(SAVED_NEW, $status, t('Created media bundle %bundle.', ['%bundle' => $bundle->id()]));
205
206     return $bundle;
207   }
208
209 }