From 3001e93a139025a39efa77a3e3480ff9e35fd54b Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 4 Mar 2014 12:50:20 +0000
Subject: [PATCH] Issue #1992954 by lokapujya, sphism, cwells, djevans, Stefan
 Freudenberg, Gaelan: Trimming of long usernames results in incorrect RDF.

---
 .../Drupal/rdf/Tests/UserAttributesTest.php   | 103 ++++++++++--------
 core/modules/rdf/rdf.module                   |   5 +
 core/modules/user/user.module                 |   5 +
 3 files changed, 66 insertions(+), 47 deletions(-)

diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/UserAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/UserAttributesTest.php
index 7d2a099da7de..847d89306395 100644
--- a/core/modules/rdf/lib/Drupal/rdf/Tests/UserAttributesTest.php
+++ b/core/modules/rdf/lib/Drupal/rdf/Tests/UserAttributesTest.php
@@ -48,59 +48,68 @@ public function setUp() {
    * being used.
    */
   function testUserAttributesInMarkup() {
-    // Creates two users, one with access to user profiles.
+    // Creates users that should and should not be truncated
+    // by template_preprocess_username (20 characters)
+    // one of these users tests right on the cusp (20).
     $user1 = $this->drupalCreateUser(array('access user profiles'));
-    $user2 = $this->drupalCreateUser();
+
+    $authors = array(
+      $this->drupalCreateUser(array(), $this->randomName(30)),
+      $this->drupalCreateUser(array(), $this->randomName(20)),
+      $this->drupalCreateUser(array(), $this->randomName(5))
+    );
+
     $this->drupalLogin($user1);
 
-    $account_uri = url('user/' . $user2->id(), array('absolute' => TRUE));
+    foreach($authors as $author) {
+      $account_uri = url('user/' . $author->id(), array('absolute' => TRUE));
 
-    // Parses the user profile page where the default bundle mapping for user
-    // should be used.
-    $parser = new \EasyRdf_Parser_Rdfa();
-    $graph = new \EasyRdf_Graph();
-    $base_uri = url('<front>', array('absolute' => TRUE));
-    $parser->parse($graph, $this->drupalGet('user/' . $user2->id()), 'rdfa', $base_uri);
+      // Parses the user profile page where the default bundle mapping for user
+      // should be used.
+      $parser = new \EasyRdf_Parser_Rdfa();
+      $graph = new \EasyRdf_Graph();
+      $base_uri = url('<front>', array('absolute' => TRUE));
+      $parser->parse($graph, $this->drupalGet('user/' . $author->id()), 'rdfa', $base_uri);
 
-    // Inspects RDF graph output.
-    // User type.
-    $expected_value = array(
-      'type' => 'uri',
-      'value' => 'http://rdfs.org/sioc/ns#UserAccount',
-    );
-    $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).');
-    // User name.
-    $expected_value = array(
-      'type' => 'literal',
-      'value' => $user2->getUsername(),
-    );
-    $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
+      // Inspects RDF graph output.
+      // User type.
+      $expected_value = array(
+        'type' => 'uri',
+        'value' => 'http://rdfs.org/sioc/ns#UserAccount',
+      );
+      $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).');
+      // User name.
+      $expected_value = array(
+        'type' => 'literal',
+        'value' => $author->getUsername(),
+      );
+      $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
 
-    // User 2 creates a node.
-    $this->drupalLogin($user2);
-    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
-    $this->drupalLogin($user1);
+      // User creates a node.
+      $this->drupalLogin($author);
+      $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
+      $this->drupalLogin($user1);
 
-    // Parses the user profile page where the default bundle mapping for user
-    // should be used.
-    $parser = new \EasyRdf_Parser_Rdfa();
-    $graph = new \EasyRdf_Graph();
-    $base_uri = url('<front>', array('absolute' => TRUE));
-    $parser->parse($graph, $this->drupalGet('node/' . $node->id()), 'rdfa', $base_uri);
-
-    // Ensures the default bundle mapping for user is used on the Authored By
-    // information on the node.
-    // User type.
-    $expected_value = array(
-      'type' => 'uri',
-      'value' => 'http://rdfs.org/sioc/ns#UserAccount',
-    );
-    $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).');
-    // User name.
-    $expected_value = array(
-      'type' => 'literal',
-      'value' => $user2->getUsername(),
-    );
-    $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
+      // Parses the node created by the user.
+      $parser = new \EasyRdf_Parser_Rdfa();
+      $graph = new \EasyRdf_Graph();
+      $base_uri = url('<front>', array('absolute' => TRUE));
+      $parser->parse($graph, $this->drupalGet('node/' . $node->id()), 'rdfa', $base_uri);
+
+      // Ensures the default bundle mapping for user is used on the Authored By
+      // information on the node.
+      $expected_value = array(
+        'type' => 'uri',
+        'value' => 'http://rdfs.org/sioc/ns#UserAccount',
+      );
+      $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).');
+      // User name.
+      $expected_value = array(
+        'type' => 'literal',
+        'value' => $author->getUsername(),
+      );
+      $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
+
+    }
   }
 }
diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module
index 1ef9d486173a..f925304538ef 100644
--- a/core/modules/rdf/rdf.module
+++ b/core/modules/rdf/rdf.module
@@ -414,6 +414,11 @@ function rdf_preprocess_username(&$variables) {
   if (!empty($variables['homepage']) && !empty($homepage_mapping)) {
     $attributes['rel'] = $homepage_mapping['properties'];
   }
+  // Long usernames are truncated by template_preprocess_username(). Store the
+  // full name in the content attribute so it can be extracted in RDFa.
+  if ($variables['truncated']) {
+    $attributes['content'] = check_plain($variables['name_raw']);
+  }
   // The remaining attributes can have multiple values listed, with whitespace
   // separating the values in the RDFa attributes
   // (see http://www.w3.org/TR/rdfa-syntax/#rdfa-attributes).
diff --git a/core/modules/user/user.module b/core/modules/user/user.module
index c8b51b7d0137..a944b30adc16 100644
--- a/core/modules/user/user.module
+++ b/core/modules/user/user.module
@@ -628,6 +628,10 @@ function template_preprocess_username(&$variables) {
   $name = $variables['name_raw'] = $account->getUsername();
   if (drupal_strlen($name) > 20) {
     $name = drupal_substr($name, 0, 15) . '...';
+    $variables['truncated'] = TRUE;
+  }
+  else {
+    $variables['truncated'] = FALSE;
   }
   $variables['name'] = check_plain($name);
   $variables['profile_access'] = \Drupal::currentUser()->hasPermission('access user profiles');
@@ -659,6 +663,7 @@ function template_preprocess_username(&$variables) {
  *   An associative array containing:
  *   - account: The user object to format.
  *   - name: The user's name, sanitized.
+ *   - truncated: A boolean indicating if $variables['name'] has been shortened.
  *   - extra: Additional text to append to the user's name, sanitized.
  *   - link_path: The path or URL of the user's profile page, home page, or
  *     other desired page to link to for more information about the user.
-- 
GitLab