1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660 |
- <?php
- class ImageFieldTestCase extends DrupalWebTestCase {
- protected $admin_user;
- function setUp() {
- parent::setUp('image');
- $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles'));
- $this->drupalLogin($this->admin_user);
- }
-
- function createImageField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
- $field = array(
- 'field_name' => $name,
- 'type' => 'image',
- 'settings' => array(),
- 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
- );
- $field['settings'] = array_merge($field['settings'], $field_settings);
- field_create_field($field);
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => 'node',
- 'label' => $name,
- 'bundle' => $type_name,
- 'required' => !empty($instance_settings['required']),
- 'settings' => array(),
- 'widget' => array(
- 'type' => 'image_image',
- 'settings' => array(),
- ),
- );
- $instance['settings'] = array_merge($instance['settings'], $instance_settings);
- $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
- return field_create_instance($instance);
- }
-
- function uploadNodeImage($image, $field_name, $type) {
- $edit = array(
- 'title' => $this->randomName(),
- );
- $edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($image->uri);
- $this->drupalPost('node/add/' . $type, $edit, t('Save'));
-
- $matches = array();
- preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
- return isset($matches[1]) ? $matches[1] : FALSE;
- }
- }
- class ImageStylesPathAndUrlTestCase extends DrupalWebTestCase {
- protected $style_name;
- protected $image_info;
- protected $image_filepath;
- public static function getInfo() {
- return array(
- 'name' => 'Image styles path and URL functions',
- 'description' => 'Tests functions for generating paths and URLs to image styles.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp('image_module_test');
- $this->style_name = 'style_foo';
- image_style_save(array('name' => $this->style_name));
- }
-
- function testImageStylePath() {
- $scheme = 'public';
- $actual = image_style_path($this->style_name, "$scheme://foo/bar.gif");
- $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
- $this->assertEqual($actual, $expected, t('Got the path for a file URI.'));
- $actual = image_style_path($this->style_name, 'foo/bar.gif');
- $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
- $this->assertEqual($actual, $expected, t('Got the path for a relative file path.'));
- }
-
- function testImageStyleUrlAndPathPublic() {
- $this->_testImageStyleUrlAndPath('public');
- }
-
- function testImageStyleUrlAndPathPrivate() {
- $this->_testImageStyleUrlAndPath('private');
- }
-
- function testImageStylUrlAndPathPublicUnclean() {
- $this->_testImageStyleUrlAndPath('public', FALSE);
- }
-
- function testImageStyleUrlAndPathPrivateUnclean() {
- $this->_testImageStyleUrlAndPath('private', FALSE);
- }
-
- function _testImageStyleUrlAndPath($scheme, $clean_url = TRUE) {
-
-
- variable_set('file_default_scheme', 'temporary');
- variable_set('clean_url', $clean_url);
-
- $directory = $scheme . '://styles/' . $this->style_name;
- $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
- $this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.'));
-
- $files = $this->drupalGetTestFiles('image');
- $file = array_shift($files);
- $image_info = image_get_info($file->uri);
- $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
-
-
- variable_set('image_module_test_file_download', $original_uri);
- $this->assertNotIdentical(FALSE, $original_uri, t('Created the generated image file.'));
-
- $generated_uri = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. drupal_basename($original_uri);
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $generate_url = image_style_url($this->style_name, $original_uri);
- if (!$clean_url) {
- $this->assertTrue(strpos($generate_url, '?q=') !== FALSE, 'When using non-clean URLS, the system path contains the query string.');
- }
-
- $this->drupalGet($generate_url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- $this->assertRaw(file_get_contents($generated_uri), t('URL returns expected file.'));
- $generated_image_info = image_get_info($generated_uri);
- $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], t('Expected Content-Type was reported.'));
- $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], t('Expected Content-Length was reported.'));
- if ($scheme == 'private') {
- $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
- $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', t('Cache-Control header was set to prevent caching.'));
- $this->assertEqual($this->drupalGetHeader('X-Image-Owned-By'), 'image_module_test', t('Expected custom header has been added.'));
-
-
- $this->drupalGet($generate_url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
-
-
- $file_noaccess = array_shift($files);
- $original_uri_noaccess = file_unmanaged_copy($file_noaccess->uri, $scheme . '://', FILE_EXISTS_RENAME);
- $generated_uri_noaccess = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. drupal_basename($original_uri_noaccess);
- $this->assertFalse(file_exists($generated_uri_noaccess), t('Generated file does not exist.'));
- $generate_url_noaccess = image_style_url($this->style_name, $original_uri_noaccess);
- $this->drupalGet($generate_url_noaccess);
- $this->assertResponse(403, t('Confirmed that access is denied for the private image style.') );
-
- if (strpos($generate_url, '.png') === FALSE ) {
- $this->fail('Confirming that private image styles are not appended require PNG file.');
- }
- else {
-
-
- $this->assertNoRaw( chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), 'No PNG signature found in the response body.');
- }
- }
- }
- }
- class ImageEffectsUnitTest extends ImageToolkitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image effects',
- 'description' => 'Test that the image effects pass parameters to the toolkit correctly.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp('image_module_test');
- module_load_include('inc', 'image', 'image.effects');
- }
-
- function testResizeEffect() {
- $this->assertTrue(image_resize_effect($this->image, array('width' => 1, 'height' => 2)), t('Function returned the expected value.'));
- $this->assertToolkitOperationsCalled(array('resize'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 1, t('Width was passed correctly'));
- $this->assertEqual($calls['resize'][0][2], 2, t('Height was passed correctly'));
- }
-
- function testScaleEffect() {
-
- $this->assertTrue(image_scale_effect($this->image, array('width' => 10, 'height' => 10)), t('Function returned the expected value.'));
- $this->assertToolkitOperationsCalled(array('resize'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['resize'][0][1], 10, t('Width was passed correctly'));
- $this->assertEqual($calls['resize'][0][2], 5, t('Height was based off aspect ratio and passed correctly'));
- }
-
- function testCropEffect() {
-
- $this->assertTrue(image_crop_effect($this->image, array('anchor' => 'top-1', 'width' => 3, 'height' => 4)), t('Function returned the expected value.'));
- $this->assertToolkitOperationsCalled(array('crop'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['crop'][0][1], 0, t('X was passed correctly'));
- $this->assertEqual($calls['crop'][0][2], 1, t('Y was passed correctly'));
- $this->assertEqual($calls['crop'][0][3], 3, t('Width was passed correctly'));
- $this->assertEqual($calls['crop'][0][4], 4, t('Height was passed correctly'));
- }
-
- function testScaleAndCropEffect() {
- $this->assertTrue(image_scale_and_crop_effect($this->image, array('width' => 5, 'height' => 10)), t('Function returned the expected value.'));
- $this->assertToolkitOperationsCalled(array('resize', 'crop'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['crop'][0][1], 7.5, t('X was computed and passed correctly'));
- $this->assertEqual($calls['crop'][0][2], 0, t('Y was computed and passed correctly'));
- $this->assertEqual($calls['crop'][0][3], 5, t('Width was computed and passed correctly'));
- $this->assertEqual($calls['crop'][0][4], 10, t('Height was computed and passed correctly'));
- }
-
- function testDesaturateEffect() {
- $this->assertTrue(image_desaturate_effect($this->image, array()), t('Function returned the expected value.'));
- $this->assertToolkitOperationsCalled(array('desaturate'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual(count($calls['desaturate'][0]), 1, t('Only the image was passed.'));
- }
-
- function testRotateEffect() {
-
- $this->assertTrue(image_rotate_effect($this->image, array('degrees' => 90, 'bgcolor' => '#fff')), t('Function returned the expected value.'));
- $this->assertToolkitOperationsCalled(array('rotate'));
-
- $calls = image_test_get_all_calls();
- $this->assertEqual($calls['rotate'][0][1], 90, t('Degrees were passed correctly'));
- $this->assertEqual($calls['rotate'][0][2], 0xffffff, t('Background color was passed correctly'));
- }
-
- function testImageEffectsCaching() {
- $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
-
- $effects = image_effect_definitions();
- $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
-
- drupal_static_reset('image_effect_definitions');
- drupal_static_reset('image_module_test_image_effect_info_alter');
- $cached_effects = image_effect_definitions();
- $this->assertTrue(is_null($image_effect_definitions_called), 'image_effect_definitions() returned data from cache.');
- $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
- }
- }
- class ImageAdminStylesUnitTest extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image styles and effects UI configuration',
- 'description' => 'Tests creation, deletion, and editing of image styles and effects at the UI level.',
- 'group' => 'Image',
- );
- }
-
- function createSampleImage($style) {
- static $file_path;
-
-
- if (!isset($file_path)) {
- $files = $this->drupalGetTestFiles('image');
- $file = reset($files);
- $file_path = file_unmanaged_copy($file->uri);
- }
- return image_style_url($style['name'], $file_path) ? $file_path : FALSE;
- }
-
- function getImageCount($style) {
- return count(file_scan_directory('public://styles/' . $style['name'], '/.*/'));
- }
-
- function testNumericStyleName() {
- $style_name = rand();
- $edit = array(
- 'name' => $style_name,
- );
- $this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
- $this->assertRaw(t('Style %name was created.', array('%name' => $style_name)), t('Image style successfully created.'));
- $options = image_style_options();
- $this->assertTrue(array_key_exists($style_name, $options), t('Array key %key exists.', array('%key' => $style_name)));
- }
-
- function testStyle() {
-
- $style_name = strtolower($this->randomName(10));
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
- $effect_edits = array(
- 'image_resize' => array(
- 'data[width]' => 100,
- 'data[height]' => 101,
- ),
- 'image_scale' => array(
- 'data[width]' => 110,
- 'data[height]' => 111,
- 'data[upscale]' => 1,
- ),
- 'image_scale_and_crop' => array(
- 'data[width]' => 120,
- 'data[height]' => 121,
- ),
- 'image_crop' => array(
- 'data[width]' => 130,
- 'data[height]' => 131,
- 'data[anchor]' => 'center-center',
- ),
- 'image_desaturate' => array(
-
- ),
- 'image_rotate' => array(
- 'data[degrees]' => 5,
- 'data[random]' => 1,
- 'data[bgcolor]' => '#FFFF00',
- ),
- );
-
- $edit = array(
- 'name' => $style_name,
- );
- $this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
- $this->assertRaw(t('Style %name was created.', array('%name' => $style_name)), t('Image style successfully created.'));
-
-
- foreach ($effect_edits as $effect => $edit) {
-
- $this->drupalPost($style_path, array('new' => $effect), t('Add'));
- if (!empty($edit)) {
- $this->drupalPost(NULL, $edit, t('Add effect'));
- }
- }
-
-
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
- foreach ($style['effects'] as $ieid => $effect) {
- $this->drupalGet($style_path . '/effects/' . $ieid);
- foreach ($effect_edits[$effect['name']] as $field => $value) {
- $this->assertFieldByName($field, $value, t('The %field field in the %effect effect has the correct value of %value.', array('%field' => $field, '%effect' => $effect['name'], '%value' => $value)));
- }
- }
-
-
-
- $effect_edits_order = array_keys($effect_edits);
- $effects_order = array_values($style['effects']);
- $order_correct = TRUE;
- foreach ($effects_order as $index => $effect) {
- if ($effect_edits_order[$index] != $effect['name']) {
- $order_correct = FALSE;
- }
- }
- $this->assertTrue($order_correct, t('The order of the effects is correctly set by default.'));
-
-
- $style_name = strtolower($this->randomName(10));
- $weight = count($effect_edits);
- $edit = array(
- 'name' => $style_name,
- );
- foreach ($style['effects'] as $ieid => $effect) {
- $edit['effects[' . $ieid . '][weight]'] = $weight;
- $weight--;
- }
-
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
- $this->drupalPost($style_path, $edit, t('Update style'));
-
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
-
- $this->drupalGet($style_path);
- $this->assertResponse(200, t('Image style %original renamed to %new', array('%original' => $style['name'], '%new' => $style_name)));
-
-
-
- $this->assertEqual($this->getImageCount($style), 0, t('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style['name'])));
-
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name, NULL);
-
- $effect_edits_order = array_reverse($effect_edits_order);
- $effects_order = array_values($style['effects']);
- $order_correct = TRUE;
- foreach ($effects_order as $index => $effect) {
- if ($effect_edits_order[$index] != $effect['name']) {
- $order_correct = FALSE;
- }
- }
- $this->assertTrue($order_correct, t('The order of the effects is correctly set by default.'));
-
-
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
-
- $effect = array_pop($style['effects']);
- $this->drupalPost($style_path . '/effects/' . $effect['ieid'] . '/delete', array(), t('Delete'));
- $this->assertRaw(t('The image effect %name has been deleted.', array('%name' => $effect['label'])), t('Image effect deleted.'));
-
-
- $this->drupalPost('admin/config/media/image-styles/delete/' . $style_name, array(), t('Delete'));
-
- $directory = file_default_scheme() . '://styles/' . $style_name;
- $this->assertFalse(is_dir($directory), t('Image style %style directory removed on style deletion.', array('%style' => $style['name'])));
- drupal_static_reset('image_styles');
- $this->assertFalse(image_style_load($style_name), t('Image style %style successfully deleted.', array('%style' => $style['name'])));
- }
-
- function testDefaultStyle() {
-
- $style_name = 'thumbnail';
- $edit_path = 'admin/config/media/image-styles/edit/' . $style_name;
- $delete_path = 'admin/config/media/image-styles/delete/' . $style_name;
- $revert_path = 'admin/config/media/image-styles/revert/' . $style_name;
-
- $this->drupalGet($delete_path);
- $this->assertText(t('Page not found'), t('Default styles may not be deleted.'));
-
- $this->drupalGet($edit_path);
- $this->assertNoField('edit-name', t('Default styles may not be renamed.'));
- $this->assertNoField('edit-submit', t('Default styles may not be edited.'));
- $this->assertNoField('edit-add', t('Default styles may not have new effects added.'));
-
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
-
- foreach ($style['effects'] as $effect) {
- $this->assertFalse(isset($effect['ieid']), t('The %effect effect does not have an ieid.', array('%effect' => $effect['name'])));
- }
-
- $this->drupalPost($edit_path, array(), t('Override defaults'));
- $this->assertRaw(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $style_name)), t('Default image style may be overridden.'));
-
- $this->drupalPost($edit_path, array('new' => 'image_desaturate'), t('Add'));
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
-
- foreach ($style['effects'] as $effect) {
- $this->assertTrue(isset($effect['ieid']), t('The %effect effect has an ieid.', array('%effect' => $effect['name'])));
- }
-
-
- $effects = array_values($style['effects']);
- $this->assertEqual($effects[0]['name'], 'image_scale', t('The default effect still exists in the overridden style.'));
- $this->assertEqual($effects[1]['name'], 'image_desaturate', t('The added effect exists in the overridden style.'));
-
- $this->drupalGet($edit_path);
- $this->assertNoField('edit-name', t('Overridden styles may not be renamed.'));
-
- $image_path = $this->createSampleImage($style);
- $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
-
- $this->drupalPost($revert_path, array(), t('Revert'));
- drupal_static_reset('image_styles');
- $style = image_style_load($style_name);
-
- $effects = array_values($style['effects']);
- $this->assertEqual($effects[0]['name'], 'image_scale', t('The default effect still exists in the reverted style.'));
- $this->assertFalse(array_key_exists(1, $effects), t('The added effect has been removed in the reverted style.'));
- }
-
- function testStyleReplacement() {
-
- $style_name = strtolower($this->randomName(10));
- image_style_save(array('name' => $style_name));
- $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
-
- $field_name = strtolower($this->randomName(10));
- $this->createImageField($field_name, 'article');
- $instance = field_info_instance('node', $field_name, 'article');
- $instance['display']['default']['type'] = 'image';
- $instance['display']['default']['settings']['image_style'] = $style_name;
- field_update_instance($instance);
-
- $test_image = current($this->drupalGetTestFiles('image'));
- $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
- $node = node_load($nid);
-
- $this->drupalGet('node/' . $nid);
- $this->assertRaw(image_style_url($style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style @style.', array('@style' => $style_name)));
-
- $new_style_name = strtolower($this->randomName(10));
- $edit = array(
- 'name' => $new_style_name,
- );
- $this->drupalPost('admin/config/media/image-styles/edit/' . $style_name, $edit, t('Update style'));
- $this->assertText(t('Changes to the style have been saved.'), t('Style %name was renamed to %new_name.', array('%name' => $style_name, '%new_name' => $new_style_name)));
- $this->drupalGet('node/' . $nid);
- $this->assertRaw(image_style_url($new_style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style replacement style.'));
-
- $edit = array(
- 'replacement' => 'thumbnail',
- );
- $this->drupalPost('admin/config/media/image-styles/delete/' . $new_style_name, $edit, t('Delete'));
- $message = t('Style %name was deleted.', array('%name' => $new_style_name));
- $this->assertRaw($message, $message);
- $this->drupalGet('node/' . $nid);
- $this->assertRaw(image_style_url('thumbnail', $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style replacement style.'));
- }
- }
- class ImageFieldDisplayTestCase extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image field display tests',
- 'description' => 'Test the display of image fields.',
- 'group' => 'Image',
- );
- }
-
- function testImageFieldFormattersPublic() {
- $this->_testImageFieldFormatters('public');
- }
-
- function testImageFieldFormattersPrivate() {
-
- user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access content' => FALSE));
- $this->_testImageFieldFormatters('private');
- }
-
- function _testImageFieldFormatters($scheme) {
- $field_name = strtolower($this->randomName());
- $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
-
- $test_image = current($this->drupalGetTestFiles('image'));
- $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
- $node = node_load($nid, NULL, TRUE);
-
- $image_uri = $node->{$field_name}[LANGUAGE_NONE][0]['uri'];
- $image_info = array(
- 'path' => $image_uri,
- 'width' => 40,
- 'height' => 20,
- );
- $default_output = theme('image', $image_info);
- $this->assertRaw($default_output, t('Default formatter displaying correctly on full node view.'));
-
- $instance = field_info_instance('node', $field_name, 'article');
- $instance['display']['default']['type'] = 'image';
- $instance['display']['default']['settings']['image_link'] = 'file';
- field_update_instance($instance);
- $default_output = l(theme('image', $image_info), file_create_url($image_uri), array('html' => TRUE));
- $this->drupalGet('node/' . $nid);
- $this->assertRaw($default_output, t('Image linked to file formatter displaying correctly on full node view.'));
-
- $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), t('File was downloaded successfully.'));
- if ($scheme == 'private') {
-
-
- $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png', t('Content-Type header was sent.'));
- $this->assertEqual($this->drupalGetHeader('Content-Disposition'), 'inline; filename="' . $test_image->filename . '"', t('Content-Disposition header was sent.'));
- $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'private', t('Cache-Control header was sent.'));
-
- $this->drupalLogout();
- $this->drupalGet(file_create_url($image_uri));
- $this->assertResponse('403', t('Access denied to original image as anonymous user.'));
-
- $this->drupalLogin($this->admin_user);
- }
-
- $instance['display']['default']['settings']['image_link'] = 'content';
- field_update_instance($instance);
- $default_output = l(theme('image', $image_info), 'node/' . $nid, array('html' => TRUE, 'attributes' => array('class' => 'active')));
- $this->drupalGet('node/' . $nid);
- $this->assertRaw($default_output, t('Image linked to content formatter displaying correctly on full node view.'));
-
- $instance['display']['default']['settings']['image_link'] = '';
- $instance['display']['default']['settings']['image_style'] = 'thumbnail';
- field_update_instance($instance);
-
-
- $this->drupalGet(image_style_url('thumbnail', $image_uri));
- $image_info['path'] = image_style_path('thumbnail', $image_uri);
- $image_info['width'] = 100;
- $image_info['height'] = 50;
- $default_output = theme('image', $image_info);
- $this->drupalGet('node/' . $nid);
- $this->assertRaw($default_output, t('Image style thumbnail formatter displaying correctly on full node view.'));
- if ($scheme == 'private') {
-
- $this->drupalLogout();
- $this->drupalGet(image_style_url('thumbnail', $image_uri));
- $this->assertResponse('403', t('Access denied to image style thumbnail as anonymous user.'));
- }
- }
-
- function testImageFieldSettings() {
- $test_image = current($this->drupalGetTestFiles('image'));
- list(, $test_image_extension) = explode('.', $test_image->filename);
- $field_name = strtolower($this->randomName());
- $instance_settings = array(
- 'alt_field' => 1,
- 'file_extensions' => $test_image_extension,
- 'max_filesize' => '50 KB',
- 'max_resolution' => '100x100',
- 'min_resolution' => '10x10',
- 'title_field' => 1,
- );
- $widget_settings = array(
- 'preview_image_style' => 'medium',
- );
- $field = $this->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings);
- $field['deleted'] = 0;
- $table = _field_sql_storage_tablename($field);
- $schema = drupal_get_schema($table, TRUE);
- $instance = field_info_instance('node', $field_name, 'article');
- $this->drupalGet('node/add/article');
- $this->assertText(t('Files must be less than 50 KB.'), t('Image widget max file size is displayed on article form.'));
- $this->assertText(t('Allowed file types: ' . $test_image_extension . '.'), t('Image widget allowed file types displayed on article form.'));
- $this->assertText(t('Images must be between 10x10 and 100x100 pixels.'), t('Image widget allowed resolution displayed on article form.'));
-
-
- $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
- $this->drupalGet('node/' . $nid . '/edit');
- $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][alt]', '', t('Alt field displayed on article form.'));
- $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][title]', '', t('Title field displayed on article form.'));
-
-
- $node = node_load($nid, NULL, TRUE);
- $image_info = array(
- 'path' => image_style_url('medium', $node->{$field_name}[LANGUAGE_NONE][0]['uri']),
- 'width' => 220,
- 'height' => 110,
- );
- $default_output = theme('image', $image_info);
- $this->assertRaw($default_output, t("Preview image is displayed using 'medium' style."));
-
- $image_info = array(
- 'path' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
- 'alt' => $this->randomName(),
- 'title' => $this->randomName(),
- 'width' => 40,
- 'height' => 20,
- );
- $edit = array(
- $field_name . '[' . LANGUAGE_NONE . '][0][alt]' => $image_info['alt'],
- $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $image_info['title'],
- );
- $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
- $default_output = theme('image', $image_info);
- $this->assertRaw($default_output, t('Image displayed using user supplied alt and title attributes.'));
-
- $test_size = 2000;
- $edit = array(
- $field_name . '[' . LANGUAGE_NONE . '][0][alt]' => $this->randomName($test_size),
- $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $this->randomName($test_size),
- );
- $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
- $this->assertRaw(t('Alternate text cannot be longer than %max characters but is currently %length characters long.', array(
- '%max' => $schema['fields'][$field_name .'_alt']['length'],
- '%length' => $test_size,
- )));
- $this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
- '%max' => $schema['fields'][$field_name .'_title']['length'],
- '%length' => $test_size,
- )));
- }
-
- function testImageFieldFormatterAttributes() {
- $image = theme('image_formatter', array(
- 'item' => array(
- 'uri' => 'http://example.com/example.png',
- 'attributes' => array(
- 'data-image-field-formatter' => 'testFound',
- ),
- 'alt' => t('Image field formatter attribute test.'),
- 'title' => t('Image field formatter'),
- ),
- ));
- $this->assertTrue(stripos($image, 'testFound') > 0, t('Image field formatters can have attributes.'));
- }
-
- function testImageFieldDefaultImage() {
-
- $field_name = strtolower($this->randomName());
- $this->createImageField($field_name, 'article');
-
-
- $node = $this->drupalCreateNode(array('type' => 'article'));
- $this->drupalGet('node/' . $node->nid);
-
-
- $this->assertNoPattern('<div class="(.*?)field-name-' . strtr($field_name, '_', '-') . '(.*?)">', t('No image displayed when no image is attached and no default image specified.'));
-
- $images = $this->drupalGetTestFiles('image');
- $edit = array(
- 'files[field_settings_default_image]' => drupal_realpath($images[0]->uri),
- );
- $this->drupalPost('admin/structure/types/manage/article/fields/' . $field_name, $edit, t('Save settings'));
-
- field_info_cache_clear();
- $field = field_info_field($field_name);
- $image = file_load($field['settings']['default_image']);
- $this->assertTrue($image->status == FILE_STATUS_PERMANENT, t('The default image status is permanent.'));
- $default_output = theme('image', array('path' => $image->uri));
- $this->drupalGet('node/' . $node->nid);
- $this->assertRaw($default_output, t('Default image displayed when no user supplied image is present.'));
-
-
- $nid = $this->uploadNodeImage($images[1], $field_name, 'article');
- $node = node_load($nid, NULL, TRUE);
- $image_info = array(
- 'path' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
- 'width' => 40,
- 'height' => 20,
- );
- $image_output = theme('image', $image_info);
- $this->drupalGet('node/' . $nid);
- $this->assertNoRaw($default_output, t('Default image is not displayed when user supplied image is present.'));
- $this->assertRaw($image_output, t('User supplied image is displayed.'));
-
- $edit = array(
- 'field[settings][default_image][fid]' => 0,
- );
- $this->drupalPost('admin/structure/types/manage/article/fields/' . $field_name, $edit, t('Save settings'));
-
- field_info_cache_clear();
- $field = field_info_field($field_name);
- $this->assertFalse($field['settings']['default_image'], t('Default image removed from field.'));
-
-
- $private_field_name = strtolower($this->randomName());
- $this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private'));
-
- $edit = array(
- 'files[field_settings_default_image]' => drupal_realpath($images[1]->uri),
- );
- $this->drupalPost('admin/structure/types/manage/article/fields/' . $private_field_name, $edit, t('Save settings'));
- $private_field = field_info_field($private_field_name);
- $image = file_load($private_field['settings']['default_image']);
- $this->assertEqual('private', file_uri_scheme($image->uri), t('Default image uses private:// scheme.'));
- $this->assertTrue($image->status == FILE_STATUS_PERMANENT, t('The default image status is permanent.'));
-
-
- $node = $this->drupalCreateNode(array('type' => 'article'));
- $default_output = theme('image', array('path' => $image->uri));
- $this->drupalGet('node/' . $node->nid);
- $this->assertRaw($default_output, t('Default private image displayed when no user supplied image is present.'));
- }
- }
- class ImageFieldValidateTestCase extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image field validation tests',
- 'description' => 'Tests validation functions such as min/max resolution.',
- 'group' => 'Image',
- );
- }
-
- function testResolution() {
- $field_name = strtolower($this->randomName());
- $min_resolution = 50;
- $max_resolution = 100;
- $instance_settings = array(
- 'max_resolution' => $max_resolution . 'x' . $max_resolution,
- 'min_resolution' => $min_resolution . 'x' . $min_resolution,
- );
- $this->createImageField($field_name, 'article', array(), $instance_settings);
-
-
- $image_that_is_too_big = FALSE;
- $image_that_is_too_small = FALSE;
- foreach ($this->drupalGetTestFiles('image') as $image) {
- $info = image_get_info($image->uri);
- if ($info['width'] > $max_resolution) {
- $image_that_is_too_big = $image;
- }
- if ($info['width'] < $min_resolution) {
- $image_that_is_too_small = $image;
- }
- if ($image_that_is_too_small && $image_that_is_too_big) {
- break;
- }
- }
- $nid = $this->uploadNodeImage($image_that_is_too_small, $field_name, 'article');
- $this->assertText(t('The specified file ' . $image_that_is_too_small->filename . ' could not be uploaded. The image is too small; the minimum dimensions are 50x50 pixels.'), t('Node save failed when minimum image resolution was not met.'));
- $nid = $this->uploadNodeImage($image_that_is_too_big, $field_name, 'article');
- $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'), t('Image exceeding max resolution was properly resized.'));
- }
- }
- class ImageDimensionsTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image dimensions',
- 'description' => 'Tests that images have correct dimensions when styled.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp('image_module_test');
- }
-
- function testImageDimensions() {
-
- $files = $this->drupalGetTestFiles('image');
- $file = reset($files);
- $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
-
- $style = image_style_save(array('name' => 'test'));
- $generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
- $url = image_style_url('test', $original_uri);
- $variables = array(
- 'style_name' => 'test',
- 'path' => $original_uri,
- 'width' => 40,
- 'height' => 20,
- );
-
- $effect = array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => 120,
- 'height' => 90,
- 'upscale' => TRUE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="120" height="60" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- $image_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 120, t('Expected width was found.'));
- $this->assertEqual($image_info['height'], 60, t('Expected height was found.'));
-
- $effect = array(
- 'name' => 'image_rotate',
- 'data' => array(
- 'degrees' => -90,
- 'random' => FALSE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="60" height="120" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- $image_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 60, t('Expected width was found.'));
- $this->assertEqual($image_info['height'], 120, t('Expected height was found.'));
-
- $effect = array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => 120,
- 'height' => 90,
- 'upscale' => TRUE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="45" height="90" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- $image_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 45, t('Expected width was found.'));
- $this->assertEqual($image_info['height'], 90, t('Expected height was found.'));
-
- $effect = array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => 400,
- 'height' => 200,
- 'upscale' => FALSE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="45" height="90" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- $image_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 45, t('Expected width was found.'));
- $this->assertEqual($image_info['height'], 90, t('Expected height was found.'));
-
- $effect = array(
- 'name' => 'image_desaturate',
- 'data' => array(),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="45" height="90" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- $image_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 45, t('Expected width was found.'));
- $this->assertEqual($image_info['height'], 90, t('Expected height was found.'));
-
- $effect = array(
- 'name' => 'image_rotate',
- 'data' => array(
- 'degrees' => 180,
- 'random' => TRUE,
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
-
- $effect = array(
- 'name' => 'image_crop',
- 'data' => array(
- 'width' => 30,
- 'height' => 30,
- 'anchor' => 'center-center',
- ),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="30" height="30" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- $image_info = image_get_info($generated_uri);
- $this->assertEqual($image_info['width'], 30, t('Expected width was found.'));
- $this->assertEqual($image_info['height'], 30, t('Expected height was found.'));
-
- $effect = array(
- 'name' => 'image_rotate',
- 'data' => array(
- 'degrees' => 57,
- 'random' => FALSE,
- ),
- 'isid' => $style['isid'],
- );
- $effect = image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" alt="" />', t('Expected img tag was found.'));
- $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
- $this->drupalGet($url);
- $this->assertResponse(200, t('Image was generated at the URL.'));
- $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
- image_effect_delete($effect);
-
-
- $effect = array(
- 'name' => 'image_module_test_null',
- 'data' => array(),
- 'isid' => $style['isid'],
- );
- image_effect_save($effect);
- $img_tag = theme_image_style($variables);
- $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" alt="" />', t('Expected img tag was found.'));
- }
- }
- class ImageDimensionsScaleTestCase extends DrupalUnitTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'image_dimensions_scale()',
- 'description' => 'Tests all control flow branches in image_dimensions_scale().',
- 'group' => 'Image',
- );
- }
-
- function testImageDimensionsScale() {
-
- $test = array();
-
-
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 1000,
- 'height' => 2000,
- ),
- 'width' => 200,
- 'height' => NULL,
- 'upscale' => TRUE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 200,
- 'height' => 400,
- ),
- 'return_value' => TRUE,
- ),
- );
-
-
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 1000,
- 'height' => 800,
- ),
- 'width' => NULL,
- 'height' => 140,
- 'upscale' => FALSE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 175,
- 'height' => 140,
- ),
- 'return_value' => TRUE,
- ),
- );
-
-
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 8,
- 'height' => 20,
- ),
- 'width' => 200,
- 'height' => 140,
- 'upscale' => TRUE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 56,
- 'height' => 140,
- ),
- 'return_value' => TRUE,
- ),
- );
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 2000,
- 'height' => 800,
- ),
- 'width' => 200,
- 'height' => 140,
- 'upscale' => FALSE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 200,
- 'height' => 80,
- ),
- 'return_value' => TRUE,
- ),
- );
-
- $tests[] = array(
- 'input' => array(
- 'dimensions' => array(
- 'width' => 100,
- 'height' => 50,
- ),
- 'width' => 200,
- 'height' => 140,
- 'upscale' => FALSE,
- ),
- 'output' => array(
- 'dimensions' => array(
- 'width' => 100,
- 'height' => 50,
- ),
- 'return_value' => FALSE,
- ),
- );
- foreach ($tests as $test) {
-
- $return_value = image_dimensions_scale($test['input']['dimensions'], $test['input']['width'], $test['input']['height'], $test['input']['upscale']);
-
- $this->assertEqual($test['output']['dimensions']['width'], $test['input']['dimensions']['width'], t('Computed width (@computed_width) equals expected width (@expected_width)', array('@computed_width' => $test['output']['dimensions']['width'], '@expected_width' => $test['input']['dimensions']['width'])));
-
- $this->assertEqual($test['output']['dimensions']['height'], $test['input']['dimensions']['height'], t('Computed height (@computed_height) equals expected height (@expected_height)', array('@computed_height' => $test['output']['dimensions']['height'], '@expected_height' => $test['input']['dimensions']['height'])));
-
- $this->assertEqual($test['output']['return_value'], $return_value, t('Correct return value.'));
- }
- }
- }
- class ImageFieldDefaultImagesTestCase extends ImageFieldTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image field default images tests',
- 'description' => 'Tests setting up default images both to the field and field instance.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp(array('field_ui'));
- }
-
- function testDefaultImages() {
-
- $files = $this->drupalGetTestFiles('image');
- $default_images = array();
- foreach (array('field', 'instance', 'instance2', 'field_new', 'instance_new') as $image_target) {
- $file = array_pop($files);
- $file = file_save($file);
- $default_images[$image_target] = $file;
- }
-
- $field_name = strtolower($this->randomName());
- $field_settings = array(
- 'default_image' => $default_images['field']->fid,
- );
- $instance_settings = array(
- 'default_image' => $default_images['instance']->fid,
- );
- $widget_settings = array(
- 'preview_image_style' => 'medium',
- );
- $this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings);
- $field = field_info_field($field_name);
- $instance = field_info_instance('node', $field_name, 'article');
-
- $instance2 = array_merge($instance, array(
- 'bundle' => 'page',
- 'settings' => array(
- 'default_image' => $default_images['instance2']->fid,
- ),
- ));
- field_create_instance($instance2);
- $instance2 = field_info_instance('node', $field_name, 'page');
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
- $default_images['field']->fid,
- format_string(
- 'Article image field default equals expected file ID of @fid.',
- array('@fid' => $default_images['field']->fid)
- )
- );
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- $default_images['instance']->fid,
- format_string(
- 'Article image field instance default equals expected file ID of @fid.',
- array('@fid' => $default_images['instance']->fid)
- )
- );
-
- $this->drupalGet("admin/structure/types/manage/page/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
- $default_images['field']->fid,
- format_string(
- 'Page image field default equals expected file ID of @fid.',
- array('@fid' => $default_images['field']->fid)
- )
- );
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- $default_images['instance2']->fid,
- format_string(
- 'Page image field instance default equals expected file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $article = $this->drupalCreateNode(array('type' => 'article'));
- $article_built = node_view($article);
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['instance']->fid,
- format_string(
- 'A new article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance']->fid)
- )
- );
-
- $page = $this->drupalCreateNode(array('type' => 'page'));
- $page_built = node_view($page);
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'A new page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $field['settings']['default_image'] = $default_images['field_new']->fid;
- field_update_field($field);
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="field[settings][default_image][fid]"]',
- $default_images['field_new']->fid,
- format_string(
- 'Updated image field default equals expected file ID of @fid.',
- array('@fid' => $default_images['field_new']->fid)
- )
- );
-
- $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
- $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['instance']->fid,
- format_string(
- 'An existing article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance']->fid)
- )
- );
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'An existing page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $instance['settings']['default_image'] = $default_images['instance_new']->fid;
- field_update_instance($instance);
-
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- $default_images['instance_new']->fid,
- format_string(
- 'Updated article image field instance default equals expected file ID of @fid.',
- array('@fid' => $default_images['instance_new']->fid)
- )
- );
-
- $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
- $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
-
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['instance_new']->fid,
- format_string(
- 'An existing article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance_new']->fid)
- )
- );
-
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'An existing page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
-
- $instance['settings']['default_image'] = NULL;
- field_update_instance($instance);
-
- $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
- $this->assertFieldByXpath(
- '//input[@name="instance[settings][default_image][fid]"]',
- '',
- 'Updated article image field instance default has been successfully removed.'
- );
-
- $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
- $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
-
- $this->assertEqual(
- $article_built[$field_name]['#items'][0]['fid'],
- $default_images['field_new']->fid,
- format_string(
- 'An existing article node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['field_new']->fid)
- )
- );
-
- $this->assertEqual(
- $page_built[$field_name]['#items'][0]['fid'],
- $default_images['instance2']->fid,
- format_string(
- 'An existing page node without an image has the expected default image file ID of @fid.',
- array('@fid' => $default_images['instance2']->fid)
- )
- );
- }
- }
- class ImageThemeFunctionWebTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Image theme functions',
- 'description' => 'Test that the image theme functions work correctly.',
- 'group' => 'Image',
- );
- }
- function setUp() {
- parent::setUp(array('image'));
- }
-
- function testImageFormatterTheme() {
-
- $files = $this->drupalGetTestFiles('image');
- $file = reset($files);
- $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
-
- image_style_save(array('name' => 'test'));
- $url = image_style_url('test', $original_uri);
-
-
- $path = $this->randomName();
- $element = array(
- '#theme' => 'image_formatter',
- '#image_style' => 'test',
- '#item' => array(
- 'uri' => $original_uri,
- ),
- '#path' => array(
- 'path' => $path,
- ),
- );
- $rendered_element = render($element);
- $expected_result = '<a href="' . url($path) . '"><img typeof="foaf:Image" src="' . $url . '" alt="" /></a>';
- $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders without title, alt, or path options.');
-
- $fragment = $this->randomName();
- $element['#path']['path'] = '';
- $element['#path']['options'] = array(
- 'external' => TRUE,
- 'fragment' => $fragment,
- );
- $rendered_element = render($element);
- $expected_result = '<a href="#' . $fragment . '"><img typeof="foaf:Image" src="' . $url . '" alt="" /></a>';
- $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders a link fragment.');
- }
- }
|