diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-04-22 21:41:09 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-04-22 21:41:09 +0000 |
commit | cca6d06c01e377975e8625b6743d069e08b38684 (patch) | |
tree | 77eda58d8cbf06a52125399f5c586c30988a902c | |
parent | 164b9a3268e20c7e9ee792009ea5ee803f592bd4 (diff) | |
download | brdo-cca6d06c01e377975e8625b6743d069e08b38684.tar.gz brdo-cca6d06c01e377975e8625b6743d069e08b38684.tar.bz2 |
#721082 by scor, linclark: Fixed Prevent conflicting namespaces and move hook_rdf_namespaces() invocation into rdf.module.
-rw-r--r-- | includes/common.inc | 11 | ||||
-rw-r--r-- | modules/rdf/rdf.module | 26 | ||||
-rw-r--r-- | modules/rdf/rdf.test | 37 | ||||
-rw-r--r-- | modules/rdf/tests/rdf_test.module | 11 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 32 |
5 files changed, 111 insertions, 6 deletions
diff --git a/includes/common.inc b/includes/common.inc index d039702ba..96bd470f0 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -250,14 +250,17 @@ function drupal_get_breadcrumb() { } /** - * Return a string containing RDF namespace declarations for use in XML and + * Returns a string containing RDF namespace declarations for use in XML and * XHTML output. */ function drupal_get_rdf_namespaces() { - // Serialize the RDF namespaces used in RDFa annotation. $xml_rdf_namespaces = array(); - foreach (module_invoke_all('rdf_namespaces') as $prefix => $uri) { - $xml_rdf_namespaces[] = 'xmlns:' . $prefix . '="' . $uri . '"'; + + // Serializes the RDF namespaces in XML namespace syntax. + if (function_exists('rdf_get_namespaces')) { + foreach (rdf_get_namespaces() as $prefix => $uri) { + $xml_rdf_namespaces[] = 'xmlns:' . $prefix . '="' . $uri . '"'; + } } return implode("\n ", $xml_rdf_namespaces); } diff --git a/modules/rdf/rdf.module b/modules/rdf/rdf.module index b7d5b4884..4602fae17 100644 --- a/modules/rdf/rdf.module +++ b/modules/rdf/rdf.module @@ -92,6 +92,32 @@ function rdf_rdf_namespaces() { } /** + * Returns an array of RDF namespaces defined via hook_rdf_namespaces(). + */ +function rdf_get_namespaces() { + $rdf_namespaces = module_invoke_all('rdf_namespaces'); + // module_invoke_all() uses array_merge_recursive() which might return nested + // arrays if several modules redefine the same prefix multiple times. + // We need to ensure the array of namespaces is flat and only contains + // strings as URIs. + foreach ($rdf_namespaces as $prefix => $uri) { + if (is_array($uri)) { + if (count(array_unique($uri)) == 1) { + // All namespaces declared for this prefix are the same, merge them all + // into a single namespace. + $rdf_namespaces[$prefix] = $uri[0]; + } + else { + // There are conflicting namespaces for this prefix, do not include it + // in order to avoid asserting any inaccurate RDF statement. + unset($rdf_namespaces[$prefix]); + } + } + } + return $rdf_namespaces; +} + +/** * Returns the mapping for attributes of a given type/bundle pair. * * @param $type diff --git a/modules/rdf/rdf.test b/modules/rdf/rdf.test index 64f6fbd7a..127f6a40e 100644 --- a/modules/rdf/rdf.test +++ b/modules/rdf/rdf.test @@ -39,7 +39,10 @@ class RdfMappingHookTestCase extends DrupalWebTestCase { } } -class RdfMarkupTestCase extends DrupalWebTestCase { +/** + * Test RDFa markup generation. + */ +class RdfRdfaMarkupTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'RDFa markup', @@ -55,7 +58,7 @@ class RdfMarkupTestCase extends DrupalWebTestCase { /** * Test rdf_rdfa_attributes(). */ - function testDrupalRdfaAtributes() { + function testDrupalRdfaAttributes() { // Same value as the one in the HTML tag (no callback function). $expected_attributes = array( 'property' => array('dc:title'), @@ -525,3 +528,33 @@ class RdfTrackerAttributesTestCase extends DrupalWebTestCase { $this->assertTrue(!empty($tracker_activity), t('Latest activity date found when there are comments on @user content. Latest activity date content is correct.', array('@user'=> $user))); } } + +/** + * Tests for RDF namespaces declaration with hook_rdf_namespaces(). + */ +class RdfGetRdfNamespacesTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'RDF namespaces', + 'description' => 'Test hook_rdf_namespaces() and ensure only "safe" namespaces are returned.', + 'group' => 'RDF', + ); + } + + function setUp() { + parent::setUp('rdf', 'rdf_test'); + } + + /** + * Test getting RDF namesapces. + */ + function testGetRdfNamespaces() { + // Get all RDF namespaces. + $ns = rdf_get_namespaces(); + + $this->assertEqual($ns['owl'], 'http://www.w3.org/2002/07/owl#', t('A prefix declared once is included.')); + $this->assertEqual($ns['foaf'], 'http://xmlns.com/foaf/0.1/', t('The same prefix declared in several implementations of hook_rdf_namespaces() is valid as long as all the namespaces are the same.')); + $this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', t('Two prefixes can be assigned the same namespace.')); + $this->assertTrue(!isset($ns['dc']), t('A prefix with conflicting namespaces is discarded.')); + } +} diff --git a/modules/rdf/tests/rdf_test.module b/modules/rdf/tests/rdf_test.module index dedd2fd80..0c8c3b2a1 100644 --- a/modules/rdf/tests/rdf_test.module +++ b/modules/rdf/tests/rdf_test.module @@ -54,3 +54,14 @@ function rdf_test_rdf_mapping() { ), ); } + +/** + * Implements hook_rdf_namespaces(). + */ +function rdf_test_rdf_namespaces() { + return array( + 'dc' => 'http://purl.org/conflicting/namespace', + 'foaf' => 'http://xmlns.com/foaf/0.1/', + 'foaf1' => 'http://xmlns.com/foaf/0.1/', + ); +} diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 9eaefe168..cdec4ed98 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1891,3 +1891,35 @@ class DrupalJSONTest extends DrupalUnitTestCase { $this->assertIdentical($source, $json_decoded, t('Encoding structured data to JSON and decoding back results in the original data.')); } } + +/** + * Tests for RDF namespaces XML serialization. + */ +class DrupalGetRdfNamespacesTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'RDF namespaces XML serialization tests', + 'description' => 'Confirm that the serialization of RDF namespaces via drupal_get_rdf_namespaces() is output and parsed correctly in the XHTML document.', + 'group' => 'System', + ); + } + + function setUp() { + parent::setUp('rdf', 'rdf_test'); + } + + /** + * Test RDF namespaces. + */ + function testGetRdfNamespaces() { + // Fetches the front page and extracts XML namespaces. + $this->drupalGet(''); + $xml = new SimpleXMLElement($this->content); + $ns = $xml->getDocNamespaces(); + + $this->assertEqual($ns['owl'], 'http://www.w3.org/2002/07/owl#', t('A prefix declared once is displayed.')); + $this->assertEqual($ns['foaf'], 'http://xmlns.com/foaf/0.1/', t('The same prefix declared in several implementations of hook_rdf_namespaces() is valid as long as all the namespaces are the same.')); + $this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', t('Two prefixes can be assigned the same namespace.')); + $this->assertTrue(!isset($ns['dc']), t('A prefix with conflicting namespaces is discarded.')); + } +} |