375c375 < similarity index 96% --- > similarity index 62% 378c378 < index 54c5c64a19..45a7636fb6 100644 --- > index 54c5c64a19..34b7cce08e 100644 412a413,539 > @@ -95,30 +101,28 @@ public function testImageFormatterTheme() { > > // Test using theme_image_formatter() with a NULL value for the alt option. > $element = $base_element; > - $this->setRawContent($renderer->renderRoot($element)); > - $elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]); > - $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders with a NULL value for the alt option.'); > + > + $html = $renderer->renderRoot($element); > + $elements = $this->getXPathResultCount('//a[@href="'.base_path() . $path.'"]/img[@class="image-style-test" and @src="'.$url.'" and @width="'.$image->getWidth().'" and @height="'.$image->getHeight().'"]', $html); > + $this->assertEqual($elements, 1, 'theme_image_formatter() correctly renders with a NULL value for the alt option.'); > > // Test using theme_image_formatter() without an image title, alt text, or > // link options. > $element = $base_element; > $element['#item']->alt = ''; > - $this->setRawContent($renderer->renderRoot($element)); > - $elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]); > - $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders without title, alt, or path options.'); > + > + $html = $renderer->renderRoot($element); > + $elements = $this->getXPathResultCount('//a[@href="'.base_path() . $path.'"]/img[@class="image-style-test" and @src="'.$url.'" and @width="'.$image->getWidth().'" and @height="'.$image->getHeight().'"]', $html); > + $this->assertEqual($elements, 1, 'theme_image_formatter() correctly renders without title, alt, or path options.'); > > // Link the image to a fragment on the page, and not a full URL. > $fragment = $this->randomMachineName(); > $element = $base_element; > $element['#url'] = Url::fromRoute('', [], ['fragment' => $fragment]); > - $this->setRawContent($renderer->renderRoot($element)); > - $elements = $this->xpath('//a[@href=:fragment]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', [ > - ':fragment' => '#' . $fragment, > - ':url' => $url, > - ':width' => $image->getWidth(), > - ':height' => $image->getHeight() > - ]); > - $this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.'); > + > + $html = $renderer->renderRoot($element); > + $elements = $this->getXPathResultCount('//a[@href="'.'#' . $fragment.'"]/img[@class="image-style-test" and @src="'.$url.'" and @width="'.$image->getWidth().'" and @height="'.$image->getHeight().'"]', $html); > + $this->assertEqual($elements, 1, 'theme_image_formatter() correctly renders a link fragment.'); > } > > /** > @@ -146,16 +150,19 @@ public function testImageStyleTheme() { > ]; > > $element = $base_element; > - $this->setRawContent($renderer->renderRoot($element)); > - $elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url and @alt=""]', [':url' => $url]); > - $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly.'); > + > + $html = $renderer->renderRoot($element); > + $elements = $this->getXPathResultCount('//img[@class="image-style-image-test" and @src="'.$url.'" and @alt=""]', $html); > + $this->assertEqual($elements, 1, 'theme_image_style() renders an image correctly.'); > > // Test using theme_image_style() with a NULL value for the alt option. > $element = $base_element; > $element['#alt'] = NULL; > - $this->setRawContent($renderer->renderRoot($element)); > - $elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url]', [':url' => $url]); > - $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly with a NULL value for the alt option.'); > + > + $html = $renderer->renderRoot($element); > + $elements = $this->getXPathResultCount('//img[@class="image-style-image-test" and @src="'.$url.'" and not(@alt)]', $html); > + > + $this->assertEqual($elements, 1, 'theme_image_style() renders an image correctly whitout alt option when NULL.'); > } > > /** > @@ -176,9 +183,9 @@ public function testImageAltFunctionality() { > '#attributes' => ['class' => 'image-with-regular-alt', 'id' => 'my-img'], > ]; > > - $this->setRawContent($renderer->renderRoot($image_with_alt_property)); > - $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-regular-alt", ":alt" => "Regular alt"]); > - $this->assertEqual(count($elements), 1, 'Regular alt displays correctly'); > + $html = $renderer->renderRoot($image_with_alt_property); > + $elements = $this->getXPathResultCount('//img[contains(@class, "image-with-regular-alt") and contains(@alt, "Regular alt")]', $html); > + $this->assertEqual($elements, 1, 'Regular alt displays correctly'); > > // Test using alt attribute inside attributes. > $image_with_alt_attribute_alt_attribute = [ > @@ -194,9 +201,9 @@ public function testImageAltFunctionality() { > ], > ]; > > - $this->setRawContent($renderer->renderRoot($image_with_alt_attribute_alt_attribute)); > - $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]); > - $this->assertEqual(count($elements), 1, 'Attribute alt displays correctly'); > + $html = $renderer->renderRoot($image_with_alt_attribute_alt_attribute); > + $elements = $this->getXPathResultCount('//img[contains(@class, "image-with-attribute-alt") and contains(@alt, "Attribute alt")]', $html); > + $this->assertEqual($elements, 1, 'Attribute alt displays correctly'); > > // Test using alt attribute as property and inside attributes. > $image_with_alt_attribute_both = [ > @@ -213,9 +220,28 @@ public function testImageAltFunctionality() { > ], > ]; > > - $this->setRawContent($renderer->renderRoot($image_with_alt_attribute_both)); > - $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]); > - $this->assertEqual(count($elements), 1, 'Attribute alt overrides alt property if both set.'); > + $html = $renderer->renderRoot($image_with_alt_attribute_both); > + $elements = $this->getXPathResultCount('//img[contains(@class, "image-with-attribute-alt") and contains(@alt, "Attribute alt")]', $html); > + $this->assertEqual($elements, 1, 'Attribute alt overrides alt property if both set.'); > + } > + > + /** > + * Counts the occurrences of the given XPath query in a given HTML snippet. > + * > + * @param string $query > + * The XPath query to execute. > + * @param string $html > + * The HTML snippet to check. > + * > + * @return int > + * The number of results that are found. > + */ > + protected function getXPathResultCount($query, $html) { > + $document = new \DOMDocument(); > + $document->loadHTML($html); > + $xpath = new \DOMXPath($document); > + > + return $xpath->query($query)->length; > } > > }