<?php

/**
 * @file
 * Tests for media_bulk_upload.module.
 */

/**
 * Provides methods specifically for testing Media Bulk Upload module's bulk file uploading capabilities.
 */
class MediaBulkUploadTestHelper extends DrupalWebTestCase {
  function setUp() {
    // Since this is a base class for many test cases, support the same
    // flexibility that DrupalWebTestCase::setUp() has for the modules to be
    // passed in as either an array or a variable number of string arguments.
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }
    $modules[] = 'media_bulk_upload';
    parent::setUp($modules);
  }

  /**
   * Retrieves a sample file of the specified type.
   */
  function getTestFile($type_name, $size = NULL) {
    // Get a file to upload.
    $file = current($this->drupalGetTestFiles($type_name, $size));

    // Add a filesize property to files as would be read by file_load().
    $file->filesize = filesize($file->uri);

    return $file;
  }

  /**
   * Get a file from the database based on its filename.
   *
   * @param $filename
   *   A file filename, usually generated by $this->randomName().
   * @param $reset
   *   (optional) Whether to reset the internal file_load() cache.
   *
   * @return
   *   A file object matching $filename.
   */
  function getFileByFilename($filename, $reset = FALSE) {
    $files = file_load_multiple(array(), array('filename' => $filename), $reset);
    // Load the first file returned from the database.
    $returned_file = reset($files);
    return $returned_file;
  }
}

/**
 * Test bulk file editing.
 */
class MediaBulkUploadEditTestCase extends MediaBulkUploadTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'Bulk file editing',
      'description' => 'Test file editing with multiple files.',
      'group' => 'Media Bulk Upload',
      'dependencies' => array('multiform', 'plupload'),
    );
  }

  function setUp() {
    parent::setUp();

    $web_user = $this->drupalCreateUser(array('create files', 'edit any document files', 'edit any image files'));
    $this->drupalLogin($web_user);
  }

  /**
   * Tests editing with multiple files.
   */
  function testBulkFileEditing() {
    $files = array();

    // Create multiple files for testing.
    foreach (array('image', 'text') as $type_name) {
      $test_file = $this->getTestFile($type_name);
      $file = file_save($test_file);
      $files[$file->fid] = $file;
    }

    // Visit the bulk file edit page and verify that it performs as expected.
    $path = media_bulk_upload_file_edit_url(array_keys($files));
    $this->drupalGet($path);

    foreach ($files as $file) {
      // Verify that a filename for each file is present on the page.
      $title = t('<em>Edit @type</em> @title', array('@type' => $file->type, '@title' => $file->filename));
      $this->assertRaw('<h2>' . $title . '</h2>', 'The file has the correct filename.');

      // Verify that the 'replace file' functionality is disabled.
      $this->assertNoField('multiform[media_edit_' . $file->fid . '_' . ($file->fid - 1) . '][files][replace_upload]', 'Replace file field found.');

      // Verify that the action buttons have been removed.
      $this->assertNoLinkByHref('file/' . $file->fid);
    }
  }
}
