You are here

function MediaBrowserMyFilesTestCase::testFilesBrowserMyFiles in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 tests/media.test \MediaBrowserMyFilesTestCase::testFilesBrowserMyFiles()
  2. 7.2 tests/media.test \MediaBrowserMyFilesTestCase::testFilesBrowserMyFiles()

Tests media browser 'My files' tab with different user permissions.

File

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

Class

MediaBrowserMyFilesTestCase
Tests the media browser 'My files' tab.

Code

function testFilesBrowserMyFiles() {

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

  // Verify administrators do not have any special access to files.
  $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
      ->assertNoFieldByXPath($xpath, TRUE, format_string('File with file ID %fid not found.', array(
      '%fid' => $file->fid,
    )));
  }

  // Verify users require the 'view own files' permission in order to access
  // the 'My files' tab.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->base_user_1);
  $this
    ->drupalGet('media/browser', $options);
  $this
    ->assertResponse(200);
  $xpath = $this
    ->buildXPathQuery('//div[@class="media_default--media_browser_my_files"]');
  $this
    ->assertNoFieldByXPath($xpath, TRUE, 'User with insufficient permissions was unable to view the My files tab.');

  // Verify own public files are displayed.
  $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['public_image']->fid,
  ));
  $this
    ->assertFieldByXPath($xpath, TRUE, format_string('File with file ID %fid found.', array(
    '%fid' => $files['public_image']->fid,
  )));

  // Verify own private file is displayed with permission.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->base_user_3);
  $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 files of other users.
  $xpath = $this
    ->buildXPathQuery('//ul[@class="media-list-thumbnails"]/li/div[@data-fid=:fid]/@data-fid', array(
    ':fid' => $files['public_image']->fid,
  ));
  $this
    ->assertNoFieldByXPath($xpath, TRUE, format_string('File with file ID %fid not 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['private_image']->fid,
  ));
  $this
    ->assertNoFieldByXPath($xpath, TRUE, format_string('File with file ID %fid not found.', array(
    '%fid' => $files['private_image']->fid,
  )));
}