### Eclipse Workspace Patch 1.0 #P simpletest Index: tests/functional/upload.test =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/simpletest/tests/functional/upload.test,v retrieving revision 1.15 diff -u -r1.15 upload.test --- tests/functional/upload.test 5 Apr 2008 19:19:26 -0000 1.15 +++ tests/functional/upload.test 7 Apr 2008 20:32:32 -0000 @@ -35,22 +35,23 @@ $this->drupalPost('admin/settings/uploads', $edit, t('Save configuration')); $this->assertText('The configuration options have been saved.', 'Upload setting saved.'); - $this->drupalGet('logout'); + $this->drupalLogout(); $this->drupalLogin($web_user); // Create a node and attempt to attach files. $node = $this->drupalCreateNode(); - $files = array('README.txt', 'INSTALL.txt'); + $text_files = $this->drupalGetTestFiles('text'); + $files = array(current($text_files)->filename, next($text_files)->filename); $this->uploadFile($node, $files[0]); $this->uploadFile($node, $files[1]); // Check to see that uploaded file is listed and actually accessible. - $this->assertText($files[0], $files[0] .' found on node.'); - $this->assertText($files[1], $files[1] .' found on node.'); + $this->assertText(basename($files[0]), basename($files[0]) .' found on node.'); + $this->assertText(basename($files[1]), basename($files[1]) .' found on node.'); - $this->checkUploadedFile($files[0]); - $this->checkUploadedFile($files[1]); + $this->checkUploadedFile(basename($files[0])); + $this->checkUploadedFile(basename($files[1])); // Fetch db record and use fid to rename and delete file. $upload = db_fetch_object(db_query('SELECT fid, description FROM {upload} WHERE nid = %d', array($node->nid))); @@ -80,16 +81,97 @@ } /** + * Ensure the the file filter works correctly by attempting to upload a non-allowed file extension. + */ + function testFilesFilter() { + $admin_user = $this->drupalCreateUser(array('administer site configuration')); + $web_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'upload files', 'view uploaded files')); + + $this->drupalLogin($admin_user); + + // Setup upload settings. + $settings = array(); + $settings['upload_list'] = '1'; // Yes. + $settings['upload_extensions'] = 'html'; + $settings['upload_uploadsize'] = '1'; + $settings['upload_usersize'] = '1'; + $this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user)); + + $this->drupalLogin($web_user); + + $node = $this->drupalCreateNode(); + $text_files = $this->drupalGetTestFiles('text'); + $html_files = $this->drupalGetTestFiles('html'); + $files = array(current($text_files)->filename, current($html_files)->filename); + + // Attempt to upload .txt file when .test is only extension allowed. + $this->uploadFile($node, $files[0], FALSE); + $this->assertRaw(t('The selected file %name could not be uploaded. Only files with the following extensions are allowed: %files-allowed.', array('%name' => basename($files[0]), '%files-allowed' => $settings['upload_extensions'])), 'File '. $files[0] .' was not allowed to be uploaded'); + + // Attempt to upload .test file when .test is only extension allowed. + $this->uploadFile($node, $files[1]); + } + + /** + * Attempt to upload a file that is larger than the maxsize and see that it fails. + */ + function testLimit() { + $files = $this->drupalGetTestFiles('text', 1310720); // 1 MB. + $file = current($files)->filename; + + $admin_user = $this->drupalCreateUser(array('administer site configuration')); + $web_user = $this->drupalCreateUser(array('access content', 'edit any page content', 'upload files', 'view uploaded files')); + + $this->drupalLogin($admin_user); + + // Setup upload settings. + $settings = array(); + $settings['upload_list'] = '1'; // Yes. + $settings['upload_extensions'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'; + $settings['upload_uploadsize'] = '0.5'; + $settings['upload_usersize'] = '1.5'; + $this->setUploadSettings($settings, $this->getSimpletestRoleId($web_user)); + + $this->drupalLogin($web_user); + + $node = $this->drupalCreateNode(); + + // Attempt to upload file which is bigger than the maximum size of 0.5 MB. + $this->uploadFile($node, $file, FALSE); + + $info = stat($file); + $filename = basename($file); + $filesize = format_size($info['size']); + $maxsize = format_size(parse_size(($settings['upload_uploadsize'] * 1024) .'KB')); // Won't parse decimals. + $this->assertRaw(t('The selected file %name could not be uploaded. The file is %filesize exceeding the maximum file size of %maxsize.', array('%name' => $filename, '%filesize' => $filesize, '%maxsize' => $maxsize)), t('File upload was blocked since it was larger than maxsize.')); + } + + function setUploadSettings($settings, $rid = NULL) { + $edit = array(); + foreach ($settings as $key => $value) { + $edit[$key .'_default'] = $value; + if ($rid !== NULL && $key != 'upload_list' && $key != 'upload_max_resolution') { + $edit[$key .'_'. $rid] = $value; + } + } + $this->drupalPost('admin/settings/uploads', $edit, 'Save configuration'); + $this->assertText('The configuration options have been saved.', 'Upload setting saved.'); + } + + /** * Upload file to specified node. * * @param object $node Node object. * @param string $filename Name of file to upload. + * @param boolean $assert Assert that the node was successfully updated. */ - function uploadFile($node, $filename) { + function uploadFile($node, $filename, $assert = TRUE) { $edit = array(); - $edit['files[upload]'] = $this->getFilePath($filename); + $edit['files[upload]'] = $filename; //edit-upload $this->drupalPost('node/'. $node->nid .'/edit', $edit, t('Save')); - $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File attached successfully.'); + if ($assert) { + $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File attached successfully.'); + } } /** @@ -98,20 +180,25 @@ * @param string $filename Name of file to verifiy. */ function checkUploadedFile($filename) { - $file = $this->getFilePath($filename); + $file = realpath(file_directory_path() .'/simpletest/'. $filename); $this->drupalGet(file_directory_path() .'/'. $filename); $this->assertResponse(array(200), 'Uploaded '. $filename .' is accessible.'); $this->assertEqual(file_get_contents($file), $this->drupalGetContent(), 'Uploaded contents of '. $filename .' verified.'); } /** - * Get canonicalized absolute path to file located in the SimpleTest module. + * Get the role id of the 'simpletest' role associated with a SimpleTest test user. * - * @param string $filename Name of file to get path for. - * @return string Absolute path. + * @param object $user User object. + * @return interger SimpleTest role id. */ - function getFilePath($filename) { - return realpath(drupal_get_path('module', 'simpletest') .'/'. $filename); + function getSimpletestRoleId($user) { + foreach ($user->roles as $rid => $role) { + if (strpos($role, 'simpletest') !== FALSE) { + return $rid; + } + } + return NULL; } } @@ -244,12 +331,14 @@ // changing actual setting; $old_dim = variable_get('user_picture_dimensions', '85x85'); $old_size = variable_get('user_picture_file_size', '30'); - $img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/files/image-2.jpg"); + $files = $this->drupalGetTestFiles('image'); + $file = current($files); + $img_path = realpath($file->filename); $info = image_get_info($img_path); // set new variables; $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); - $test_size = floor(filesize($img_path) / 1000) - 1; + $test_size = filesize($img_path); variable_set('user_picture_dimensions', $test_dim); variable_set('user_picture_file_size', $test_size);