Version 1
[yaffs-website] / web / core / modules / rdf / tests / src / Functional / UserAttributesTest.php
1 <?php
2
3 namespace Drupal\Tests\rdf\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests the RDFa markup of Users.
9  *
10  * @group rdf
11  */
12 class UserAttributesTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['rdf', 'node'];
20
21   protected function setUp() {
22     parent::setUp();
23     rdf_get_mapping('user', 'user')
24       ->setBundleMapping([
25         'types' => ['sioc:UserAccount'],
26       ])
27       ->setFieldMapping('name', [
28         'properties' => ['foaf:name'],
29       ])
30       ->save();
31   }
32
33   /**
34    * Tests if default mapping for user is being used.
35    *
36    * Creates a random user and ensures the default mapping for the user is
37    * being used.
38    */
39   public function testUserAttributesInMarkup() {
40     // Creates users that should and should not be truncated
41     // by template_preprocess_username (20 characters)
42     // one of these users tests right on the cusp (20).
43     $user1 = $this->drupalCreateUser(['access user profiles']);
44
45     $authors = [
46       $this->drupalCreateUser([], $this->randomMachineName(30)),
47       $this->drupalCreateUser([], $this->randomMachineName(20)),
48       $this->drupalCreateUser([], $this->randomMachineName(5))
49     ];
50
51     $this->drupalLogin($user1);
52
53     $this->drupalCreateContentType(['type' => 'article']);
54
55     /** @var \Drupal\user\UserInterface[] $authors */
56     foreach ($authors as $author) {
57       $account_uri = $author->url('canonical', ['absolute' => TRUE]);
58
59       // Parses the user profile page where the default bundle mapping for user
60       // should be used.
61       $parser = new \EasyRdf_Parser_Rdfa();
62       $graph = new \EasyRdf_Graph();
63       $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
64       $parser->parse($graph, $this->drupalGet('user/' . $author->id()), 'rdfa', $base_uri);
65
66       // Inspects RDF graph output.
67       // User type.
68       $expected_value = [
69         'type' => 'uri',
70         'value' => 'http://rdfs.org/sioc/ns#UserAccount',
71       ];
72       $this->assertTrue($graph->hasProperty($account_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'User type found in RDF output (sioc:UserAccount).');
73       // User name.
74       $expected_value = [
75         'type' => 'literal',
76         'value' => $author->getUsername(),
77       ];
78       $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
79
80       // User creates a node.
81       $this->drupalLogin($author);
82       $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
83       $this->drupalLogin($user1);
84
85       // Parses the node created by the user.
86       $parser = new \EasyRdf_Parser_Rdfa();
87       $graph = new \EasyRdf_Graph();
88       $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
89       $parser->parse($graph, $this->drupalGet('node/' . $node->id()), 'rdfa', $base_uri);
90
91       // Ensures the default bundle mapping for user is used on the Authored By
92       // information on the node.
93       $expected_value = [
94         'type' => 'uri',
95         'value' => 'http://rdfs.org/sioc/ns#UserAccount',
96       ];
97       $this->assertTrue($graph->hasProperty($account_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'User type found in RDF output (sioc:UserAccount).');
98       // User name.
99       $expected_value = [
100         'type' => 'literal',
101         'value' => $author->getUsername(),
102       ];
103       $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
104
105     }
106   }
107
108 }