function FileVideoPosterFormatterTest::testPosterNoImageStyle

Same name and namespace in other branches
  1. main core/modules/file/tests/src/Functional/Formatter/FileVideoPosterFormatterTest.php \Drupal\Tests\file\Functional\Formatter\FileVideoPosterFormatterTest::testPosterNoImageStyle()

Tests rendering poster on video.

File

core/modules/file/tests/src/Functional/Formatter/FileVideoPosterFormatterTest.php, line 40

Class

FileVideoPosterFormatterTest
Tests rendering poster on video.

Namespace

Drupal\Tests\file\Functional\Formatter

Code

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}']");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.