Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Tests/FileEntityReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ public function testReplaceFile() {
// Test that Upload widget does not appear for non-local file.
$this->drupalGet('file/' . $file2->id() . '/edit');
$this->assertNoFieldByName('files[replace_upload]');
}

public function testReplaceImageFile() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a PHPDoc block.

// Select the first image test file to use.
$original = $this->files['image'][1];
$this->assertEqual($original->getFilename(), 'image-test-transparent-indexed.gif');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add meaningful assert messages. This applies to the entire PR.


// Create a user with file edit permissions.
$user = $this->drupalCreateUser(array('bypass file access'));
$this->drupalLogin($user);

// Get the next text file to use as a replacement.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that file is an image, not text.

$replacement = $this->files['image'][4];
$this->assertEqual($replacement->getFilename(), 'image-test-no-transparency.gif');

// Test that the file saves when uploading a replacement file.
$edit = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer short array syntax, but I am not strongly opposing old format either. :)

$edit['files[replace_upload]'] = drupal_realpath($replacement->getFileUri());
$this->drupalPostForm('file/' . $original->id() . '/edit', $edit, t('Save'));
$this->assertText(t('@file has been updated.', array('@file' => $original->getFilename()))/*, 'File was updated with file upload.'*/);

// Re-load the file from the database.
/** @var \Drupal\file\FileInterface $file */
$file = File::load($original->id());

// Test how file properties changed after the file has been replaced.
$this->assertNotEqual($file->getSize(), $original->getSize(), 'Updated file size changed from previous file.');
$this->assertEqual($file->getSize(), $replacement->getSize(), 'Updated file size does match the new file.');
}
}