public function YouTubeTest::testRemoteImage in YouTube Field 8
Test downloading a remote image.
File
- tests/
src/ Functional/ YouTubeTest.php, line 65
Class
- YouTubeTest
- Tests youtube field widgets and formatters.
Namespace
Drupal\Tests\youtube\FunctionalCode
public function testRemoteImage() {
$field_name = mb_strtolower($this
->randomMachineName());
// Create a field.
$field_storage = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->create([
'field_name' => $field_name,
'entity_type' => 'node',
'translatable' => FALSE,
'type' => 'youtube',
'cardinality' => '1',
]);
$field_storage
->save();
$field = \Drupal::entityTypeManager()
->getStorage('field_config')
->create([
'field_storage' => $field_storage,
'bundle' => 'article',
'title' => DRUPAL_DISABLED,
]);
$field
->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.article.default')
->setComponent($field_name, [
'type' => 'youtube',
'settings' => [],
])
->save();
\Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('node.article.default')
->setComponent($field_name, [
'type' => 'youtube_thumbnail',
'settings' => [
'image_style' => FALSE,
],
])
->save();
// Display creation form.
$this
->drupalGet('node/add/article');
$this
->assertFieldByName("{$field_name}[0][input]", '', t('Video input field is displayed'));
// Verify that a valid URL can be submitted.
$video_id = 'T5y3dJYHb_A';
$value = 'http://www.youtube.com/watch?v=' . $video_id;
$edit = [
"title[0][value]" => "Test1",
"{$field_name}[0][input]" => $value,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText(t('Article Test1 has been created.'));
$video_id = 'T5y3dJYHb_A';
// Verify that the image markup is displayed.
$matches = [];
$subject = $this
->getSession()
->getPage()
->getContent();
$pattern = '/<img .*src="(.*?' . $video_id . '[\\/\\d+]*\\.[jpg].*?)"/s';
preg_match($pattern, $subject, $matches);
$this
->assertPattern($pattern);
$img_url = $matches[1];
// Verify that the remote image is created.
$this
->drupalGet($img_url);
$this
->assertResponse(200, 'Remote image downloaded');
}