class FileVideoPosterFormatterTest
Same name and namespace in other branches
- 11.x core/modules/file/tests/src/Functional/Formatter/FileVideoPosterFormatterTest.php \Drupal\Tests\file\Functional\Formatter\FileVideoPosterFormatterTest
Tests rendering poster on video.
Attributes
#[CoversClass(FileVideoFormatter::class)]
#[Group('file')]
#[RunTestsInSeparateProcesses]
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\file\Functional\FileFieldTestBase uses \Drupal\Tests\file\Functional\FileFieldCreationTrait, \Drupal\Tests\TestFileCreationTrait extends \Drupal\Tests\BrowserTestBase
- class \Drupal\Tests\file\Functional\Formatter\FileVideoPosterFormatterTest uses \Drupal\Tests\image\Kernel\ImageFieldCreationTrait extends \Drupal\Tests\file\Functional\FileFieldTestBase
- class \Drupal\Tests\file\Functional\FileFieldTestBase uses \Drupal\Tests\file\Functional\FileFieldCreationTrait, \Drupal\Tests\TestFileCreationTrait extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of FileVideoPosterFormatterTest
File
-
core/
modules/ file/ tests/ src/ Functional/ Formatter/ FileVideoPosterFormatterTest.php, line 18
Namespace
Drupal\Tests\file\Functional\FormatterView source
class FileVideoPosterFormatterTest extends FileFieldTestBase {
use ImageFieldCreationTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'image',
];
/**
* Tests rendering poster on video.
*/
public function testPosterNoImageStyle() : void {
// Video field configuration.
$video_fieldname = 'field_' . mb_strtolower($this->randomMachineName());
$this->createFileField($video_fieldname, 'node', 'article', [], [
'file_extensions' => 'mp4',
]);
// Poster image field configuration.
$poster_fieldname = 'field_' . mb_strtolower($this->randomMachineName());
$this->createImageField($poster_fieldname, 'node', 'article', [], [
'file_extensions' => 'jpg',
]);
// Configure node display.
$display = \Drupal::service('entity_display.repository')->getViewDisplay('node', 'article');
$display_options = [
'type' => 'file_video',
'settings' => [
'poster' => $poster_fieldname,
'poster_image_style' => '',
],
];
$display->setComponent($video_fieldname, $display_options)
->save();
// Create files.
file_put_contents('public://file.mp4', str_repeat('t', 10));
$video1 = File::create([
'uri' => 'public://file.mp4',
'filename' => 'file.mp4',
]);
$video1->save();
$file_url = \Drupal::service('file_url_generator')->generate($video1->getFileUri())
->toString();
file_put_contents('public://file.jpg', str_repeat('t', 10));
$poster1 = File::create([
'uri' => 'public://file.jpg',
'filename' => 'file.jpg',
]);
$poster1->save();
$poster_url = \Drupal::service('file_url_generator')->generate($poster1->getFileUri())
->toString();
// Create test node.
$node = $this->drupalCreateNode([
'title' => 'Hello, world!',
'type' => 'article',
$video_fieldname => [
[
'target_id' => $video1->id(),
],
],
$poster_fieldname => [
[
'target_id' => $poster1->id(),
],
],
]);
$node->save();
$this->drupalGet('/node/' . $node->id());
$assert_session = $this->assertSession();
$assert_session->elementExists('css', "video > source[src='{$file_url}'][type='video/mp4']");
$assert_session->elementExists('css', "video[poster='{$poster_url}']");
}
/**
* Tests rendering poster on video using an image style.
*/
public function testPosterImageStyle() : void {
// Video field configuration.
$video_fieldname = 'field_' . mb_strtolower($this->randomMachineName());
$this->createFileField($video_fieldname, 'node', 'article', [], [
'file_extensions' => 'mp4',
]);
// Poster image field configuration.
$poster_fieldname = 'field_' . mb_strtolower($this->randomMachineName());
$this->createImageField($poster_fieldname, 'node', 'article', [], [
'file_extensions' => 'jpg',
]);
// Configure node display.
$display = \Drupal::service('entity_display.repository')->getViewDisplay('node', 'article');
$display_options = [
'type' => 'file_video',
'settings' => [
'poster' => $poster_fieldname,
'poster_image_style' => 'medium',
],
];
$display->setComponent($video_fieldname, $display_options)
->save();
// Create files.
file_put_contents('public://file.mp4', str_repeat('t', 10));
$video1 = File::create([
'uri' => 'public://file.mp4',
'filename' => 'file.mp4',
]);
$video1->save();
$file_url = \Drupal::service('file_url_generator')->generate($video1->getFileUri())
->toString();
file_put_contents('public://file.jpg', str_repeat('t', 10));
$poster1 = File::create([
'uri' => 'public://file.jpg',
'filename' => 'file.jpg',
]);
$poster1->save();
// Create test node.
$node = $this->drupalCreateNode([
'title' => 'Hello, world!',
'type' => 'article',
$video_fieldname => [
[
'target_id' => $video1->id(),
],
],
$poster_fieldname => [
[
'target_id' => $poster1->id(),
],
],
]);
$node->save();
$this->drupalGet('/node/' . $node->id());
$assert_session = $this->assertSession();
$assert_session->elementExists('css', "video > source[src='{$file_url}'][type='video/mp4']");
// Ensure the image style is served:
$assert_session->elementAttributeContains('css', 'video', 'poster', 'styles/medium/public/file.jpg');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.