| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: filefield.test,v 1.4 2009/03/27 04:22:58 quicksketch Exp $ 3 4 class ImageFieldTestCase extends FileFieldTestCase { 5 /** 6 * Implementation of setUp(). 7 */ 8 function setUp() { 9 // Views is included here just so that it doesn't whine when CCK tries to 10 // clear the caches. 11 $modules = array_merge(func_get_args(), array('content', 'filefield', 'imagefield', 'token', 'views')); 12 call_user_func_array(array($this, 'parent::setUp'), $modules); 13 } 14 15 /** 16 * Create a new image field. 17 * 18 * @param $name 19 * The name of the new field (all lowercase), exclude the "field_" prefix. 20 * @param $type 21 * The node type that this field will be added to. 22 * @param $field_options 23 * A list of field options that will be added to the defaults. 24 * @param $widget_options 25 * A list of widget options that will be added to the widget defaults. 26 */ 27 function createImageField($name, $type, $field_options = array(), $widget_options = array()) { 28 $display_options = array( 29 'label' => array('format' => 'above'), 30 'teaser' => array('format' => 'image_plain', 'exclude' => 0), 31 'full' => array('format' => 'image_plain', 'exclude' => 0), 32 ); 33 34 $field_options = array_merge(array('widget_type' => 'imagefield_widget', 'display_settings' => $display_options), $field_options); 35 $widget_options = array_merge(array('type' => 'imagefield_widget', 'file_extensions' => 'jpg jpeg png gif', 'custom_alt' => '1', 'custom_title' => '1', 'max_resolution' => '', 'min_resolution' => ''), $widget_options); 36 return parent::createFileField($name, $type, $field_options, $widget_options); 37 } 38 } 39 40 /** 41 * Test class to check that formatters are working properly. 42 */ 43 class ImageFieldDisplayTestCase extends ImageFieldTestCase { 44 function getInfo() { 45 return array( 46 'name' => t('ImageField display tests'), 47 'description' => t('Test the display of image fields in nodes.'), 48 'group' => t('ImageField'), 49 ); 50 } 51 52 /** 53 * Test normal formatter display on node display. 54 */ 55 function testNodeDisplay() { 56 $field_name = 'field_' . strtolower($this->randomName()); 57 $type = $this->drupalCreateContentType(); 58 $field = $this->createImageField($field_name, $type->name, array(), array('custom_alt' => '1', 'custom_title' => '1')); 59 $test_file = $this->getTestFile('image'); 60 61 // Create a new node with the uploaded file. 62 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 63 64 // Add an alt and title value. 65 $edit = array( 66 $field['field_name'] . '[0][data][alt]' => 'foo', 67 $field['field_name'] . '[0][data][title]' => 'bar', 68 ); 69 $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save')); 70 71 // Check that the image formatter is displaying. 72 $node = node_load($nid, NULL, TRUE); 73 $node_file = $node->{$field['field_name']}[0]; 74 75 $class = 'imagefield imagefield-'. $field['field_name']; 76 $default_output = theme('imagefield_image', $node_file, $node_file['data']['alt'], $node_file['data']['title'], array('class' => $class)); 77 $this->assertRaw($default_output, t('Image formatter displaying correctly on full node view.')); 78 } 79 80 /** 81 * Test normal formatter display on node display. 82 */ 83 function testDefaultImage() { 84 $field_name = 'field_' . strtolower($this->randomName()); 85 $type = $this->drupalCreateContentType(); 86 $type->url_name = str_replace('_', '-', $type->name); 87 $field = $this->createImageField($field_name, $type->name); 88 89 $test_file = $this->getTestFile('image'); 90 91 // Set a default image. 92 $destination = file_directory_path(); 93 $default_image = field_file_save_file($test_file->filepath, array(), $destination); 94 $widget_options = array( 95 'use_default_image' => '1', 96 'default_image' => $default_image, 97 ); 98 $field = $this->updateFileField($field_name, $type->name, array(), $widget_options); 99 100 // TODO: Figure out why saving through the form in SimpleTest doesn't work. 101 /** 102 // Set a default image. 103 $edit = array( 104 'use_default_image' => '1', 105 'files[default_image_upload]' => realpath($test_file->filepath), 106 ); 107 $this->drupalPost('admin/content/node-type/' . $type->url_name . '/fields/' . $field['field_name'], $edit, t('Save field settings')); 108 109 // Check that the field was saved. 110 $field = content_fields($field['field_name'], $type->name); 111 **/ 112 113 // Create a new node without a file and check the default. 114 $edit = array('title' => $this->randomName()); 115 $this->drupalPost('node/add/' . $type->url_name, $edit, t('Save')); 116 117 // Check that the image formatter is displaying. 118 $class = 'imagefield imagefield-'. $field['field_name']; 119 $default_output = theme('imagefield_image', $default_image, NULL, NULL, array('class' => $class)); 120 121 $this->assertRaw($default_output, t('Default image displaying correctly on full node view.')); 122 } 123 124 } 125 126 /** 127 * Test class to check for various validations. 128 * 129 * This class subclasses FileField's validations, making it so that all 130 * validation tests in FileField are run again on the ImageField widget. 131 */ 132 class ImageFieldValidateTestCase extends FileFieldValidateTestCase { 133 134 function getInfo() { 135 return array( 136 'name' => t('ImageField validation tests'), 137 'description' => t('Tests validation functions such as miniumum and maximum resolution, plus running all normal FileField validation tests.'), 138 'group' => t('ImageField'), 139 ); 140 } 141 142 /** 143 * Implementation of setUp(). 144 */ 145 function setUp() { 146 // Views is included here just so that it doesn't whine when CCK tries to 147 // clear the caches. 148 $modules = array_merge(func_get_args(), array('content', 'filefield', 'imagefield', 'token', 'views')); 149 call_user_func_array(array($this, 'parent::setUp'), $modules); 150 151 // Then we'll convert it to an ImageField. 152 $widget_options = array( 153 'type' => 'imagefield_widget', 154 'module' => 'imagefield', 155 'custom_alt' => '1', 156 'custom_title' => '1', 157 'max_resolution' => '', 158 'min_resolution' => '', 159 ); 160 $this->field = $this->updateFileField($this->field['field_name'], $this->node_type->name, array(), $widget_options); 161 } 162 163 /** 164 * Test resolution settings are checked on upload. 165 */ 166 function testResolution() { 167 $type = $this->node_type; 168 $field = $this->field; 169 170 // Get our test file (PNG, 360x240). 171 $test_file = $this->getTestFile('image', 64027); 172 $test_image_info = image_get_info($test_file->filepath); 173 $width = $test_image_info['width']; 174 $height = $test_image_info['height']; 175 176 // Set the resolution to a minimum to check on x axis. 177 $widget_options = array('min_resolution' => ($width + 1) . 'x1'); 178 $this->updateFileField($this->field['field_name'], $type->name, array(), $widget_options); 179 $this->uploadNodeFile($test_file, $this->field, $type->name); 180 $this->assertRaw(t('The image is too small; the minimum dimensions are'), t('Image could not be saved when too small horizontally.')); 181 182 // Set the resolution to a minimum to check on y axis. 183 $widget_options = array('min_resolution' => '1x' . ($height + 1)); 184 $this->updateFileField($this->field['field_name'], $type->name, array(), $widget_options); 185 $this->uploadNodeFile($test_file, $this->field, $type->name); 186 $this->assertRaw('The image is too small; the minimum dimensions are', t('Image could not be saved when too small vertically.')); 187 188 // Remove the minimum dimension add a max dimension on the x axis. 189 $widget_options = array('min_resolution' => '', 'max_resolution' => ($width/4) . 'x40000'); 190 $this->updateFileField($this->field['field_name'], $type->name, array(), $widget_options); 191 $nid = $this->uploadNodeFile($test_file, $this->field, $type->name); 192 $this->assertRaw('The image was resized to fit within the maximum allowed dimensions', t('Image resized when too large horizontally.')); 193 // Validate the image size. 194 $node = node_load($nid, NULL, TRUE); 195 $node_file = $node->{$field['field_name']}[0]; 196 $image_info = image_get_info($node_file['filepath']); 197 $this->assertTrue(($image_info['height'] == ($height/4) && $image_info['width'] == ($width/4)), t('Resized image has proper dimensions.')); 198 199 // Add a max dimension on the y axis. 200 $widget_options = array('max_resolution' => '40000x' . ($height/2)); 201 $this->updateFileField($this->field['field_name'], $type->name, array(), $widget_options); 202 $nid = $this->uploadNodeFile($test_file, $this->field, $type->name); 203 $this->assertRaw('The image was resized to fit within the maximum allowed dimensions', t('Image resized when too large vertically.')); 204 // Validate the image size. 205 $node = node_load($nid, NULL, TRUE); 206 $node_file = $node->{$field['field_name']}[0]; 207 $image_info = image_get_info($node_file['filepath']); 208 $this->assertTrue(($image_info['height'] == ($height/2) && $image_info['width'] == ($width/2)), t('Resized image has proper dimensions.')); 209 210 // Set exact dimensions on the field and make sure the image doesn't get 211 // resized and the upload is allowed. 212 $widget_options = array('min_resolution' => '360x240', 'max_resolution' => '360x240'); 213 $this->updateFileField($this->field['field_name'], $type->name, array(), $widget_options); 214 $nid = $this->uploadNodeFile($test_file, $this->field, $type->name); 215 $this->assertNoRaw('The image was resized to fit within the maximum allowed dimensions', t('Image was not resized when uploaded with exact dimensions.')); 216 // Validate the image size. 217 $node = node_load($nid, NULL, TRUE); 218 $node_file = $node->{$field['field_name']}[0]; 219 $image_info = image_get_info($node_file['filepath']); 220 $this->assertTrue(($image_info['height'] == $height && $image_info['width'] == $width), t('Resized image has proper dimensions.')); 221 222 // Check that a scaled image will fit between the resolutions. 223 $widget_options = array('min_resolution' => '300x200', 'max_resolution' => '340x220'); 224 $this->updateFileField($this->field['field_name'], $type->name, array(), $widget_options); 225 $nid = $this->uploadNodeFile($test_file, $this->field, $type->name); 226 $this->assertRaw('The image was resized to fit within the maximum allowed dimensions', t('Image was resized when fitting between maximum and minimum dimensions.')); 227 228 // Check that an image not fitting between dimensions will not upload. 229 $widget_options = array('min_resolution' => '220x360', 'max_resolution' => '240x360'); 230 $this->updateFileField($this->field['field_name'], $type->name, array(), $widget_options); 231 $nid = $this->uploadNodeFile($test_file, $this->field, $type->name); 232 $this->assertRaw('The image will not fit between the dimensions of', t('Image was not uploaded when not fitting between maximum and minimum dimensions.')); 233 } 234 235 /** 236 * Skip the max file size test, since it uses text files. 237 */ 238 function testFileMaxSize() { 239 return; 240 } 241 242 /** 243 * Skip the max file size per node test, since it uses text files. 244 */ 245 function testNodeMaxSize() { 246 return; 247 } 248 249 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |