You are here

function MediaElementSettingsTestCase::testWidgetSettings in D7 Media 7.4

Same name and namespace in other branches
  1. 7.2 tests/media.test \MediaElementSettingsTestCase::testWidgetSettings()
  2. 7.3 tests/media.test \MediaElementSettingsTestCase::testWidgetSettings()

Tests the media file field widget settings.

File

tests/media.test, line 974
Tests for media.module.

Class

MediaElementSettingsTestCase
Tests the 'media' element type settings.

Code

function testWidgetSettings() {

  // Use 'page' instead of 'article', so that the 'article' image field does
  // not conflict with this test. If in the future the 'page' type gets its
  // own default file or image field, this test can be made more robust by
  // using a custom node type.
  $type_name = 'page';
  $field_name = strtolower($this
    ->randomName());
  $this
    ->createFileField($field_name, $type_name);
  $field = field_info_field($field_name);
  $instance = field_info_instance('node', $field_name, $type_name);
  $javascript = $this
    ->drupalGet("node/add/{$type_name}");
  $multiselect = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  $field_widget = array(
    'elements' => array(
      '.js-media-element-edit-' . $field_name . '-' . LANGUAGE_NONE . '-0-upload' => array(
        'global' => array(
          'types' => array(
            'image' => 'image',
          ),
          'enabledPlugins' => array(),
          'schemes' => array(
            'public' => 'public',
          ),
          'file_directory' => '',
          'file_extensions' => 'txt',
          'max_filesize' => '',
          'uri_scheme' => 'public',
          'multiselect' => $multiselect,
          'field' => $field_name,
        ),
      ),
    ),
  );
  $settings = drupal_json_encode(drupal_array_merge_deep_array($field_widget));
  $string_with_options = '-0-upload":{"global":{"options":"';
  $index_of_cid = strpos($javascript, $string_with_options) + strlen($string_with_options);
  $index_end_of_cid = strpos($javascript, '"', $index_of_cid + 1);
  $cid = substr($javascript, $index_of_cid, $index_end_of_cid - $index_of_cid);

  // Retrieve the security sensitive options from the cache using the cid parsed out from the $javascript variable
  $retrieved_settings = cache_get('media_options:' . $cid, 'cache_form');
  $retrieved_settings = array(
    '.js-media-element-edit-' . $field_name . '-' . LANGUAGE_NONE . '-0-upload' => array(
      'global' => $retrieved_settings->data,
    ),
  );
  $retrieved_settings_json = drupal_json_encode($retrieved_settings);
  $this
    ->assertTrue($retrieved_settings_json == $settings, 'Media file field widget retrieved from cache and has element-specific settings.');
  $this
    ->assertTrue(strpos($javascript, $cid) > 0, 'Media file field widget is cached and its` cache id is found.');
}