You are here

function MediaBrowserLibraryTestCase::testFilesBrowserLibrary in D7 Media 7.4

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

Tests media browser 'Library' tab with different user permissions.

File

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

Class

MediaBrowserLibraryTestCase
Tests the media browser 'Library' tab.

Code

function testFilesBrowserLibrary() {

  // Load only the 'Library' tab of the media browser.
  $options = array(
    'query' => array(
      'enabledPlugins' => array(
        'media_default--media_browser_1' => 'media_default--media_browser_1',
      ),
    ),
  );
  $files['public_image'] = $this
    ->createFileEntity(array(
    'scheme' => 'public',
    'uid' => $this->base_user_1->uid,
    'type' => 'image',
  ));
  $files['public_document'] = $this
    ->createFileEntity(array(
    'scheme' => 'public',
    'uid' => $this->base_user_2->uid,
    'type' => 'document',
  ));
  $files['private_image'] = $this
    ->createFileEntity(array(
    'scheme' => 'private',
    'uid' => $this->base_user_1->uid,
    'type' => 'image',
  ));
  $files['private_document'] = $this
    ->createFileEntity(array(
    'scheme' => 'private',
    'uid' => $this->base_user_2->uid,
    'type' => 'document',
  ));

  // Verify all files are displayed for administrators.
  $this
    ->drupalGet('media/browser', $options);
  $this
    ->assertResponse(200);
  foreach ($files as $file) {
    $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 public files are displayed.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->base_user_1);
  $this
    ->drupalGet('media/browser', $options);
  $this
    ->assertResponse(200);
  $xpath = $this
    ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
    ':fid' => $files['public_image']->fid,
  ));
  $this
    ->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array(
    '%fid' => $files['public_image']->fid,
  )));
  $xpath = $this
    ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
    ':fid' => $files['public_document']->fid,
  ));
  $this
    ->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array(
    '%fid' => $files['public_document']->fid,
  )));

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

  // Verify user cannot see private file of other users.
  $xpath = $this
    ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
    ':fid' => $files['private_image']->fid,
  ));
  $this
    ->assertNoFieldByXPath($xpath, TRUE, format_string('File with file ID %fid not found.', array(
    '%fid' => $files['private_image']->fid,
  )));

  // Verify private file is displayed with permission.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->base_user_3);
  $this
    ->drupalGet('media/browser', $options);
  $this
    ->assertResponse(200);

  // Verify user can see private file of other users.
  $xpath = $this
    ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
    ':fid' => $files['private_document']->fid,
  ));
  $this
    ->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array(
    '%fid' => $files['private_document']->fid,
  )));
  $xpath = $this
    ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
    ':fid' => $files['private_image']->fid,
  ));
  $this
    ->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array(
    '%fid' => $files['private_image']->fid,
  )));

  // Verify file access can be bypassed.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet('media/browser', $options);
  $this
    ->assertResponse(200);
  foreach ($files as $file) {
    $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,
    )));
  }
}