View source
<?php
class UploadPictureTests extends DrupalTestCase {
function get_info() {
return array(
'name' => 'Upload user picture',
'desc' => 'Assure that dimension check, extension check and image scaling work as designed.',
'group' => 'Upload Tests',
);
}
function testDirectories() {
$file_dir = variable_get('file_directory_path', 'files');
$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');
$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);
$user = $this
->drupalCreateUserRolePerm(array(
'access content',
));
$this
->drupalLoginUser($user);
$img_path = realpath(drupal_get_path('module', 'simpletest') . "/tests/upload_tests.test");
$edit = array(
'files[picture_upload]' => $img_path,
);
$this
->drupalPostRequest('user/' . $user->uid . '/edit', $edit, 'Submit');
$this
->assertWantedText(t('The uploaded file was not an image.'), 'The uploaded file was not an image.');
variable_set('user_pictures', $old_pic_set);
}
function testWithGDinvalidDimension() {
if ($this->_directory_Test) {
if (image_get_toolkit()) {
$old_pic_set = variable_get('user_pictures', 0);
variable_set('user_pictures', 1);
$user = $this
->drupalCreateUserRolePerm(array(
'access content',
));
$this
->drupalLoginUser($user);
$old_dim = variable_get('user_picture_dimensions', '85x85');
$old_size = variable_get('user_picture_file_size', '30');
$img_path = realpath("modules/tests/pictureTesting.jpg");
$info = image_get_info($img_path);
$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);
$edit = array(
'picture' => $img_path,
);
$this
->drupalPostRequest('user/' . $user->uid . '/edit', $edit, 'Submit');
$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';
$picture = file_create_url($pic_path);
$content = $this->_browser
->getContent();
$this
->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page");
$this
->assertTrue(is_file($pic_path), "File is located in proper directory");
variable_set('user_picture_file_size', $old_size);
variable_set('user_picture_dimensions', $old_dim);
variable_set('user_pictures', $old_pic_set);
}
}
}
function testWithGDinvalidSize() {
if ($this->_directory_Test) {
if (image_get_toolkit()) {
$old_pic_set = variable_get('user_pictures', 0);
variable_set('user_pictures', 1);
$user = $this
->drupalCreateUserRolePerm(array(
'access content',
));
$this
->drupalLoginUser($user);
$old_dim = variable_get('user_picture_dimensions', '85x85');
$old_size = variable_get('user_picture_file_size', '30');
$img_path = realpath("modules/tests/pictureTesting.jpg");
$info = image_get_info($img_path);
$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
->drupalPostRequest('user/' . $user->uid . '/edit', $edit, 'Submit');
$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';
$picture = file_create_url($pic_path);
$content = $this->_browser
->getContent();
$this
->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page");
$this
->assertTrue(is_file($pic_path), "File is located in proper directory");
variable_set('user_picture_file_size', $old_size);
variable_set('user_picture_dimensions', $old_dim);
variable_set('user_pictures', $old_pic_set);
}
}
}
function testWithoutGDinvalidDimension() {
if ($this->_directory_test) {
if (!image_get_toolkit()) {
$old_pic_set = variable_get('user_pictures', 0);
variable_set('user_pictures', 1);
$user = $this
->drupalCreateUserRolePerm(array(
'access content',
));
$this
->drupalLoginUser($user);
$old_dim = variable_get('user_picture_dimensions', '85x85');
$old_size = variable_get('user_picture_file_size', '30');
$img_path = realpath("modules/tests/pictureTesting.jpg");
$info = image_get_info($img_path);
$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);
$edit = array(
'picture' => $img_path,
);
$this
->drupalPostRequest('user/' . $user->uid . '/edit', $edit, 'Submit');
$text = t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array(
'%dimensions' => variable_get('user_picture_dimensions', '85x85'),
));
$this
->assertWantedText($text, 'Checking response on invalid image (dimensions).');
$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");
variable_set('user_picture_file_size', $old_size);
variable_set('user_picture_dimensions', $old_dim);
variable_set('user_pictures', $old_pic_set);
}
}
}
function testWithoutGDinvalidSize() {
if ($this->_directory_test) {
if (!image_get_toolkit()) {
$old_pic_set = variable_get('user_pictures', 0);
variable_set('user_pictures', 1);
$user = $this
->drupalCreateUserRolePerm(array(
'access content',
));
$this
->drupalLoginUser($user);
$old_dim = variable_get('user_picture_dimensions', '85x85');
$old_size = variable_get('user_picture_file_size', '30');
$img_path = realpath("modules/tests/pictureTesting.jpg");
$info = image_get_info($img_path);
$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
->drupalPostRequest('user/' . $user->uid . '/edit', $edit, 'Submit');
$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
->assertWantedText($text, 'Checking response on invalid image size.');
$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");
variable_set('user_picture_file_size', $old_size);
variable_set('user_picture_dimensions', $old_dim);
variable_set('user_pictures', $old_pic_set);
}
}
}
function testPictureIsValid() {
if ($this->_directory_test) {
$old_pic_set = variable_get('user_pictures', 0);
variable_set('user_pictures', 1);
$user = $this
->drupalCreateUserRolePerm(array(
'access content',
));
$this
->drupalLoginUser($user);
$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/pictureTesting.jpg");
$info = image_get_info($img_path);
$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(
'files[picture_upload]' => $img_path,
);
$this
->drupalPostRequest('user/' . $user->uid . '/edit', $edit, 'Submit');
$picture_dir = variable_get('user_picture_path', 'pictures');
$pic_path = file_directory_path() . '/' . $picture_dir . '/picture-' . $user->uid . '.jpg';
$picture = file_create_url($pic_path);
$content = $this
->drupalGetContent();
$this
->assertTrue(strpos($content, $picture), "Image is displayed in user's profile page");
$this
->assertTrue(is_file($pic_path), "File is located in proper directory");
variable_set('user_picture_file_size', $old_size);
variable_set('user_picture_dimensions', $old_dim);
variable_set('user_pictures', $old_pic_set);
}
}
}