image.test 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. <?php
  2. /**
  3. * @file
  4. * Tests for image.module.
  5. */
  6. /**
  7. * TODO: Test the following functions.
  8. *
  9. * image.effects.inc:
  10. * image_style_generate()
  11. * image_style_create_derivative()
  12. *
  13. * image.module:
  14. * image_style_load()
  15. * image_style_save()
  16. * image_style_delete()
  17. * image_style_options()
  18. * image_style_flush()
  19. * image_effect_definition_load()
  20. * image_effect_load()
  21. * image_effect_save()
  22. * image_effect_delete()
  23. * image_filter_keyword()
  24. */
  25. /**
  26. * This class provides methods specifically for testing Image's field handling.
  27. */
  28. class ImageFieldTestCase extends DrupalWebTestCase {
  29. protected $admin_user;
  30. function setUp() {
  31. parent::setUp('image');
  32. $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'));
  33. $this->drupalLogin($this->admin_user);
  34. }
  35. /**
  36. * Create a new image field.
  37. *
  38. * @param $name
  39. * The name of the new field (all lowercase), exclude the "field_" prefix.
  40. * @param $type_name
  41. * The node type that this field will be added to.
  42. * @param $field_settings
  43. * A list of field settings that will be added to the defaults.
  44. * @param $instance_settings
  45. * A list of instance settings that will be added to the instance defaults.
  46. * @param $widget_settings
  47. * A list of widget settings that will be added to the widget defaults.
  48. */
  49. function createImageField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
  50. $field = array(
  51. 'field_name' => $name,
  52. 'type' => 'image',
  53. 'settings' => array(),
  54. 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
  55. );
  56. $field['settings'] = array_merge($field['settings'], $field_settings);
  57. field_create_field($field);
  58. $instance = array(
  59. 'field_name' => $field['field_name'],
  60. 'entity_type' => 'node',
  61. 'label' => $name,
  62. 'bundle' => $type_name,
  63. 'required' => !empty($instance_settings['required']),
  64. 'settings' => array(),
  65. 'widget' => array(
  66. 'type' => 'image_image',
  67. 'settings' => array(),
  68. ),
  69. );
  70. $instance['settings'] = array_merge($instance['settings'], $instance_settings);
  71. $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
  72. return field_create_instance($instance);
  73. }
  74. /**
  75. * Upload an image to a node.
  76. *
  77. * @param $image
  78. * A file object representing the image to upload.
  79. * @param $field_name
  80. * Name of the image field the image should be attached to.
  81. * @param $type
  82. * The type of node to create.
  83. */
  84. function uploadNodeImage($image, $field_name, $type) {
  85. $edit = array(
  86. 'title' => $this->randomName(),
  87. );
  88. $edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($image->uri);
  89. $this->drupalPost('node/add/' . $type, $edit, t('Save'));
  90. // Retrieve ID of the newly created node from the current URL.
  91. $matches = array();
  92. preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
  93. return isset($matches[1]) ? $matches[1] : FALSE;
  94. }
  95. }
  96. /**
  97. * Tests the functions for generating paths and URLs for image styles.
  98. */
  99. class ImageStylesPathAndUrlTestCase extends DrupalWebTestCase {
  100. protected $style_name;
  101. protected $image_info;
  102. protected $image_filepath;
  103. public static function getInfo() {
  104. return array(
  105. 'name' => 'Image styles path and URL functions',
  106. 'description' => 'Tests functions for generating paths and URLs to image styles.',
  107. 'group' => 'Image',
  108. );
  109. }
  110. function setUp() {
  111. parent::setUp('image_module_test');
  112. $this->style_name = 'style_foo';
  113. image_style_save(array('name' => $this->style_name));
  114. }
  115. /**
  116. * Test image_style_path().
  117. */
  118. function testImageStylePath() {
  119. $scheme = 'public';
  120. $actual = image_style_path($this->style_name, "$scheme://foo/bar.gif");
  121. $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
  122. $this->assertEqual($actual, $expected, t('Got the path for a file URI.'));
  123. $actual = image_style_path($this->style_name, 'foo/bar.gif');
  124. $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
  125. $this->assertEqual($actual, $expected, t('Got the path for a relative file path.'));
  126. }
  127. /**
  128. * Test image_style_url() with a file using the "public://" scheme.
  129. */
  130. function testImageStyleUrlAndPathPublic() {
  131. $this->_testImageStyleUrlAndPath('public');
  132. }
  133. /**
  134. * Test image_style_url() with a file using the "private://" scheme.
  135. */
  136. function testImageStyleUrlAndPathPrivate() {
  137. $this->_testImageStyleUrlAndPath('private');
  138. }
  139. /**
  140. * Test image_style_url() with the "public://" scheme and unclean URLs.
  141. */
  142. function testImageStylUrlAndPathPublicUnclean() {
  143. $this->_testImageStyleUrlAndPath('public', FALSE);
  144. }
  145. /**
  146. * Test image_style_url() with the "private://" schema and unclean URLs.
  147. */
  148. function testImageStyleUrlAndPathPrivateUnclean() {
  149. $this->_testImageStyleUrlAndPath('private', FALSE);
  150. }
  151. /**
  152. * Test image_style_url().
  153. */
  154. function _testImageStyleUrlAndPath($scheme, $clean_url = TRUE) {
  155. // Make the default scheme neither "public" nor "private" to verify the
  156. // functions work for other than the default scheme.
  157. variable_set('file_default_scheme', 'temporary');
  158. variable_set('clean_url', $clean_url);
  159. // Create the directories for the styles.
  160. $directory = $scheme . '://styles/' . $this->style_name;
  161. $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  162. $this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.'));
  163. // Create a working copy of the file.
  164. $files = $this->drupalGetTestFiles('image');
  165. $file = array_shift($files);
  166. $image_info = image_get_info($file->uri);
  167. $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
  168. // Let the image_module_test module know about this file, so it can claim
  169. // ownership in hook_file_download().
  170. variable_set('image_module_test_file_download', $original_uri);
  171. $this->assertNotIdentical(FALSE, $original_uri, t('Created the generated image file.'));
  172. // Get the URL of a file that has not been generated and try to create it.
  173. $generated_uri = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. drupal_basename($original_uri);
  174. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  175. $generate_url = image_style_url($this->style_name, $original_uri);
  176. if (!$clean_url) {
  177. $this->assertTrue(strpos($generate_url, '?q=') !== FALSE, 'When using non-clean URLS, the system path contains the query string.');
  178. }
  179. // Fetch the URL that generates the file.
  180. $this->drupalGet($generate_url);
  181. $this->assertResponse(200, t('Image was generated at the URL.'));
  182. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  183. $this->assertRaw(file_get_contents($generated_uri), t('URL returns expected file.'));
  184. $generated_image_info = image_get_info($generated_uri);
  185. $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], t('Expected Content-Type was reported.'));
  186. $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], t('Expected Content-Length was reported.'));
  187. if ($scheme == 'private') {
  188. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', t('Expires header was sent.'));
  189. $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.'));
  190. $this->assertEqual($this->drupalGetHeader('X-Image-Owned-By'), 'image_module_test', t('Expected custom header has been added.'));
  191. // Make sure that a second request to the already existing derivate works
  192. // too.
  193. $this->drupalGet($generate_url);
  194. $this->assertResponse(200, t('Image was generated at the URL.'));
  195. // Repeat this with a different file that we do not have access to and
  196. // make sure that access is denied.
  197. $file_noaccess = array_shift($files);
  198. $original_uri_noaccess = file_unmanaged_copy($file_noaccess->uri, $scheme . '://', FILE_EXISTS_RENAME);
  199. $generated_uri_noaccess = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. drupal_basename($original_uri_noaccess);
  200. $this->assertFalse(file_exists($generated_uri_noaccess), t('Generated file does not exist.'));
  201. $generate_url_noaccess = image_style_url($this->style_name, $original_uri_noaccess);
  202. $this->drupalGet($generate_url_noaccess);
  203. $this->assertResponse(403, t('Confirmed that access is denied for the private image style.') );
  204. // Verify that images are not appended to the response. Currently this test only uses PNG images.
  205. if (strpos($generate_url, '.png') === FALSE ) {
  206. $this->fail('Confirming that private image styles are not appended require PNG file.');
  207. }
  208. else {
  209. // Check for PNG-Signature (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2) in the
  210. // response body.
  211. $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.');
  212. }
  213. }
  214. }
  215. }
  216. /**
  217. * Use the image_test.module's mock toolkit to ensure that the effects are
  218. * properly passing parameters to the image toolkit.
  219. */
  220. class ImageEffectsUnitTest extends ImageToolkitTestCase {
  221. public static function getInfo() {
  222. return array(
  223. 'name' => 'Image effects',
  224. 'description' => 'Test that the image effects pass parameters to the toolkit correctly.',
  225. 'group' => 'Image',
  226. );
  227. }
  228. function setUp() {
  229. parent::setUp('image_module_test');
  230. module_load_include('inc', 'image', 'image.effects');
  231. }
  232. /**
  233. * Test the image_resize_effect() function.
  234. */
  235. function testResizeEffect() {
  236. $this->assertTrue(image_resize_effect($this->image, array('width' => 1, 'height' => 2)), t('Function returned the expected value.'));
  237. $this->assertToolkitOperationsCalled(array('resize'));
  238. // Check the parameters.
  239. $calls = image_test_get_all_calls();
  240. $this->assertEqual($calls['resize'][0][1], 1, t('Width was passed correctly'));
  241. $this->assertEqual($calls['resize'][0][2], 2, t('Height was passed correctly'));
  242. }
  243. /**
  244. * Test the image_scale_effect() function.
  245. */
  246. function testScaleEffect() {
  247. // @todo: need to test upscaling.
  248. $this->assertTrue(image_scale_effect($this->image, array('width' => 10, 'height' => 10)), t('Function returned the expected value.'));
  249. $this->assertToolkitOperationsCalled(array('resize'));
  250. // Check the parameters.
  251. $calls = image_test_get_all_calls();
  252. $this->assertEqual($calls['resize'][0][1], 10, t('Width was passed correctly'));
  253. $this->assertEqual($calls['resize'][0][2], 5, t('Height was based off aspect ratio and passed correctly'));
  254. }
  255. /**
  256. * Test the image_crop_effect() function.
  257. */
  258. function testCropEffect() {
  259. // @todo should test the keyword offsets.
  260. $this->assertTrue(image_crop_effect($this->image, array('anchor' => 'top-1', 'width' => 3, 'height' => 4)), t('Function returned the expected value.'));
  261. $this->assertToolkitOperationsCalled(array('crop'));
  262. // Check the parameters.
  263. $calls = image_test_get_all_calls();
  264. $this->assertEqual($calls['crop'][0][1], 0, t('X was passed correctly'));
  265. $this->assertEqual($calls['crop'][0][2], 1, t('Y was passed correctly'));
  266. $this->assertEqual($calls['crop'][0][3], 3, t('Width was passed correctly'));
  267. $this->assertEqual($calls['crop'][0][4], 4, t('Height was passed correctly'));
  268. }
  269. /**
  270. * Test the image_scale_and_crop_effect() function.
  271. */
  272. function testScaleAndCropEffect() {
  273. $this->assertTrue(image_scale_and_crop_effect($this->image, array('width' => 5, 'height' => 10)), t('Function returned the expected value.'));
  274. $this->assertToolkitOperationsCalled(array('resize', 'crop'));
  275. // Check the parameters.
  276. $calls = image_test_get_all_calls();
  277. $this->assertEqual($calls['crop'][0][1], 7.5, t('X was computed and passed correctly'));
  278. $this->assertEqual($calls['crop'][0][2], 0, t('Y was computed and passed correctly'));
  279. $this->assertEqual($calls['crop'][0][3], 5, t('Width was computed and passed correctly'));
  280. $this->assertEqual($calls['crop'][0][4], 10, t('Height was computed and passed correctly'));
  281. }
  282. /**
  283. * Test the image_desaturate_effect() function.
  284. */
  285. function testDesaturateEffect() {
  286. $this->assertTrue(image_desaturate_effect($this->image, array()), t('Function returned the expected value.'));
  287. $this->assertToolkitOperationsCalled(array('desaturate'));
  288. // Check the parameters.
  289. $calls = image_test_get_all_calls();
  290. $this->assertEqual(count($calls['desaturate'][0]), 1, t('Only the image was passed.'));
  291. }
  292. /**
  293. * Test the image_rotate_effect() function.
  294. */
  295. function testRotateEffect() {
  296. // @todo: need to test with 'random' => TRUE
  297. $this->assertTrue(image_rotate_effect($this->image, array('degrees' => 90, 'bgcolor' => '#fff')), t('Function returned the expected value.'));
  298. $this->assertToolkitOperationsCalled(array('rotate'));
  299. // Check the parameters.
  300. $calls = image_test_get_all_calls();
  301. $this->assertEqual($calls['rotate'][0][1], 90, t('Degrees were passed correctly'));
  302. $this->assertEqual($calls['rotate'][0][2], 0xffffff, t('Background color was passed correctly'));
  303. }
  304. /**
  305. * Test image effect caching.
  306. */
  307. function testImageEffectsCaching() {
  308. $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
  309. // First call should grab a fresh copy of the data.
  310. $effects = image_effect_definitions();
  311. $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
  312. // Second call should come from cache.
  313. drupal_static_reset('image_effect_definitions');
  314. drupal_static_reset('image_module_test_image_effect_info_alter');
  315. $cached_effects = image_effect_definitions();
  316. $this->assertTrue(is_null($image_effect_definitions_called), 'image_effect_definitions() returned data from cache.');
  317. $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
  318. }
  319. }
  320. /**
  321. * Tests creation, deletion, and editing of image styles and effects.
  322. */
  323. class ImageAdminStylesUnitTest extends ImageFieldTestCase {
  324. public static function getInfo() {
  325. return array(
  326. 'name' => 'Image styles and effects UI configuration',
  327. 'description' => 'Tests creation, deletion, and editing of image styles and effects at the UI level.',
  328. 'group' => 'Image',
  329. );
  330. }
  331. /**
  332. * Given an image style, generate an image.
  333. */
  334. function createSampleImage($style) {
  335. static $file_path;
  336. // First, we need to make sure we have an image in our testing
  337. // file directory. Copy over an image on the first run.
  338. if (!isset($file_path)) {
  339. $files = $this->drupalGetTestFiles('image');
  340. $file = reset($files);
  341. $file_path = file_unmanaged_copy($file->uri);
  342. }
  343. return image_style_url($style['name'], $file_path) ? $file_path : FALSE;
  344. }
  345. /**
  346. * Count the number of images currently create for a style.
  347. */
  348. function getImageCount($style) {
  349. return count(file_scan_directory('public://styles/' . $style['name'], '/.*/'));
  350. }
  351. /**
  352. * Test creating an image style with a numeric name and ensuring it can be
  353. * applied to an image.
  354. */
  355. function testNumericStyleName() {
  356. $style_name = rand();
  357. $edit = array(
  358. 'name' => $style_name,
  359. );
  360. $this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
  361. $this->assertRaw(t('Style %name was created.', array('%name' => $style_name)), t('Image style successfully created.'));
  362. $options = image_style_options();
  363. $this->assertTrue(array_key_exists($style_name, $options), t('Array key %key exists.', array('%key' => $style_name)));
  364. }
  365. /**
  366. * General test to add a style, add/remove/edit effects to it, then delete it.
  367. */
  368. function testStyle() {
  369. // Setup a style to be created and effects to add to it.
  370. $style_name = strtolower($this->randomName(10));
  371. $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
  372. $effect_edits = array(
  373. 'image_resize' => array(
  374. 'data[width]' => 100,
  375. 'data[height]' => 101,
  376. ),
  377. 'image_scale' => array(
  378. 'data[width]' => 110,
  379. 'data[height]' => 111,
  380. 'data[upscale]' => 1,
  381. ),
  382. 'image_scale_and_crop' => array(
  383. 'data[width]' => 120,
  384. 'data[height]' => 121,
  385. ),
  386. 'image_crop' => array(
  387. 'data[width]' => 130,
  388. 'data[height]' => 131,
  389. 'data[anchor]' => 'center-center',
  390. ),
  391. 'image_desaturate' => array(
  392. // No options for desaturate.
  393. ),
  394. 'image_rotate' => array(
  395. 'data[degrees]' => 5,
  396. 'data[random]' => 1,
  397. 'data[bgcolor]' => '#FFFF00',
  398. ),
  399. );
  400. // Add style form.
  401. $edit = array(
  402. 'name' => $style_name,
  403. );
  404. $this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
  405. $this->assertRaw(t('Style %name was created.', array('%name' => $style_name)), t('Image style successfully created.'));
  406. // Add effect form.
  407. // Add each sample effect to the style.
  408. foreach ($effect_edits as $effect => $edit) {
  409. // Add the effect.
  410. $this->drupalPost($style_path, array('new' => $effect), t('Add'));
  411. if (!empty($edit)) {
  412. $this->drupalPost(NULL, $edit, t('Add effect'));
  413. }
  414. }
  415. // Edit effect form.
  416. // Revisit each form to make sure the effect was saved.
  417. drupal_static_reset('image_styles');
  418. $style = image_style_load($style_name);
  419. foreach ($style['effects'] as $ieid => $effect) {
  420. $this->drupalGet($style_path . '/effects/' . $ieid);
  421. foreach ($effect_edits[$effect['name']] as $field => $value) {
  422. $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)));
  423. }
  424. }
  425. // Image style overview form (ordering and renaming).
  426. // Confirm the order of effects is maintained according to the order we
  427. // added the fields.
  428. $effect_edits_order = array_keys($effect_edits);
  429. $effects_order = array_values($style['effects']);
  430. $order_correct = TRUE;
  431. foreach ($effects_order as $index => $effect) {
  432. if ($effect_edits_order[$index] != $effect['name']) {
  433. $order_correct = FALSE;
  434. }
  435. }
  436. $this->assertTrue($order_correct, t('The order of the effects is correctly set by default.'));
  437. // Test the style overview form.
  438. // Change the name of the style and adjust the weights of effects.
  439. $style_name = strtolower($this->randomName(10));
  440. $weight = count($effect_edits);
  441. $edit = array(
  442. 'name' => $style_name,
  443. );
  444. foreach ($style['effects'] as $ieid => $effect) {
  445. $edit['effects[' . $ieid . '][weight]'] = $weight;
  446. $weight--;
  447. }
  448. // Create an image to make sure it gets flushed after saving.
  449. $image_path = $this->createSampleImage($style);
  450. $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
  451. $this->drupalPost($style_path, $edit, t('Update style'));
  452. // Note that after changing the style name, the style path is changed.
  453. $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
  454. // Check that the URL was updated.
  455. $this->drupalGet($style_path);
  456. $this->assertResponse(200, t('Image style %original renamed to %new', array('%original' => $style['name'], '%new' => $style_name)));
  457. // Check that the image was flushed after updating the style.
  458. // This is especially important when renaming the style. Make sure that
  459. // the old image directory has been deleted.
  460. $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'])));
  461. // Load the style by the new name with the new weights.
  462. drupal_static_reset('image_styles');
  463. $style = image_style_load($style_name, NULL);
  464. // Confirm the new style order was saved.
  465. $effect_edits_order = array_reverse($effect_edits_order);
  466. $effects_order = array_values($style['effects']);
  467. $order_correct = TRUE;
  468. foreach ($effects_order as $index => $effect) {
  469. if ($effect_edits_order[$index] != $effect['name']) {
  470. $order_correct = FALSE;
  471. }
  472. }
  473. $this->assertTrue($order_correct, t('The order of the effects is correctly set by default.'));
  474. // Image effect deletion form.
  475. // Create an image to make sure it gets flushed after deleting an effect.
  476. $image_path = $this->createSampleImage($style);
  477. $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
  478. // Test effect deletion form.
  479. $effect = array_pop($style['effects']);
  480. $this->drupalPost($style_path . '/effects/' . $effect['ieid'] . '/delete', array(), t('Delete'));
  481. $this->assertRaw(t('The image effect %name has been deleted.', array('%name' => $effect['label'])), t('Image effect deleted.'));
  482. // Style deletion form.
  483. // Delete the style.
  484. $this->drupalPost('admin/config/media/image-styles/delete/' . $style_name, array(), t('Delete'));
  485. // Confirm the style directory has been removed.
  486. $directory = file_default_scheme() . '://styles/' . $style_name;
  487. $this->assertFalse(is_dir($directory), t('Image style %style directory removed on style deletion.', array('%style' => $style['name'])));
  488. drupal_static_reset('image_styles');
  489. $this->assertFalse(image_style_load($style_name), t('Image style %style successfully deleted.', array('%style' => $style['name'])));
  490. }
  491. /**
  492. * Test to override, edit, then revert a style.
  493. */
  494. function testDefaultStyle() {
  495. // Setup a style to be created and effects to add to it.
  496. $style_name = 'thumbnail';
  497. $edit_path = 'admin/config/media/image-styles/edit/' . $style_name;
  498. $delete_path = 'admin/config/media/image-styles/delete/' . $style_name;
  499. $revert_path = 'admin/config/media/image-styles/revert/' . $style_name;
  500. // Ensure deleting a default is not possible.
  501. $this->drupalGet($delete_path);
  502. $this->assertText(t('Page not found'), t('Default styles may not be deleted.'));
  503. // Ensure that editing a default is not possible (without overriding).
  504. $this->drupalGet($edit_path);
  505. $this->assertNoField('edit-name', t('Default styles may not be renamed.'));
  506. $this->assertNoField('edit-submit', t('Default styles may not be edited.'));
  507. $this->assertNoField('edit-add', t('Default styles may not have new effects added.'));
  508. // Create an image to make sure the default works before overriding.
  509. drupal_static_reset('image_styles');
  510. $style = image_style_load($style_name);
  511. $image_path = $this->createSampleImage($style);
  512. $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
  513. // Verify that effects attached to a default style do not have an ieid key.
  514. foreach ($style['effects'] as $effect) {
  515. $this->assertFalse(isset($effect['ieid']), t('The %effect effect does not have an ieid.', array('%effect' => $effect['name'])));
  516. }
  517. // Override the default.
  518. $this->drupalPost($edit_path, array(), t('Override defaults'));
  519. $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.'));
  520. // Add sample effect to the overridden style.
  521. $this->drupalPost($edit_path, array('new' => 'image_desaturate'), t('Add'));
  522. drupal_static_reset('image_styles');
  523. $style = image_style_load($style_name);
  524. // Verify that effects attached to the style have an ieid now.
  525. foreach ($style['effects'] as $effect) {
  526. $this->assertTrue(isset($effect['ieid']), t('The %effect effect has an ieid.', array('%effect' => $effect['name'])));
  527. }
  528. // The style should now have 2 effect, the original scale provided by core
  529. // and the desaturate effect we added in the override.
  530. $effects = array_values($style['effects']);
  531. $this->assertEqual($effects[0]['name'], 'image_scale', t('The default effect still exists in the overridden style.'));
  532. $this->assertEqual($effects[1]['name'], 'image_desaturate', t('The added effect exists in the overridden style.'));
  533. // Check that we are unable to rename an overridden style.
  534. $this->drupalGet($edit_path);
  535. $this->assertNoField('edit-name', t('Overridden styles may not be renamed.'));
  536. // Create an image to ensure the override works properly.
  537. $image_path = $this->createSampleImage($style);
  538. $this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%file' => $image_path)));
  539. // Revert the image style.
  540. $this->drupalPost($revert_path, array(), t('Revert'));
  541. drupal_static_reset('image_styles');
  542. $style = image_style_load($style_name);
  543. // The style should now have the single effect for scale.
  544. $effects = array_values($style['effects']);
  545. $this->assertEqual($effects[0]['name'], 'image_scale', t('The default effect still exists in the reverted style.'));
  546. $this->assertFalse(array_key_exists(1, $effects), t('The added effect has been removed in the reverted style.'));
  547. }
  548. /**
  549. * Test deleting a style and choosing a replacement style.
  550. */
  551. function testStyleReplacement() {
  552. // Create a new style.
  553. $style_name = strtolower($this->randomName(10));
  554. image_style_save(array('name' => $style_name));
  555. $style_path = 'admin/config/media/image-styles/edit/' . $style_name;
  556. // Create an image field that uses the new style.
  557. $field_name = strtolower($this->randomName(10));
  558. $this->createImageField($field_name, 'article');
  559. $instance = field_info_instance('node', $field_name, 'article');
  560. $instance['display']['default']['type'] = 'image';
  561. $instance['display']['default']['settings']['image_style'] = $style_name;
  562. field_update_instance($instance);
  563. // Create a new node with an image attached.
  564. $test_image = current($this->drupalGetTestFiles('image'));
  565. $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
  566. $node = node_load($nid);
  567. // Test that image is displayed using newly created style.
  568. $this->drupalGet('node/' . $nid);
  569. $this->assertRaw(image_style_url($style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style @style.', array('@style' => $style_name)));
  570. // Rename the style and make sure the image field is updated.
  571. $new_style_name = strtolower($this->randomName(10));
  572. $edit = array(
  573. 'name' => $new_style_name,
  574. );
  575. $this->drupalPost('admin/config/media/image-styles/edit/' . $style_name, $edit, t('Update style'));
  576. $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)));
  577. $this->drupalGet('node/' . $nid);
  578. $this->assertRaw(image_style_url($new_style_name, $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style replacement style.'));
  579. // Delete the style and choose a replacement style.
  580. $edit = array(
  581. 'replacement' => 'thumbnail',
  582. );
  583. $this->drupalPost('admin/config/media/image-styles/delete/' . $new_style_name, $edit, t('Delete'));
  584. $message = t('Style %name was deleted.', array('%name' => $new_style_name));
  585. $this->assertRaw($message, $message);
  586. $this->drupalGet('node/' . $nid);
  587. $this->assertRaw(image_style_url('thumbnail', $node->{$field_name}[LANGUAGE_NONE][0]['uri']), t('Image displayed using style replacement style.'));
  588. }
  589. }
  590. /**
  591. * Test class to check that formatters and display settings are working.
  592. */
  593. class ImageFieldDisplayTestCase extends ImageFieldTestCase {
  594. public static function getInfo() {
  595. return array(
  596. 'name' => 'Image field display tests',
  597. 'description' => 'Test the display of image fields.',
  598. 'group' => 'Image',
  599. );
  600. }
  601. /**
  602. * Test image formatters on node display for public files.
  603. */
  604. function testImageFieldFormattersPublic() {
  605. $this->_testImageFieldFormatters('public');
  606. }
  607. /**
  608. * Test image formatters on node display for private files.
  609. */
  610. function testImageFieldFormattersPrivate() {
  611. // Remove access content permission from anonymous users.
  612. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access content' => FALSE));
  613. $this->_testImageFieldFormatters('private');
  614. }
  615. /**
  616. * Test image formatters on node display.
  617. */
  618. function _testImageFieldFormatters($scheme) {
  619. $field_name = strtolower($this->randomName());
  620. $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme));
  621. // Create a new node with an image attached.
  622. $test_image = current($this->drupalGetTestFiles('image'));
  623. $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
  624. $node = node_load($nid, NULL, TRUE);
  625. // Test that the default formatter is being used.
  626. $image_uri = $node->{$field_name}[LANGUAGE_NONE][0]['uri'];
  627. $image_info = array(
  628. 'path' => $image_uri,
  629. 'width' => 40,
  630. 'height' => 20,
  631. );
  632. $default_output = theme('image', $image_info);
  633. $this->assertRaw($default_output, t('Default formatter displaying correctly on full node view.'));
  634. // Test the image linked to file formatter.
  635. $instance = field_info_instance('node', $field_name, 'article');
  636. $instance['display']['default']['type'] = 'image';
  637. $instance['display']['default']['settings']['image_link'] = 'file';
  638. field_update_instance($instance);
  639. $default_output = l(theme('image', $image_info), file_create_url($image_uri), array('html' => TRUE));
  640. $this->drupalGet('node/' . $nid);
  641. $this->assertRaw($default_output, t('Image linked to file formatter displaying correctly on full node view.'));
  642. // Verify that the image can be downloaded.
  643. $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), t('File was downloaded successfully.'));
  644. if ($scheme == 'private') {
  645. // Only verify HTTP headers when using private scheme and the headers are
  646. // sent by Drupal.
  647. $this->assertEqual($this->drupalGetHeader('Content-Type'), 'image/png', t('Content-Type header was sent.'));
  648. $this->assertEqual($this->drupalGetHeader('Content-Disposition'), 'inline; filename="' . $test_image->filename . '"', t('Content-Disposition header was sent.'));
  649. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'private', t('Cache-Control header was sent.'));
  650. // Log out and try to access the file.
  651. $this->drupalLogout();
  652. $this->drupalGet(file_create_url($image_uri));
  653. $this->assertResponse('403', t('Access denied to original image as anonymous user.'));
  654. // Log in again.
  655. $this->drupalLogin($this->admin_user);
  656. }
  657. // Test the image linked to content formatter.
  658. $instance['display']['default']['settings']['image_link'] = 'content';
  659. field_update_instance($instance);
  660. $default_output = l(theme('image', $image_info), 'node/' . $nid, array('html' => TRUE, 'attributes' => array('class' => 'active')));
  661. $this->drupalGet('node/' . $nid);
  662. $this->assertRaw($default_output, t('Image linked to content formatter displaying correctly on full node view.'));
  663. // Test the image style 'thumbnail' formatter.
  664. $instance['display']['default']['settings']['image_link'] = '';
  665. $instance['display']['default']['settings']['image_style'] = 'thumbnail';
  666. field_update_instance($instance);
  667. // Ensure the derivative image is generated so we do not have to deal with
  668. // image style callback paths.
  669. $this->drupalGet(image_style_url('thumbnail', $image_uri));
  670. $image_info['path'] = image_style_path('thumbnail', $image_uri);
  671. $image_info['width'] = 100;
  672. $image_info['height'] = 50;
  673. $default_output = theme('image', $image_info);
  674. $this->drupalGet('node/' . $nid);
  675. $this->assertRaw($default_output, t('Image style thumbnail formatter displaying correctly on full node view.'));
  676. if ($scheme == 'private') {
  677. // Log out and try to access the file.
  678. $this->drupalLogout();
  679. $this->drupalGet(image_style_url('thumbnail', $image_uri));
  680. $this->assertResponse('403', t('Access denied to image style thumbnail as anonymous user.'));
  681. }
  682. }
  683. /**
  684. * Tests for image field settings.
  685. */
  686. function testImageFieldSettings() {
  687. $test_image = current($this->drupalGetTestFiles('image'));
  688. list(, $test_image_extension) = explode('.', $test_image->filename);
  689. $field_name = strtolower($this->randomName());
  690. $instance_settings = array(
  691. 'alt_field' => 1,
  692. 'file_extensions' => $test_image_extension,
  693. 'max_filesize' => '50 KB',
  694. 'max_resolution' => '100x100',
  695. 'min_resolution' => '10x10',
  696. 'title_field' => 1,
  697. );
  698. $widget_settings = array(
  699. 'preview_image_style' => 'medium',
  700. );
  701. $field = $this->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings);
  702. $field['deleted'] = 0;
  703. $table = _field_sql_storage_tablename($field);
  704. $schema = drupal_get_schema($table, TRUE);
  705. $instance = field_info_instance('node', $field_name, 'article');
  706. $this->drupalGet('node/add/article');
  707. $this->assertText(t('Files must be less than 50 KB.'), t('Image widget max file size is displayed on article form.'));
  708. $this->assertText(t('Allowed file types: ' . $test_image_extension . '.'), t('Image widget allowed file types displayed on article form.'));
  709. $this->assertText(t('Images must be between 10x10 and 100x100 pixels.'), t('Image widget allowed resolution displayed on article form.'));
  710. // We have to create the article first and then edit it because the alt
  711. // and title fields do not display until the image has been attached.
  712. $nid = $this->uploadNodeImage($test_image, $field_name, 'article');
  713. $this->drupalGet('node/' . $nid . '/edit');
  714. $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][alt]', '', t('Alt field displayed on article form.'));
  715. $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][title]', '', t('Title field displayed on article form.'));
  716. // Verify that the attached image is being previewed using the 'medium'
  717. // style.
  718. $node = node_load($nid, NULL, TRUE);
  719. $image_info = array(
  720. 'path' => image_style_url('medium', $node->{$field_name}[LANGUAGE_NONE][0]['uri']),
  721. 'width' => 220,
  722. 'height' => 110,
  723. );
  724. $default_output = theme('image', $image_info);
  725. $this->assertRaw($default_output, t("Preview image is displayed using 'medium' style."));
  726. // Add alt/title fields to the image and verify that they are displayed.
  727. $image_info = array(
  728. 'path' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
  729. 'alt' => $this->randomName(),
  730. 'title' => $this->randomName(),
  731. 'width' => 40,
  732. 'height' => 20,
  733. );
  734. $edit = array(
  735. $field_name . '[' . LANGUAGE_NONE . '][0][alt]' => $image_info['alt'],
  736. $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $image_info['title'],
  737. );
  738. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  739. $default_output = theme('image', $image_info);
  740. $this->assertRaw($default_output, t('Image displayed using user supplied alt and title attributes.'));
  741. // Verify that alt/title longer than allowed results in a validation error.
  742. $test_size = 2000;
  743. $edit = array(
  744. $field_name . '[' . LANGUAGE_NONE . '][0][alt]' => $this->randomName($test_size),
  745. $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $this->randomName($test_size),
  746. );
  747. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  748. $this->assertRaw(t('Alternate text cannot be longer than %max characters but is currently %length characters long.', array(
  749. '%max' => $schema['fields'][$field_name .'_alt']['length'],
  750. '%length' => $test_size,
  751. )));
  752. $this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
  753. '%max' => $schema['fields'][$field_name .'_title']['length'],
  754. '%length' => $test_size,
  755. )));
  756. }
  757. /**
  758. * Test passing attributes into the image field formatters.
  759. */
  760. function testImageFieldFormatterAttributes() {
  761. $image = theme('image_formatter', array(
  762. 'item' => array(
  763. 'uri' => 'http://example.com/example.png',
  764. 'attributes' => array(
  765. 'data-image-field-formatter' => 'testFound',
  766. ),
  767. 'alt' => t('Image field formatter attribute test.'),
  768. 'title' => t('Image field formatter'),
  769. ),
  770. ));
  771. $this->assertTrue(stripos($image, 'testFound') > 0, t('Image field formatters can have attributes.'));
  772. }
  773. /**
  774. * Test use of a default image with an image field.
  775. */
  776. function testImageFieldDefaultImage() {
  777. // Create a new image field.
  778. $field_name = strtolower($this->randomName());
  779. $this->createImageField($field_name, 'article');
  780. // Create a new node, with no images and verify that no images are
  781. // displayed.
  782. $node = $this->drupalCreateNode(array('type' => 'article'));
  783. $this->drupalGet('node/' . $node->nid);
  784. // Verify that no image is displayed on the page by checking for the class
  785. // that would be used on the image field.
  786. $this->assertNoPattern('<div class="(.*?)field-name-' . strtr($field_name, '_', '-') . '(.*?)">', t('No image displayed when no image is attached and no default image specified.'));
  787. // Add a default image to the public imagefield instance.
  788. $images = $this->drupalGetTestFiles('image');
  789. $edit = array(
  790. 'files[field_settings_default_image]' => drupal_realpath($images[0]->uri),
  791. );
  792. $this->drupalPost('admin/structure/types/manage/article/fields/' . $field_name, $edit, t('Save settings'));
  793. // Clear field info cache so the new default image is detected.
  794. field_info_cache_clear();
  795. $field = field_info_field($field_name);
  796. $image = file_load($field['settings']['default_image']);
  797. $this->assertTrue($image->status == FILE_STATUS_PERMANENT, t('The default image status is permanent.'));
  798. $default_output = theme('image', array('path' => $image->uri));
  799. $this->drupalGet('node/' . $node->nid);
  800. $this->assertRaw($default_output, t('Default image displayed when no user supplied image is present.'));
  801. // Create a node with an image attached and ensure that the default image
  802. // is not displayed.
  803. $nid = $this->uploadNodeImage($images[1], $field_name, 'article');
  804. $node = node_load($nid, NULL, TRUE);
  805. $image_info = array(
  806. 'path' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
  807. 'width' => 40,
  808. 'height' => 20,
  809. );
  810. $image_output = theme('image', $image_info);
  811. $this->drupalGet('node/' . $nid);
  812. $this->assertNoRaw($default_output, t('Default image is not displayed when user supplied image is present.'));
  813. $this->assertRaw($image_output, t('User supplied image is displayed.'));
  814. // Remove default image from the field and make sure it is no longer used.
  815. $edit = array(
  816. 'field[settings][default_image][fid]' => 0,
  817. );
  818. $this->drupalPost('admin/structure/types/manage/article/fields/' . $field_name, $edit, t('Save settings'));
  819. // Clear field info cache so the new default image is detected.
  820. field_info_cache_clear();
  821. $field = field_info_field($field_name);
  822. $this->assertFalse($field['settings']['default_image'], t('Default image removed from field.'));
  823. // Create an image field that uses the private:// scheme and test that the
  824. // default image works as expected.
  825. $private_field_name = strtolower($this->randomName());
  826. $this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private'));
  827. // Add a default image to the new field.
  828. $edit = array(
  829. 'files[field_settings_default_image]' => drupal_realpath($images[1]->uri),
  830. );
  831. $this->drupalPost('admin/structure/types/manage/article/fields/' . $private_field_name, $edit, t('Save settings'));
  832. $private_field = field_info_field($private_field_name);
  833. $image = file_load($private_field['settings']['default_image']);
  834. $this->assertEqual('private', file_uri_scheme($image->uri), t('Default image uses private:// scheme.'));
  835. $this->assertTrue($image->status == FILE_STATUS_PERMANENT, t('The default image status is permanent.'));
  836. // Create a new node with no image attached and ensure that default private
  837. // image is displayed.
  838. $node = $this->drupalCreateNode(array('type' => 'article'));
  839. $default_output = theme('image', array('path' => $image->uri));
  840. $this->drupalGet('node/' . $node->nid);
  841. $this->assertRaw($default_output, t('Default private image displayed when no user supplied image is present.'));
  842. }
  843. }
  844. /**
  845. * Test class to check for various validations.
  846. */
  847. class ImageFieldValidateTestCase extends ImageFieldTestCase {
  848. public static function getInfo() {
  849. return array(
  850. 'name' => 'Image field validation tests',
  851. 'description' => 'Tests validation functions such as min/max resolution.',
  852. 'group' => 'Image',
  853. );
  854. }
  855. /**
  856. * Test min/max resolution settings.
  857. */
  858. function testResolution() {
  859. $field_name = strtolower($this->randomName());
  860. $min_resolution = 50;
  861. $max_resolution = 100;
  862. $instance_settings = array(
  863. 'max_resolution' => $max_resolution . 'x' . $max_resolution,
  864. 'min_resolution' => $min_resolution . 'x' . $min_resolution,
  865. );
  866. $this->createImageField($field_name, 'article', array(), $instance_settings);
  867. // We want a test image that is too small, and a test image that is too
  868. // big, so cycle through test image files until we have what we need.
  869. $image_that_is_too_big = FALSE;
  870. $image_that_is_too_small = FALSE;
  871. foreach ($this->drupalGetTestFiles('image') as $image) {
  872. $info = image_get_info($image->uri);
  873. if ($info['width'] > $max_resolution) {
  874. $image_that_is_too_big = $image;
  875. }
  876. if ($info['width'] < $min_resolution) {
  877. $image_that_is_too_small = $image;
  878. }
  879. if ($image_that_is_too_small && $image_that_is_too_big) {
  880. break;
  881. }
  882. }
  883. $nid = $this->uploadNodeImage($image_that_is_too_small, $field_name, 'article');
  884. $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.'));
  885. $nid = $this->uploadNodeImage($image_that_is_too_big, $field_name, 'article');
  886. $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.'));
  887. }
  888. }
  889. /**
  890. * Tests that images have correct dimensions when styled.
  891. */
  892. class ImageDimensionsTestCase extends DrupalWebTestCase {
  893. public static function getInfo() {
  894. return array(
  895. 'name' => 'Image dimensions',
  896. 'description' => 'Tests that images have correct dimensions when styled.',
  897. 'group' => 'Image',
  898. );
  899. }
  900. function setUp() {
  901. parent::setUp('image_module_test');
  902. }
  903. /**
  904. * Test styled image dimensions cumulatively.
  905. */
  906. function testImageDimensions() {
  907. // Create a working copy of the file.
  908. $files = $this->drupalGetTestFiles('image');
  909. $file = reset($files);
  910. $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  911. // Create a style.
  912. $style = image_style_save(array('name' => 'test'));
  913. $generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
  914. $url = image_style_url('test', $original_uri);
  915. $variables = array(
  916. 'style_name' => 'test',
  917. 'path' => $original_uri,
  918. 'width' => 40,
  919. 'height' => 20,
  920. );
  921. // Scale an image that is wider than it is high.
  922. $effect = array(
  923. 'name' => 'image_scale',
  924. 'data' => array(
  925. 'width' => 120,
  926. 'height' => 90,
  927. 'upscale' => TRUE,
  928. ),
  929. 'isid' => $style['isid'],
  930. );
  931. image_effect_save($effect);
  932. $img_tag = theme_image_style($variables);
  933. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="120" height="60" alt="" />', t('Expected img tag was found.'));
  934. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  935. $this->drupalGet($url);
  936. $this->assertResponse(200, t('Image was generated at the URL.'));
  937. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  938. $image_info = image_get_info($generated_uri);
  939. $this->assertEqual($image_info['width'], 120, t('Expected width was found.'));
  940. $this->assertEqual($image_info['height'], 60, t('Expected height was found.'));
  941. // Rotate 90 degrees anticlockwise.
  942. $effect = array(
  943. 'name' => 'image_rotate',
  944. 'data' => array(
  945. 'degrees' => -90,
  946. 'random' => FALSE,
  947. ),
  948. 'isid' => $style['isid'],
  949. );
  950. image_effect_save($effect);
  951. $img_tag = theme_image_style($variables);
  952. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="60" height="120" alt="" />', t('Expected img tag was found.'));
  953. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  954. $this->drupalGet($url);
  955. $this->assertResponse(200, t('Image was generated at the URL.'));
  956. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  957. $image_info = image_get_info($generated_uri);
  958. $this->assertEqual($image_info['width'], 60, t('Expected width was found.'));
  959. $this->assertEqual($image_info['height'], 120, t('Expected height was found.'));
  960. // Scale an image that is higher than it is wide (rotated by previous effect).
  961. $effect = array(
  962. 'name' => 'image_scale',
  963. 'data' => array(
  964. 'width' => 120,
  965. 'height' => 90,
  966. 'upscale' => TRUE,
  967. ),
  968. 'isid' => $style['isid'],
  969. );
  970. image_effect_save($effect);
  971. $img_tag = theme_image_style($variables);
  972. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="45" height="90" alt="" />', t('Expected img tag was found.'));
  973. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  974. $this->drupalGet($url);
  975. $this->assertResponse(200, t('Image was generated at the URL.'));
  976. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  977. $image_info = image_get_info($generated_uri);
  978. $this->assertEqual($image_info['width'], 45, t('Expected width was found.'));
  979. $this->assertEqual($image_info['height'], 90, t('Expected height was found.'));
  980. // Test upscale disabled.
  981. $effect = array(
  982. 'name' => 'image_scale',
  983. 'data' => array(
  984. 'width' => 400,
  985. 'height' => 200,
  986. 'upscale' => FALSE,
  987. ),
  988. 'isid' => $style['isid'],
  989. );
  990. image_effect_save($effect);
  991. $img_tag = theme_image_style($variables);
  992. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="45" height="90" alt="" />', t('Expected img tag was found.'));
  993. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  994. $this->drupalGet($url);
  995. $this->assertResponse(200, t('Image was generated at the URL.'));
  996. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  997. $image_info = image_get_info($generated_uri);
  998. $this->assertEqual($image_info['width'], 45, t('Expected width was found.'));
  999. $this->assertEqual($image_info['height'], 90, t('Expected height was found.'));
  1000. // Add a desaturate effect.
  1001. $effect = array(
  1002. 'name' => 'image_desaturate',
  1003. 'data' => array(),
  1004. 'isid' => $style['isid'],
  1005. );
  1006. image_effect_save($effect);
  1007. $img_tag = theme_image_style($variables);
  1008. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="45" height="90" alt="" />', t('Expected img tag was found.'));
  1009. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  1010. $this->drupalGet($url);
  1011. $this->assertResponse(200, t('Image was generated at the URL.'));
  1012. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  1013. $image_info = image_get_info($generated_uri);
  1014. $this->assertEqual($image_info['width'], 45, t('Expected width was found.'));
  1015. $this->assertEqual($image_info['height'], 90, t('Expected height was found.'));
  1016. // Add a random rotate effect.
  1017. $effect = array(
  1018. 'name' => 'image_rotate',
  1019. 'data' => array(
  1020. 'degrees' => 180,
  1021. 'random' => TRUE,
  1022. ),
  1023. 'isid' => $style['isid'],
  1024. );
  1025. image_effect_save($effect);
  1026. $img_tag = theme_image_style($variables);
  1027. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" alt="" />', t('Expected img tag was found.'));
  1028. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  1029. $this->drupalGet($url);
  1030. $this->assertResponse(200, t('Image was generated at the URL.'));
  1031. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  1032. // Add a crop effect.
  1033. $effect = array(
  1034. 'name' => 'image_crop',
  1035. 'data' => array(
  1036. 'width' => 30,
  1037. 'height' => 30,
  1038. 'anchor' => 'center-center',
  1039. ),
  1040. 'isid' => $style['isid'],
  1041. );
  1042. image_effect_save($effect);
  1043. $img_tag = theme_image_style($variables);
  1044. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" width="30" height="30" alt="" />', t('Expected img tag was found.'));
  1045. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  1046. $this->drupalGet($url);
  1047. $this->assertResponse(200, t('Image was generated at the URL.'));
  1048. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  1049. $image_info = image_get_info($generated_uri);
  1050. $this->assertEqual($image_info['width'], 30, t('Expected width was found.'));
  1051. $this->assertEqual($image_info['height'], 30, t('Expected height was found.'));
  1052. // Rotate to a non-multiple of 90 degrees.
  1053. $effect = array(
  1054. 'name' => 'image_rotate',
  1055. 'data' => array(
  1056. 'degrees' => 57,
  1057. 'random' => FALSE,
  1058. ),
  1059. 'isid' => $style['isid'],
  1060. );
  1061. $effect = image_effect_save($effect);
  1062. $img_tag = theme_image_style($variables);
  1063. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" alt="" />', t('Expected img tag was found.'));
  1064. $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  1065. $this->drupalGet($url);
  1066. $this->assertResponse(200, t('Image was generated at the URL.'));
  1067. $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  1068. image_effect_delete($effect);
  1069. // Ensure that an effect with no dimensions callback unsets the dimensions.
  1070. // This ensures compatibility with 7.0 contrib modules.
  1071. $effect = array(
  1072. 'name' => 'image_module_test_null',
  1073. 'data' => array(),
  1074. 'isid' => $style['isid'],
  1075. );
  1076. image_effect_save($effect);
  1077. $img_tag = theme_image_style($variables);
  1078. $this->assertEqual($img_tag, '<img typeof="foaf:Image" src="' . $url . '" alt="" />', t('Expected img tag was found.'));
  1079. }
  1080. }
  1081. /**
  1082. * Tests image_dimensions_scale().
  1083. */
  1084. class ImageDimensionsScaleTestCase extends DrupalUnitTestCase {
  1085. public static function getInfo() {
  1086. return array(
  1087. 'name' => 'image_dimensions_scale()',
  1088. 'description' => 'Tests all control flow branches in image_dimensions_scale().',
  1089. 'group' => 'Image',
  1090. );
  1091. }
  1092. /**
  1093. * Tests all control flow branches in image_dimensions_scale().
  1094. */
  1095. function testImageDimensionsScale() {
  1096. // Define input / output datasets to test different branch conditions.
  1097. $test = array();
  1098. // Test branch conditions:
  1099. // - No height.
  1100. // - Upscale, don't need to upscale.
  1101. $tests[] = array(
  1102. 'input' => array(
  1103. 'dimensions' => array(
  1104. 'width' => 1000,
  1105. 'height' => 2000,
  1106. ),
  1107. 'width' => 200,
  1108. 'height' => NULL,
  1109. 'upscale' => TRUE,
  1110. ),
  1111. 'output' => array(
  1112. 'dimensions' => array(
  1113. 'width' => 200,
  1114. 'height' => 400,
  1115. ),
  1116. 'return_value' => TRUE,
  1117. ),
  1118. );
  1119. // Test branch conditions:
  1120. // - No width.
  1121. // - Don't upscale, don't need to upscale.
  1122. $tests[] = array(
  1123. 'input' => array(
  1124. 'dimensions' => array(
  1125. 'width' => 1000,
  1126. 'height' => 800,
  1127. ),
  1128. 'width' => NULL,
  1129. 'height' => 140,
  1130. 'upscale' => FALSE,
  1131. ),
  1132. 'output' => array(
  1133. 'dimensions' => array(
  1134. 'width' => 175,
  1135. 'height' => 140,
  1136. ),
  1137. 'return_value' => TRUE,
  1138. ),
  1139. );
  1140. // Test branch conditions:
  1141. // - Source aspect ratio greater than target.
  1142. // - Upscale, need to upscale.
  1143. $tests[] = array(
  1144. 'input' => array(
  1145. 'dimensions' => array(
  1146. 'width' => 8,
  1147. 'height' => 20,
  1148. ),
  1149. 'width' => 200,
  1150. 'height' => 140,
  1151. 'upscale' => TRUE,
  1152. ),
  1153. 'output' => array(
  1154. 'dimensions' => array(
  1155. 'width' => 56,
  1156. 'height' => 140,
  1157. ),
  1158. 'return_value' => TRUE,
  1159. ),
  1160. );
  1161. // Test branch condition: target aspect ratio greater than source.
  1162. $tests[] = array(
  1163. 'input' => array(
  1164. 'dimensions' => array(
  1165. 'width' => 2000,
  1166. 'height' => 800,
  1167. ),
  1168. 'width' => 200,
  1169. 'height' => 140,
  1170. 'upscale' => FALSE,
  1171. ),
  1172. 'output' => array(
  1173. 'dimensions' => array(
  1174. 'width' => 200,
  1175. 'height' => 80,
  1176. ),
  1177. 'return_value' => TRUE,
  1178. ),
  1179. );
  1180. // Test branch condition: don't upscale, need to upscale.
  1181. $tests[] = array(
  1182. 'input' => array(
  1183. 'dimensions' => array(
  1184. 'width' => 100,
  1185. 'height' => 50,
  1186. ),
  1187. 'width' => 200,
  1188. 'height' => 140,
  1189. 'upscale' => FALSE,
  1190. ),
  1191. 'output' => array(
  1192. 'dimensions' => array(
  1193. 'width' => 100,
  1194. 'height' => 50,
  1195. ),
  1196. 'return_value' => FALSE,
  1197. ),
  1198. );
  1199. foreach ($tests as $test) {
  1200. // Process the test dataset.
  1201. $return_value = image_dimensions_scale($test['input']['dimensions'], $test['input']['width'], $test['input']['height'], $test['input']['upscale']);
  1202. // Check the width.
  1203. $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'])));
  1204. // Check the height.
  1205. $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'])));
  1206. // Check the return value.
  1207. $this->assertEqual($test['output']['return_value'], $return_value, t('Correct return value.'));
  1208. }
  1209. }
  1210. }
  1211. /**
  1212. * Tests default image settings.
  1213. */
  1214. class ImageFieldDefaultImagesTestCase extends ImageFieldTestCase {
  1215. public static function getInfo() {
  1216. return array(
  1217. 'name' => 'Image field default images tests',
  1218. 'description' => 'Tests setting up default images both to the field and field instance.',
  1219. 'group' => 'Image',
  1220. );
  1221. }
  1222. function setUp() {
  1223. parent::setUp(array('field_ui'));
  1224. }
  1225. /**
  1226. * Tests CRUD for fields and fields instances with default images.
  1227. */
  1228. function testDefaultImages() {
  1229. // Create files to use as the default images.
  1230. $files = $this->drupalGetTestFiles('image');
  1231. $default_images = array();
  1232. foreach (array('field', 'instance', 'instance2', 'field_new', 'instance_new') as $image_target) {
  1233. $file = array_pop($files);
  1234. $file = file_save($file);
  1235. $default_images[$image_target] = $file;
  1236. }
  1237. // Create an image field and add an instance to the article content type.
  1238. $field_name = strtolower($this->randomName());
  1239. $field_settings = array(
  1240. 'default_image' => $default_images['field']->fid,
  1241. );
  1242. $instance_settings = array(
  1243. 'default_image' => $default_images['instance']->fid,
  1244. );
  1245. $widget_settings = array(
  1246. 'preview_image_style' => 'medium',
  1247. );
  1248. $this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings);
  1249. $field = field_info_field($field_name);
  1250. $instance = field_info_instance('node', $field_name, 'article');
  1251. // Add another instance with another default image to the page content type.
  1252. $instance2 = array_merge($instance, array(
  1253. 'bundle' => 'page',
  1254. 'settings' => array(
  1255. 'default_image' => $default_images['instance2']->fid,
  1256. ),
  1257. ));
  1258. field_create_instance($instance2);
  1259. $instance2 = field_info_instance('node', $field_name, 'page');
  1260. // Confirm the defaults are present on the article field admin form.
  1261. $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
  1262. $this->assertFieldByXpath(
  1263. '//input[@name="field[settings][default_image][fid]"]',
  1264. $default_images['field']->fid,
  1265. format_string(
  1266. 'Article image field default equals expected file ID of @fid.',
  1267. array('@fid' => $default_images['field']->fid)
  1268. )
  1269. );
  1270. $this->assertFieldByXpath(
  1271. '//input[@name="instance[settings][default_image][fid]"]',
  1272. $default_images['instance']->fid,
  1273. format_string(
  1274. 'Article image field instance default equals expected file ID of @fid.',
  1275. array('@fid' => $default_images['instance']->fid)
  1276. )
  1277. );
  1278. // Confirm the defaults are present on the page field admin form.
  1279. $this->drupalGet("admin/structure/types/manage/page/fields/$field_name");
  1280. $this->assertFieldByXpath(
  1281. '//input[@name="field[settings][default_image][fid]"]',
  1282. $default_images['field']->fid,
  1283. format_string(
  1284. 'Page image field default equals expected file ID of @fid.',
  1285. array('@fid' => $default_images['field']->fid)
  1286. )
  1287. );
  1288. $this->assertFieldByXpath(
  1289. '//input[@name="instance[settings][default_image][fid]"]',
  1290. $default_images['instance2']->fid,
  1291. format_string(
  1292. 'Page image field instance default equals expected file ID of @fid.',
  1293. array('@fid' => $default_images['instance2']->fid)
  1294. )
  1295. );
  1296. // Confirm that the image default is shown for a new article node.
  1297. $article = $this->drupalCreateNode(array('type' => 'article'));
  1298. $article_built = node_view($article);
  1299. $this->assertEqual(
  1300. $article_built[$field_name]['#items'][0]['fid'],
  1301. $default_images['instance']->fid,
  1302. format_string(
  1303. 'A new article node without an image has the expected default image file ID of @fid.',
  1304. array('@fid' => $default_images['instance']->fid)
  1305. )
  1306. );
  1307. // Confirm that the image default is shown for a new page node.
  1308. $page = $this->drupalCreateNode(array('type' => 'page'));
  1309. $page_built = node_view($page);
  1310. $this->assertEqual(
  1311. $page_built[$field_name]['#items'][0]['fid'],
  1312. $default_images['instance2']->fid,
  1313. format_string(
  1314. 'A new page node without an image has the expected default image file ID of @fid.',
  1315. array('@fid' => $default_images['instance2']->fid)
  1316. )
  1317. );
  1318. // Upload a new default for the field.
  1319. $field['settings']['default_image'] = $default_images['field_new']->fid;
  1320. field_update_field($field);
  1321. // Confirm that the new field default is used on the article admin form.
  1322. $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
  1323. $this->assertFieldByXpath(
  1324. '//input[@name="field[settings][default_image][fid]"]',
  1325. $default_images['field_new']->fid,
  1326. format_string(
  1327. 'Updated image field default equals expected file ID of @fid.',
  1328. array('@fid' => $default_images['field_new']->fid)
  1329. )
  1330. );
  1331. // Reload the nodes and confirm the field instance defaults are used.
  1332. $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
  1333. $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
  1334. $this->assertEqual(
  1335. $article_built[$field_name]['#items'][0]['fid'],
  1336. $default_images['instance']->fid,
  1337. format_string(
  1338. 'An existing article node without an image has the expected default image file ID of @fid.',
  1339. array('@fid' => $default_images['instance']->fid)
  1340. )
  1341. );
  1342. $this->assertEqual(
  1343. $page_built[$field_name]['#items'][0]['fid'],
  1344. $default_images['instance2']->fid,
  1345. format_string(
  1346. 'An existing page node without an image has the expected default image file ID of @fid.',
  1347. array('@fid' => $default_images['instance2']->fid)
  1348. )
  1349. );
  1350. // Upload a new default for the article's field instance.
  1351. $instance['settings']['default_image'] = $default_images['instance_new']->fid;
  1352. field_update_instance($instance);
  1353. // Confirm the new field instance default is used on the article field
  1354. // admin form.
  1355. $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
  1356. $this->assertFieldByXpath(
  1357. '//input[@name="instance[settings][default_image][fid]"]',
  1358. $default_images['instance_new']->fid,
  1359. format_string(
  1360. 'Updated article image field instance default equals expected file ID of @fid.',
  1361. array('@fid' => $default_images['instance_new']->fid)
  1362. )
  1363. );
  1364. // Reload the nodes.
  1365. $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
  1366. $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
  1367. // Confirm the article uses the new default.
  1368. $this->assertEqual(
  1369. $article_built[$field_name]['#items'][0]['fid'],
  1370. $default_images['instance_new']->fid,
  1371. format_string(
  1372. 'An existing article node without an image has the expected default image file ID of @fid.',
  1373. array('@fid' => $default_images['instance_new']->fid)
  1374. )
  1375. );
  1376. // Confirm the page remains unchanged.
  1377. $this->assertEqual(
  1378. $page_built[$field_name]['#items'][0]['fid'],
  1379. $default_images['instance2']->fid,
  1380. format_string(
  1381. 'An existing page node without an image has the expected default image file ID of @fid.',
  1382. array('@fid' => $default_images['instance2']->fid)
  1383. )
  1384. );
  1385. // Remove the instance default from articles.
  1386. $instance['settings']['default_image'] = NULL;
  1387. field_update_instance($instance);
  1388. // Confirm the article field instance default has been removed.
  1389. $this->drupalGet("admin/structure/types/manage/article/fields/$field_name");
  1390. $this->assertFieldByXpath(
  1391. '//input[@name="instance[settings][default_image][fid]"]',
  1392. '',
  1393. 'Updated article image field instance default has been successfully removed.'
  1394. );
  1395. // Reload the nodes.
  1396. $article_built = node_view($article = node_load($article->nid, NULL, $reset = TRUE));
  1397. $page_built = node_view($page = node_load($page->nid, NULL, $reset = TRUE));
  1398. // Confirm the article uses the new field (not instance) default.
  1399. $this->assertEqual(
  1400. $article_built[$field_name]['#items'][0]['fid'],
  1401. $default_images['field_new']->fid,
  1402. format_string(
  1403. 'An existing article node without an image has the expected default image file ID of @fid.',
  1404. array('@fid' => $default_images['field_new']->fid)
  1405. )
  1406. );
  1407. // Confirm the page remains unchanged.
  1408. $this->assertEqual(
  1409. $page_built[$field_name]['#items'][0]['fid'],
  1410. $default_images['instance2']->fid,
  1411. format_string(
  1412. 'An existing page node without an image has the expected default image file ID of @fid.',
  1413. array('@fid' => $default_images['instance2']->fid)
  1414. )
  1415. );
  1416. }
  1417. }
  1418. /**
  1419. * Tests image theme functions.
  1420. */
  1421. class ImageThemeFunctionWebTestCase extends DrupalWebTestCase {
  1422. public static function getInfo() {
  1423. return array(
  1424. 'name' => 'Image theme functions',
  1425. 'description' => 'Test that the image theme functions work correctly.',
  1426. 'group' => 'Image',
  1427. );
  1428. }
  1429. function setUp() {
  1430. parent::setUp(array('image'));
  1431. }
  1432. /**
  1433. * Tests usage of the image field formatters.
  1434. */
  1435. function testImageFormatterTheme() {
  1436. // Create an image.
  1437. $files = $this->drupalGetTestFiles('image');
  1438. $file = reset($files);
  1439. $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  1440. // Create a style.
  1441. image_style_save(array('name' => 'test'));
  1442. $url = image_style_url('test', $original_uri);
  1443. // Test using theme_image_formatter() without an image title, alt text, or
  1444. // link options.
  1445. $path = $this->randomName();
  1446. $element = array(
  1447. '#theme' => 'image_formatter',
  1448. '#image_style' => 'test',
  1449. '#item' => array(
  1450. 'uri' => $original_uri,
  1451. ),
  1452. '#path' => array(
  1453. 'path' => $path,
  1454. ),
  1455. );
  1456. $rendered_element = render($element);
  1457. $expected_result = '<a href="' . url($path) . '"><img typeof="foaf:Image" src="' . $url . '" alt="" /></a>';
  1458. $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders without title, alt, or path options.');
  1459. // Link the image to a fragment on the page, and not a full URL.
  1460. $fragment = $this->randomName();
  1461. $element['#path']['path'] = '';
  1462. $element['#path']['options'] = array(
  1463. 'external' => TRUE,
  1464. 'fragment' => $fragment,
  1465. );
  1466. $rendered_element = render($element);
  1467. $expected_result = '<a href="#' . $fragment . '"><img typeof="foaf:Image" src="' . $url . '" alt="" /></a>';
  1468. $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders a link fragment.');
  1469. }
  1470. }