diff options
Diffstat (limited to 'modules/file/tests/file.test')
-rw-r--r-- | modules/file/tests/file.test | 98 |
1 files changed, 97 insertions, 1 deletions
diff --git a/modules/file/tests/file.test b/modules/file/tests/file.test index 22c6ca4f8..88d8afcda 100644 --- a/modules/file/tests/file.test +++ b/modules/file/tests/file.test @@ -13,7 +13,7 @@ class FileFieldTestCase extends DrupalWebTestCase { protected $admin_user; function setUp() { - parent::setUp('file'); + parent::setUp('file', 'file_module_test'); $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer nodes', 'bypass node access')); $this->drupalLogin($this->admin_user); } @@ -32,6 +32,13 @@ class FileFieldTestCase extends DrupalWebTestCase { } /** + * Get the fid of the last inserted file. + */ + function getLastFileId() { + return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField(); + } + + /** * Create a new file field. * * @param $name @@ -201,6 +208,95 @@ class FileFieldTestCase extends DrupalWebTestCase { } } +/** + * Test class for testing the 'managed_file' element type on its own, not as part of a file field. + * + * @todo Create a FileTestCase base class and move FileFieldTestCase methods + * that aren't related to fields into it. + */ +class FileManagedFileElementTestCase extends FileFieldTestCase { + public static function getInfo() { + return array( + 'name' => 'Managed file element test', + 'description' => 'Tests the managed_file element type.', + 'group' => 'File', + ); + } + + /** + * Tests the managed_file element type. + */ + function testManagedFile() { + // 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->drupalPost($path, array(), t('Save')); + $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submitted without a file.')); + + // Submit a new file, without using the Upload button. + $last_fid_prior = $this->getLastFileId(); + $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri)); + $this->drupalPost($path, $edit, t('Save')); + $last_fid = $this->getLastFileId(); + $this->assertTrue($last_fid > $last_fid_prior, t('New file got saved.')); + $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Submit handler has correct file info.')); + + // Submit no new input, but with a default file. + $this->drupalPost($path . '/' . $last_fid, array(), t('Save')); + $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('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->drupalGet($path); + $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri)); + if ($ajax) { + $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button'); + } + else { + $this->drupalPost(NULL, $edit, t('Upload')); + } + $last_fid = $this->getLastFileId(); + $this->assertTrue($last_fid > $last_fid_prior, t('New file got uploaded.')); + $this->drupalPost(NULL, array(), t('Save')); + $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Submit handler has correct file info.')); + + // Remove, then Submit. + $this->drupalGet($path . '/' . $last_fid); + if ($ajax) { + $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button'); + } + else { + $this->drupalPost(NULL, array(), t('Remove')); + } + $this->drupalPost(NULL, array(), t('Save')); + $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submission after file removal was successful.')); + + // Upload, then Remove, then Submit. + $this->drupalGet($path); + $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri)); + if ($ajax) { + $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button'); + $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button'); + } + else { + $this->drupalPost(NULL, $edit, t('Upload')); + $this->drupalPost(NULL, array(), t('Remove')); + } + $this->drupalPost(NULL, array(), t('Save')); + $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submission after file upload and removal was successful.')); + } + } + } + } +} /** * Test class to test file field widget, single and multi-valued, with and without AJAX, with public and private files. |