commit 2cb7b9b26154010c50209bdef89bc29142448cf5 Author: Shane Auckland Date: Wed Apr 17 13:24:27 2013 +0100 1898420-new_changes diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 68eabdd..46e8fa9 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -1010,6 +1010,11 @@ function template_preprocess_image_style(&$variables) { // Add in the image style name as an HTML class. $variables['attributes']['class'][] = 'image-style-' . drupal_html_class($variables['style_name']); + + // Remove 'image-style' from the class array. This is added through + // template_preprocess() but we do not need it. + // @todo Remove after http://drupal.org/node/1938430 is resolved. + $variables['attributes']['class'] = array_diff($variables['attributes']['class'], array('image-style')); // Determine the URL for the styled image. $variables['uri'] = image_style_url($variables['style_name'], $variables['uri']); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php index eadbf5d..2f6a939 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php @@ -30,6 +30,68 @@ public static function getInfo() { 'group' => 'Image', ); } + + /** + * Constructs an xpath query string + * + * @param string $name + * Name of the element to search for + * @param array $attributes + * List of attributes the element must contain + * + * @return string + * Xpath expression + */ + protected function buildXPath($name, array $attributes) { + $expression = '/' . $name . '['; + $attribs = ''; + foreach ($attributes as $attr => $value) { + $attribs .= ' and @' . $attr . '=\'' . $value . '\''; + } + $expression .= substr($attribs,5) . ']'; + return $expression; + } + + /** + * Compare a string of HTML to an element definition. + * + * An element can be defined by its name and attributes. This compares a + * provided string of HTML containing a target element with an XPath + * expression constructed from the expected name and attributes. Comparison + * fails if the XPath expression does not match an element, or if it matches + * an element that contains unexpected attributes. Example: + * @code + * $this->compareElement($rawHtml, 'img', array( + * 'src' => 'some-uri.jpg', + * 'width => 50, + * 'alt' => '', + * ); + * @endcode + * + * @param string $element + * Raw HTML to search for an element. + * @param string $name + * The name of the element to match. + * @param array $attributes + * Array of expected attributes. + */ + protected function compareElement($element, $name, array $attributes) { + $expression = $this->buildXPath($name, $attributes); + + $xml = new \SimpleXMLElement($element); + $found = $xml->xpath($expression); + $number_of_elements = count($found); + // Assert that the xpath matches an element + $this->assertEqual($number_of_elements, 1, htmlentities($element) . " matches the expected expression: $expression."); + if ($number_of_elements) { + // Assert that the matched element doesn't contain unexpected attributes. + foreach ($found[0]->attributes as $attribute => $value) { + if (!array_key_exists($attribute, $attributes)) { + $this->fail("Unexpected attribute $attribute found."); + } + } + } + } /** * Test styled image dimensions cumulatively. @@ -70,7 +132,13 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'width' => 120, + 'height' => 60, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -91,7 +159,13 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'width' => 60, + 'height' => 120, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -113,7 +187,13 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'width' => 45, + 'height' => 90, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -135,7 +215,13 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'width' => 45, + 'height' => 90, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -153,7 +239,13 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'width' => 45, + 'height' => 90, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -174,7 +266,11 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -194,7 +290,13 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'width' => 30, + 'height' => 30, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -215,7 +317,11 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'alt' => '' + )); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -233,6 +339,10 @@ function testImageDimensions() { image_effect_save($style, $effect); $img_tag = theme('image_style', $variables); - $this->assertEqual($img_tag, ''); + $this->compareElement($img_tag, 'img', array( + 'class' => 'image-style-test', + 'src' => $url, + 'alt' => '' + )); } }