diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
index 488adf4..b4a192d 100644
--- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
@@ -405,7 +405,6 @@ protected function assertNoID($id, $html) {
*/
protected function getXPathResultCount($query, $html) {
$document = new \DOMDocument;
- $document->preserveWhiteSpace = FALSE;
$document->loadHTML($html);
$xpath = new \DOMXPath($document);
diff --git a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
index df0ad23..f28f6e2 100644
--- a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
+++ b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
@@ -109,8 +109,7 @@ public function testGenerateHrefs($route_name, array $parameters, $absolute, $ex
$url = new Url($route_name, $parameters, array('absolute' => $absolute));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array('href' => $expected_url),
), $result);
}
@@ -134,8 +133,7 @@ public function testGenerate() {
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'href' => '/test-route-1#the-fragment',
),
@@ -172,8 +170,7 @@ public function testGenerateExternal() {
$url->setOption('set_active_class', TRUE);
$result = $this->linkGenerator->generate('Drupal', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'href' => 'http://drupal.org',
),
@@ -200,8 +197,7 @@ public function testGenerateAttributes() {
));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'href' => '/test-route-1',
'title' => 'Tooltip',
@@ -227,8 +223,7 @@ public function testGenerateQuery() {
));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'href' => '/test-route-1?test=value',
),
@@ -251,8 +246,7 @@ public function testGenerateParametersAsQuery() {
$url = new Url('test_route_1', array('test' => 'value'), array());
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'href' => '/test-route-1?test=value',
),
@@ -277,8 +271,7 @@ public function testGenerateOptions() {
));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'href' => '/test-route-1?test=value',
),
@@ -302,13 +295,7 @@ public function testGenerateXss() {
$url = new Url('test_route_4');
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate("", $url);
- $this->assertNotTag(array(
- 'tag' => 'a',
- 'attributes' => array('href' => '/test-route-4'),
- 'child' => array(
- 'tag' => 'script',
- ),
- ), $result);
+ $this->assertNoXPathResults('//a[@href="/test-route-4"]/script', $result);
}
/**
@@ -336,8 +323,7 @@ public function testGenerateWithHtml() {
));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'href' => '/test-route-5',
'title' => 'HTML Tooltip',
@@ -348,8 +334,7 @@ public function testGenerateWithHtml() {
$url = new Url('test_route_5', array(), array('html' => TRUE));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('HTML output', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array('href' => '/test-route-5'),
'child' => array(
'tag' => 'em',
@@ -390,8 +375,7 @@ public function testGenerateActive() {
$url = new Url('test_route_1', array(), array('set_active_class' => TRUE));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array('data-drupal-link-system-path' => 'test-route-1'),
), $result);
@@ -399,10 +383,7 @@ public function testGenerateActive() {
$url = new Url('test_route_1', array(), array('set_active_class' => FALSE));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertNotTag(array(
- 'tag' => 'a',
- 'attributes' => array('data-drupal-link-system-path' => 'test-route-1'),
- ), $result);
+ $this->assertNoXPathResults('//a[@data-drupal-link-system-path="test-route-1"]', $result);
// Render a link with an associated language.
$url = new Url('test_route_1', array(), array(
@@ -411,8 +392,7 @@ public function testGenerateActive() {
));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'data-drupal-link-system-path' => 'test-route-1',
'hreflang' => 'de',
@@ -426,8 +406,7 @@ public function testGenerateActive() {
));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'data-drupal-link-system-path' => 'test-route-3',
'data-drupal-link-query' => '{"value":"example_1"}',
@@ -441,8 +420,7 @@ public function testGenerateActive() {
));
$url->setUrlGenerator($this->urlGenerator);
$result = $this->linkGenerator->generate('Test', $url);
- $this->assertTag(array(
- 'tag' => 'a',
+ $this->assertLink(array(
'attributes' => array(
'data-drupal-link-system-path' => 'test-route-4/1',
'data-drupal-link-query' => '{"value":"example_1"}',
@@ -451,24 +429,31 @@ public function testGenerateActive() {
}
/**
- * {@inheritdoc}
+ * Checks that a link with certain properties exists in a given HTML snippet.
+ *
+ * @param array $properties
+ * An associative array of link properties, with the following keys:
+ * - attributes: optional array of HTML attributes that should be present.
+ * - content: optional link content.
+ * @param string $html
+ * The HTML to check.
+ * @param int $count
+ * How many times the link should be present in the HTML. Defaults to 1.
*/
- public static function assertTag($matcher, $actual, $message = '', $isHtml = TRUE) {
- // The parent method is deprecated in PHPUnit 4.2. This is an alternative
- // implementation that covers the use cases in this test.
+ public static function assertLink(array $properties, $html, $count = 1) {
// Provide default values.
- $matcher += array('attributes' => array());
+ $properties += array('attributes' => array());
- // Create an XPath query to select the requested tag.
- $query = '//' . $matcher['tag'];
+ // Create an XPath query that selects a link element.
+ $query = '//a';
// Append XPath predicates for the attributes and content text.
$predicates = array();
- foreach ($matcher['attributes'] as $attribute => $value) {
+ foreach ($properties['attributes'] as $attribute => $value) {
$predicates[] = "@$attribute='$value'";
}
- if (!empty($matcher['content'])) {
- $predicates[] = "contains(.,'{$matcher['content']}')";
+ if (!empty($properties['content'])) {
+ $predicates[] = "contains(.,'{$properties['content']}')";
}
if (!empty($predicates)) {
$query .= '[' . implode(' and ', $predicates) . ']';
@@ -476,11 +461,29 @@ public static function assertTag($matcher, $actual, $message = '', $isHtml = TRU
// Execute the query.
$document = new \DOMDocument;
- $document->preserveWhiteSpace = FALSE;
- $document->loadHTML($actual);
+ $document->loadHTML($html);
+ $xpath = new \DOMXPath($document);
+
+ self::assertEquals($count, $xpath->query($query)->length);
+ }
+
+ /**
+ * Checks that the given XPath query has no results 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 assertNoXPathResults($query, $html) {
+ $document = new \DOMDocument;
+ $document->loadHTML($html);
$xpath = new \DOMXPath($document);
- self::assertEquals(1, $xpath->query($query)->length);
+ self::assertFalse((bool) $xpath->query($query)->length);
}
}