View source
<?php
namespace Drupal\Tests\youtube\Functional;
use Drupal\Tests\BrowserTestBase;
class YouTubeTest extends BrowserTestBase {
public static $modules = [
'node',
'youtube',
'field_ui',
'image',
'file',
];
protected $defaultTheme = 'stark';
public static function getInfo() {
return [
'name' => 'YouTube field',
'description' => 'Tests youtube field widgets and formatters.',
'group' => 'YouTube',
];
}
public function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$this->admin_user = $this
->drupalCreateUser([
'access content',
'access administration pages',
'administer site configuration',
'administer content types',
'administer node fields',
'administer nodes',
'create article content',
'edit any article content',
'delete any article content',
'administer image styles',
]);
$this
->drupalLogin($this->rootUser);
}
public function testRemoteImage() {
$field_name = mb_strtolower($this
->randomMachineName());
$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();
$this
->drupalGet('node/add/article');
$this
->assertFieldByName("{$field_name}[0][input]", '', t('Video input field is displayed'));
$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';
$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];
$this
->drupalGet($img_url);
$this
->assertResponse(200, 'Remote image downloaded');
}
public function testVideo() {
$field_name = mb_strtolower($this
->randomMachineName());
$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_video',
])
->save();
$this
->drupalGet('node/add/article');
$this
->assertFieldByName("{$field_name}[0][input]", '', t('Video input field is displayed'));
$video_id = 'T5y3dJYHb_A';
$value = 'https://www.youtube.com/watch?v=' . $video_id;
$embed_value = 'https://www.youtube.com/embed/' . $video_id;
$edit = [
"title[0][value]" => 'Test',
"{$field_name}[0][input]" => $value,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText(t('Article Test has been created.'));
$this
->assertRaw($embed_value);
$pattern = '<iframe.*src="' . $embed_value;
$pattern = str_replace('/', '\\/', $pattern);
$pattern = '/' . $pattern . '/s';
$this
->assertPattern($pattern);
$this
->drupalGet('node/add/article');
$value = 'not-a-url';
$edit = [
"title[0][value]" => 'Test1',
"{$field_name}[0][input]" => $value,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText(t('Please provide a valid YouTube URL.'));
}
}