1 file.test FileReplaceTestCase::testReplaceFile()

@todo Test image dimensions for an image field are reset when a file is replaced. @todo Test image styles are cleared when an image is updated.

File

core/modules/file/tests/file.test, line 2926
Tests for file.module.

Class

FileReplaceTestCase
Tests replacing the file associated with a file entity.

Code

function testReplaceFile() {
  // Select the first text test file to use.
  $file = $this->createFile(array('type' => 'document'));

  // Create a user with file edit permissions.
  $user = $this->backdropCreateUser(array('edit any document files'));
  // $this->backdropLogin($user); // Todo: check permissions.

  // Test that the Upload widget appears for a local file.
  $this->backdropGet('file/' . $file->fid . '/manage');
  $this->assertFieldByName('files[replace_upload]');

  // Test that file saves without uploading a file.
  $this->backdropPost(NULL, array(), t('Save'));
  $this->assertText(t('File: @file has been updated.', array('@file' => $file->filename)), 'File was updated without file upload.');

  // Get a text file to use as a replacement.
  $original = clone $file;
  $replacement = $this->getTestFile('text');

  // Test that the file saves when uploading a replacement file.
  $edit = array();
  $edit['files[replace_upload]'] = backdrop_realpath($replacement->uri);
  $this->backdropPost('file/' . $file->fid . '/manage', $edit, t('Save'));
  $this->assertText(t('File: @file has been updated.', array('@file' => $file->filename)), 'File was updated with file upload.');

  // Re-load the file from the database.
  $file = file_load($file->fid);

  // Test how file properties changed after the file has been replaced.
  $this->assertEqual($file->filename, $original->filename, 'Updated file name did not change.');
  $this->assertNotEqual($file->filesize, $original->filesize, 'Updated file size changed from previous file.');
  $this->assertEqual($file->filesize, $replacement->filesize, 'Updated file size matches uploaded file.');
  $this->assertEqual(file_get_contents($file->uri), file_get_contents($replacement->uri), 'Updated file contents matches uploaded file.');
  $this->assertFalse(entity_load('file', FALSE, array('status' => 0)), 'Temporary file used for replacement was deleted.');

  // Get an image file.
  $image = $this->getTestFile('image');
  $edit['files[replace_upload]'] = backdrop_realpath($image->uri);

  // Test that validation works by uploading a non-text file as a replacement.
  $this->backdropPost('file/' . $file->fid . '/manage', $edit, t('Save'));
  $this->assertRaw(t('The specified file %file could not be uploaded. Only files with the following extensions are allowed:', array('%file' => $image->filename)), 'File validation works, upload failed correctly.');

  // Create a non-local file record.
  $file2 = new stdClass();
  $file2->uri = 'oembed://' . $this->randomName();
  $file2->filename = backdrop_basename($file2->uri);
  $file2->filemime = 'image/oembed';
  $file2->type = 'image';
  $file2->uid = 1;
  $file2->timestamp = REQUEST_TIME;
  $file2->filesize = 0;
  $file2->status = 0;
  // Write the record directly rather than calling file_save() so we don't
  // invoke the hooks.
  $this->assertTrue(backdrop_write_record('file_managed', $file2), 'Non-local file was added to the database.');

  // Test that Upload widget does not appear for non-local file.
  $this->backdropGet('file/' . $file2->fid . '/manage');
  $this->assertNoFieldByName('files[replace_upload]');
}