You are here

function MediaBrowserSettingsTestCase::testBrowserSettings in D7 Media 7.2

Same name and namespace in other branches
  1. 7.4 tests/media.test \MediaBrowserSettingsTestCase::testBrowserSettings()
  2. 7.3 tests/media.test \MediaBrowserSettingsTestCase::testBrowserSettings()

Tests the media browser settings.

File

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

Class

MediaBrowserSettingsTestCase
Tests the media browser settings.

Code

function testBrowserSettings() {
  $settings = array(
    'scheme' => array(
      'public',
      'private',
    ),
    'type' => array(
      'image',
      'document',
    ),
    'extension' => array(
      'jpg',
      'txt',
    ),
  );

  // Perform the tests with unique permutations of $scheme, $type and
  // $extension.
  foreach ($settings['scheme'] as $scheme) {
    foreach ($settings['type'] as $type) {
      foreach ($settings['extension'] as $extension) {
        $file = $this
          ->createFileEntity(array(
          'scheme' => $scheme,
          'uid' => $this->admin_user->uid,
          'type' => $type,
          'filemime' => media_get_extension_mimetype($extension),
        ));

        // Some of the settings such as the scheme and extension are unsafe to
        // pass as query arguments, cache them and pass the cache ID.
        $options = array(
          'enabledPlugins' => array(
            'media_default--media_browser_1' => 'media_default--media_browser_1',
          ),
          'schemes' => array(
            $scheme,
          ),
          'types' => array(
            $type,
          ),
          'file_extensions' => $extension,
        );
        $cid = drupal_get_token(drupal_random_bytes(32));
        cache_set('media_options:' . $cid, $options, 'cache_form', REQUEST_TIME + 21600);

        // Verify that the file is displayed.
        $this
          ->drupalGet('media/browser', array(
          'query' => array(
            'options' => $cid,
          ),
        ));
        $this
          ->assertResponse(200);
        $xpath = $this
          ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
          ':fid' => $file->fid,
        ));
        $this
          ->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array(
          '%fid' => $file->fid,
        )));

        // Verify that no other files are also displayed.
        $files = $this
          ->xpath('//ul[@class="media-list-thumbnails"]/li/div[@data-fid]');
        $this
          ->assertEqual(count($files), 1, 'There is only one file that matches the current browser configuration.');
      }
    }
  }

  // Perform the tests with none and all of the restrictions.
  foreach (array(
    'none',
    'all',
  ) as $restrictions) {
    $options = array(
      'enabledPlugins' => array(
        'media_default--media_browser_1' => 'media_default--media_browser_1',
      ),
    );
    switch ($restrictions) {
      case 'none':
        $options['schemes'] = array();
        $options['types'] = array();
        $options['file_extensions'] = array();
        break;
      case 'all':
        $options['schemes'] = $settings['scheme'];
        $options['types'] = $settings['type'];
        $options['file_extensions'] = implode(' ', $settings['extension']);
        break;
    }
    $cid = drupal_get_token(drupal_random_bytes(32));
    cache_set('media_options:' . $cid, $options, 'cache_form', REQUEST_TIME + 21600);

    // Verify that all of the files are displayed.
    $this
      ->drupalGet('media/browser', array(
      'query' => array(
        'options' => $cid,
      ),
    ));
    $this
      ->assertResponse(200);
    $files = $this
      ->xpath('//ul[@class="media-list-thumbnails"]/li/div[@data-fid]');
    $this
      ->assertEqual(count($files), 8, format_string('All of the files were displayed when %restrictions of the restrictions were enabled.', array(
      '%restrictions' => $restrictions,
    )));
  }

  // Verify that extension restrictions do not affect remote files.
  $scheme = 'dummy-remote';
  $type = 'video';
  $extension = 'mp4';
  $file = $this
    ->createFileEntity(array(
    'scheme' => $scheme,
    'uid' => $this->admin_user->uid,
    'type' => $type,
    'filemime' => media_get_extension_mimetype($extension),
  ));
  $options = array(
    'enabledPlugins' => array(
      'media_default--media_browser_1' => 'media_default--media_browser_1',
    ),
    'schemes' => array(
      $scheme,
      'public',
    ),
    // Include a local stream wrapper in order to trigger extension restrictions.
    'types' => array(
      $type,
    ),
    'file_extensions' => 'fake',
  );
  $cid = drupal_get_token(drupal_random_bytes(32));
  cache_set('media_options:' . $cid, $options, 'cache_form', REQUEST_TIME + 21600);

  // Verify that the file is displayed.
  $this
    ->drupalGet('media/browser', array(
    'query' => array(
      'options' => $cid,
    ),
  ));
  $this
    ->assertResponse(200);
  $xpath = $this
    ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
    ':fid' => $file->fid,
  ));
  $this
    ->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array(
    '%fid' => $file->fid,
  )));

  // Verify that no other files are also displayed.
  $files = $this
    ->xpath('//ul[@class="media-list-thumbnails"]/li/div[@data-fid]');
  $this
    ->assertEqual(count($files), 1, 'There is only one file that matches the current browser configuration.');
}