1625f49441dbb688c620e80b4e400e656b76dc22
[yaffs-website] / web / modules / contrib / media_entity_twitter / src / Tests / TweetEmbedFormatterTest.php
1 <?php
2
3 namespace Drupal\media_entity_twitter\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\media_entity\Tests\MediaTestTrait;
7
8 /**
9  * Tests for Twitter embed formatter.
10  *
11  * @group media_entity_twitter
12  */
13 class TweetEmbedFormatterTest extends WebTestBase {
14
15   use MediaTestTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = array(
23     'media_entity_twitter',
24     'media_entity',
25     'node',
26     'field_ui',
27     'views_ui',
28     'block',
29     'link',
30   );
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 = 'twitter';
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, 'twitter');
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 a twitter embed formatter.
83    */
84   public function testManageEmbedFormatter() {
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->assertFieldByName('label', $bundle->label());
91     $this->assertFieldByName('type', 'twitter');
92
93     // Add and save link field type settings (Embed code).
94     $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
95     $edit_conf = [
96       'new_storage_type' => 'link',
97       'label' => 'Link URL',
98       'field_name' => 'link_url',
99     ];
100     $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
101     $this->assertText('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->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
108
109     // Set the new link field type as required.
110     $edit = [
111       'required' => TRUE,
112       'settings[link_type]' => '16',
113       'settings[title]' => '0',
114     ];
115     $this->drupalPostForm(NULL, $edit, t('Save settings'));
116     $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
117
118     // Add and save string_long field type settings (Embed code).
119     $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/fields/add-field');
120     $edit_conf = [
121       'new_storage_type' => 'string_long',
122       'label' => 'Embed code',
123       'field_name' => 'embed_code',
124     ];
125     $this->drupalPostForm(NULL, $edit_conf, t('Save and continue'));
126     $this->assertText('These settings apply to the ' . $edit_conf['label'] . ' field everywhere it is used.');
127     $edit = [
128       'cardinality' => 'number',
129       'cardinality_number' => '1',
130     ];
131     $this->drupalPostForm(NULL, $edit, t('Save field settings'));
132     $this->assertText('Updated field ' . $edit_conf['label'] . ' field settings.');
133
134     // Set the new string_long field type as required.
135     $edit = [
136       'required' => TRUE,
137     ];
138     $this->drupalPostForm(NULL, $edit, t('Save settings'));
139     $this->assertText('Saved ' . $edit_conf['label'] . ' configuration.');
140
141     // Assert that the new field types configurations have been successfully
142     // saved.
143     $xpath = $this->xpath('//*[@id="field-link-url"]');
144     $this->assertEqual((string) $xpath[0]->td[0], 'Link URL');
145     $this->assertEqual((string) $xpath[0]->td[1], 'field_link_url');
146     $this->assertEqual((string) $xpath[0]->td[2]->a, 'Link');
147
148     $xpath = $this->xpath('//*[@id="field-embed-code"]');
149     $this->assertEqual((string) $xpath[0]->td[0], 'Embed code');
150     $this->assertEqual((string) $xpath[0]->td[1], 'field_embed_code');
151     $this->assertEqual((string) $xpath[0]->td[2]->a, 'Text (plain, long)');
152
153     // Test if edit worked and if new fields values have been saved as
154     // expected.
155     $this->drupalGet('admin/structure/media/manage/' . $bundle->id());
156     $this->assertFieldByName('label', $bundle->label());
157     $this->assertFieldByName('type', 'twitter');
158     $this->assertFieldByName('type_configuration[twitter][source_field]', 'field_embed_code');
159     $this->drupalPostForm(NULL, NULL, t('Save media bundle'));
160     $this->assertText('The media bundle ' . $bundle->label() . ' has been updated.');
161     $this->assertText($bundle->label());
162
163     $this->drupalGet('admin/structure/media/manage/' . $bundle->id() . '/display');
164
165     // Set and save the settings of the new field types.
166     $edit = [
167       'fields[field_link_url][label]' => 'above',
168       'fields[field_link_url][type]' => 'twitter_embed',
169       'fields[field_embed_code][label]' => 'above',
170       'fields[field_embed_code][type]' => 'twitter_embed',
171     ];
172     $this->drupalPostForm(NULL, $edit, t('Save'));
173     $this->assertText('Your settings have been saved.');
174
175     // Create and save the media with a twitter media code.
176     $this->drupalGet('media/add/' . $bundle->id());
177
178     // Random image url from twitter.
179     $tweet_url = 'https://twitter.com/RamzyStinson/status/670650348319576064';
180
181     // Random image from twitter.
182     $tweet = '<blockquote class="twitter-tweet" lang="it"><p lang="en" dir="ltr">' .
183              'Midnight project. I ain&#39;t got no oven. So I improvise making this milo crunchy kek batik. hahahaha ' .
184              '<a href="https://twitter.com/hashtag/itssomething?src=hash">#itssomething</a> ' .
185              '<a href="https://t.co/Nvn4Q1v2ae">pic.twitter.com/Nvn4Q1v2ae</a></p>&mdash; Zi (@RamzyStinson) ' .
186              '<a href="https://twitter.com/RamzyStinson/status/670650348319576064">' .
187              '28 Novembre 2015</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>';
188
189     $edit = [
190       'name[0][value]' => 'Title',
191       'field_link_url[0][uri]' => $tweet_url,
192       'field_embed_code[0][value]' => $tweet,
193     ];
194     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
195
196     // Assert that the media has been successfully saved.
197     $this->assertText('Title');
198
199     // Assert that the link url formatter exists on this page.
200     $this->assertText('Link URL');
201     $this->assertRaw('<a href="https://twitter.com/RamzyStinson/statuses/670650348319576064">', 'Link in embedded Tweet found.');
202
203     // Assert that the string_long code formatter exists on this page.
204     $this->assertText('Embed code');
205     $this->assertRaw('<blockquote class="twitter-tweet', 'Embedded Tweet found.');
206   }
207
208 }