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