0b162a4a905556f5b485e4c12af43f0624635960
[yaffs-website] / web / core / modules / rdf / tests / src / Functional / GetRdfNamespacesTest.php
1 <?php
2
3 namespace Drupal\Tests\rdf\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests hook_rdf_namespaces().
9  *
10  * @group rdf
11  */
12 class GetRdfNamespacesTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['rdf', 'rdf_test_namespaces'];
20
21   /**
22    * Tests getting RDF namespaces.
23    */
24   public function testGetRdfNamespaces() {
25     // Fetches the front page and extracts RDFa 1.1 prefixes.
26     $this->drupalGet('');
27
28     // We have to use the find() method on the driver directly because //html is
29     // prepended to all xpath queries otherwise.
30     $driver = $this->getSession()->getDriver();
31
32     $element = $driver->find('//html[contains(@prefix, "rdfs: http://www.w3.org/2000/01/rdf-schema#")]');
33     $this->assertCount(1, $element, 'A prefix declared once is displayed.');
34
35     $element = $driver->find('//html[contains(@prefix, "foaf: http://xmlns.com/foaf/0.1/")]');
36     $this->assertCount(1, $element, 'The same prefix declared in several implementations of hook_rdf_namespaces() is valid as long as all the namespaces are the same.');
37
38     $element = $driver->find('//html[contains(@prefix, "foaf1: http://xmlns.com/foaf/0.1/")]');
39     $this->assertCount(1, $element, 'Two prefixes can be assigned the same namespace.');
40
41     $element = $driver->find('//html[contains(@prefix, "dc: http://purl.org/dc/terms/")]');
42     $this->assertCount(1, $element, 'When a prefix has conflicting namespaces, the first declared one is used.');
43
44     // Get all RDF namespaces.
45     $ns = rdf_get_namespaces();
46
47     $this->assertEqual($ns['rdfs'], 'http://www.w3.org/2000/01/rdf-schema#', 'A prefix declared once is included.');
48     $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.');
49     $this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', 'Two prefixes can be assigned the same namespace.');
50
51     // Enable rdf_conflicting_namespaces to ensure that an exception is thrown
52     // when RDF namespaces are conflicting.
53     \Drupal::service('module_installer')->install(['rdf_conflicting_namespaces'], TRUE);
54     try {
55       $ns = rdf_get_namespaces();
56       $this->fail('Expected exception not thrown for conflicting namespace declaration.');
57     }
58     catch (\Exception $e) {
59       $this->pass('Expected exception thrown: ' . $e->getMessage());
60     }
61   }
62
63 }