image.test 63 KB

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