diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-06-06 10:36:45 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-06-06 10:36:45 +0000 |
commit | 5c9d4b9c97ad4097676bd1da4c4467dc7f2e8125 (patch) | |
tree | d740e49c83236a3bbf7429c9509310a2c5955127 /modules/user | |
parent | 4f1af19d101c1940235850a7ea67a0ee7af85880 (diff) | |
download | brdo-5c9d4b9c97ad4097676bd1da4c4467dc7f2e8125.tar.gz brdo-5c9d4b9c97ad4097676bd1da4c4467dc7f2e8125.tar.bz2 |
- Patch #253506 by Daniel, flobruit and catch: made the upload tests work.
Diffstat (limited to 'modules/user')
-rw-r--r-- | modules/user/user.test | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/modules/user/user.test b/modules/user/user.test index a5de598cd..937a4db95 100644 --- a/modules/user/user.test +++ b/modules/user/user.test @@ -205,6 +205,213 @@ class UserDeleteTestCase extends DrupalWebTestCase { } } +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; |