Index: modules/user/user.test =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.test,v retrieving revision 1.5 diff -u -r1.5 user.test --- modules/user/user.test 2 Jun 2008 17:42:58 -0000 1.5 +++ modules/user/user.test 5 Jun 2008 18:14:21 -0000 @@ -205,6 +205,213 @@ } } +class UserPictureTestCase extends DrupalWebTestCase { + protected $user; + protected $_directory_test; + + function getInfo() { + return array( + 'name' => t('Upload user picture'), + 'description' => t('Assure that dimension check, extension check and image scaling work as designed.'), + 'group' => t('User') + ); + } + + function setUp() { + parent::setUp(); + // Enable user pictures. + variable_set('user_pictures', 1); + + $this->user = $this->drupalCreateUser(); + + // Test if directories specified in settings exist in filesystem. + $file_dir = file_directory_path(); + $file_check = file_check_directory($file_dir, FILE_CREATE_DIRECTORY, 'file_directory_path'); + + $picture_dir = variable_get('user_picture_path', 'pictures'); + $picture_path = $file_dir .'/'.$picture_dir; + + $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path'); + $this->_directory_test = is_writable($picture_path); + $this->assertTrue($this->_directory_test, "The directory $picture_path doesn't exist or is not writable. Further tests won't be made."); + } + + function testNoPicture() { + $this->drupalLogin($this->user); + + // Try to upload a file that is not an image for the user picture. + $not_an_image = current($this->drupalGetTestFiles('html')); + $this->saveUserPicture($not_an_image); + $this->assertRaw(t('Only JPEG, PNG and GIF images are allowed.'), t('Non-image files are not accepted.')); + } + + /** + * Do the test: + * GD Toolkit is installed + * Picture has invalid dimension + * + * results: The image should be uploaded because ImageGDToolkit resizes the picture + */ + function testWithGDinvalidDimension() { + if ($this->_directory_test) + if (image_get_toolkit()) { + $this->drupalLogin($this->user); + + $image = current($this->drupalGetTestFiles('image')); + $info = image_get_info($image->filename); + + // set new variables; + $test_size = floor(filesize($image->filename) / 1000) + 1; + $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10); + variable_set('user_picture_dimensions', $test_dim); + variable_set('user_picture_file_size', $test_size); + + $pic_path = $this->saveUserPicture($image); + + // check if image is displayed in user's profile page + $this->assertRaw(file_create_url($pic_path), "Image is displayed in user's profile page"); + + // check if file is located in proper directory + $this->assertTrue(is_file($pic_path), "File is located in proper directory"); + } + } + + /** + * Do the test: + * GD Toolkit is installed + * Picture has invalid size + * + * results: The image should be uploaded because ImageGDToolkit resizes the picture + */ + function testWithGDinvalidSize() { + if ($this->_directory_test) + if (image_get_toolkit()) { + + $this->drupalLogin($this->user); + + $image = current($this->drupalGetTestFiles('image')); + $info = image_get_info($image->filename); + + // Set new variables. + $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); + $test_size = filesize($image->filename); + variable_set('user_picture_dimensions', $test_dim); + variable_set('user_picture_file_size', $test_size); + + $picture_path = $this->saveUserPicture($image); + $this->assertText(t('The changes have been saved.')); + + // Check if image is displayed in user's profile page. + $this->assertRaw(file_create_url($picture_url), t("Image is displayed in user's profile page")); + + // Check if file is located in proper directory. + $this->assertTrue(is_file($picture_path), t('File is located in proper directory')); + } + } + + /** + * Do the test: + * GD Toolkit is not installed + * Picture has invalid size + * + * results: The image shouldn't be uploaded + */ + function testWithoutGDinvalidDimension() { + if ($this->_directory_test) + if (!image_get_toolkit()) { + + $this->drupalLogin($this->user); + + $image = current($this->drupalGetTestFiles('image')); + $info = image_get_info($image->filename); + + // Set new variables. + $test_size = floor(filesize($image->filename) / 1000) + 1; + $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10); + variable_set('user_picture_dimensions', $test_dim); + variable_set('user_picture_file_size', $test_size); + + $pic_path = $this->saveUserPicture($image); + $text = t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'))); + $this->assertText($text, t('Checking response on invalid image (dimensions).')); + + // check if file is not uploaded + $this->assertFalse(is_file($pic_path), t('File is not uploaded')); + } + } + + /** + * Do the test: + * GD Toolkit is not installed + * Picture has invalid size + * + * results: The image shouldn't be uploaded + */ + function testWithoutGDinvalidSize() { + if ($this->_directory_test) + if (!image_get_toolkit()) { + $this->drupalLogin($this->user); + + $image = current($this->drupalGetTestFiles('image')); + $image->filename = realpath("modules/tests/image-2.jpg"); + $info = image_get_info($image->filename); + // invalid size + // restore one and set another + $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); + $test_size = floor(filesize($image->filename) / 1000) - 1; + variable_set('user_picture_dimensions', $test_dim); + variable_set('user_picture_file_size', $test_size); + + $pic_path = $this->saveUserPicture($image); + $text = t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30'))); + $this->assertText($text, t('Checking response on invalid image size.')); + + // check if file is not uploaded + $this->assertFalse(is_file($pic_path), t('File is not uploaded.')); + } + } + + /** + * Do the test: + * Picture is valid (proper size and dimension) + * + * results: The image should be uploaded + */ + function testPictureIsValid() { + if ($this->_directory_test) { + $this->drupalLogin($this->user); + + $image = current($this->drupalGetTestFiles('image')); + $info = image_get_info($image->filename); + + // valid size & dimensions + // restore one and set another + $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); + $test_size = floor(filesize($image->filename) / 1000) + 1; + variable_set('user_picture_dimensions', $test_dim); + variable_set('user_picture_file_size', $test_size); + + $pic_path = $this->saveUserPicture($image); + + // check if image is displayed in user's profile page + $this->assertRaw($pic_path, t("Image is displayed in user's profile page")); + + // check if file is located in proper directory + $this->assertTrue(is_file($pic_path), t('File is located in proper directory')); + } + } + + function saveUserPicture($image) { + $edit = array('files[picture_upload]' => realpath($image->filename)); + $this->drupalPost('user/' . $this->user->uid.'/edit', $edit, t('Save')); + + $picture_dir = variable_get('user_picture_path', 'pictures'); + $pic_path = file_directory_path() .'/'.$picture_dir .'/picture-'.$this->user->uid.'.jpg'; + + return $pic_path; + } +} + class UserPermissionsTestCase extends DrupalWebTestCase { protected $admin_user; Index: modules/user/upload.test =================================================================== RCS file: modules/user/upload.test diff -N modules/user/upload.test --- modules/user/upload.test 30 May 2008 07:30:53 -0000 1.2 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,522 +0,0 @@ - t('Upload functionality'), - 'description' => t('Check content uploaded to nodes.'), - 'group' => t('Upload'), - ); - } - - function setUp() { - parent::setUp('upload'); - } - - /** - * Create node; upload files to node; and edit, and delete uploads. - */ - function testNodeUpload() { - $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. - $edit = array(); - $edit['upload_list_default'] = '1'; // Yes. - $edit['upload_extensions_default'] = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'; - $edit['upload_uploadsize_default'] = '1'; - $edit['upload_usersize_default'] = '1'; - $this->drupalPost('admin/settings/uploads', $edit, t('Save configuration')); - $this->assertText('The configuration options have been saved.', 'Upload setting saved.'); - - $this->drupalLogout(); - $this->drupalLogin($web_user); - - // Create a node and attempt to attach files. - $node = $this->drupalCreateNode(); - $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(basename($files[0]), basename($files[0]) . ' found on node.'); - $this->assertText(basename($files[1]), basename($files[1]) . ' found on node.'); - - $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))); - if ($upload) { - // Rename file. - $edit = array(); - $edit['files[' . $upload->fid . '][description]'] = $new_name = substr($upload->description, 1); - $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); - $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File renamed successfully.'); - - $this->assertText($new_name, $new_name . ' found on node.'); - $this->assertNoText($upload->description, $upload->description . ' not found on node.'); - - // Delete a file. - $edit = array(); - $edit['files[' . $upload->fid . '][remove]'] = TRUE; - $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); - $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File deleted successfully.'); - - $this->assertNoText($new_name, $new_name . ' not found on node.'); - $this->drupalGet(file_directory_path() . '/' . $upload->description); - $this->assertResponse(array(404), 'Uploaded ' . $upload->description . ' is not accessible.'); - } - else { - $this->fail('File upload record not found in database.'); - } - } - - /** - * 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, $assert = TRUE) { - $edit = array(); - $edit['files[upload]'] = $filename; //edit-upload - $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); - if ($assert) { - $this->assertRaw(t('Page %title has been updated.', array('%title' => $node->title)), 'File attached successfully.'); - } - } - - /** - * Check that uploaded file is accessible and verify the contents against the original. - * - * @param string $filename Name of file to verifiy. - */ - function checkUploadedFile($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 the role id of the 'simpletest' role associated with a SimpleTest test user. - * - * @param object $user User object. - * @return interger SimpleTest role id. - */ - function getSimpletestRoleId($user) { - foreach ($user->roles as $rid => $role) { - if (strpos($role, 'simpletest') !== FALSE) { - return $rid; - } - } - return NULL; - } -} - -class UploadPictureTestCase extends DrupalWebTestCase { - function getInfo() { - return array( - 'name' => t('Upload user picture'), - 'description' => t('Assure that dimension check, extension check and image scaling work as designed.'), - 'group' => t('Upload') - ); - } - - /* - * Test if directories specified in settings exist in filesystem - */ - function testDirectories() { - // test if filepath is proper - $file_dir = file_directory_path(); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $file_check = file_check_directory($file_dir, FILE_CREATE_DIRECTORY, 'file_directory_path'); - $picture_path = $file_dir . '/' . $picture_dir; - - $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path'); - // check directories - //$this->assertTrue($file_check,"The directory $file_dir doesn't exist or cannot be created."); - //$this->assertTrue($pic_check,"The directory $picture_path doesn't exist or cannot be created."); - $this->_directory_test = is_writable($picture_path); - $this->assertTrue($this->_directory_test, "The directory $picture_path doesn't exist or is not writable. Further tests won't be made."); - } - - function testNoPicture() { - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // not a image - $img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/functional/upload.test"); - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $this->assertRaw(t('The selected file %file could not be uploaded. Only JPEG, PNG and GIF images are allowed.', array('%file' => 'upload.test')), 'The uploaded file was not an image.'); - variable_set('user_pictures', $old_pic_set); - - // do we have to check users roles? - // delete test user and roles - - } - - /* - * Do one test if ImageGDToolkit is installed - */ - - /* - * Do the test: - * GD Toolkit is installed - * Picture has invalid dimension - * - * results: The image should be uploaded because ImageGDToolkit resizes the picture - */ - function testWithGDinvalidDimension() { - if ($this->_directory_test) - if (image_get_toolkit()) { - - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // 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"); - $info = image_get_info($img_path); - - // set new variables; - $test_size = floor(filesize($img_path) / 1000) + 1; - $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10); - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // TEST: - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture = $picture_dir . '/picture-' . $user->uid. '.jpg'; - - // get full url to the user's image - $picture_url = file_create_url($picture); - $picture_path = file_create_path($picture); - - // check if image is displayed in user's profile page - $this->assertRaw($picture_url, "Image is displayed in user's profile page"); - - // check if file is located in proper directory - $this->assertTrue(is_file($picture_path), "File is located in proper directory"); - - // RESTORING: - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - - } - - /* - * Do the test: - * GD Toolkit is installed - * Picture has invalid size - * - * results: The image should be uploaded because ImageGDToolkit resizes the picture - */ - - function testWithGDinvalidSize() { - if ($this->_directory_test) - if (image_get_toolkit()) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // changing actual setting; - $old_dim = variable_get('user_picture_dimensions', '85x85'); - $old_size = variable_get('user_picture_file_size', '30'); - $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 = filesize($img_path); - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // TEST: - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture = $picture_dir . '/picture-' . $user->uid. '.jpg'; - - // get full url to the user's image - $picture_url = file_create_url($picture); - $picture_path = file_create_path($picture); - - // check if image is displayed in user's profile page - $this->assertRaw($picture_url, "Image is displayed in user's profile page"); - - // check if file is located in proper directory - $this->assertTrue(is_file($picture_path), "File is located in proper directory"); - - // RESTORING: - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - } - - /* - * Do the test: - * GD Toolkit is not installed - * Picture has invalid size - * - * results: The image shouldn't be uploaded - */ - function testWithoutGDinvalidDimension() { - if ($this->_directory_test) - if (!image_get_toolkit()) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // 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"); - $info = image_get_info($img_path); - // set new variables; - $test_size = floor(filesize($img_path) / 1000) + 1; - $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10); - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // TEST: - $edit = array('picture' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $text = t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'))); - $this->assertText($text, 'Checking response on invalid image (dimensions).'); - - // check if file is not uploaded - $file_dir = variable_get('file_directory_path', 'files'); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid. '.jpg'; - $this->assertFalse(is_file($pic_path), "File is not uploaded"); - - // restore variables; - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - } - - /* - * Do the test: - * GD Toolkit is not installed - * Picture has invalid size - * - * results: The image shouldn't be uploaded - */ - function testWithoutGDinvalidSize() { - if ($this->_directory_test) - if (!image_get_toolkit()) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // changing actual setting; - $old_dim = variable_get('user_picture_dimensions', '85x85'); - $old_size = variable_get('user_picture_file_size', '30'); - $img_path = realpath("modules/tests/image-2.jpg"); - $info = image_get_info($img_path); - // invalid size - // restore one and set another - $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); - $test_size = floor(filesize($img_path) / 1000) - 1; - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - $edit = array('picture' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $text = t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30'))); - $this->assertText($text, 'Checking response on invalid image size.'); - - // check if file is not uploaded - $file_dir = variable_get('file_directory_path', 'files'); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid. '.jpg'; - $this->assertFalse(is_file($pic_path), "File is not uploaded"); - // restore variables; - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - } - - /* - * Do the test: - * Picture is valid (proper size and dimension) - * - * results: The image should be uploaded - */ - function testPictureIsValid() { - if ($this->_directory_test) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // 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"); - $info = image_get_info($img_path); - - // valid size & dimensions - // restore one and set another - $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); - $test_size = floor(filesize($img_path) / 1000) + 1; - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // TEST: - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $pic_path = file_directory_path() . '/' . $picture_dir . '/picture-' . $user->uid. '.jpg'; - - // get full url to the user's image - $picture = file_create_url($pic_path); - - // check if image is displayed in user's profile page - $content = $this->drupalGetContent(); - - $this->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page"); - - // check if file is located in proper directory - $this->assertTrue(is_file($pic_path), "File is located in proper directory"); - - // RESTORING: - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - - // DELETING UPLOADED PIC - file_delete($pic_path); - } - } -} Index: modules/upload/upload.test =================================================================== RCS file: /cvs/drupal/drupal/modules/upload/upload.test,v retrieving revision 1.2 diff -u -r1.2 upload.test --- modules/upload/upload.test 30 May 2008 07:30:53 -0000 1.2 +++ modules/upload/upload.test 5 Jun 2008 18:14:20 -0000 @@ -201,350 +201,3 @@ return NULL; } } - -class UploadPictureTestCase extends DrupalWebTestCase { - function getInfo() { - return array( - 'name' => t('Upload user picture'), - 'description' => t('Assure that dimension check, extension check and image scaling work as designed.'), - 'group' => t('Upload') - ); - } - - /* - * Test if directories specified in settings exist in filesystem - */ - function testDirectories() { - // test if filepath is proper - $file_dir = file_directory_path(); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $file_check = file_check_directory($file_dir, FILE_CREATE_DIRECTORY, 'file_directory_path'); - $picture_path = $file_dir . '/' . $picture_dir; - - $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path'); - // check directories - //$this->assertTrue($file_check,"The directory $file_dir doesn't exist or cannot be created."); - //$this->assertTrue($pic_check,"The directory $picture_path doesn't exist or cannot be created."); - $this->_directory_test = is_writable($picture_path); - $this->assertTrue($this->_directory_test, "The directory $picture_path doesn't exist or is not writable. Further tests won't be made."); - } - - function testNoPicture() { - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // not a image - //$img_path = realpath(drupal_get_path('module', 'simpletest'). "/tests/functional/upload.test"); - $img_path = realpath(drupal_get_path('module', 'simpletest'). "/files/html-1.txt"); - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $this->assertRaw(t('The selected file %file could not be uploaded. Only JPEG, PNG and GIF images are allowed.', array('%file' => basename($img_path))), 'The uploaded file was not an image.'); - variable_set('user_pictures', $old_pic_set); - - // do we have to check users roles? - // delete test user and roles - - } - - /* - * Do one test if ImageGDToolkit is installed - */ - - /* - * Do the test: - * GD Toolkit is installed - * Picture has invalid dimension - * - * results: The image should be uploaded because ImageGDToolkit resizes the picture - */ - - function testWithGDinvalidDimension() { - if ($this->_directory_test) - if (image_get_toolkit()) { - - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // 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"); - $img_path = realpath(drupal_get_path('module', 'simpletest'). "/files/image-2.jpg"); - $info = image_get_info($img_path); - - // set new variables; - $test_size = floor(filesize($img_path) / 1000) + 1; - $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10); - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // Create pictures folder - $file_dir = file_directory_path(); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture_path = $file_dir . '/' . $picture_dir; - $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path'); - - // TEST: - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture = $picture_dir . '/picture-' . $user->uid. '.jpg'; - - // get full url to the user's image - $picture_url = file_create_url($picture); - $picture_path = file_create_path($picture); - - // check if image is displayed in user's profile page - $this->assertRaw($picture_url, "Image is displayed in user's profile page"); - - // check if file is located in proper directory - $this->assertTrue(is_file($picture_path), "File is located in proper directory"); - - // RESTORING: - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - - } - - /* - * Do the test: - * GD Toolkit is installed - * Picture has invalid size - * - * results: The image should be uploaded because ImageGDToolkit resizes the picture - */ - - function tstWithGDinvalidSize() { - if ($this->_directory_test) - if (image_get_toolkit()) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // changing actual setting; - $old_dim = variable_get('user_picture_dimensions', '85x85'); - $old_size = variable_get('user_picture_file_size', '30'); - $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 = filesize($img_path); - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // Create pictures folder - $file_dir = file_directory_path(); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture_path = $file_dir . '/' . $picture_dir; - $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path'); - - // TEST: - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture = $picture_dir . '/picture-' . $user->uid. '.png'; - - // get full url to the user's image - $picture_url = file_create_url($picture); - $picture_path = file_create_path($picture); - - // check if image is displayed in user's profile page - $this->assertRaw($picture_url, "Image is displayed in user's profile page"); - - // check if file is located in proper directory - $this->assertTrue(is_file($picture_path), "File is located in proper directory"); - - // RESTORING: - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - } - - /* - * Do the test: - * GD Toolkit is not installed - * Picture has invalid size - * - * results: The image shouldn't be uploaded - */ - function tstWithoutGDinvalidDimension() { - if ($this->_directory_test) - if (!image_get_toolkit()) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // 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'). "/files/image-2.jpg"); - $info = image_get_info($img_path); - // set new variables; - $test_size = floor(filesize($img_path) / 1000) + 1; - $test_dim = ($info['width'] - 10) . 'x' . ($info['height'] - 10); - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // Create pictures folder - $file_dir = file_directory_path(); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture_path = $file_dir . '/' . $picture_dir; - $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path'); - - // TEST: - $edit = array('picture' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $text = t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'))); - $this->assertText($text, 'Checking response on invalid image (dimensions).'); - - // check if file is not uploaded - $file_dir = variable_get('file_directory_path', 'files'); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid. '.jpg'; - $this->assertFalse(is_file($pic_path), "File is not uploaded"); - - // restore variables; - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - } - - /* - * Do the test: - * GD Toolkit is not installed - * Picture has invalid size - * - * results: The image shouldn't be uploaded - */ - function tstWithoutGDinvalidSize() { - if ($this->_directory_test) - if (!image_get_toolkit()) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // changing actual setting; - $old_dim = variable_get('user_picture_dimensions', '85x85'); - $old_size = variable_get('user_picture_file_size', '30'); - //$img_path = realpath("modules/tests/image-2.jpg"); - $img_path = realpath(drupal_get_path('module', 'simpletest'). "/files/image-2.jpg"); - $info = image_get_info($img_path); - // invalid size - // restore one and set another - $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); - $test_size = floor(filesize($img_path) / 1000) - 1; - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - $edit = array('picture' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $text = t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30'))); - $this->assertText($text, 'Checking response on invalid image size.'); - - // check if file is not uploaded - $file_dir = variable_get('file_directory_path', 'files'); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $pic_path = $file_dir . '/' . $picture_dir . '/picture-' . $user->uid. '.jpg'; - $this->assertFalse(is_file($pic_path), "File is not uploaded"); - // restore variables; - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - } - } - - /* - * Do the test: - * Picture is valid (proper size and dimension) - * - * results: The image should be uploaded - */ - function tstPictureIsValid() { - if ($this->_directory_test) { - // PREPARE: - $old_pic_set = variable_get('user_pictures', 0); - variable_set('user_pictures', 1); - - /* Prepare a user to do the stuff */ - $user = $this->drupalCreateUser(array('access content')); - $this->drupalLogin($user); - - // 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'). "/files/image-2.jpg"); - $info = image_get_info($img_path); - - // valid size & dimensions - // restore one and set another - $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10); - $test_size = floor(filesize($img_path) / 1000) + 1; - variable_set('user_picture_dimensions', $test_dim); - variable_set('user_picture_file_size', $test_size); - - // Create pictures folder - $file_dir = file_directory_path(); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $picture_path = $file_dir . '/' . $picture_dir; - $pic_check = file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path'); - - // TEST: - $edit = array('files[picture_upload]' => $img_path); - $this->drupalPost('user/' . $user->uid. '/edit', $edit, t('Save')); - $picture_dir = variable_get('user_picture_path', 'pictures'); - $pic_path = file_directory_path() . '/' . $picture_dir . '/picture-' . $user->uid. '.jpg'; - - // get full url to the user's image - $picture = file_create_url($pic_path); - - // check if image is displayed in user's profile page - $content = $this->drupalGetContent(); - - $this->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page"); - - // check if file is located in proper directory - $this->assertTrue(is_file($pic_path), "File is located in proper directory"); - - // RESTORING: - variable_set('user_picture_file_size', $old_size); - variable_set('user_picture_dimensions', $old_dim); - - variable_set('user_pictures', $old_pic_set); - - // DELETING UPLOADED PIC - file_delete($pic_path); - } - } -} Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.13 diff -u -r1.13 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 3 Jun 2008 19:53:42 -0000 1.13 +++ modules/simpletest/drupal_web_test_case.php 5 Jun 2008 18:14:20 -0000 @@ -606,29 +606,11 @@ // We post only if we managed to handle every field in edit and the // submit button matches. if (!$edit && $submit_matches) { - // This part is not pretty. There is very little I can do. - if ($upload) { - foreach ($post as &$value) { - if (strlen($value) > 0 && $value[0] == '@') { - $this->fail(t("Can't upload and post a value starting with @")); - return FALSE; - } - } - foreach ($upload as $key => $file) { - $post[$key] = '@' . realpath($file); - } - } - else { - $post_array = $post; - $post = array(); - foreach ($post_array as $key => $value) { - // Whether this needs to be urlencode or rawurlencode, is not - // quite clear, but this seems to be the better choice. - $post[] = urlencode($key) . '=' . urlencode($value); - } - $post = implode('&', $post); + // cURL will handle file upload for us if asked kindly. + foreach ($upload as $key => $file) { + $post[$key] = '@' . realpath($file); } - $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POSTFIELDS => $post, CURLOPT_POST => TRUE)); + $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post)); // Ensure that any changes to variables in the other thread are picked up. $this->refreshVariables(); return $out;