View source
<?php
class ImageModuleTest extends DrupalTestCase {
function get_info() {
return array(
'name' => t('Image Module'),
'desc' => t('Testing add/edit/delete image node. If GD tooklit is found, perform image crop test. Requires image.module'),
'group' => 'Image',
);
}
function setUp() {
parent::setUp();
$dir = file_directory_path() . '/' . variable_get('image_default_path', 'images');
$temp_dir = $dir . '/temp';
$dir_created = file_check_directory($dir, TRUE);
$temp_dir_created = file_check_directory($temp_dir, TRUE);
if (!$dir_created || !$temp_dir_created) {
print "Images dir could not be create";
exit;
}
}
function _rolesApi($op, $edit) {
if ($op == 'delete') {
$id = $edit['rid'];
db_query('DELETE FROM {role} WHERE rid = %d', $id);
db_query('DELETE FROM {permission} WHERE rid = %d', $id);
$result = db_query('SELECT DISTINCT(ur1.uid) FROM {users_roles} ur1 LEFT JOIN {users_roles} ur2 ON ur2.uid = ur1.uid WHERE ur1.rid = %d AND ur2.rid != ur1.rid', $id);
$uid = array();
while ($u = db_fetch_object($result)) {
$uid[] = $u->uid;
}
if ($uid) {
db_query('DELETE FROM {users_roles} WHERE rid = %d AND uid IN (%s)', $id, implode(', ', $uid));
}
db_query('UPDATE {users_roles} SET rid = %d WHERE rid = %d', _user_authenticated_id(), $id);
}
else {
if ($op == 'add') {
if (isset($edit['name'])) {
db_query("INSERT INTO {role} (name) VALUES ('%s')", $edit['name']);
$result = db_query("SELECT rid FROM {role} WHERE name = '%s'", $edit['name']);
$rid = db_result($result);
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, $edit['perm']);
return $rid;
}
else {
return 0;
}
}
}
}
function testImageNode() {
global $base_url;
$this
->drupalModuleEnable('image');
$perm = array(
'access content',
'create images',
'administer nodes',
);
$account = $this
->drupalCreateUserRolePerm($perm);
$this
->drupalLoginUser($account);
$title = $this
->randomName(10);
$description = $this
->randomName(100);
$img_path = dirname(__FILE__) . '/ImageModuleTest.png';
$my_size = getimagesize($img_path);
if (image_get_toolkit()) {
$temppath = variable_get('image_default_path', 'images') . '/temp';
$path = realpath(file_create_path($temppath));
$new_image = $path . '/ImageTestGd.png';
$width = rand(20, $my_size[0]);
$height = rand(20, $my_size[1]);
image_crop($img_path, $new_image, 0, 0, $width, $height);
$my_size = getimagesize($new_image);
$img_path = $new_image;
}
unset($edit);
$edit = array(
'title' => $title,
'body' => $description,
'files[image]' => $img_path,
);
$this
->drupalPostRequest('node/add/image', $edit, 'Submit');
$content = $this->_browser
->getContent();
$this
->assertWantedRaw(t('Your %post has been created.', array(
'%post' => 'Image',
)), "Checking message");
$node = node_load(array(
'title' => $title,
));
$this
->drupalGet(url("node/{$node->nid}", NULL, NULL, TRUE));
$content = $this->_browser
->getContent();
$this
->assertWantedText($title, "Checking title : {$title}");
$this
->assertWantedText($description, "Checking body: {$description}");
preg_match_all('~<img.*?src="?([^"]*png?)"?[^>]*>~i', $content, $matches);
$found = false;
foreach ($matches[1] as $file) {
if (strripos($file, "TestGD")) {
$size = getimagesize($file);
if ($size == $my_size) {
$found = true;
break;
}
}
}
$this
->assertTrue($found, 'Checking for presence and size of uploaded image');
$title = $this
->randomName(10);
$description = $this
->randomName(100);
$img_path = dirname(__FILE__) . '/ImageModuleTest.png';
unset($edit);
$edit = array(
'title' => $title,
'body' => $description,
'files[image]' => $img_path,
);
$this
->drupalPostRequest('node/' . $node->nid . '/edit', $edit, 'Submit');
$content = $this->_browser
->getContent();
$this
->drupalGet(url("node/{$node->nid}"));
$this
->assertWantedText($title, "Checking title : {$title}");
$this
->assertWantedText($description, "Checking body: {$description}");
}
}