You are here

public function JwPlayer7ConfigurationTest::testJwPlayerCreation in JW Player 8

Tests the jw player creation.

File

tests/src/Functional/JwPlayer7ConfigurationTest.php, line 55

Class

JwPlayer7ConfigurationTest
Tests configuration of a jw player 7 preset and creation of jw player content.

Namespace

Drupal\Tests\jw_player\Functional

Code

public function testJwPlayerCreation() {
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer site configuration',
    'administer JW Player presets',
    'administer nodes',
    'create jw_player content',
    'administer content types',
    'administer node fields',
    'administer node display',
    'access administration pages',
  ));
  $this
    ->drupalLogin($admin_user);

  // Add a random "Cloud-Hosted Account Token".
  $edit = [
    'cloud_player_library_url' => $cloud_library = 'https://content.jwplatform.com/libraries/' . $this
      ->randomMachineName(8) . '.js',
    'jw_player_hosting' => 'cloud',
    'jw_player_version' => 7,
  ];
  $this
    ->drupalPostForm('admin/config/media/jw_player/settings', $edit, t('Save configuration'));

  // Create a jw player preset.
  $edit = array(
    'label' => 'Test preset',
    'id' => 'test_preset',
    'description' => 'Test preset description',
    'settings[width]' => 100,
    'settings[height]' => 100,
    'settings[advertising][client]' => 'vast',
    'settings[advertising][tag]' => 'www.example.com/vast',
    'settings[advertising][tag_post]' => 'www.example.com/vast',
    'settings[advertising][skipoffset]' => 5,
    'settings[advertising][skipmessage]' => 'Skip ad in xx',
    'settings[advertising][skiptext]' => 'Skip',
    'settings[controlbar]' => 'bottom',
    'settings[mute]' => TRUE,
    'settings[autostart]' => TRUE,
    'settings[sharing]' => TRUE,
    'settings[sharing_sites][sites][linkedin][enabled]' => TRUE,
    'settings[sharing_sites][sites][email][enabled]' => TRUE,
  );
  $this
    ->drupalPostForm('admin/config/media/jw_player/add', $edit, t('Save'));
  $this
    ->assertText('Saved the Test preset Preset.');

  // Make sure preset has correct values.
  $this
    ->drupalGet('admin/config/media/jw_player/test_preset');
  $this
    ->assertFieldByName('label', 'Test preset');
  $this
    ->assertFieldByName('description', 'Test preset description');
  $this
    ->assertNoField('settings[mode]');
  $this
    ->assertFieldByName('settings[preset_source]', 'drupal');
  $this
    ->assertFieldByName('settings[mute]', '1');
  $this
    ->assertFieldByName('settings[sharing]', '1');
  $this
    ->assertFieldByName('settings[skin]', NULL);
  $this
    ->assertFieldByName('settings[advertising][client]', 'vast');
  $this
    ->assertFieldByName('settings[advertising][tag]', 'www.example.com/vast');
  $this
    ->assertFieldByName('settings[advertising][tag_post]', 'www.example.com/vast');
  $this
    ->assertFieldByName('settings[advertising][skipoffset]', 5);
  $this
    ->assertFieldByName('settings[advertising][skipmessage]', 'Skip ad in xx');
  $this
    ->assertFieldByName('settings[advertising][skiptext]', 'Skip');
  $this
    ->assertFieldByName('settings[controlbar]', 'bottom');
  $this
    ->assertFieldByName('settings[mute]', TRUE);
  $this
    ->assertFieldByName('settings[autostart]', TRUE);
  $this
    ->assertFieldByName('settings[sharing]', TRUE);
  $this
    ->assertFieldByName('settings[sharing_sites][sites][linkedin][enabled]', TRUE);
  $this
    ->assertFieldByName('settings[sharing_sites][sites][email][enabled]', TRUE);

  // Create a JW player format file field in JW content type.
  static::fieldUIAddNewField('admin/structure/types/manage/jw_player', 'video', 'Video', 'file', array(), array(
    'settings[file_extensions]' => 'mp4',
  ));

  // Create a Image field in JW content type.
  static::fieldUIAddNewField('admin/structure/types/manage/jw_player', 'image_preview', 'image_preview', 'image', [], []);
  $this
    ->drupalPostForm('admin/structure/types/manage/jw_player/display', array(
    'fields[field_video][type]' => 'jwplayer_formatter',
  ), t('Save'));
  $this
    ->drupalPostForm(NULL, NULL, 'field_video_settings_edit');

  // Set the image field as preview of the jw player video.
  $edit = [
    'fields[field_video][settings_edit_form][settings][jwplayer_preset]' => 'test_preset',
    'fields[field_video][settings_edit_form][settings][preview_image_field]' => 'node:jw_player|field_image_preview',
    'fields[field_video][settings_edit_form][settings][preview_image_style]' => 'medium',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Update'));
  $this
    ->drupalPostForm(NULL, NULL, t('Save'));

  // Make sure JW preset is correct.
  $this
    ->assertText('Preset: Test preset');
  $this
    ->assertText('Dimensions: 100x100, uniform');
  $this
    ->assertText('Preview: image_preview (Medium');
  $this
    ->assertText('Enabled options: Autostart, Mute, Sharing');
  $this
    ->assertText('Sharing sites: Email, LinkedIn');

  // Make sure the formatter reports correct dependencies.

  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $view_display */
  $view_display = EntityViewDisplay::load('node.jw_player.default');
  $this
    ->assertTrue(in_array('jw_player.preset.test_preset', $view_display
    ->getDependencies()['config']));

  // Create a 'video' file, which has .mp4 extension.
  $text = 'Trust me I\'m a video';
  file_put_contents('temporary://myVideo.mp4', $text);

  // Upload an image in the node.
  $images = $this
    ->getTestFiles('image')[1];
  $this
    ->drupalPostForm('node/add/jw_player', [
    'files[field_image_preview_0]' => $images->uri,
  ], t('Upload'));

  // Create video content from JW content type.
  $edit = array(
    'title[0][value]' => 'Test video',
    'files[field_video_0]' => \Drupal::service('file_system')
      ->realpath('temporary://myVideo.mp4'),
    'field_image_preview[0][alt]' => 'preview_image',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('JW content Test video has been created.');
  $element = $this
    ->getSession()
    ->getPage()
    ->find('css', 'video');
  $id = $element
    ->getAttribute('id');

  // Get the player js.
  $player_info = $this
    ->xpath('//script[@data-drupal-selector="drupal-settings-json"]')[0]
    ->getHtml();
  $decoded_info = json_decode($player_info, TRUE);

  // Assert the image and file.
  $image = file_create_url(\Drupal::token()
    ->replace('public://styles/medium/public/[date:custom:Y]-[date:custom:m]/' . $images->filename));
  $this
    ->assertTrue(strpos($decoded_info['jw_player']['players'][$id]['image'], $image) !== FALSE);
  $file = file_create_url(\Drupal::token()
    ->replace('public://[date:custom:Y]-[date:custom:m]/myVideo.mp4'));
  $this
    ->assertEqual($file, $decoded_info['jw_player']['players'][$id]['file']);

  // Make sure the hash is there.
  $this
    ->assertEquals(1, preg_match('/jwplayer-[a-zA-Z0-9]{1,}$/', $id));

  // Check the library created because of cloud hosting.
  $this
    ->assertRaw('<script src="' . $cloud_library . '"></script>');

  // Change player hosting.
  $edit = [
    'jw_player_hosting' => 'self',
    'jw_player_key' => 'this_is_my_fancy_license_key',
  ];
  $this
    ->drupalPostForm('admin/config/media/jw_player/settings', $edit, t('Save configuration'));
  $this
    ->drupalGet('node/1');
  $element = $this
    ->getSession()
    ->getPage()
    ->find('css', 'video');
  $id = $element
    ->getAttribute('id');

  // Get the player js.
  $player_info = $this
    ->xpath('//script[@data-drupal-selector="drupal-settings-json"]')[0]
    ->getHtml();
  $decoded_info = json_decode($player_info, TRUE);

  // Assert the json has been updated.
  $this
    ->assertEquals('100', $decoded_info['jw_player']['players'][$id]['width']);
  $this
    ->assertEquals('100', $decoded_info['jw_player']['players'][$id]['height']);
  $this
    ->assertTrue($decoded_info['jw_player']['players'][$id]['autostart']);
  $this
    ->assertTrue($decoded_info['jw_player']['players'][$id]['mute']);
  $this
    ->assertEquals([
    0 => 'email',
    1 => 'linkedin',
  ], $decoded_info['jw_player']['players'][$id]['sharing']['sites']);
  $this
    ->assertEquals('5', $decoded_info['jw_player']['players'][$id]['advertising']['skipoffset']);
  $this
    ->assertEquals('Skip ad in xx', $decoded_info['jw_player']['players'][$id]['advertising']['skipmessage']);
  $this
    ->assertEquals('Skip', $decoded_info['jw_player']['players'][$id]['advertising']['skiptext']);

  // Check the library created because of cloud hosting.
  $this
    ->assertNoRaw('<script src="' . $cloud_library . '"></script>');

  // Test the formatter for a link field.
  static::fieldUIAddNewField('admin/structure/types/manage/jw_player', 'jw_link', 'JW link', 'link', [], []);
  $this
    ->drupalPostForm('admin/structure/types/manage/jw_player/display', array(
    'fields[field_jw_link][type]' => 'jwplayer_formatter',
  ), t('Save'));

  // Add a new node.
  $this
    ->drupalGet('node/add/jw_player');
  $edit = [
    'title[0][value]' => 'jw_link',
    'field_jw_link[0][uri]' => 'https://www.youtube.com/watch?v=mAAIfi0pYHw',
    'field_jw_link[0][title]' => 'Jw Player Drupal 7',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle('jw_link');
  $this
    ->drupalGet('node/' . $node
    ->id());
  $element = $this
    ->getSession()
    ->getPage()
    ->find('css', 'video');
  $id = $element
    ->getAttribute('id');

  // Get the player js.
  $player_info = (string) $this
    ->xpath('//script[@data-drupal-selector="drupal-settings-json"]')[0]
    ->getHtml();
  $decoded_info = json_decode($player_info, TRUE);

  // Check the link info is in the player js.
  $this
    ->assertEqual('https://www.youtube.com/watch?v=mAAIfi0pYHw', $decoded_info['jw_player']['players'][$id]['file']);
}