image_example.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * Test case for testing the image example module.
  5. *
  6. * This file contains the tests cases to check if the module is performing as
  7. * expected.
  8. */
  9. /**
  10. * Functional tests for the Image Example module.
  11. *
  12. * @ingroup image_example
  13. */
  14. class ImageExampleTestCase extends DrupalWebTestCase {
  15. protected $webUser;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static function getInfo() {
  20. return array(
  21. 'name' => 'Image example functionality',
  22. 'description' => 'Test functionality of the Image Example module.',
  23. 'group' => 'Examples',
  24. );
  25. }
  26. /**
  27. * Enable modules and create user with specific permissions.
  28. */
  29. public function setUp() {
  30. parent::setUp('image_example');
  31. // Create user with permission to administer image styles.
  32. $this->webUser = $this->drupalCreateUser(array('administer image styles', 'administer blocks'));
  33. }
  34. /**
  35. * Test implementations of image API hooks.
  36. */
  37. public function testImageExample() {
  38. // Login the admin user.
  39. $this->drupalLogin($this->webUser);
  40. // Verify that the default style added by
  41. // image_example_image_default_styles() is in the list of image styles.
  42. $image_styles = image_styles();
  43. $this->assertTrue(isset($image_styles['image_example_style']), 'The default style image_example_style is in the list of image styles.');
  44. // Verify that the effect added to the default 'thumbnail' style by
  45. // image_example_image_styles_alter() is present.
  46. $this->assertTrue((isset($image_styles['thumbnail']['effects'][1]['name']) && $image_styles['thumbnail']['effects'][1]['name'] == 'image_example_colorize'), 'Effect added to the thumbnail style via hook_image_styles_alter() is present.');
  47. // Create a new image style and add the effect provided by
  48. // image_example_effect_info().
  49. $new_style = array('name' => drupal_strtolower($this->randomName()));
  50. $new_style = image_style_save($new_style);
  51. $this->assertTrue(isset($new_style['isid']), format_string('Image style @style_name created.', array('@style_name' => $new_style['name'])));
  52. $edit = array(
  53. 'new' => 'image_example_colorize',
  54. );
  55. $this->drupalPost('admin/config/media/image-styles/edit/' . $new_style['name'], $edit, t('Add'));
  56. // Verify the 'color' field provided by image_example_colorize_form()
  57. // appears on the effect configuration page. And that we can fill it out.
  58. $this->assertField('data[color]', 'Color field provided by image_example_effect_colorize_form is present on effect configuration page.');
  59. $edit = array(
  60. 'data[color]' => '#000000',
  61. );
  62. $this->drupalPost(NULL, $edit, t('Add effect'));
  63. $this->assertText(t('The image effect was successfully applied.'), format_string('Colorize effect added to @style_name.', array('@style_name' => $new_style['name'])));
  64. // Set the variable 'image_example_style_name' to the name of our new style
  65. // then rename the style and ensure the variable name is changed.
  66. // @todo Enable this block once http://drupal.org/node/713872 is fixed.
  67. if (defined('bug_713872_fixed')) {
  68. $style = image_style_load($new_style['name']);
  69. variable_set('image_example_style_name', $style['name']);
  70. $style['name'] = drupal_strtolower($this->randomName());
  71. $style = image_style_save($style);
  72. $variable = variable_get('image_example_style_name', '');
  73. $this->assertTrue(($variable == $style['name']), 'Variable image_example_style_name successfully updated when renaming image style.');
  74. }
  75. }
  76. /**
  77. * Tests for image block provided by module.
  78. */
  79. public function testImageExamplePage() {
  80. // Login the admin user.
  81. $this->drupalLogin($this->webUser);
  82. $this->drupalCreateNode(array('promote' => 1));
  83. // Upload an image to the image page.
  84. $images = $this->drupalGetTestFiles('image');
  85. $edit = array(
  86. 'files[image_example_image_fid]' => drupal_realpath($images[0]->uri),
  87. 'image_example_style_name' => 'image_example_style',
  88. );
  89. $this->drupalPost('image_example/styles', $edit, t('Save'));
  90. $this->assertText(t('The image @image_name was uploaded', array('@image_name' => $images[0]->filename)), 'Image uploaded to image block.');
  91. // Verify the image is displayed.
  92. $this->drupalGet('image_example/styles');
  93. $fid = variable_get('image_example_image_fid', FALSE);
  94. $image = isset($fid) ? file_load($fid) : NULL;
  95. $this->assertRaw(file_uri_target($image->uri), 'Image is displayed');
  96. }
  97. }