image.test 67 KB

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