Commit d63062ab authored by alexpott's avatar alexpott

Issue #1778410 by linclark, ashepherd, Stefan Freudenberg, tim.plunkett,...

Issue #1778410 by linclark, ashepherd, Stefan Freudenberg, tim.plunkett, kay_v, adorsk: Throw exception when RDF namespaces collide.
parent 2020cb49
...@@ -24,7 +24,7 @@ class GetRdfNamespacesTest extends WebTestBase { ...@@ -24,7 +24,7 @@ class GetRdfNamespacesTest extends WebTestBase {
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'RDF namespaces', 'name' => 'RDF namespaces',
'description' => 'Test hook_rdf_namespaces() and ensure only "safe" namespaces are returned.', 'description' => 'Test hook_rdf_namespaces().',
'group' => 'RDF', 'group' => 'RDF',
); );
} }
...@@ -39,6 +39,16 @@ function testGetRdfNamespaces() { ...@@ -39,6 +39,16 @@ function testGetRdfNamespaces() {
$this->assertEqual($ns['rdfs'], 'http://www.w3.org/2000/01/rdf-schema#', 'A prefix declared once is included.'); $this->assertEqual($ns['rdfs'], 'http://www.w3.org/2000/01/rdf-schema#', 'A prefix declared once is included.');
$this->assertEqual($ns['foaf'], 'http://xmlns.com/foaf/0.1/', '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['foaf'], 'http://xmlns.com/foaf/0.1/', '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/', 'Two prefixes can be assigned the same namespace.'); $this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', 'Two prefixes can be assigned the same namespace.');
$this->assertEqual($ns['dc'], 'http://purl.org/dc/terms/', 'When a prefix has conflicting namespaces, the first declared one is used.');
// Enable rdf_conflicting_namespaces to ensure that an exception is thrown
// when RDF namespaces are conflicting.
\Drupal::moduleHandler()->install(array('rdf_conflicting_namespaces'), TRUE);
try {
$ns = rdf_get_namespaces();
$this->fail('Expected exception not thrown for conflicting namespace declaration.');
}
catch (\Exception $e) {
$this->pass('Expected exception thrown: ' . $e->getMessage());
}
} }
} }
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
* Enables semantically enriched output for Drupal sites in the form of RDFa. * Enables semantically enriched output for Drupal sites in the form of RDFa.
*/ */
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Template\Attribute; use Drupal\Core\Template\Attribute;
use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface;
...@@ -113,8 +112,13 @@ function rdf_get_namespaces() { ...@@ -113,8 +112,13 @@ function rdf_get_namespaces() {
// namespace, do not use \Drupal::moduleHandler()->invokeAll(). // namespace, do not use \Drupal::moduleHandler()->invokeAll().
foreach (\Drupal::moduleHandler()->getImplementations('rdf_namespaces') as $module) { foreach (\Drupal::moduleHandler()->getImplementations('rdf_namespaces') as $module) {
$function = $module . '_rdf_namespaces'; $function = $module . '_rdf_namespaces';
if (function_exists($function)) { foreach($function() as $prefix => $namespace) {
$namespaces = NestedArray::mergeDeep($function(), $namespaces); if (array_key_exists($prefix, $namespaces) && $namespace !== $namespaces[$prefix]) {
throw new Exception(t('Tried to map @prefix to @namespace, but @prefix is already mapped to @orig_namespace.', array('@prefix' => $prefix, '@namespace' => $namespace, '@orig_namespace' => $namespaces[$prefix])));
}
else {
$namespaces[$prefix] = $namespace;
}
} }
} }
return $namespaces; return $namespaces;
......
name: 'RDF module conflicting namespaces test'
type: module
description: 'Test conflicting namespace declaration.'
package: Testing
version: VERSION
core: 8.x
hidden: true
dependencies:
- rdf
<?php
/**
* @file
* Test the namespace registration functionality.
*/
/**
* Implements hook_rdf_namespaces().
*/
function rdf_conflicting_namespaces_rdf_namespaces() {
return array(
'dc' => 'http://purl.org/conflicting/namespace',
);
}
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
*/ */
function rdf_test_namespaces_rdf_namespaces() { function rdf_test_namespaces_rdf_namespaces() {
return array( return array(
'dc' => 'http://purl.org/conflicting/namespace',
'foaf' => 'http://xmlns.com/foaf/0.1/', 'foaf' => 'http://xmlns.com/foaf/0.1/',
'foaf1' => 'http://xmlns.com/foaf/0.1/', 'foaf1' => 'http://xmlns.com/foaf/0.1/',
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment