From 1af32393e42e8572f8fb729ac776d39bd3ec8999 Mon Sep 17 00:00:00 2001
From: Dries <dries@buytaert.net>
Date: Tue, 12 Feb 2013 16:38:06 -0500
Subject: [PATCH] Issue #1869914 by scor: Refactor RdfMappingDefinitionTestCase
 and split it in dedicated test cases.

---
 .../rdf/Tests/MappingDefinitionTest.php       | 200 ++++++++----------
 .../Drupal/rdf/Tests/NodeAttributesTest.php   |  81 +++++++
 .../rdf/Tests/TaxonomyAttributesTest.php      |  68 ++++++
 3 files changed, 237 insertions(+), 112 deletions(-)
 create mode 100644 core/modules/rdf/lib/Drupal/rdf/Tests/NodeAttributesTest.php
 create mode 100644 core/modules/rdf/lib/Drupal/rdf/Tests/TaxonomyAttributesTest.php

diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/MappingDefinitionTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/MappingDefinitionTest.php
index 52bcc7ee658e..ef33e6f1796d 100644
--- a/core/modules/rdf/lib/Drupal/rdf/Tests/MappingDefinitionTest.php
+++ b/core/modules/rdf/lib/Drupal/rdf/Tests/MappingDefinitionTest.php
@@ -2,17 +2,17 @@
 
 /**
  * @file
- * Definition of Drupal\rdf\Tests\MappingDefinitionTest.
+ * Contains Drupal\rdf\Tests\MappingDefinitionTest.
  */
 
 namespace Drupal\rdf\Tests;
 
-use Drupal\taxonomy\Tests\TaxonomyTestBase;
+use Drupal\node\Tests\NodeTestBase;
 
 /**
  * Tests the RDF mapping definition functionality.
  */
-class MappingDefinitionTest extends TaxonomyTestBase {
+class MappingDefinitionTest extends NodeTestBase {
 
   /**
    * Modules to enable.
@@ -21,139 +21,115 @@ class MappingDefinitionTest extends TaxonomyTestBase {
    */
   public static $modules = array('rdf', 'rdf_test');
 
-  protected $profile = 'standard';
-
   public static function getInfo() {
     return array(
       'name' => 'RDF mapping definition functionality',
-      'description' => 'Test the different types of RDF mappings and ensure the proper RDFa markup in included in nodes and user profile pages.',
+      'description' => 'Tests that RDF definitions are properly attached to entities.',
       'group' => 'RDF',
     );
   }
 
+  function setUp() {
+    parent::setUp();
+    // NodeTestBase creates page content type for us.
+    // Defines RDF mapping for page content type.
+    $page_rdf_mapping = array(
+      'type' => 'node',
+      'bundle' => 'page',
+      'mapping' => array(
+        'rdftype' => array('foaf:DocumentBar'),
+        'body' => array(
+          'predicates' => array('dc:dummy-property'),
+        ),
+        'created' => array(
+          'predicates' => array('dc:dummy-date'),
+          'callback' => 'date_iso8601_foo',
+          'datatype' => 'xsd:dateTimeFoo',
+        ),
+      ),
+    );
+    rdf_mapping_save($page_rdf_mapping);
+  }
+
   /**
-   * Create a node of type article and test whether the RDF mapping defined for
-   * this node type in rdf_test.module is used in the node page.
+   * Creates a node of type page and tests whether the RDF mapping is
+   * attached to the node.
    */
-  function testAttributesInMarkup1() {
-    $node = $this->drupalCreateNode(array('type' => 'article'));
-    $isoDate = date('c', $node->changed);
-    $url = url('node/' . $node->nid);
-    $this->drupalGet('node/' . $node->nid);
-
-    // Ensure the default bundle mapping for node is used. These attributes come
-    // from the node default bundle definition.
-    $node_title = $this->xpath("//meta[@property='dc:title' and @content='$node->title']");
-    $node_meta = $this->xpath("//article[(@about='$url')]//span[contains(@property, 'dc:date') and contains(@property, 'dc:created') and @datatype='xsd:dateTime' and @content='$isoDate']");
-    $this->assertTrue(!empty($node_title), 'Property dc:title is present in meta tag.');
-    $this->assertTrue(!empty($node_meta), 'RDF type is present on post. Properties dc:date and dc:created are present on post date.');
+  function testMappingDefinitionPage() {
+    $node = $this->drupalCreateNode(array('type' => 'page'));
+
+    $expected_mapping = array(
+      'rdftype' => array('foaf:DocumentBar'),
+      'title' => array(
+        'predicates' => array('dc:title'),
+      ),
+      'body' => array(
+        'predicates' => array('dc:dummy-property'),
+      ),
+      'created' => array(
+        'predicates' => array('dc:dummy-date'),
+        'callback' => 'date_iso8601_foo',
+        'datatype' => 'xsd:dateTimeFoo',
+      ),
+    );
+    $node = node_load($node->nid);
+    foreach ($expected_mapping as $key => $mapping) {
+      $this->assertEqual($node->rdf_mapping[$key], $mapping, format_string('Expected mapping found for @key.', array('@key' => $key)));
+    }
   }
 
   /**
-   * Tests if RDF mapping defined in rdf_test.install is used.
-   *
    * Creates a content type and a node of type test_bundle_hook_install and
    * tests whether the RDF mapping defined in rdf_test.install is used.
    */
-  function testAttributesInMarkup2() {
-    $type = $this->drupalCreateContentType(array('type' => 'test_bundle_hook_install'));
+  function testMappingDefinitionTestBundleInstall() {
+    $this->drupalCreateContentType(array('type' => 'test_bundle_hook_install'));
     $node = $this->drupalCreateNode(array('type' => 'test_bundle_hook_install'));
-    $isoDate = date('c', $node->changed);
-    $url = url('node/' . $node->nid);
-    $this->drupalGet('node/' . $node->nid);
 
-    // Ensure the mapping defined in rdf_module.test is used.
-    $test_bundle_title = $this->xpath("//meta[@property='dc:title' and @content='$node->title']");
-    $test_bundle_meta = $this->xpath("//article[(@about='$url') and contains(@typeof, 'foo:mapping_install1') and contains(@typeof, 'bar:mapping_install2')]//span[contains(@property, 'dc:date') and contains(@property, 'dc:created') and @datatype='xsd:dateTime' and @content='$isoDate']");
-    $this->assertTrue(!empty($test_bundle_title), 'Property dc:title is present in meta tag.');
-    $this->assertTrue(!empty($test_bundle_meta), 'RDF type is present on post. Properties dc:date and dc:created are present on post date.');
+    $expected_mapping = array(
+      'rdftype' => array('foo:mapping_install1', 'bar:mapping_install2'),
+      'title' => array(
+        'predicates' => array('dc:title'),
+      ),
+      'body' => array(
+        'predicates' => array('content:encoded'),
+      ),
+      'created' => array(
+        'predicates' => array('dc:date', 'dc:created'),
+        'callback' => 'date_iso8601',
+        'datatype' => 'xsd:dateTime',
+      ),
+    );
+    $node = node_load($node->nid);
+    foreach ($expected_mapping as $key => $mapping) {
+      $this->assertEqual($node->rdf_mapping[$key], $mapping, format_string('Expected mapping found for @key.', array('@key' => $key)));
+    }
   }
 
   /**
-   * Tests if the default mapping for a node is being used.
-   *
    * Creates a random content type and node and ensures the default mapping for
    * the node is being used.
    */
-  function testAttributesInMarkup3() {
+  function testMappingDefinitionRandomContentType() {
     $type = $this->drupalCreateContentType();
     $node = $this->drupalCreateNode(array('type' => $type->type));
-    $isoDate = date('c', $node->changed);
-    $url = url('node/' . $node->nid);
-    $this->drupalGet('node/' . $node->nid);
-
-    // Ensure the default bundle mapping for node is used. These attributes come
-    // from the node default bundle definition.
-    $random_bundle_title = $this->xpath("//meta[@property='dc:title' and @content='$node->title']");
-    $random_bundle_meta = $this->xpath("//article[(@about='$url') and contains(@typeof, 'sioc:Item') and contains(@typeof, 'foaf:Document')]//span[contains(@property, 'dc:date') and contains(@property, 'dc:created') and @datatype='xsd:dateTime' and @content='$isoDate']");
-    $this->assertTrue(!empty($random_bundle_title), 'Property dc:title is present in meta tag.');
-    $this->assertTrue(!empty($random_bundle_meta), 'RDF type is present on post. Properties dc:date and dc:created are present on post date.');
-  }
-
-  /**
-   * Tests if default mapping for user is being used.
-   *
-   * Creates a random user and ensures the default mapping for the user is
-   * being used.
-   */
-  function testUserAttributesInMarkup() {
-    // Create two users, one with access to user profiles.
-    $user1 = $this->drupalCreateUser(array('access user profiles'));
-    $user2 = $this->drupalCreateUser();
-    $username = $user2->name;
-    $this->drupalLogin($user1);
-    // Browse to the user profile page.
-    $this->drupalGet('user/' . $user2->uid);
-    // Ensure the default bundle mapping for user is used on the user profile
-    // page. These attributes come from the user default bundle definition.
-    $account_uri = url('user/' . $user2->uid);
-    $person_uri = url('user/' . $user2->uid, array('fragment' => 'me'));
-
-    $user2_profile_about = $this->xpath('//article[@class="profile" and @typeof="sioc:UserAccount" and @about=:account-uri]', array(
-      ':account-uri' => $account_uri,
-    ));
-    $this->assertTrue(!empty($user2_profile_about), 'RDFa markup found on user profile page');
-
-    $user_account_holder = $this->xpath('//meta[contains(@typeof, "foaf:Person") and @about=:person-uri and @resource=:account-uri and contains(@rel, "foaf:account")]', array(
-      ':person-uri' => $person_uri,
-      ':account-uri' => $account_uri,
-    ));
-    $this->assertTrue(!empty($user_account_holder), 'URI created for account holder and username set on sioc:UserAccount.');
-
-    $user_username = $this->xpath('//meta[@about=:account-uri and contains(@property, "foaf:name") and @content=:username]', array(
-      ':account-uri' => $account_uri,
-      ':username' => $username,
-    ));
-    $this->assertTrue(!empty($user_username), 'foaf:name set on username.');
-
-    // User 2 creates node.
-    $this->drupalLogin($user2);
-    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
-    $this->drupalLogin($user1);
-    $this->drupalGet('node/' . $node->nid);
-    // Ensures the default bundle mapping for user is used on the Authored By
-    // information on the node.
-    $author_about = $this->xpath('//a[@typeof="sioc:UserAccount" and @about=:account-uri and @property="foaf:name" and @datatype="" and contains(@lang, "")]', array(
-      ':account-uri' => $account_uri,
-    ));
-    $this->assertTrue(!empty($author_about), 'RDFa markup found on author information on post. The lang attribute on username is set to empty string.');
-  }
-
-  /**
-   * Creates a random term and ensures the right RDFa markup is used.
-   */
-  function testTaxonomyTermRdfaAttributes() {
-    $vocabulary = $this->createVocabulary();
-    $term = $this->createTerm($vocabulary);
-
-    // Views the term and checks that the RDFa markup is correct.
-    $this->drupalGet('taxonomy/term/' . $term->tid);
-    $term_url = url('taxonomy/term/' . $term->tid);
-    $term_label = $term->label();
-    $term_rdfa_meta = $this->xpath('//meta[@typeof="skos:Concept" and @about=:term-url and contains(@property, "rdfs:label") and contains(@property, "skos:prefLabel") and @content=:term-label]', array(
-      ':term-url' => $term_url,
-      ':term-label' => $term_label,
-    ));
-    $this->assertTrue(!empty($term_rdfa_meta), 'RDFa markup found on term page.');
+    $expected_mapping = array(
+      'rdftype' => array('sioc:Item', 'foaf:Document'),
+      'title' => array(
+        'predicates' => array('dc:title'),
+      ),
+      'body' => array(
+        'predicates' => array('content:encoded'),
+      ),
+      'created' => array(
+        'predicates' => array('dc:date', 'dc:created'),
+        'callback' => 'date_iso8601',
+        'datatype' => 'xsd:dateTime',
+      ),
+    );
+    $node = node_load($node->nid);
+    foreach ($expected_mapping as $key => $mapping) {
+      $this->assertEqual($node->rdf_mapping[$key], $mapping, format_string('Expected mapping found for @key.', array('@key' => $key)));
+    }
   }
 }
diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/NodeAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/NodeAttributesTest.php
new file mode 100644
index 000000000000..e8df6658fdbb
--- /dev/null
+++ b/core/modules/rdf/lib/Drupal/rdf/Tests/NodeAttributesTest.php
@@ -0,0 +1,81 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\rdf\Tests\NodeAttributesTest.
+ */
+
+namespace Drupal\rdf\Tests;
+
+use Drupal\node\Tests\NodeTestBase;
+
+/**
+ * Tests the RDFa markup of Nodes.
+ */
+class NodeAttributesTest extends NodeTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array('rdf');
+
+  public static function getInfo() {
+    return array(
+      'name' => 'RDFa markup for nodes',
+      'description' => 'Tests the RDFa markup of nodes.',
+      'group' => 'RDF',
+    );
+  }
+
+  /**
+   * Creates a node of type article and tests its RDFa markup.
+   */
+  function testNodeAttributes() {
+    $node = $this->drupalCreateNode(array('type' => 'article'));
+
+    $node_uri = url('node/' . $node->nid, array('absolute' => TRUE));
+    $base_uri = url('<front>', array('absolute' => TRUE));
+
+    // Parses front page where the node is displayed in its teaser form.
+    $parser = new \EasyRdf_Parser_Rdfa();
+    $graph = new \EasyRdf_Graph();
+    $parser->parse($graph, $this->drupalGet('node/' . $node->nid), 'rdfa', $base_uri);
+
+    // Inspects RDF graph output.
+    // Node type.
+    $expected_value = array(
+      'type' => 'uri',
+      'value' => 'http://rdfs.org/sioc/ns#Item',
+    );
+    $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Node type found in RDF output (sioc:Item).');
+    // Node type.
+    $expected_value = array(
+      'type' => 'uri',
+      'value' => 'http://xmlns.com/foaf/0.1/Document',
+    );
+    $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Node type found in RDF output (foaf:Document).');
+    // Node title.
+    $expected_value = array(
+      'type' => 'literal',
+      'value' => $node->title,
+      'lang' => 'en',
+    );
+    $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Node title found in RDF output (dc:title).');
+    // Node date.
+    $expected_value = array(
+      'type' => 'literal',
+      'value' => date('c', $node->created),
+      'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
+    );
+    $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Node date found in RDF output (dc:date).');
+    // Node date.
+    $expected_value = array(
+      'type' => 'literal',
+      'value' => date('c', $node->created),
+      'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
+    );
+    $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Node date found in RDF output (dc:created).');
+  }
+}
diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/TaxonomyAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/TaxonomyAttributesTest.php
new file mode 100644
index 000000000000..83e6ec992260
--- /dev/null
+++ b/core/modules/rdf/lib/Drupal/rdf/Tests/TaxonomyAttributesTest.php
@@ -0,0 +1,68 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\rdf\Tests\TaxonomyAttributesTest.
+ */
+
+namespace Drupal\rdf\Tests;
+
+use Drupal\taxonomy\Tests\TaxonomyTestBase;
+
+/**
+ * Tests the RDFa markup of Taxonomy terms.
+ */
+class TaxonomyAttributesTest extends TaxonomyTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array('rdf');
+
+  public static function getInfo() {
+    return array(
+      'name' => 'RDFa markup for taxonomy terms',
+      'description' => 'Tests the RDFa markup of taxonomy terms.',
+      'group' => 'RDF',
+    );
+  }
+
+  /**
+   * Creates a random term and ensures the RDF output is correct.
+   */
+  function testTaxonomyTermRdfaAttributes() {
+    $vocabulary = $this->createVocabulary();
+    $term = $this->createTerm($vocabulary);
+    $term_uri = url('taxonomy/term/' . $term->tid, array('absolute' => TRUE));
+
+    // Parses the term's page and checks that the RDF output is correct.
+    $parser = new \EasyRdf_Parser_Rdfa();
+    $graph = new \EasyRdf_Graph();
+    $base_uri = url('<front>', array('absolute' => TRUE));
+    $parser->parse($graph, $this->drupalGet('taxonomy/term/' . $term->tid), 'rdfa', $base_uri);
+
+    // Inspects RDF graph output.
+    // Term type.
+    $expected_value = array(
+      'type' => 'uri',
+      'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
+    );
+    $this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Term type found in RDF output (skos:Concept).');
+    // Term label.
+    $expected_value = array(
+      'type' => 'literal',
+      'value' => $term->label(),
+      'lang' => 'en',
+    );
+    $this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Term label found in RDF output (rdfs:label).');
+    // Term label.
+    $expected_value = array(
+      'type' => 'literal',
+      'value' => $term->label(),
+      'lang' => 'en',
+    );
+    $this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2004/02/skos/core#prefLabel', $expected_value), 'Term label found in RDF output (skos:prefLabel).');
+  }
+}
-- 
GitLab