Commit 3001e93a authored by alexpott's avatar alexpott

Issue #1992954 by lokapujya, sphism, cwells, djevans, Stefan Freudenberg,...

Issue #1992954 by lokapujya, sphism, cwells, djevans, Stefan Freudenberg, Gaelan: Trimming of long usernames results in incorrect RDF.
parent d63062ab
......@@ -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).');
}
}
}
......@@ -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).
......
......@@ -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.
......
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