You are here

public function FileFieldPathsRedirectTestCase::testRedirect in File (Field) Paths 7

Test File (Field) Paths Redirect functionality.

File

tests/redirect.test, line 51
Redirect module tests for the File (Field) Paths module.

Class

FileFieldPathsRedirectTestCase
Class FileFieldPathsRedirectTestCase

Code

public function testRedirect() {
  global $base_path;
  $langcode = LANGUAGE_NONE;

  // Create a File field with a random File path.
  $field_name = drupal_strtolower($this
    ->randomName());
  $instance_settings['filefield_paths']['file_path']['value'] = $this
    ->randomName();
  $this
    ->createFileField($field_name, $this->content_type, array(), $instance_settings);

  // Create a node with a test file.
  $test_file = $this
    ->getTestFile('text');
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $this->content_type);

  // Get processed source file uri.
  $node = node_load($nid, NULL, TRUE);
  $source = $node->{$field_name}[$langcode][0]['uri'];

  // Update file path and create redirect.
  $edit = array(
    'instance[settings][filefield_paths][file_path][value]' => $this
      ->randomName(),
    'instance[settings][filefield_paths][redirect]' => TRUE,
    'instance[settings][filefield_paths][retroactive_update]' => TRUE,
  );
  $this
    ->drupalPost("admin/structure/types/manage/{$this->content_type}/fields/{$field_name}", $edit, t('Save settings'));

  // Get processed destination file uri.
  $node = node_load($nid, NULL, TRUE);
  $destination = $node->{$field_name}[$langcode][0]['uri'];

  // Ensure that the source uri redirects to the destination uri.
  $parsed_source = parse_url(file_create_url($source), PHP_URL_PATH);
  $redirect_source = drupal_substr(urldecode($parsed_source), drupal_strlen($base_path));
  $parsed_destination = parse_url(file_create_url($destination), PHP_URL_PATH);
  $redirect_destination = drupal_substr(urldecode($parsed_destination), drupal_strlen($base_path));
  $redirect = redirect_load_by_source($redirect_source);
  $this
    ->assert(is_object($redirect) && $redirect->redirect == $redirect_destination, t('Redirect created for relocated file.'));
}