1.20.x file.test | FileManagedFileElementTestCase::testManagedFile() |
Tests the managed_file element type.
File
- modules/
file/ tests/ file.test, line 479 - Tests for file.module.
Class
- FileManagedFileElementTestCase
- Tests the 'managed_file' element type.
Code
function testManagedFile() {
// Check that $element['#size'] is passed to the child upload element.
$this->backdropGet('file/test');
$this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
// Perform the tests with all permutations of $form['#tree'] and
// $element['#extended'].
foreach (array(0, 1) as $tree) {
foreach (array(0, 1) as $extended) {
$test_file = $this->getTestFile('text');
$path = 'file/test/' . $tree . '/' . $extended;
$input_base_name = $tree ? 'nested_file' : 'file';
// Submit without a file.
$this->backdropPost($path, array(), t('Save'));
$this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submitted without a file.');
// Submit with a file, but with an invalid form token. Ensure the file
// was not saved.
$last_fid_prior = $this->getLastFileId();
$edit = array(
'files[' . $input_base_name . ']' => backdrop_realpath($test_file->uri),
'form_token' => 'invalid token',
);
$this->backdropPost($path, $edit, t('Save'));
$this->assertText('The form has become outdated.');
$last_fid = $this->getLastFileId();
$this->assertEqual($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
// Submit a new file, without using the Upload button.
$last_fid_prior = $this->getLastFileId();
$edit = array('files[' . $input_base_name . ']' => backdrop_realpath($test_file->uri));
$this->backdropPost($path, $edit, t('Save'));
$last_fid = $this->getLastFileId();
$this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
$this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
// Submit no new input, but with a default file.
$this->backdropPost($path . '/' . $last_fid, array(), t('Save'));
$this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Empty submission did not change an existing file.');
// Now, test the Upload and Remove buttons, with and without Ajax.
foreach (array(FALSE, TRUE) as $ajax) {
// Upload, then Submit.
$last_fid_prior = $this->getLastFileId();
$this->backdropGet($path);
$edit = array('files[' . $input_base_name . ']' => backdrop_realpath($test_file->uri));
if ($ajax) {
$this->backdropPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
}
else {
$this->backdropPost(NULL, $edit, t('Upload'));
}
$last_fid = $this->getLastFileId();
$this->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
$this->backdropPost(NULL, array(), t('Save'));
$this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
// Remove, then Submit.
$this->backdropGet($path . '/' . $last_fid);
if ($ajax) {
$this->backdropPostAJAX(NULL, array(), $input_base_name . '_remove_button');
}
else {
$this->backdropPost(NULL, array(), t('Remove'));
}
$this->backdropPost(NULL, array(), t('Save'));
$this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file removal was successful.');
// Upload, then Remove, then Submit.
$this->backdropGet($path);
$edit = array('files[' . $input_base_name . ']' => backdrop_realpath($test_file->uri));
if ($ajax) {
$this->backdropPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
$this->backdropPostAJAX(NULL, array(), $input_base_name . '_remove_button');
}
else {
$this->backdropPost(NULL, $edit, t('Upload'));
$this->backdropPost(NULL, array(), t('Remove'));
}
$this->backdropPost(NULL, array(), t('Save'));
$this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file upload and removal was successful.');
}
}
}
}