Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
29
core/modules/rdf/config/schema/rdf.data_types.schema.yml
Normal file
29
core/modules/rdf/config/schema/rdf.data_types.schema.yml
Normal file
|
@ -0,0 +1,29 @@
|
|||
rdf_field_mapping:
|
||||
type: mapping
|
||||
label: 'RDF mapping'
|
||||
mapping:
|
||||
properties:
|
||||
type: sequence
|
||||
label: 'Properties'
|
||||
sequence:
|
||||
type: string
|
||||
datatype:
|
||||
type: string
|
||||
label: 'Data type'
|
||||
datatype_callback:
|
||||
type: mapping
|
||||
label: 'Data type callback'
|
||||
mapping:
|
||||
callable:
|
||||
type: string
|
||||
label: 'Callable'
|
||||
arguments:
|
||||
type: mapping
|
||||
label: 'Arguments'
|
||||
mapping:
|
||||
interaction_type:
|
||||
type: string
|
||||
label: 'Interaction type'
|
||||
mapping_type:
|
||||
type: string
|
||||
label: 'Mapping type'
|
25
core/modules/rdf/config/schema/rdf.schema.yml
Normal file
25
core/modules/rdf/config/schema/rdf.schema.yml
Normal file
|
@ -0,0 +1,25 @@
|
|||
# Schema for the configuration files of the RDF module.
|
||||
|
||||
rdf.mapping.*.*:
|
||||
type: config_entity
|
||||
label: 'RDF mapping'
|
||||
mapping:
|
||||
id:
|
||||
type: string
|
||||
label: 'ID'
|
||||
targetEntityType:
|
||||
type: string
|
||||
label: 'Target entity type'
|
||||
bundle:
|
||||
type: string
|
||||
label: 'Bundle'
|
||||
types:
|
||||
type: sequence
|
||||
label: 'Types'
|
||||
sequence:
|
||||
type: string
|
||||
fieldMappings:
|
||||
type: sequence
|
||||
label: 'Field mappings'
|
||||
sequence:
|
||||
type: rdf_field_mapping
|
43
core/modules/rdf/rdf.api.php
Normal file
43
core/modules/rdf/rdf.api.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Hooks provided by the RDF module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup hooks
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Allow modules to define namespaces for RDF mappings.
|
||||
*
|
||||
* Many common namespace prefixes are defined in rdf_rdf_namespaces(). However,
|
||||
* if a module implements rdf mappings that use prefixes that are not
|
||||
* defined in rdf_rdf_namespaces(), this hook should be used to define the new
|
||||
* namespace prefixes.
|
||||
*
|
||||
* @return
|
||||
* An associative array of namespaces where the key is the namespace prefix
|
||||
* and the value is the namespace URI.
|
||||
*
|
||||
* @ingroup rdf
|
||||
*/
|
||||
function hook_rdf_namespaces() {
|
||||
return array(
|
||||
'content' => 'http://purl.org/rss/1.0/modules/content/',
|
||||
'dc' => 'http://purl.org/dc/terms/',
|
||||
'foaf' => 'http://xmlns.com/foaf/0.1/',
|
||||
'og' => 'http://ogp.me/ns#',
|
||||
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
|
||||
'sioc' => 'http://rdfs.org/sioc/ns#',
|
||||
'sioct' => 'http://rdfs.org/sioc/types#',
|
||||
'skos' => 'http://www.w3.org/2004/02/skos/core#',
|
||||
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup hooks".
|
||||
*/
|
6
core/modules/rdf/rdf.info.yml
Normal file
6
core/modules/rdf/rdf.info.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
name: RDF
|
||||
type: module
|
||||
description: 'Enriches your content with metadata to let other applications (e.g. search engines, aggregators) better understand its relationships and attributes.'
|
||||
package: Core
|
||||
version: VERSION
|
||||
core: 8.x
|
575
core/modules/rdf/rdf.module
Normal file
575
core/modules/rdf/rdf.module
Normal file
|
@ -0,0 +1,575 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Enables semantically enriched output for Drupal sites in the form of RDFa.
|
||||
*/
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Template\Attribute;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function rdf_help($route_name, RouteMatchInterface $route_match) {
|
||||
switch ($route_name) {
|
||||
case 'help.page.rdf':
|
||||
$output = '';
|
||||
$output .= '<h3>' . t('About') . '</h3>';
|
||||
$output .= '<p>' . t('The RDF module enriches your content with metadata to let other applications (e.g., search engines, aggregators, and so on) better understand its relationships and attributes. This semantically enriched, machine-readable output for your website uses the <a href="!rdfa">RDFa specification</a>, which allows RDF data to be embedded in HTML markup. Other modules can define mappings of their data to RDF terms, and the RDF module makes this RDF data available to the theme. The core modules define RDF mappings for their data model, and the core themes output this RDF metadata information along with the human-readable visual information. For more information, see the <a href="!rdf">online documentation for the RDF module</a>.', array('!rdfa' => 'http://www.w3.org/TR/xhtml-rdfa-primer/', '!rdf' => 'https://www.drupal.org/documentation/modules/rdf')) . '</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @defgroup rdf RDF Mapping API
|
||||
* @{
|
||||
* Functions to describe entities and bundles in RDF.
|
||||
*
|
||||
* The RDF module introduces RDF and RDFa to Drupal. RDF is a W3C standard to
|
||||
* describe structured data. RDF can be serialized as RDFa in XHTML attributes
|
||||
* to augment visual data with machine-readable hints.
|
||||
* @see http://www.w3.org/RDF/
|
||||
* @see http://www.w3.org/TR/xhtml-rdfa-primer/
|
||||
*
|
||||
* Modules can provide mappings of their bundles' data and metadata to RDF
|
||||
* classes and properties. This module takes care of injecting these mappings
|
||||
* into variables available to theme functions and templates. All Drupal core
|
||||
* themes are coded to be RDFa compatible.
|
||||
*/
|
||||
/**
|
||||
* Returns the RDF mapping object associated to a bundle.
|
||||
*
|
||||
* The function reads the rdf_mapping object from the current configuration,
|
||||
* or returns a ready-to-use empty one if no configuration entry exists yet for
|
||||
* this bundle. This streamlines the manipulation of mapping objects by always
|
||||
* returning a consistent object that reflects the current state of the
|
||||
* configuration.
|
||||
*
|
||||
* Example usage:
|
||||
* -Map the 'article' bundle to 'sioc:Post' and the 'title' field to 'dc:title'.
|
||||
* @code
|
||||
* rdf_get_mapping('node', 'article')
|
||||
* ->setBundleMapping(array(
|
||||
* 'types' => array('sioc:Post'),
|
||||
* ))
|
||||
* ->setFieldMapping('title', array(
|
||||
* 'properties' => array('dc:title')
|
||||
* ))
|
||||
* ->save();
|
||||
* @endcode
|
||||
*
|
||||
* @param string $entity_type
|
||||
* The entity type.
|
||||
* @param string $bundle
|
||||
* The bundle.
|
||||
*
|
||||
* @return \Drupal\rdf\Entity\RdfMapping
|
||||
* The RdfMapping object.
|
||||
*/
|
||||
function rdf_get_mapping($entity_type, $bundle) {
|
||||
// Try loading the mapping from configuration.
|
||||
$mapping = entity_load('rdf_mapping', $entity_type . '.' . $bundle);
|
||||
|
||||
// If not found, create a fresh mapping object.
|
||||
if (!$mapping) {
|
||||
$mapping = entity_create('rdf_mapping', array(
|
||||
'targetEntityType' => $entity_type,
|
||||
'bundle' => $bundle,
|
||||
));
|
||||
}
|
||||
|
||||
return $mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_rdf_namespaces().
|
||||
*/
|
||||
function rdf_rdf_namespaces() {
|
||||
return array(
|
||||
'content' => 'http://purl.org/rss/1.0/modules/content/',
|
||||
'dc' => 'http://purl.org/dc/terms/',
|
||||
'foaf' => 'http://xmlns.com/foaf/0.1/',
|
||||
'og' => 'http://ogp.me/ns#',
|
||||
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
|
||||
'schema' => 'http://schema.org/',
|
||||
'sioc' => 'http://rdfs.org/sioc/ns#',
|
||||
'sioct' => 'http://rdfs.org/sioc/types#',
|
||||
'skos' => 'http://www.w3.org/2004/02/skos/core#',
|
||||
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves RDF namespaces.
|
||||
*
|
||||
* Invokes hook_rdf_namespaces() and collects RDF namespaces from modules that
|
||||
* implement it.
|
||||
*/
|
||||
function rdf_get_namespaces() {
|
||||
$namespaces = array();
|
||||
// In order to resolve duplicate namespaces by using the earliest defined
|
||||
// namespace, do not use \Drupal::moduleHandler()->invokeAll().
|
||||
foreach (\Drupal::moduleHandler()->getImplementations('rdf_namespaces') as $module) {
|
||||
$function = $module . '_rdf_namespaces';
|
||||
foreach($function() as $prefix => $namespace) {
|
||||
if (array_key_exists($prefix, $namespaces) && $namespace !== $namespaces[$prefix]) {
|
||||
throw new Exception(t('Tried to map @prefix to @namespace, but @prefix is already mapped to @orig_namespace.', array('@prefix' => $prefix, '@namespace' => $namespace, '@orig_namespace' => $namespaces[$prefix])));
|
||||
}
|
||||
else {
|
||||
$namespaces[$prefix] = $namespace;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $namespaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "defgroup rdf".
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup rdf
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Builds an array of RDFa attributes for a given mapping.
|
||||
*
|
||||
* This array will typically be passed through Drupal\Core\Template\Attribute
|
||||
* to create the attributes variables that are available to template files.
|
||||
* These include $attributes, $title_attributes, $content_attributes and the
|
||||
* field-specific $item_attributes variables.
|
||||
*
|
||||
* @param array $mapping
|
||||
* An array containing a mandatory 'properties' key and optional 'datatype',
|
||||
* 'datatype_callback' and 'type' keys. For example:
|
||||
* @code
|
||||
* array(
|
||||
* 'properties' => array('schema:interactionCount'),
|
||||
* 'datatype' => 'xsd:integer',
|
||||
* 'datatype_callback' => array(
|
||||
* 'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
|
||||
* 'arguments' => array(
|
||||
* 'interaction_type' => 'UserComments'
|
||||
* ),
|
||||
* ),
|
||||
* );
|
||||
* @endcode
|
||||
* @param mixed $data
|
||||
* (optional) A value that needs to be converted by the provided callback
|
||||
* function.
|
||||
*
|
||||
* @return array
|
||||
* RDFa attributes suitable for Drupal\Core\Template\Attribute.
|
||||
*/
|
||||
function rdf_rdfa_attributes($mapping, $data = NULL) {
|
||||
$attributes = array();
|
||||
|
||||
// The type of mapping defaults to 'property'.
|
||||
$type = isset($mapping['mapping_type']) ? $mapping['mapping_type'] : 'property';
|
||||
|
||||
switch ($type) {
|
||||
// The mapping expresses the relationship between two resources.
|
||||
case 'rel':
|
||||
case 'rev':
|
||||
$attributes[$type] = $mapping['properties'];
|
||||
break;
|
||||
|
||||
// The mapping expresses the relationship between a resource and some
|
||||
// literal text.
|
||||
case 'property':
|
||||
if (!empty($mapping['properties'])) {
|
||||
$attributes['property'] = $mapping['properties'];
|
||||
// Convert $data to a specific format as per the callback function.
|
||||
if (isset($data) && !empty($mapping['datatype_callback'])) {
|
||||
$callback = $mapping['datatype_callback']['callable'];
|
||||
$arguments = isset($mapping['datatype_callback']['arguments']) ? $mapping['datatype_callback']['arguments'] : NULL;
|
||||
$attributes['content'] = call_user_func($callback, $data, $arguments);
|
||||
}
|
||||
if (isset($mapping['datatype'])) {
|
||||
$attributes['datatype'] = $mapping['datatype'];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup rdf".
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_entity_prepare_view().
|
||||
*/
|
||||
function rdf_entity_prepare_view($entity_type, array $entities, array $displays) {
|
||||
// Iterate over the RDF mappings for each entity and prepare the RDFa
|
||||
// attributes to be added inside field formatters.
|
||||
foreach ($entities as $entity) {
|
||||
$mapping = rdf_get_mapping($entity_type, $entity->bundle());
|
||||
// Only prepare the RDFa attributes for the fields which are configured to
|
||||
// be displayed.
|
||||
foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) {
|
||||
$field_mapping = $mapping->getPreparedFieldMapping($name);
|
||||
if ($field_mapping) {
|
||||
foreach ($entity->get($name) as $item) {
|
||||
$item->_attributes += rdf_rdfa_attributes($field_mapping, $item->toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_ENTITY_TYPE_storage_load() for comment entities.
|
||||
*/
|
||||
function rdf_comment_storage_load($comments) {
|
||||
foreach ($comments as $comment) {
|
||||
// Pages with many comments can show poor performance. This information
|
||||
// isn't needed until rdf_preprocess_comment() is called, but set it here
|
||||
// to optimize performance for websites that implement an entity cache.
|
||||
$created_mapping = rdf_get_mapping('comment', $comment->bundle())
|
||||
->getPreparedFieldMapping('created');
|
||||
$comment->rdf_data['date'] = rdf_rdfa_attributes($created_mapping, $comment->get('created')->first()->toArray());
|
||||
$entity = $comment->getCommentedEntity();
|
||||
$comment->rdf_data['entity_uri'] = $entity->url();
|
||||
if ($comment->hasParentComment()) {
|
||||
$comment->rdf_data['pid_uri'] = $comment->getParentComment()->url();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function rdf_theme() {
|
||||
return array(
|
||||
'rdf_metadata' => array(
|
||||
'variables' => array('metadata' => array()),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_HOOK() for HTML document templates.
|
||||
*/
|
||||
function rdf_preprocess_html(&$variables) {
|
||||
// Adds RDF namespace prefix bindings in the form of an RDFa 1.1 prefix
|
||||
// attribute inside the html element.
|
||||
if (!isset($variables['html_attributes']['prefix'])) {
|
||||
$variables['html_attributes']['prefix'] = array();
|
||||
}
|
||||
foreach(rdf_get_namespaces() as $prefix => $uri) {
|
||||
$variables['html_attributes']['prefix'][] = $prefix . ': ' . $uri . " ";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_HOOK() for node templates.
|
||||
*/
|
||||
function rdf_preprocess_node(&$variables) {
|
||||
// Adds RDFa markup to the node container. The about attribute specifies the
|
||||
// URI of the resource described within the HTML element, while the @typeof
|
||||
// attribute indicates its RDF type (e.g., foaf:Document, sioc:Person, and so
|
||||
// on.)
|
||||
$bundle = $variables['node']->bundle();
|
||||
$mapping = rdf_get_mapping('node', $bundle);
|
||||
$bundle_mapping = $mapping->getPreparedBundleMapping('node', $bundle);
|
||||
$variables['attributes']['about'] = empty($variables['url']) ? NULL: $variables['url'];
|
||||
$variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types'];
|
||||
|
||||
// Adds RDFa markup for the node title as metadata because wrapping the title
|
||||
// with markup is not reliable and the title output is different depending on
|
||||
// the view mode (e.g. full vs. teaser).
|
||||
$title_mapping = $mapping->getPreparedFieldMapping('title');
|
||||
if ($title_mapping) {
|
||||
$title_attributes['property'] = empty($title_mapping['properties']) ? NULL : $title_mapping['properties'];
|
||||
$title_attributes['content'] = $variables['node']->label();
|
||||
$variables['title_suffix']['rdf_meta_title'] = array(
|
||||
'#theme' => 'rdf_metadata',
|
||||
'#metadata' => array($title_attributes),
|
||||
);
|
||||
}
|
||||
|
||||
// Adds RDFa markup for the relation between the node and its author.
|
||||
$author_mapping = $mapping->getPreparedFieldMapping('uid');
|
||||
if (!empty($author_mapping['properties']) && $variables['display_submitted']) {
|
||||
$variables['author_attributes']['rel'] = $author_mapping['properties'];
|
||||
}
|
||||
|
||||
// Adds RDFa markup for the date.
|
||||
$created_mapping = $mapping->getPreparedFieldMapping('created');
|
||||
if (!empty($created_mapping) && $variables['display_submitted']) {
|
||||
$date_attributes = rdf_rdfa_attributes($created_mapping, $variables['node']->get('created')->first()->toArray());
|
||||
$rdf_metadata = array(
|
||||
'#theme' => 'rdf_metadata',
|
||||
'#metadata' => array($date_attributes),
|
||||
);
|
||||
$variables['metadata'] = drupal_render($rdf_metadata);
|
||||
}
|
||||
|
||||
// Adds RDFa markup annotating the number of comments a node has.
|
||||
if (\Drupal::moduleHandler()->moduleExists('comment') && \Drupal::currentUser()->hasPermission('access comments')) {
|
||||
$comment_count_mapping = $mapping->getPreparedFieldMapping('comment_count');
|
||||
if (!empty($comment_count_mapping['properties'])) {
|
||||
$fields = array_keys(\Drupal::service('comment.manager')->getFields('node'));
|
||||
$definitions = array_keys($variables['node']->getFieldDefinitions());
|
||||
$valid_fields = array_intersect($fields, $definitions);
|
||||
$count = 0;
|
||||
foreach ($valid_fields as $field_name) {
|
||||
$count += $variables['node']->get($field_name)->comment_count;
|
||||
// Adds RDFa markup for the comment count near the node title as
|
||||
// metadata.
|
||||
$comment_count_attributes = rdf_rdfa_attributes($comment_count_mapping, $variables['node']->get($field_name)->comment_count);
|
||||
$variables['title_suffix']['rdf_meta_comment_count'] = array(
|
||||
'#theme' => 'rdf_metadata',
|
||||
'#metadata' => array($comment_count_attributes),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_HOOK() for user templates.
|
||||
*/
|
||||
function rdf_preprocess_user(&$variables) {
|
||||
/** @var $account \Drupal\user\UserInterface */
|
||||
$account = $variables['elements']['#user'];
|
||||
$uri = $account->urlInfo();
|
||||
$mapping = rdf_get_mapping('user', 'user');
|
||||
$bundle_mapping = $mapping->getPreparedBundleMapping();
|
||||
|
||||
// Adds RDFa markup to the user profile page. Fields displayed in this page
|
||||
// will automatically describe the user.
|
||||
if (!empty($bundle_mapping['types'])) {
|
||||
$variables['attributes']['typeof'] = $bundle_mapping['types'];
|
||||
$variables['attributes']['about'] = $account->url();
|
||||
}
|
||||
// If we are on the user account page, add the relationship between the
|
||||
// sioc:UserAccount and the foaf:Person who holds the account.
|
||||
if (\Drupal::routeMatch()->getRouteName() == $uri->getRouteName()) {
|
||||
// Adds the markup for username as language neutral literal, see
|
||||
// rdf_preprocess_username().
|
||||
$name_mapping = $mapping->getPreparedFieldMapping('name');
|
||||
if (!empty($name_mapping['properties'])) {
|
||||
$username_meta = array(
|
||||
'#tag' => 'meta',
|
||||
'#attributes' => array(
|
||||
'about' => $account->url(),
|
||||
'property' => $name_mapping['properties'],
|
||||
'content' => $account->getUsername(),
|
||||
'lang' => '',
|
||||
),
|
||||
);
|
||||
$variables['#attached']['html_head'][] = [$username_meta, 'rdf_user_username'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_HOOK() for username.html.twig.
|
||||
*/
|
||||
function rdf_preprocess_username(&$variables) {
|
||||
// Because lang is set on the HTML element that wraps the page, the
|
||||
// username inherits this language attribute. However, since the username
|
||||
// might not be transliterated to the same language that the content is in,
|
||||
// we do not want it to inherit the language attribute, so we set the
|
||||
// attribute to an empty string.
|
||||
if (empty($variables['attributes']['lang'])) {
|
||||
$variables['attributes']['lang'] = '';
|
||||
}
|
||||
|
||||
// The profile URI is used to identify the user account. The about attribute
|
||||
// is used to set the URI as the default subject of the properties embedded
|
||||
// as RDFa in the child elements. Even if the user profile is not accessible
|
||||
// to the current user, we use its URI in order to identify the user in RDF.
|
||||
// We do not use this attribute for the anonymous user because we do not have
|
||||
// a user profile URI for it (only a homepage which cannot be used as user
|
||||
// profile in RDF.)
|
||||
if ($variables['uid'] > 0) {
|
||||
$variables['attributes']['about'] = \Drupal::url('entity.user.canonical', ['user' => $variables['uid']]);
|
||||
}
|
||||
|
||||
// Add RDF type of user.
|
||||
$mapping = rdf_get_mapping('user', 'user');
|
||||
$bundle_mapping = $mapping->getPreparedBundleMapping();
|
||||
if (!empty($bundle_mapping['types'])) {
|
||||
$variables['attributes']['typeof'] = $bundle_mapping['types'];
|
||||
}
|
||||
// Annotate the username in RDFa. A property attribute is used with an empty
|
||||
// datatype attribute to ensure the username is parsed as a plain literal
|
||||
// in RDFa 1.0 and 1.1.
|
||||
$name_mapping = $mapping->getPreparedFieldMapping('name');
|
||||
if (!empty($name_mapping)) {
|
||||
$variables['attributes']['property'] = $name_mapping['properties'];
|
||||
$variables['attributes']['datatype'] = '';
|
||||
}
|
||||
// Add the homepage RDFa markup if present.
|
||||
$homepage_mapping = $mapping->getPreparedFieldMapping('homepage');
|
||||
if (!empty($variables['homepage']) && !empty($homepage_mapping)) {
|
||||
$variables['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']) {
|
||||
$variables['attributes']['content'] = SafeMarkup::checkPlain($variables['name_raw']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_HOOK() for comment templates.
|
||||
*/
|
||||
function rdf_preprocess_comment(&$variables) {
|
||||
$comment = $variables['comment'];
|
||||
$mapping = rdf_get_mapping('comment', $comment->bundle());
|
||||
$bundle_mapping = $mapping->getPreparedBundleMapping();
|
||||
|
||||
if (!empty($bundle_mapping['types']) && !isset($comment->in_preview)) {
|
||||
// Adds RDFa markup to the comment container. The about attribute specifies
|
||||
// the URI of the resource described within the HTML element, while the
|
||||
// typeof attribute indicates its RDF type (e.g., sioc:Post, foaf:Document,
|
||||
// and so on.)
|
||||
$variables['attributes']['about'] = $comment->url();
|
||||
$variables['attributes']['typeof'] = $bundle_mapping['types'];
|
||||
}
|
||||
|
||||
// Adds RDFa markup for the relation between the comment and its author.
|
||||
$author_mapping = $mapping->getPreparedFieldMapping('uid');
|
||||
if (!empty($author_mapping)) {
|
||||
$author_attributes = array('rel' => $author_mapping['properties']);
|
||||
// Wraps the author variable and the submitted variable which are both
|
||||
// available in comment.html.twig.
|
||||
$variables['author'] = SafeMarkup::set('<span ' . new Attribute($author_attributes) . '>' . $variables['author'] . '</span>');
|
||||
$variables['submitted'] = SafeMarkup::set('<span ' . new Attribute($author_attributes) . '>' . $variables['submitted'] . '</span>');
|
||||
}
|
||||
// Adds RDFa markup for the date of the comment.
|
||||
$created_mapping = $mapping->getPreparedFieldMapping('created');
|
||||
if (!empty($created_mapping)) {
|
||||
// The comment date is precomputed as part of the rdf_data so that it can be
|
||||
// cached as part of the entity.
|
||||
$date_attributes = $comment->rdf_data['date'];
|
||||
|
||||
$rdf_metadata = array(
|
||||
'#theme' => 'rdf_metadata',
|
||||
'#metadata' => array($date_attributes),
|
||||
);
|
||||
$created_metadata_markup = drupal_render($rdf_metadata);
|
||||
// Appends the markup to the created variable and the submitted variable
|
||||
// which are both available in comment.html.twig.
|
||||
$variables['created'] = SafeMarkup::set(SafeMarkup::escape($variables['created']) . $created_metadata_markup);
|
||||
$variables['submitted'] = SafeMarkup::set($variables['submitted'] . $created_metadata_markup);
|
||||
}
|
||||
$title_mapping = $mapping->getPreparedFieldMapping('subject');
|
||||
if (!empty($title_mapping)) {
|
||||
// Adds RDFa markup to the subject of the comment. Because the RDFa markup
|
||||
// is added to an <h3> tag which might contain HTML code, we specify an
|
||||
// empty datatype to ensure the value of the title read by the RDFa parsers
|
||||
// is a literal.
|
||||
$variables['title_attributes']['property'] = $title_mapping['properties'];
|
||||
$variables['title_attributes']['datatype'] = '';
|
||||
}
|
||||
|
||||
// Annotates the parent relationship between the current comment and the node
|
||||
// it belongs to. If available, the parent comment is also annotated.
|
||||
// @todo When comments are turned into fields, this should be changed.
|
||||
// Currently there is no mapping relating a comment to its node.
|
||||
$pid_mapping = $mapping->getPreparedFieldMapping('pid');
|
||||
if (!empty($pid_mapping)) {
|
||||
// Adds the relation to the parent entity.
|
||||
$parent_entity_attributes['rel'] = $pid_mapping['properties'];
|
||||
// The parent entity URI is precomputed as part of the rdf_data so that it
|
||||
// can be cached as part of the entity.
|
||||
$parent_entity_attributes['resource'] = $comment->rdf_data['entity_uri'];
|
||||
$variables['rdf_metadata_attributes'][] = $parent_entity_attributes;
|
||||
|
||||
// Adds the relation to parent comment, if it exists.
|
||||
if ($comment->hasParentComment()) {
|
||||
$parent_comment_attributes['rel'] = $pid_mapping['properties'];
|
||||
// The parent comment URI is precomputed as part of the rdf_data so that
|
||||
// it can be cached as part of the entity.
|
||||
$parent_comment_attributes['resource'] = $comment->rdf_data['pid_uri'];
|
||||
$variables['rdf_metadata_attributes'][] = $parent_comment_attributes;
|
||||
}
|
||||
}
|
||||
// Adds RDF metadata markup above comment body if any.
|
||||
if (!empty($variables['rdf_metadata_attributes']) && isset($variables['content']['comment_body'])) {
|
||||
$rdf_metadata = array(
|
||||
'#theme' => 'rdf_metadata',
|
||||
'#metadata' => $variables['rdf_metadata_attributes'],
|
||||
);
|
||||
if (!empty($variables['content']['comment_body']['#prefix'])) {
|
||||
$rdf_metadata['#suffix'] = $variables['content']['comment_body']['#prefix'];
|
||||
}
|
||||
$variables['content']['comment_body']['#prefix'] = drupal_render($rdf_metadata);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_HOOK() for taxonomy term templates.
|
||||
*/
|
||||
function rdf_preprocess_taxonomy_term(&$variables) {
|
||||
// Adds RDFa markup to the taxonomy term container.
|
||||
// The @about attribute specifies the URI of the resource described within
|
||||
// the HTML element, while the @typeof attribute indicates its RDF type
|
||||
// (e.g., schema:Thing, skos:Concept, and so on).
|
||||
$term = $variables['term'];
|
||||
$mapping = rdf_get_mapping('taxonomy_term', $term->bundle());
|
||||
$bundle_mapping = $mapping->getPreparedBundleMapping();
|
||||
$variables['attributes']['about'] = $term->url();
|
||||
$variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types'];
|
||||
|
||||
// Add RDFa markup for the taxonomy term name as metadata, if present.
|
||||
$name_field_mapping = $mapping->getPreparedFieldMapping('name');
|
||||
if (!empty($name_field_mapping) && !empty($name_field_mapping['properties'])) {
|
||||
$name_attributes = array(
|
||||
'property' => $name_field_mapping['properties'],
|
||||
'content' => $term->getName(),
|
||||
);
|
||||
$variables['title_suffix']['taxonomy_term_rdfa'] = array(
|
||||
'#theme' => 'rdf_metadata',
|
||||
'#metadata' => array($name_attributes),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_HOOK() for image.html.twig.
|
||||
*/
|
||||
function rdf_preprocess_image(&$variables) {
|
||||
// Adds the RDF type for image. We cannot use the usual entity-based mapping
|
||||
// to get 'foaf:Image' because image does not have its own entity type or
|
||||
// bundle.
|
||||
$variables['attributes']['typeof'] = array('foaf:Image');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares variables for RDF metadata templates.
|
||||
*
|
||||
* Default template: rdf-metadata.html.twig.
|
||||
*
|
||||
* Sometimes it is useful to export data which is not semantically present in
|
||||
* the HTML output. For example, a hierarchy of comments is visible for a human
|
||||
* but not for machines because this hierarchy is not present in the DOM tree.
|
||||
* We can express it in RDFa via empty <span> tags. These aren't visible and
|
||||
* give machines extra information about the content and its structure.
|
||||
*
|
||||
* @param array $variables
|
||||
* An associative array containing:
|
||||
* - metadata: An array of attribute arrays. Each item in the array
|
||||
* corresponds to its own set of attributes, and therefore, needs its own
|
||||
* element.
|
||||
*/
|
||||
function template_preprocess_rdf_metadata(&$variables) {
|
||||
foreach ($variables['metadata'] as $key => $attributes) {
|
||||
if (!is_null($attributes)) {
|
||||
$variables['metadata'][$key] = new Attribute($attributes);
|
||||
}
|
||||
else {
|
||||
$variables['metadata'][$key] = new Attribute();
|
||||
}
|
||||
}
|
||||
}
|
40
core/modules/rdf/src/CommonDataConverter.php
Normal file
40
core/modules/rdf/src/CommonDataConverter.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\CommonDataConverter.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf;
|
||||
|
||||
/**
|
||||
* Contains methods for common data conversions.
|
||||
*/
|
||||
class CommonDataConverter {
|
||||
|
||||
/**
|
||||
* Provides a passthrough to place unformatted values in content attributes.
|
||||
*
|
||||
* @param mixed $data
|
||||
* The data to be placed in the content attribute.
|
||||
*
|
||||
* @return mixed
|
||||
* Returns the data.
|
||||
*/
|
||||
public static function rawValue($data) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a date entity field array into an ISO 8601 timestamp string.
|
||||
*
|
||||
* @param array $data
|
||||
* The array containing the 'value' element.
|
||||
*
|
||||
* @return string
|
||||
* Returns the ISO 8601 timestamp.
|
||||
*/
|
||||
public static function dateIso8601Value($data) {
|
||||
return date_iso8601($data['value']);
|
||||
}
|
||||
|
||||
}
|
167
core/modules/rdf/src/Entity/RdfMapping.php
Normal file
167
core/modules/rdf/src/Entity/RdfMapping.php
Normal file
|
@ -0,0 +1,167 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Entity\RdfMapping.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\rdf\RdfMappingInterface;
|
||||
|
||||
/**
|
||||
* Config entity for working with RDF mappings.
|
||||
*
|
||||
* @ConfigEntityType(
|
||||
* id = "rdf_mapping",
|
||||
* label = @Translation("RDF mapping"),
|
||||
* config_prefix = "mapping",
|
||||
* entity_keys = {
|
||||
* "id" = "id"
|
||||
* },
|
||||
* config_export = {
|
||||
* "id",
|
||||
* "targetEntityType",
|
||||
* "bundle",
|
||||
* "types",
|
||||
* "fieldMappings",
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class RdfMapping extends ConfigEntityBase implements RdfMappingInterface {
|
||||
|
||||
/**
|
||||
* Unique ID for the config entity.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* Entity type to be mapped.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $targetEntityType;
|
||||
|
||||
/**
|
||||
* Bundle to be mapped.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $bundle;
|
||||
|
||||
/**
|
||||
* The RDF type mapping for this bundle.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $types = array();
|
||||
|
||||
/**
|
||||
* The mappings for fields on this bundle.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fieldMappings = array();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPreparedBundleMapping() {
|
||||
return array('types' => $this->types);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBundleMapping() {
|
||||
if (!empty($this->types)) {
|
||||
return array('types' => $this->types);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setBundleMapping(array $mapping) {
|
||||
if (isset($mapping['types'])) {
|
||||
$this->types = $mapping['types'];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPreparedFieldMapping($field_name) {
|
||||
$field_mapping = array(
|
||||
'properties' => NULL,
|
||||
'datatype' => NULL,
|
||||
'datatype_callback' => NULL,
|
||||
'mapping_type' => NULL,
|
||||
);
|
||||
if (isset($this->fieldMappings[$field_name])) {
|
||||
$field_mapping = array_merge($field_mapping, $this->fieldMappings[$field_name]);
|
||||
}
|
||||
return empty($field_mapping['properties']) ? array() : $field_mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldMapping($field_name) {
|
||||
if (isset($this->fieldMappings[$field_name])) {
|
||||
return $this->fieldMappings[$field_name];
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setFieldMapping($field_name, array $mapping = array()) {
|
||||
$this->fieldMappings[$field_name] = $mapping;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function id() {
|
||||
return $this->targetEntityType . '.' . $this->bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
parent::calculateDependencies();
|
||||
$entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType);
|
||||
$this->addDependency('module', $entity_type->getProvider());
|
||||
$bundle_entity_type_id = $entity_type->getBundleEntityType();
|
||||
if ($bundle_entity_type_id != 'bundle') {
|
||||
// If the target entity type uses entities to manage its bundles then
|
||||
// depend on the bundle entity.
|
||||
$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($this->bundle);
|
||||
$this->addDependency('config', $bundle_entity->getConfigDependencyName());
|
||||
}
|
||||
return $this->dependencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
||||
parent::postSave($storage, $update);
|
||||
|
||||
if (\Drupal::entityManager()->hasHandler($this->targetEntityType, 'view_builder')) {
|
||||
\Drupal::entityManager()->getViewBuilder($this->targetEntityType)->resetCache();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
110
core/modules/rdf/src/RdfMappingInterface.php
Normal file
110
core/modules/rdf/src/RdfMappingInterface.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\RdfMappingInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
|
||||
/**
|
||||
* Provides an interface defining an RDF mapping entity.
|
||||
*/
|
||||
interface RdfMappingInterface extends ConfigEntityInterface {
|
||||
|
||||
/**
|
||||
* Gets the mapping for the bundle-level data.
|
||||
*
|
||||
* The prepared bundle mapping should be used when outputting data in RDF
|
||||
* serializations such as RDFa. In the prepared mapping, the mapping
|
||||
* configuration's CURIE arrays are processed into CURIE strings suitable for
|
||||
* output.
|
||||
*
|
||||
* @return array
|
||||
* The bundle mapping.
|
||||
*/
|
||||
public function getPreparedBundleMapping();
|
||||
|
||||
/**
|
||||
* Gets the mapping config for the bundle-level data.
|
||||
*
|
||||
* This function returns the bundle mapping as stored in config, which may
|
||||
* contain CURIE arrays. If the mapping is needed for output in a
|
||||
* serialization format, such as RDFa, then getPreparedBundleMapping() should
|
||||
* be used instead.
|
||||
*
|
||||
* @return array
|
||||
* The bundle mapping, or an empty array if there is no mapping.
|
||||
*/
|
||||
public function getBundleMapping();
|
||||
|
||||
/**
|
||||
* Sets the mapping config for the bundle-level data.
|
||||
*
|
||||
* This only sets bundle-level mappings, such as the RDF type. Mappings for
|
||||
* a bundle's fields should be handled with setFieldMapping.
|
||||
*
|
||||
* Example usage:
|
||||
* -Map the 'article' bundle to 'sioc:Post'.
|
||||
* @code
|
||||
* rdf_get_mapping('node', 'article')
|
||||
* ->setBundleMapping(array(
|
||||
* 'types' => array('sioc:Post'),
|
||||
* ))
|
||||
* ->save();
|
||||
* @endcode
|
||||
*
|
||||
* @param array $mapping
|
||||
* The bundle mapping.
|
||||
*
|
||||
* @return \Drupal\rdf\Entity\RdfMapping
|
||||
* The RdfMapping object.
|
||||
*/
|
||||
public function setBundleMapping(array $mapping);
|
||||
|
||||
/**
|
||||
* Gets the prepared mapping for a field.
|
||||
*
|
||||
* The prepared field mapping should be used when outputting data in RDF
|
||||
* serializations such as RDFa. In the prepared mapping, the mapping
|
||||
* configuration's CURIE arrays are processed into CURIE strings suitable for
|
||||
* output.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The name of the field.
|
||||
*
|
||||
* @return array
|
||||
* The prepared field mapping, or an empty array if there is no mapping.
|
||||
*/
|
||||
public function getPreparedFieldMapping($field_name);
|
||||
|
||||
/**
|
||||
* Gets the mapping config for a field.
|
||||
*
|
||||
* This function returns the field mapping as stored in config, which may
|
||||
* contain CURIE arrays. If the mapping is needed for output in a
|
||||
* serialization format, such as RDFa, then getPreparedFieldMapping() should
|
||||
* be used instead.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The name of the field.
|
||||
*
|
||||
* @return array
|
||||
* The field mapping config array, or an empty array if there is no mapping.
|
||||
*/
|
||||
public function getFieldMapping($field_name);
|
||||
|
||||
/**
|
||||
* Sets the mapping config for a field.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The name of the field.
|
||||
* @param array $mapping
|
||||
* The field mapping.
|
||||
*
|
||||
* @return \Drupal\rdf\Entity\RdfMapping
|
||||
* The RdfMapping object.
|
||||
*/
|
||||
public function setFieldMapping($field_name, array $mapping = array());
|
||||
}
|
33
core/modules/rdf/src/SchemaOrgDataConverter.php
Normal file
33
core/modules/rdf/src/SchemaOrgDataConverter.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\SchemaOrgDataConverter.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf;
|
||||
|
||||
class SchemaOrgDataConverter {
|
||||
|
||||
/**
|
||||
* Converts an interaction count to a string with the interaction type.
|
||||
*
|
||||
* Schema.org defines a number of different interaction types.
|
||||
*
|
||||
* @param int $count
|
||||
* The interaction count.
|
||||
* @param array $arguments
|
||||
* An array of arguments defined in the mapping.
|
||||
* Expected keys are:
|
||||
* - interaction_type: The string to use for the type of interaction
|
||||
* (e.g. UserComments).
|
||||
*
|
||||
* @return string
|
||||
* The formatted string.
|
||||
*
|
||||
* @see http://schema.org/UserInteraction
|
||||
*/
|
||||
static function interactionCount($count, $arguments) {
|
||||
$interaction_type = $arguments['interaction_type'];
|
||||
return "$interaction_type:$count";
|
||||
}
|
||||
}
|
361
core/modules/rdf/src/Tests/CommentAttributesTest.php
Normal file
361
core/modules/rdf/src/Tests/CommentAttributesTest.php
Normal file
|
@ -0,0 +1,361 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\CommentAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\comment\Tests\CommentTestBase;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the RDFa markup of comments.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class CommentAttributesTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('views', 'node', 'comment', 'rdf');
|
||||
|
||||
/**
|
||||
* URI of the front page of the Drupal site.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $baseUri;
|
||||
|
||||
/**
|
||||
* URI of the test node created by CommentTestBase::setUp().
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $nodeUri;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Enables anonymous user comments.
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => TRUE,
|
||||
'post comments' => TRUE,
|
||||
'skip comment approval' => TRUE,
|
||||
));
|
||||
// Allows anonymous to leave their contact information.
|
||||
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
|
||||
$this->setCommentPreview(DRUPAL_OPTIONAL);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentSettings('comment_default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
|
||||
// Prepares commonly used URIs.
|
||||
$this->baseUri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
|
||||
$this->nodeUri = $this->node->url('canonical', ['absolute' => TRUE]);
|
||||
|
||||
// Set relation between node and comment.
|
||||
$article_mapping = rdf_get_mapping('node', 'article');
|
||||
$comment_count_mapping = array(
|
||||
'properties' => array('sioc:num_replies'),
|
||||
'datatype' => 'xsd:integer',
|
||||
'datatype_callback' => array('callable' => 'Drupal\rdf\CommonDataConverter::rawValue'),
|
||||
);
|
||||
$article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save();
|
||||
|
||||
// Save user mapping.
|
||||
$user_mapping = rdf_get_mapping('user', 'user');
|
||||
$username_mapping = array(
|
||||
'properties' => array('foaf:name'),
|
||||
);
|
||||
$user_mapping->setFieldMapping('name', $username_mapping)->save();
|
||||
$user_mapping->setFieldMapping('homepage', array('properties' => array('foaf:page'), 'mapping_type' => 'rel'))->save();
|
||||
|
||||
// Save comment mapping.
|
||||
$mapping = rdf_get_mapping('comment', 'comment');
|
||||
$mapping->setBundleMapping(array('types' => array('sioc:Post', 'sioct:Comment')))->save();
|
||||
$field_mappings = array(
|
||||
'subject' => array(
|
||||
'properties' => array('dc:title'),
|
||||
),
|
||||
'created' => array(
|
||||
'properties' => array('dc:date', 'dc:created'),
|
||||
'datatype' => 'xsd:dateTime',
|
||||
'datatype_callback' => array('callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'),
|
||||
),
|
||||
'changed' => array(
|
||||
'properties' => array('dc:modified'),
|
||||
'datatype' => 'xsd:dateTime',
|
||||
'datatype_callback' => array('callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'),
|
||||
),
|
||||
'comment_body' => array(
|
||||
'properties' => array('content:encoded'),
|
||||
),
|
||||
'pid' => array(
|
||||
'properties' => array('sioc:reply_of'),
|
||||
'mapping_type' => 'rel',
|
||||
),
|
||||
'uid' => array(
|
||||
'properties' => array('sioc:has_creator'),
|
||||
'mapping_type' => 'rel',
|
||||
),
|
||||
'name' => array(
|
||||
'properties' => array('foaf:name'),
|
||||
),
|
||||
);
|
||||
// Iterate over shared field mappings and save.
|
||||
foreach ($field_mappings as $field_name => $field_mapping) {
|
||||
$mapping->setFieldMapping($field_name, $field_mapping)->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the presence of the RDFa markup for the number of comments.
|
||||
*/
|
||||
public function testNumberOfCommentsRdfaMarkup() {
|
||||
// Posts 2 comments on behalf of registered user.
|
||||
$this->saveComment($this->node->id(), $this->webUser->id());
|
||||
$this->saveComment($this->node->id(), $this->webUser->id());
|
||||
|
||||
// Tests number of comments in teaser view.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node'), 'rdfa', $this->baseUri);
|
||||
|
||||
// Number of comments.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => 2,
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#integer',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of teaser view (sioc:num_replies).');
|
||||
|
||||
// Tests number of comments in full node view, expected value is the same.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
|
||||
$this->assertTrue($graph->hasProperty($this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of full node view mode (sioc:num_replies).');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if RDFa markup for meta information is present in comments.
|
||||
*
|
||||
* Tests presence of RDFa markup for the title, date and author and homepage
|
||||
* on comments from registered and anonymous users.
|
||||
*/
|
||||
public function testCommentRdfaMarkup() {
|
||||
// Posts comment #1 on behalf of registered user.
|
||||
$comment1 = $this->saveComment($this->node->id(), $this->webUser->id());
|
||||
|
||||
// Tests comment #1 with access to the user profile.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment1);
|
||||
|
||||
// Tests comment #1 with no access to the user profile (as anonymous user).
|
||||
$this->drupalLogout();
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment1);
|
||||
|
||||
// Posts comment #2 as anonymous user.
|
||||
$anonymous_user = array();
|
||||
$anonymous_user['name'] = $this->randomMachineName();
|
||||
$anonymous_user['mail'] = 'tester@simpletest.org';
|
||||
$anonymous_user['homepage'] = 'http://example.org/';
|
||||
$comment2 = $this->saveComment($this->node->id(), 0, $anonymous_user);
|
||||
|
||||
// Tests comment #2 as anonymous user.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
|
||||
|
||||
// Tests comment #2 as logged in user.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests RDF comment replies.
|
||||
*/
|
||||
public function testCommentReplyOfRdfaMarkup() {
|
||||
// Posts comment #1 on behalf of registered user.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$comment_1 = $this->saveComment($this->node->id(), $this->webUser->id());
|
||||
|
||||
$comment_1_uri = $comment_1->url('canonical', ['absolute' => TRUE]);
|
||||
|
||||
// Posts a reply to the first comment.
|
||||
$comment_2 = $this->saveComment($this->node->id(), $this->webUser->id(), NULL, $comment_1->id());
|
||||
$comment_2_uri = $comment_2->url('canonical', ['absolute' => TRUE]);
|
||||
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
|
||||
|
||||
// Tests the reply_of relationship of a first level comment.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->nodeUri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_1_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its node found in RDF output (sioc:reply_of).');
|
||||
|
||||
// Tests the reply_of relationship of a second level comment.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->nodeUri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_2_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its node found in RDF output (sioc:reply_of).');
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $comment_1_uri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_2_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its parent comment found in RDF output (sioc:reply_of).');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for testCommentRdfaMarkup().
|
||||
*
|
||||
* Tests the current page for basic comment RDFa markup.
|
||||
*
|
||||
* @param $comment
|
||||
* Comment object.
|
||||
* @param $account
|
||||
* An array containing information about an anonymous user.
|
||||
*/
|
||||
function _testBasicCommentRdfaMarkup($graph, CommentInterface $comment, $account = array()) {
|
||||
$comment_uri = $comment->url('canonical', array('absolute' => TRUE));
|
||||
|
||||
// Comment type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://rdfs.org/sioc/types#Comment',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioct:Comment).');
|
||||
// Comment type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://rdfs.org/sioc/ns#Post',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioc:Post).');
|
||||
|
||||
// Comment title.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $comment->getSubject(),
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');
|
||||
|
||||
// Comment date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date('c', $comment->getCreatedTime()),
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');
|
||||
// Comment date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date('c', $comment->getCreatedTime()),
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');
|
||||
|
||||
// Comment body.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $comment->comment_body->value . "\n",
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/rss/1.0/modules/content/encoded', $expected_value), 'Comment body found in RDF output (content:encoded).');
|
||||
|
||||
// The comment author can be a registered user or an anonymous user.
|
||||
if ($comment->getOwnerId() > 0) {
|
||||
$author_uri = \Drupal::url('entity.user.canonical', ['user' => $comment->getOwnerId()], array('absolute' => TRUE));
|
||||
// Comment relation to author.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $author_uri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://rdfs.org/sioc/ns#has_creator', $expected_value), 'Comment relation to author found in RDF output (sioc:has_creator).');
|
||||
}
|
||||
else {
|
||||
// The author is expected to be a blank node.
|
||||
$author_uri = $graph->get($comment_uri, '<http://rdfs.org/sioc/ns#has_creator>');
|
||||
if ($author_uri instanceof \EasyRdf_Resource) {
|
||||
$this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator) and author is blank node.');
|
||||
}
|
||||
else {
|
||||
$this->fail('Comment relation to author found in RDF output (sioc:has_creator).');
|
||||
}
|
||||
}
|
||||
|
||||
// Author name.
|
||||
$name = empty($account["name"]) ? $this->webUser->getUsername() : $account["name"] . " (not verified)";
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $name,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'Comment author name found in RDF output (foaf:name).');
|
||||
|
||||
// Comment author homepage (only for anonymous authors).
|
||||
if ($comment->getOwnerId() == 0) {
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://example.org/',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/page', $expected_value), 'Comment author link found in RDF output (foaf:page).');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comment entity.
|
||||
*
|
||||
* @param $nid
|
||||
* Node id which will hold the comment.
|
||||
* @param $uid
|
||||
* User id of the author of the comment. Can be NULL if $contact provided.
|
||||
* @param $contact
|
||||
* Set to NULL for no contact info, TRUE to ignore success checking, and
|
||||
* array of values to set contact info.
|
||||
* @param $pid
|
||||
* Comment id of the parent comment in a thread.
|
||||
*
|
||||
* @return \Drupal\comment\Entity\Comment
|
||||
* The saved comment.
|
||||
*/
|
||||
function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
|
||||
$values = array(
|
||||
'entity_id' => $nid,
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'uid' => $uid,
|
||||
'pid' => $pid,
|
||||
'subject' => $this->randomMachineName(),
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'status' => 1,
|
||||
);
|
||||
if ($contact) {
|
||||
$values += $contact;
|
||||
}
|
||||
|
||||
$comment = entity_create('comment', $values);
|
||||
$comment->save();
|
||||
return $comment;
|
||||
}
|
||||
}
|
115
core/modules/rdf/src/Tests/CrudTest.php
Normal file
115
core/modules/rdf/src/Tests/CrudTest.php
Normal file
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\CrudTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\simpletest\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the RDF mapping CRUD functions.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class CrudTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('entity_test', 'rdf', 'system');
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $entityType;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $bundle;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->prefix = 'rdf.mapping';
|
||||
$this->entityType = $this->bundle = 'entity_test';
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creation of RDF mapping.
|
||||
*/
|
||||
function testMappingCreation() {
|
||||
$mapping_config_name = "{$this->prefix}.{$this->entityType}.{$this->bundle}";
|
||||
|
||||
// Save bundle mapping config.
|
||||
rdf_get_mapping($this->entityType, $this->bundle)->save();
|
||||
// Test that config file was saved.
|
||||
$mapping_config = \Drupal::configFactory()->listAll('rdf.mapping.');
|
||||
$this->assertTrue(in_array($mapping_config_name, $mapping_config), 'Rdf mapping config saved.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the handling of bundle mappings.
|
||||
*/
|
||||
function testBundleMapping() {
|
||||
// Test that the bundle mapping can be saved.
|
||||
$types = array('sioc:Post', 'foaf:Document');
|
||||
rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->setBundleMapping(array('types' => $types))
|
||||
->save();
|
||||
$bundle_mapping = rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->getBundleMapping();
|
||||
$this->assertEqual($types, $bundle_mapping['types'], 'Bundle mapping saved.');
|
||||
|
||||
// Test that the bundle mapping can be edited.
|
||||
$types = array('schema:BlogPosting');
|
||||
rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->setBundleMapping(array('types' => $types))
|
||||
->save();
|
||||
$bundle_mapping = rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->getBundleMapping();
|
||||
$this->assertEqual($types, $bundle_mapping['types'], 'Bundle mapping updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the handling of field mappings.
|
||||
*/
|
||||
function testFieldMapping() {
|
||||
$field_name = 'created';
|
||||
|
||||
// Test that the field mapping can be saved.
|
||||
$mapping = array(
|
||||
'properties' => array('dc:created'),
|
||||
'datatype' => 'xsd:dateTime',
|
||||
'datatype_callback' => array('callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'),
|
||||
);
|
||||
rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->setFieldMapping($field_name, $mapping)
|
||||
->save();
|
||||
$field_mapping = rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->getFieldMapping($field_name);
|
||||
$this->assertEqual($mapping, $field_mapping, 'Field mapping saved.');
|
||||
|
||||
// Test that the field mapping can be edited.
|
||||
$mapping = array(
|
||||
'properties' => array('dc:date'),
|
||||
'datatype' => 'foo:bar',
|
||||
'datatype_callback' => array('callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'),
|
||||
);
|
||||
rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->setFieldMapping($field_name, $mapping)
|
||||
->save();
|
||||
$field_mapping = rdf_get_mapping($this->entityType, $this->bundle)
|
||||
->getFieldMapping($field_name);
|
||||
$this->assertEqual($mapping, $field_mapping, 'Field mapping updated.');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\EntityReferenceFieldAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\taxonomy\Tests\TaxonomyTestBase;
|
||||
|
||||
/**
|
||||
* Tests RDFa markup generation for taxonomy term fields.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class EntityReferenceFieldAttributesTest extends TaxonomyTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf', 'field_test', 'file', 'image');
|
||||
|
||||
/**
|
||||
* The name of the taxonomy term reference field used in the test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldName;
|
||||
|
||||
/**
|
||||
* The vocabulary object used in the test.
|
||||
*
|
||||
* @var \Drupal\taxonomy\VocabularyInterface
|
||||
*/
|
||||
protected $vocabulary;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$web_user = $this->drupalCreateUser(array('bypass node access', 'administer taxonomy'));
|
||||
$this->drupalLogin($web_user);
|
||||
$this->vocabulary = $this->createVocabulary();
|
||||
|
||||
// Create the field.
|
||||
$this->fieldName = 'field_taxonomy_test';
|
||||
$handler_settings = array(
|
||||
'target_bundles' => array(
|
||||
$this->vocabulary->id() => $this->vocabulary->id(),
|
||||
),
|
||||
'auto_create' => TRUE,
|
||||
);
|
||||
$this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
||||
|
||||
entity_get_form_display('node', 'article', 'default')
|
||||
->setComponent($this->fieldName, array('type' => 'options_select'))
|
||||
->save();
|
||||
entity_get_display('node', 'article', 'full')
|
||||
->setComponent($this->fieldName, array('type' => 'entity_reference_label'))
|
||||
->save();
|
||||
|
||||
// Set the RDF mapping for the new field.
|
||||
rdf_get_mapping('node', 'article')
|
||||
->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('dc:subject'),
|
||||
'mapping_type' => 'rel',
|
||||
))
|
||||
->save();
|
||||
|
||||
rdf_get_mapping('taxonomy_term', $this->vocabulary->id())
|
||||
->setBundleMapping(array('types' => array('skos:Concept')))
|
||||
->setFieldMapping('name', array('properties' => array('rdfs:label')))
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if file fields in teasers have correct resources.
|
||||
*
|
||||
* Ensure that file fields have the correct resource as the object in RDFa
|
||||
* when displayed as a teaser.
|
||||
*/
|
||||
function testNodeTeaser() {
|
||||
// Set the teaser display to show this field.
|
||||
entity_get_display('node', 'article', 'teaser')
|
||||
->setComponent($this->fieldName, array('type' => 'entity_reference_label'))
|
||||
->save();
|
||||
|
||||
// Create a term in each vocabulary.
|
||||
$term1 = $this->createTerm($this->vocabulary);
|
||||
$term2 = $this->createTerm($this->vocabulary);
|
||||
$taxonomy_term_1_uri = $term1->url('canonical', ['absolute' => TRUE]);
|
||||
$taxonomy_term_2_uri = $term2->url('canonical', ['absolute' => TRUE]);
|
||||
|
||||
// Create the node.
|
||||
$node = $this->drupalCreateNode(array('type' => 'article'));
|
||||
$node->set($this->fieldName, array(
|
||||
array('target_id' => $term1->id()),
|
||||
array('target_id' => $term2->id()),
|
||||
));
|
||||
|
||||
// Render the node.
|
||||
$node_render_array = entity_view_multiple(array($node), 'teaser');
|
||||
$html = \Drupal::service('renderer')->renderRoot($node_render_array);
|
||||
|
||||
// Parse the teaser.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
|
||||
$parser->parse($graph, $html, 'rdfa', $base_uri);
|
||||
|
||||
// Node relations to taxonomy terms.
|
||||
$node_uri = $node->url('canonical', ['absolute' => TRUE]);
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $taxonomy_term_1_uri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $taxonomy_term_2_uri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
|
||||
// Taxonomy terms triples.
|
||||
// Term 1.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
|
||||
);
|
||||
// @todo Enable with https://www.drupal.org/node/2072791.
|
||||
//$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $term1->getName(),
|
||||
);
|
||||
//$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
|
||||
// Term 2.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
|
||||
);
|
||||
//$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $term2->getName(),
|
||||
);
|
||||
//$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
|
||||
}
|
||||
|
||||
}
|
57
core/modules/rdf/src/Tests/Field/DateTimeFieldRdfaTest.php
Normal file
57
core/modules/rdf/src/Tests/Field/DateTimeFieldRdfaTest.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\DateTimeFieldRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
|
||||
|
||||
/**
|
||||
* Tests RDFa output by datetime field formatters.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class DateTimeFieldRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'datetime';
|
||||
|
||||
/**
|
||||
* The 'value' property value for testing.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $testValue = '2014-01-28T06:01:01';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('datetime');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->createTestField();
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:dateCreated'),
|
||||
))->save();
|
||||
|
||||
// Set up test entity.
|
||||
$this->entity = entity_create('entity_test', array());
|
||||
$this->entity->{$this->fieldName}->value = $this->testValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the default formatter.
|
||||
*/
|
||||
public function testDefaultFormatter() {
|
||||
$this->assertFormatterRdfa(array('type'=>'datetime_default'), 'http://schema.org/dateCreated', array('value' => $this->testValue . 'Z', 'type' => 'literal', 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime'));
|
||||
}
|
||||
}
|
54
core/modules/rdf/src/Tests/Field/EmailFieldRdfaTest.php
Normal file
54
core/modules/rdf/src/Tests/Field/EmailFieldRdfaTest.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\EmailFieldRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
|
||||
|
||||
/**
|
||||
* Tests RDFa output by email field formatters.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class EmailFieldRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'email';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('text');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->createTestField();
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:email'),
|
||||
))->save();
|
||||
|
||||
// Set up test values.
|
||||
$this->testValue = 'test@example.com';
|
||||
$this->entity = entity_create('entity_test', array());
|
||||
$this->entity->{$this->fieldName}->value = $this->testValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all email formatters.
|
||||
*/
|
||||
public function testAllFormatters() {
|
||||
// Test the plain formatter.
|
||||
$this->assertFormatterRdfa(array('type'=>'string'), 'http://schema.org/email', array('value' => $this->testValue));
|
||||
// Test the mailto formatter.
|
||||
$this->assertFormatterRdfa(array('type'=>'email_mailto'), 'http://schema.org/email', array('value' => $this->testValue));
|
||||
}
|
||||
}
|
95
core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php
Normal file
95
core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php
Normal file
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\EntityReferenceRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the RDFa output of the entity reference field formatter.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class EntityReferenceRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
use EntityReferenceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'entity_reference';
|
||||
|
||||
/**
|
||||
* The entity type used in this test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityType = 'entity_test';
|
||||
|
||||
/**
|
||||
* The bundle used in this test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $bundle = 'entity_test';
|
||||
|
||||
/**
|
||||
* The term for testing.
|
||||
*
|
||||
* @var \Drupal\taxonomy\Entity\Term
|
||||
*/
|
||||
protected $targetEntity;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('entity_reference', 'text', 'filter');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('entity_test_rev');
|
||||
|
||||
// Give anonymous users permission to view test entities.
|
||||
$this->installConfig(array('user'));
|
||||
Role::load(RoleInterface::ANONYMOUS_ID)
|
||||
->grantPermission('view test entity')
|
||||
->save();
|
||||
|
||||
$this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:knows'),
|
||||
))->save();
|
||||
|
||||
// Create the entity to be referenced.
|
||||
$this->targetEntity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
|
||||
$this->targetEntity->save();
|
||||
|
||||
// Create the entity that will have the entity reference field.
|
||||
$this->entity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
|
||||
$this->entity->save();
|
||||
$this->entity->{$this->fieldName}->entity = $this->targetEntity;
|
||||
$this->uri = $this->getAbsoluteUri($this->entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all the entity reference formatters.
|
||||
*/
|
||||
public function testAllFormatters() {
|
||||
$entity_uri = $this->getAbsoluteUri($this->targetEntity);
|
||||
|
||||
// Tests the label formatter.
|
||||
$this->assertFormatterRdfa(array('type' => 'entity_reference_label'), 'http://schema.org/knows', array('value' => $entity_uri, 'type' => 'uri'));
|
||||
// Tests the entity formatter.
|
||||
$this->assertFormatterRdfa(array('type' => 'entity_reference_entity_view'), 'http://schema.org/knows', array('value' => $entity_uri, 'type' => 'uri'));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\FieldRdfaDatatypeCallbackTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
/**
|
||||
* Tests the RDFa output of a text field formatter with a datatype callback.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class FieldRdfaDatatypeCallbackTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'text';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('text', 'filter');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->createTestField();
|
||||
|
||||
$this->installConfig(array('filter'));
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:interactionCount'),
|
||||
'datatype_callback' => array(
|
||||
'callable' => 'Drupal\rdf\Tests\Field\TestDataConverter::convertFoo',
|
||||
),
|
||||
))->save();
|
||||
|
||||
// Set up test values.
|
||||
$this->testValue = $this->randomMachineName();
|
||||
$this->entity = entity_create('entity_test');
|
||||
$this->entity->{$this->fieldName}->value = $this->testValue;
|
||||
$this->entity->save();
|
||||
|
||||
$this->uri = $this->getAbsoluteUri($this->entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the default formatter.
|
||||
*/
|
||||
public function testDefaultFormatter() {
|
||||
// Expected value is the output of the datatype callback, not the raw value.
|
||||
$this->assertFormatterRdfa(array('type'=>'text_default'), 'http://schema.org/interactionCount', array('value' => 'foo' . $this->testValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
191
core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
Normal file
191
core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
Normal file
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\FieldRdfaTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
use Drupal\field\Tests\FieldUnitTestBase;
|
||||
|
||||
abstract class FieldRdfaTestBase extends FieldUnitTestBase {
|
||||
|
||||
/**
|
||||
* The machine name of the field type to test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldType;
|
||||
|
||||
/**
|
||||
* The name of the field to create for testing.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldName = 'field_test';
|
||||
|
||||
/**
|
||||
* The URI to identify the entity.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $uri = 'http://ex.com';
|
||||
|
||||
/**
|
||||
* The entity to render for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\ContentEntityBase
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* TRUE if verbose debugging is enabled.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $debug = TRUE;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf');
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $testValue;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test the formatter's RDFa.
|
||||
*
|
||||
* @param array $formatter
|
||||
* An associative array describing the formatter to test and its settings
|
||||
* containing:
|
||||
* - type: The machine name of the field formatter to test.
|
||||
* - settings: The settings of the field formatter to test.
|
||||
* @param string $property
|
||||
* The property that should be found.
|
||||
* @param array $expected_rdf_value
|
||||
* An associative array describing the expected value of the property
|
||||
* containing:
|
||||
* - value: The actual value of the string or URI.
|
||||
* - type: The type of RDF value, e.g. 'literal' for a string, or 'uri'.
|
||||
* Defaults to 'literal'.
|
||||
* - datatype: (optional) The datatype of the value (e.g. xsd:dateTime).
|
||||
*/
|
||||
protected function assertFormatterRdfa($formatter, $property, $expected_rdf_value) {
|
||||
$expected_rdf_value += array('type' => 'literal');
|
||||
|
||||
// The field formatter will be rendered inside the entity. Set the field
|
||||
// formatter in the entity display options before rendering the entity.
|
||||
entity_get_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($this->fieldName, $formatter)
|
||||
->save();
|
||||
$build = entity_view($this->entity, 'default');
|
||||
$output = \Drupal::service('renderer')->renderRoot($build);
|
||||
$graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa');
|
||||
$this->setRawContent($output);
|
||||
|
||||
// If verbose debugging is turned on, display the HTML and parsed RDF
|
||||
// in the results.
|
||||
if ($this->debug) {
|
||||
debug($output);
|
||||
debug($graph->toRdfPhp());
|
||||
}
|
||||
|
||||
$this->assertTrue($graph->hasProperty($this->uri, $property, $expected_rdf_value), "Formatter {$formatter['type']} exposes data correctly for {$this->fieldType} fields.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the field for testing.
|
||||
*
|
||||
* @param array $field_settings
|
||||
* (optional) An array of field settings.
|
||||
*/
|
||||
protected function createTestField($field_settings = array()) {
|
||||
entity_create('field_storage_config', array(
|
||||
'field_name' => $this->fieldName,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $this->fieldType,
|
||||
))->save();
|
||||
entity_create('field_config', array(
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => $this->fieldName,
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => $field_settings,
|
||||
))->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the absolute URI of an entity.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\ContentEntityBase $entity
|
||||
* The entity for which to generate the URI.
|
||||
*
|
||||
* @return string
|
||||
* The absolute URI.
|
||||
*/
|
||||
protected function getAbsoluteUri($entity) {
|
||||
return $entity->url('canonical', array('absolute' => TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a content and return the html element.
|
||||
*
|
||||
* @param string $content
|
||||
* The html to parse.
|
||||
*
|
||||
* @return array
|
||||
* An array containing simplexml objects.
|
||||
*/
|
||||
protected function parseContent($content) {
|
||||
$htmlDom = new \DOMDocument();
|
||||
@$htmlDom->loadHTML('<?xml encoding="UTF-8">' . $content);
|
||||
$elements = simplexml_import_dom($htmlDom);
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs an xpath search on a certain content.
|
||||
*
|
||||
* The search is relative to the root element of the $content variable.
|
||||
*
|
||||
* @param string $content
|
||||
* The html to parse.
|
||||
* @param string $xpath
|
||||
* The xpath string to use in the search.
|
||||
* @param array $arguments
|
||||
* Some arguments for the xpath.
|
||||
*
|
||||
* @return array|FALSE
|
||||
* The return value of the xpath search. For details on the xpath string
|
||||
* format and return values see the SimpleXML documentation,
|
||||
* http://php.net/manual/function.simplexml-element-xpath.php.
|
||||
*/
|
||||
protected function xpathContent($content, $xpath, array $arguments = array()) {
|
||||
if ($elements = $this->parseContent($content)) {
|
||||
$xpath = $this->buildXPathQuery($xpath, $arguments);
|
||||
$result = $elements->xpath($xpath);
|
||||
// Some combinations of PHP / libxml versions return an empty array
|
||||
// instead of the documented FALSE. Forcefully convert any falsish values
|
||||
// to an empty array to allow foreach(...) constructions.
|
||||
return $result ? $result : array();
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
189
core/modules/rdf/src/Tests/Field/LinkFieldRdfaTest.php
Normal file
189
core/modules/rdf/src/Tests/Field/LinkFieldRdfaTest.php
Normal file
|
@ -0,0 +1,189 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\LinkFieldRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
|
||||
|
||||
/**
|
||||
* Tests the placement of RDFa in link field formatters.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class LinkFieldRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'link';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('link', 'text');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->createTestField();
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:link'),
|
||||
))->save();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all formatters with link to external page.
|
||||
*/
|
||||
public function testAllFormattersExternal() {
|
||||
// Set up test values.
|
||||
$this->testValue = 'http://test.me/foo/bar/neque/porro/quisquam/est/qui-dolorem?foo/bar/neque/porro/quisquam/est/qui-dolorem';
|
||||
$this->entity = entity_create('entity_test', array());
|
||||
$this->entity->{$this->fieldName}->uri = $this->testValue;
|
||||
|
||||
// Set up the expected result.
|
||||
$expected_rdf = array(
|
||||
'value' => $this->testValue,
|
||||
'type' => 'uri',
|
||||
);
|
||||
|
||||
$this->runTestAllFormatters($expected_rdf, 'external');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all formatters with link to internal page.
|
||||
*/
|
||||
public function testAllFormattersInternal() {
|
||||
// Set up test values.
|
||||
$this->testValue = 'admin';
|
||||
$this->entity = entity_create('entity_test', array());
|
||||
$this->entity->{$this->fieldName}->uri = 'internal:/admin';
|
||||
|
||||
// Set up the expected result.
|
||||
// AssertFormatterRdfa looks for a full path.
|
||||
$expected_rdf = array(
|
||||
'value' => $this->uri . '/' . $this->testValue,
|
||||
'type' => 'uri',
|
||||
);
|
||||
|
||||
$this->runTestAllFormatters($expected_rdf, 'internal');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all formatters with link to frontpage.
|
||||
*/
|
||||
public function testAllFormattersFront() {
|
||||
// Set up test values.
|
||||
$this->testValue = '/';
|
||||
$this->entity = entity_create('entity_test', array());
|
||||
$this->entity->{$this->fieldName}->uri = 'internal:/';
|
||||
|
||||
// Set up the expected result.
|
||||
$expected_rdf = array(
|
||||
'value' => $this->uri . '/',
|
||||
'type' => 'uri',
|
||||
);
|
||||
|
||||
$this->runTestAllFormatters($expected_rdf, 'front');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test all link formatters.
|
||||
*/
|
||||
public function runTestAllFormatters($expected_rdf, $type = NULL) {
|
||||
|
||||
// Test the link formatter: trim at 80, no other settings.
|
||||
$formatter = array(
|
||||
'type' => 'link',
|
||||
'settings' => array(
|
||||
'trim_length' => 80,
|
||||
'url_only' => FALSE,
|
||||
'url_plain' => FALSE,
|
||||
'rel' => '',
|
||||
'target' => '',
|
||||
),
|
||||
);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
|
||||
|
||||
// Test the link formatter: trim at 40, nofollow, new window.
|
||||
$formatter = array(
|
||||
'type' => 'link',
|
||||
'settings' => array(
|
||||
'trim_length' => 40,
|
||||
'url_only' => FALSE,
|
||||
'url_plain' => FALSE,
|
||||
'rel' => 'nofollow',
|
||||
'target' => '_blank',
|
||||
),
|
||||
);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
|
||||
|
||||
// Test the link formatter: trim at 40, URL only (not plaintext) nofollow,
|
||||
// new window.
|
||||
$formatter = array(
|
||||
'type' => 'link',
|
||||
'settings' => array(
|
||||
'trim_length' => 40,
|
||||
'url_only' => TRUE,
|
||||
'url_plain' => FALSE,
|
||||
'rel' => 'nofollow',
|
||||
'target' => '_blank',
|
||||
),
|
||||
);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
|
||||
|
||||
// Test the link_separate formatter: trim at 40, nofollow, new window.
|
||||
$formatter = array(
|
||||
'type' => 'link_separate',
|
||||
'settings' => array(
|
||||
'trim_length' => 40,
|
||||
'rel' => 'nofollow',
|
||||
'target' => '_blank',
|
||||
),
|
||||
);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
|
||||
|
||||
// Change the expected value here to literal. When formatted as plaintext
|
||||
// then the RDF is expecting a 'literal' not a 'uri'.
|
||||
$expected_rdf = array(
|
||||
'value' => $this->testValue,
|
||||
'type' => 'literal',
|
||||
);
|
||||
// Test the link formatter: trim at 20, url only (as plaintext.)
|
||||
$formatter = array(
|
||||
'type' => 'link',
|
||||
'settings' => array(
|
||||
'trim_length' => 20,
|
||||
'url_only' => TRUE,
|
||||
'url_plain' => TRUE,
|
||||
'rel' => '0',
|
||||
'target' => '0',
|
||||
),
|
||||
);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
|
||||
|
||||
// Test the link formatter: do not trim, url only (as plaintext.)
|
||||
$formatter = array(
|
||||
'type' => 'link',
|
||||
'settings' => array(
|
||||
'trim_length' => 0,
|
||||
'url_only' => TRUE,
|
||||
'url_plain' => TRUE,
|
||||
'rel' => '0',
|
||||
'target' => '0',
|
||||
),
|
||||
);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
|
||||
}
|
||||
|
||||
}
|
197
core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
Normal file
197
core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
Normal file
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\NumberFieldRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
/**
|
||||
* Tests RDFa output by number field formatters.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class NumberFieldRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* Tests the integer formatter.
|
||||
*/
|
||||
public function testIntegerFormatter() {
|
||||
$this->fieldType = 'integer';
|
||||
$testValue = 3;
|
||||
$this->createTestField();
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa(array('type' => 'number_integer'), 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is not created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]');
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the integer formatter with settings.
|
||||
*/
|
||||
public function testIntegerFormatterWithSettings() {
|
||||
$this->fieldType = 'integer';
|
||||
$formatter = array(
|
||||
'type' => 'number_integer',
|
||||
'settings' => array(
|
||||
'thousand_separator' => '.',
|
||||
'prefix_suffix' => TRUE,
|
||||
),
|
||||
);
|
||||
$testValue = 3333333.33;
|
||||
$field_settings = array(
|
||||
'prefix' => '#',
|
||||
'suffix' => ' llamas.',
|
||||
);
|
||||
$this->createTestField($field_settings);
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content=:testValue]', array(':testValue' => $testValue));
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the float formatter.
|
||||
*/
|
||||
public function testFloatFormatter() {
|
||||
$this->fieldType = 'float';
|
||||
$testValue = 3.33;
|
||||
$this->createTestField();
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa(array('type' => 'number_unformatted'), 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is not created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]');
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the float formatter with settings.
|
||||
*/
|
||||
public function testFloatFormatterWithSettings() {
|
||||
$this->fieldType = 'float';
|
||||
$formatter = array(
|
||||
'type' => 'number_decimal',
|
||||
'settings' => array(
|
||||
'thousand_separator' => '.',
|
||||
'decimal_separator' => ',',
|
||||
'prefix_suffix' => TRUE,
|
||||
),
|
||||
);
|
||||
$testValue = 3333333.33;
|
||||
$field_settings = array(
|
||||
'prefix' => '$',
|
||||
'suffix' => ' more.',
|
||||
);
|
||||
$this->createTestField($field_settings);
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content=:testValue]', array(':testValue' => $testValue));
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the float formatter with a scale. Scale is not exercised.
|
||||
*/
|
||||
public function testFloatFormatterWithScale() {
|
||||
$this->fieldType = 'float';
|
||||
$formatter = array(
|
||||
'type' => 'number_decimal',
|
||||
'settings' => array(
|
||||
'scale' => 5,
|
||||
),
|
||||
);
|
||||
$testValue = 3.33;
|
||||
$this->createTestField();
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is not created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]');
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the float formatter with a scale. Scale is exercised.
|
||||
*/
|
||||
public function testFloatFormatterWithScaleExercised() {
|
||||
$this->fieldType = 'float';
|
||||
$formatter = array(
|
||||
'type' => 'number_decimal',
|
||||
'settings' => array(
|
||||
'scale' => 5,
|
||||
),
|
||||
);
|
||||
$testValue = 3.1234567;
|
||||
$this->createTestField();
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content=:testValue]', array(':testValue' => $testValue));
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the decimal formatter.
|
||||
*/
|
||||
public function testDecimalFormatter() {
|
||||
$this->fieldType = 'decimal';
|
||||
$testValue = 3.33;
|
||||
$this->createTestField();
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa(array('type' => 'number_decimal'), 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is not created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]');
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the decimal formatter with settings.
|
||||
*/
|
||||
public function testDecimalFormatterWithSettings() {
|
||||
$this->fieldType = 'decimal';
|
||||
$formatter = array(
|
||||
'type' => 'number_decimal',
|
||||
'settings' => array(
|
||||
'thousand_separator' => 't',
|
||||
'decimal_separator' => '#',
|
||||
'prefix_suffix' => TRUE,
|
||||
),
|
||||
);
|
||||
$testValue = 3333333.33;
|
||||
$field_settings = array(
|
||||
'prefix' => '$',
|
||||
'suffix' => ' more.',
|
||||
);
|
||||
$this->createTestField($field_settings);
|
||||
$this->createTestEntity($testValue);
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue));
|
||||
|
||||
// Test that the content attribute is created.
|
||||
$result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content=:testValue]', array(':testValue' => $testValue));
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the RDF mapping for the field.
|
||||
*/
|
||||
protected function createTestEntity($testValue) {
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:baseSalary'),
|
||||
))->save();
|
||||
|
||||
// Set up test entity.
|
||||
$this->entity = entity_create('entity_test', array());
|
||||
$this->entity->{$this->fieldName}->value = $testValue;
|
||||
}
|
||||
}
|
59
core/modules/rdf/src/Tests/Field/StringFieldRdfaTest.php
Normal file
59
core/modules/rdf/src/Tests/Field/StringFieldRdfaTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\StringFieldRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
/**
|
||||
* Tests RDFa output by text field formatters.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class StringFieldRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'string';
|
||||
|
||||
/**
|
||||
* The 'value' property value for testing.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $testValue = 'test_text_value';
|
||||
|
||||
/**
|
||||
* The 'summary' property value for testing.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $testSummary = 'test_summary_value';
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->createTestField();
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:text'),
|
||||
))->save();
|
||||
|
||||
// Set up test entity.
|
||||
$this->entity = entity_create('entity_test');
|
||||
$this->entity->{$this->fieldName}->value = $this->testValue;
|
||||
$this->entity->{$this->fieldName}->summary = $this->testSummary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests string formatters.
|
||||
*/
|
||||
public function testStringFormatters() {
|
||||
// Tests the string formatter.
|
||||
$this->assertFormatterRdfa(array('type'=>'string'), 'http://schema.org/text', array('value' => $this->testValue));
|
||||
}
|
||||
}
|
70
core/modules/rdf/src/Tests/Field/TelephoneFieldRdfaTest.php
Normal file
70
core/modules/rdf/src/Tests/Field/TelephoneFieldRdfaTest.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\TelephoneFieldRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
/**
|
||||
* Tests RDFa output by telephone field formatters.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class TelephoneFieldRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* A test value for the telephone field.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $testValue;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'telephone';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('telephone', 'text');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->createTestField();
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:telephone'),
|
||||
))->save();
|
||||
|
||||
// Set up test values.
|
||||
$this->testValue = '555-555-5555';
|
||||
$this->entity = entity_create('entity_test', array());
|
||||
$this->entity->{$this->fieldName}->value = $this->testValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the field formatters.
|
||||
*/
|
||||
public function testAllFormatters() {
|
||||
// Tests the plain formatter.
|
||||
$this->assertFormatterRdfa(array('type' => 'string'), 'http://schema.org/telephone', array('value' => $this->testValue));
|
||||
// Tests the telephone link formatter.
|
||||
$this->assertFormatterRdfa(array('type' => 'telephone_link'), 'http://schema.org/telephone', array('value' => 'tel:' . $this->testValue, 'type' => 'uri'));
|
||||
|
||||
$formatter = array(
|
||||
'type' => 'telephone_link',
|
||||
'settings' => array('title' => 'Contact us'),
|
||||
);
|
||||
$expected_rdf_value = array(
|
||||
'value' => 'tel:' . $this->testValue,
|
||||
'type' => 'uri',
|
||||
);
|
||||
// Tests the telephone link formatter with custom title.
|
||||
$this->assertFormatterRdfa($formatter, 'http://schema.org/telephone', $expected_rdf_value);
|
||||
}
|
||||
}
|
27
core/modules/rdf/src/Tests/Field/TestDataConverter.php
Normal file
27
core/modules/rdf/src/Tests/Field/TestDataConverter.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\TestDataConverter.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
/**
|
||||
* Contains methods for test data conversions.
|
||||
*/
|
||||
class TestDataConverter {
|
||||
|
||||
/**
|
||||
* Converts data into a string for placement into a content attribute.
|
||||
*
|
||||
* @param array $data
|
||||
* The data to be altered and placed in the content attribute.
|
||||
*
|
||||
* @return string
|
||||
* Returns the data.
|
||||
*/
|
||||
static function convertFoo($data) {
|
||||
return 'foo' . $data['value'];
|
||||
}
|
||||
|
||||
}
|
76
core/modules/rdf/src/Tests/Field/TextFieldRdfaTest.php
Normal file
76
core/modules/rdf/src/Tests/Field/TextFieldRdfaTest.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\Field\TextFieldRdfaTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests\Field;
|
||||
|
||||
use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
|
||||
|
||||
/**
|
||||
* Tests RDFa output by text field formatters.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class TextFieldRdfaTest extends FieldRdfaTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $fieldType = 'text';
|
||||
|
||||
/**
|
||||
* The 'value' property value for testing.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $testValue = 'test_text_value';
|
||||
|
||||
/**
|
||||
* The 'summary' property value for testing.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $testSummary = 'test_summary_value';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('text', 'filter');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installConfig(array('filter'));
|
||||
|
||||
$this->createTestField();
|
||||
|
||||
// Add the mapping.
|
||||
$mapping = rdf_get_mapping('entity_test', 'entity_test');
|
||||
$mapping->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('schema:text'),
|
||||
))->save();
|
||||
|
||||
// Set up test entity.
|
||||
$this->entity = entity_create('entity_test');
|
||||
$this->entity->{$this->fieldName}->value = $this->testValue;
|
||||
$this->entity->{$this->fieldName}->summary = $this->testSummary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all formatters.
|
||||
*
|
||||
* @todo Check for the summary mapping.
|
||||
*/
|
||||
public function testAllFormatters() {
|
||||
$formatted_value = strip_tags($this->entity->{$this->fieldName}->processed);
|
||||
|
||||
// Tests the default formatter.
|
||||
$this->assertFormatterRdfa(array('type'=>'text_default'), 'http://schema.org/text', array('value' => $formatted_value));
|
||||
// Tests the summary formatter.
|
||||
$this->assertFormatterRdfa(array('type'=>'text_summary_or_trimmed'), 'http://schema.org/text', array('value' => $formatted_value));
|
||||
// Tests the trimmed formatter.
|
||||
$this->assertFormatterRdfa(array('type'=>'text_trimmed'), 'http://schema.org/text', array('value' => $formatted_value));
|
||||
}
|
||||
}
|
104
core/modules/rdf/src/Tests/FileFieldAttributesTest.php
Normal file
104
core/modules/rdf/src/Tests/FileFieldAttributesTest.php
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\FileFieldAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\file\Tests\FileFieldTestBase;
|
||||
|
||||
/**
|
||||
* Tests the RDFa markup of filefields.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class FileFieldAttributesTest extends FileFieldTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf', 'file');
|
||||
|
||||
/**
|
||||
* The name of the file field used in the test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldName;
|
||||
|
||||
/**
|
||||
* The file object used in the test.
|
||||
*
|
||||
* @var \Drupal\file\FileInterface
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* The node object used in the test.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $node;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
||||
$this->fieldName = strtolower($this->randomMachineName());
|
||||
|
||||
$type_name = 'article';
|
||||
$this->createFileField($this->fieldName, 'node', $type_name);
|
||||
|
||||
// Set the teaser display to show this field.
|
||||
entity_get_display('node', 'article', 'teaser')
|
||||
->setComponent($this->fieldName, array('type' => 'file_default'))
|
||||
->save();
|
||||
|
||||
// Set the RDF mapping for the new field.
|
||||
$mapping = rdf_get_mapping('node', 'article');
|
||||
$mapping->setFieldMapping($this->fieldName, array('properties' => array('rdfs:seeAlso'), 'mapping_type' => 'rel'))->save();
|
||||
|
||||
$test_file = $this->getTestFile('text');
|
||||
|
||||
// Create a new node with the uploaded file.
|
||||
$nid = $this->uploadNodeFile($test_file, $this->fieldName, $type_name);
|
||||
|
||||
$node_storage->resetCache(array($nid));
|
||||
$this->node = $node_storage->load($nid);
|
||||
$this->file = file_load($this->node->{$this->fieldName}->target_id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if file fields in teasers have correct resources.
|
||||
*
|
||||
* Ensure that file fields have the correct resource as the object in RDFa
|
||||
* when displayed as a teaser.
|
||||
*/
|
||||
function testNodeTeaser() {
|
||||
// Render the teaser.
|
||||
$node_render_array = entity_view_multiple(array($this->node), 'teaser');
|
||||
$html = \Drupal::service('renderer')->renderRoot($node_render_array);
|
||||
|
||||
// Parses front page where the node is displayed in its teaser form.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
|
||||
$parser->parse($graph, $html, 'rdfa', $base_uri);
|
||||
|
||||
$node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
|
||||
$file_uri = file_create_url($this->file->getFileUri());
|
||||
|
||||
// Node relation to attached file.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $file_uri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).');
|
||||
$this->drupalGet('node');
|
||||
}
|
||||
|
||||
}
|
54
core/modules/rdf/src/Tests/GetNamespacesTest.php
Normal file
54
core/modules/rdf/src/Tests/GetNamespacesTest.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\GetNamespacesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Confirm that the serialization of RDF namespaces in present in the HTML
|
||||
* markup.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class GetNamespacesTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf', 'rdf_test_namespaces');
|
||||
|
||||
/**
|
||||
* Tests RDF namespaces.
|
||||
*/
|
||||
function testGetRdfNamespaces() {
|
||||
// Fetches the front page and extracts RDFa 1.1 prefixes.
|
||||
$this->drupalGet('');
|
||||
|
||||
$element = $this->xpath('//html[contains(@prefix, :prefix_binding)]', array(
|
||||
':prefix_binding' => 'rdfs: http://www.w3.org/2000/01/rdf-schema#',
|
||||
));
|
||||
$this->assertTrue(!empty($element), 'A prefix declared once is displayed.');
|
||||
|
||||
$element = $this->xpath('//html[contains(@prefix, :prefix_binding)]', array(
|
||||
':prefix_binding' => 'foaf: http://xmlns.com/foaf/0.1/',
|
||||
));
|
||||
$this->assertTrue(!empty($element), 'The same prefix declared in several implementations of hook_rdf_namespaces() is valid as long as all the namespaces are the same.');
|
||||
|
||||
$element = $this->xpath('//html[contains(@prefix, :prefix_binding)]', array(
|
||||
':prefix_binding' => 'foaf1: http://xmlns.com/foaf/0.1/',
|
||||
));
|
||||
$this->assertTrue(!empty($element), 'Two prefixes can be assigned the same namespace.');
|
||||
|
||||
$element = $this->xpath('//html[contains(@prefix, :prefix_binding)]', array(
|
||||
':prefix_binding' => 'dc: http://purl.org/dc/terms/',
|
||||
));
|
||||
$this->assertTrue(!empty($element), 'When a prefix has conflicting namespaces, the first declared one is used.');
|
||||
}
|
||||
}
|
48
core/modules/rdf/src/Tests/GetRdfNamespacesTest.php
Normal file
48
core/modules/rdf/src/Tests/GetRdfNamespacesTest.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\GetRdfNamespacesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests hook_rdf_namespaces().
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class GetRdfNamespacesTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf', 'rdf_test_namespaces');
|
||||
|
||||
/**
|
||||
* Tests getting RDF namespaces.
|
||||
*/
|
||||
function testGetRdfNamespaces() {
|
||||
// Get all RDF namespaces.
|
||||
$ns = rdf_get_namespaces();
|
||||
|
||||
$this->assertEqual($ns['rdfs'], 'http://www.w3.org/2000/01/rdf-schema#', 'A prefix declared once is included.');
|
||||
$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.');
|
||||
$this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', 'Two prefixes can be assigned the same namespace.');
|
||||
|
||||
// Enable rdf_conflicting_namespaces to ensure that an exception is thrown
|
||||
// when RDF namespaces are conflicting.
|
||||
\Drupal::service('module_installer')->install(array('rdf_conflicting_namespaces'), TRUE);
|
||||
try {
|
||||
$ns = rdf_get_namespaces();
|
||||
$this->fail('Expected exception not thrown for conflicting namespace declaration.');
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
$this->pass('Expected exception thrown: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
117
core/modules/rdf/src/Tests/ImageFieldAttributesTest.php
Normal file
117
core/modules/rdf/src/Tests/ImageFieldAttributesTest.php
Normal file
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\ImageFieldAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\image\Entity\ImageStyle;
|
||||
use Drupal\image\Tests\ImageFieldTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
|
||||
/**
|
||||
* Tests the RDFa markup of imagefields.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class ImageFieldAttributesTest extends ImageFieldTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf', 'image');
|
||||
|
||||
/**
|
||||
* The name of the image field used in the test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldName;
|
||||
|
||||
/**
|
||||
* The file object used in the test.
|
||||
*
|
||||
* @var \Drupal\file\FileInterface
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* The node object used in the test.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $node;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->fieldName = 'field_image';
|
||||
|
||||
// Create the image field.
|
||||
$this->createImageField($this->fieldName, 'article');
|
||||
|
||||
// Set the RDF mapping for the new field.
|
||||
rdf_get_mapping('node', 'article')
|
||||
->setFieldMapping($this->fieldName, array(
|
||||
'properties' => array('og:image'),
|
||||
'mapping_type' => 'rel',
|
||||
))
|
||||
->setBundleMapping(array('types' => array()))
|
||||
->save();
|
||||
|
||||
// Get the test image that simpletest provides.
|
||||
$image = current($this->drupalGetTestFiles('image'));
|
||||
|
||||
// Save a node with the image.
|
||||
$nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName());
|
||||
$this->node = Node::load($nid);
|
||||
$this->file = file_load($this->node->{$this->fieldName}->target_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that image fields in teasers have correct resources.
|
||||
*/
|
||||
function testNodeTeaser() {
|
||||
// Set the display options for the teaser.
|
||||
$display_options = array(
|
||||
'type' => 'image',
|
||||
'settings' => array('image_style' => 'medium', 'image_link' => 'content'),
|
||||
);
|
||||
$display = entity_get_display('node', 'article', 'teaser');
|
||||
$display->setComponent($this->fieldName, $display_options)
|
||||
->save();
|
||||
|
||||
// Render the teaser.
|
||||
$node_render_array = node_view($this->node, 'teaser');
|
||||
$html = \Drupal::service('renderer')->renderRoot($node_render_array);
|
||||
|
||||
// Parse the teaser.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
|
||||
$parser->parse($graph, $html, 'rdfa', $base_uri);
|
||||
|
||||
// Construct the node and image URIs for testing.
|
||||
$node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
|
||||
$image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri());
|
||||
|
||||
// Test relations from node to image.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $image_uri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).');
|
||||
|
||||
// Test image type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://xmlns.com/foaf/0.1/Image',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($image_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Image type found in RDF output (foaf:Image).');
|
||||
}
|
||||
|
||||
}
|
98
core/modules/rdf/src/Tests/NodeAttributesTest.php
Normal file
98
core/modules/rdf/src/Tests/NodeAttributesTest.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\NodeAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\node\Tests\NodeTestBase;
|
||||
|
||||
/**
|
||||
* Tests the RDFa markup of Nodes.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class NodeAttributesTest extends NodeTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
rdf_get_mapping('node', 'article')
|
||||
->setBundleMapping(array(
|
||||
'types' => array('sioc:Item', 'foaf:Document'),
|
||||
))
|
||||
->setFieldMapping('title', array(
|
||||
'properties' => array('dc:title'),
|
||||
))
|
||||
->setFieldMapping('created', array(
|
||||
'properties' => array('dc:date', 'dc:created'),
|
||||
'datatype' => 'xsd:dateTime',
|
||||
'datatype_callback' => array('callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'),
|
||||
))
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a node of type article and tests its RDFa markup.
|
||||
*/
|
||||
function testNodeAttributes() {
|
||||
// Create node with single quotation mark title to ensure it does not get
|
||||
// escaped more than once.
|
||||
$node = $this->drupalCreateNode(array(
|
||||
'type' => 'article',
|
||||
'title' => $this->randomMachineName(8) . "'",
|
||||
));
|
||||
|
||||
$node_uri = $node->url('canonical', ['absolute' => TRUE]);
|
||||
$base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
|
||||
|
||||
// Parses front page where the node is displayed in its teaser form.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $node->id()), 'rdfa', $base_uri);
|
||||
|
||||
// Inspects RDF graph output.
|
||||
// Node type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://rdfs.org/sioc/ns#Item',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Node type found in RDF output (sioc:Item).');
|
||||
// Node type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://xmlns.com/foaf/0.1/Document',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Node type found in RDF output (foaf:Document).');
|
||||
// Node title.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $node->getTitle(),
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Node title found in RDF output (dc:title).');
|
||||
// Node date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date('c', $node->getCreatedTime()),
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Node date found in RDF output (dc:date).');
|
||||
// Node date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date('c', $node->getCreatedTime()),
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Node date found in RDF output (dc:created).');
|
||||
}
|
||||
}
|
141
core/modules/rdf/src/Tests/RdfaAttributesTest.php
Normal file
141
core/modules/rdf/src/Tests/RdfaAttributesTest.php
Normal file
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\RdfaAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\simpletest\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests RDFa attribute generation from RDF mapping.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class RdfaAttributesTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf');
|
||||
|
||||
/**
|
||||
* Test attribute creation for mappings which use 'property'.
|
||||
*/
|
||||
function testProperty() {
|
||||
$properties = array('dc:title');
|
||||
|
||||
$mapping = array('properties' => $properties);
|
||||
$expected_attributes = array('property' => $properties);
|
||||
|
||||
$this->_testAttributes($expected_attributes, $mapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test attribute creation for mappings which use 'datatype'.
|
||||
*/
|
||||
function testDatatype() {
|
||||
$properties = array('foo:bar1');
|
||||
$datatype = 'foo:bar1type';
|
||||
|
||||
$mapping = array(
|
||||
'datatype' => $datatype,
|
||||
'properties' => $properties,
|
||||
);
|
||||
$expected_attributes = array(
|
||||
'datatype' => $datatype,
|
||||
'property' => $properties,
|
||||
);
|
||||
|
||||
$this->_testAttributes($expected_attributes, $mapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test attribute creation for mappings which override human-readable content.
|
||||
*/
|
||||
function testDatatypeCallback() {
|
||||
$properties = array('dc:created');
|
||||
$datatype = 'xsd:dateTime';
|
||||
|
||||
$date = 1252750327;
|
||||
$iso_date = date('c', $date);
|
||||
|
||||
$mapping = array(
|
||||
'datatype' => $datatype,
|
||||
'properties' => $properties,
|
||||
'datatype_callback' => array('callable' => 'date_iso8601'),
|
||||
);
|
||||
$expected_attributes = array(
|
||||
'datatype' => $datatype,
|
||||
'property' => $properties,
|
||||
'content' => $iso_date,
|
||||
);
|
||||
|
||||
$this->_testAttributes($expected_attributes, $mapping, $date);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test attribute creation for mappings which use data converters.
|
||||
*/
|
||||
function testDatatypeCallbackWithConverter() {
|
||||
$properties = array('schema:interactionCount');
|
||||
|
||||
$data = "23";
|
||||
$content = "UserComments:23";
|
||||
|
||||
$mapping = array(
|
||||
'properties' => $properties,
|
||||
'datatype_callback' => array(
|
||||
'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
|
||||
'arguments' => array('interaction_type' => 'UserComments'),
|
||||
),
|
||||
);
|
||||
$expected_attributes = array(
|
||||
'property' => $properties,
|
||||
'content' => $content,
|
||||
);
|
||||
|
||||
$this->_testAttributes($expected_attributes, $mapping, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test attribute creation for mappings which use 'rel'.
|
||||
*/
|
||||
function testRel() {
|
||||
$properties = array('sioc:has_creator', 'dc:creator');
|
||||
|
||||
$mapping = array(
|
||||
'properties' => $properties,
|
||||
'mapping_type' => 'rel',
|
||||
);
|
||||
$expected_attributes = array('rel' => $properties);
|
||||
|
||||
$this->_testAttributes($expected_attributes, $mapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test attribute generation.
|
||||
*
|
||||
* @param array $expected_attributes
|
||||
* The expected return of rdf_rdfa_attributes.
|
||||
* @param array $field_mapping
|
||||
* The field mapping to merge into the RDF mapping config.
|
||||
* @param mixed $data
|
||||
* The data to pass into the datatype callback, if specified.
|
||||
*/
|
||||
protected function _testAttributes($expected_attributes, $field_mapping, $data = NULL) {
|
||||
$mapping = rdf_get_mapping('node', 'article')
|
||||
->setFieldMapping('field_test', $field_mapping)
|
||||
->getPreparedFieldMapping('field_test');
|
||||
$attributes = rdf_rdfa_attributes($mapping, $data);
|
||||
ksort($expected_attributes);
|
||||
ksort($attributes);
|
||||
$this->assertEqual($expected_attributes, $attributes);
|
||||
}
|
||||
|
||||
}
|
534
core/modules/rdf/src/Tests/StandardProfileTest.php
Normal file
534
core/modules/rdf/src/Tests/StandardProfileTest.php
Normal file
|
@ -0,0 +1,534 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\StandardProfileTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\image\Entity\ImageStyle;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the RDF mappings and RDFa markup of the standard profile.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class StandardProfileTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* The profile used during tests.
|
||||
*
|
||||
* This purposefully uses the standard profile.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $profile = 'standard';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $baseUri;
|
||||
|
||||
/**
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $webUser;
|
||||
|
||||
/**
|
||||
* @var \Drupal\taxonomy\TermInterface
|
||||
*/
|
||||
protected $term;
|
||||
|
||||
/**
|
||||
* @var \Drupal\file\FileInterface
|
||||
*/
|
||||
protected $image;
|
||||
|
||||
/**
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $article;
|
||||
|
||||
/**
|
||||
* @var \Drupal\comment\CommentInterface
|
||||
*/
|
||||
protected $articleComment;
|
||||
|
||||
/**
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $imageUri;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $termUri;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $articleUri;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $pageUri;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $authorUri;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $articleCommentUri;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $commenterUri;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->baseUri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
|
||||
|
||||
// Create two test users.
|
||||
$this->adminUser = $this->drupalCreateUser(array(
|
||||
'administer content types',
|
||||
'administer comments',
|
||||
'access comments',
|
||||
'access content',
|
||||
));
|
||||
$this->webUser = $this->drupalCreateUser(array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
'access content',
|
||||
));
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Create term.
|
||||
$this->term = entity_create('taxonomy_term', array(
|
||||
'name' => $this->randomMachineName(),
|
||||
'description' => $this->randomMachineName(),
|
||||
'vid' => 'tags',
|
||||
));
|
||||
$this->term->save();
|
||||
|
||||
// Create image.
|
||||
file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
|
||||
$this->image = entity_create('file', array('uri' => 'public://example.jpg'));
|
||||
$this->image->save();
|
||||
|
||||
// Create article.
|
||||
$article_settings = array(
|
||||
'type' => 'article',
|
||||
'promote' => NODE_PROMOTED,
|
||||
'field_image' => array(
|
||||
array(
|
||||
'target_id' => $this->image->id(),
|
||||
),
|
||||
),
|
||||
'field_tags' => array(
|
||||
array(
|
||||
'target_id' => $this->term->id(),
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->article = $this->drupalCreateNode($article_settings);
|
||||
// Create second article to test teaser list.
|
||||
$this->drupalCreateNode(array('type' => 'article', 'promote' => NODE_PROMOTED,));
|
||||
|
||||
// Create article comment.
|
||||
$this->articleComment = $this->saveComment($this->article->id(), $this->webUser->id(), NULL, 0);
|
||||
|
||||
// Create page.
|
||||
$this->page = $this->drupalCreateNode(array('type' => 'page'));
|
||||
|
||||
// Set URIs.
|
||||
// Image.
|
||||
$image_file = $this->article->get('field_image')->entity;
|
||||
$this->imageUri = ImageStyle::load('large')->buildUrl($image_file->getFileUri());
|
||||
// Term.
|
||||
$this->termUri = $this->term->url('canonical', array('absolute' => TRUE));
|
||||
// Article.
|
||||
$this->articleUri = $this->article->url('canonical', array('absolute' => TRUE));
|
||||
// Page.
|
||||
$this->pageUri = $this->page->url('canonical', array('absolute' => TRUE));
|
||||
// Author.
|
||||
$this->authorUri = $this->adminUser->url('canonical', array('absolute' => TRUE));
|
||||
// Comment.
|
||||
$this->articleCommentUri = $this->articleComment->url('canonical', array('absolute' => TRUE));
|
||||
// Commenter.
|
||||
$this->commenterUri = $this->webUser->url('canonical', array('absolute' => TRUE));
|
||||
|
||||
$this->drupalLogout();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that data is exposed correctly when using standard profile.
|
||||
*
|
||||
* Because tests using standard profile take a very long time to run, and
|
||||
* because there is no manipulation of config or data within the test, simply
|
||||
* run all the tests from within this function.
|
||||
*/
|
||||
public function testRdfaOutput() {
|
||||
$this->doFrontPageRdfaTests();
|
||||
$this->doArticleRdfaTests();
|
||||
$this->doPageRdfaTests();
|
||||
$this->doUserRdfaTests();
|
||||
$this->doTermRdfaTests();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that data is exposed in the front page teasers.
|
||||
*/
|
||||
protected function doFrontPageRdfaTests() {
|
||||
// Feed the HTML into the parser.
|
||||
$graph = $this->getRdfGraph(Url::fromRoute('<front>'));
|
||||
|
||||
// Ensure that both articles are listed.
|
||||
$this->assertEqual(2, count($graph->allOfType('http://schema.org/Article')), 'Two articles found on front page.');
|
||||
|
||||
// Test interaction count.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => 'UserComments:1',
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/interactionCount', $expected_value), "Teaser comment count was found (schema:interactionCount).");
|
||||
|
||||
// Test the properties that are common between pages and articles and are
|
||||
// displayed in full and teaser mode.
|
||||
$this->assertRdfaCommonNodeProperties($graph, $this->article, "Teaser");
|
||||
// Test properties that are displayed in both teaser and full mode.
|
||||
$this->assertRdfaArticleProperties($graph, "Teaser");
|
||||
|
||||
// @todo Once the image points to the original instead of the processed
|
||||
// image, move this to testArticleProperties().
|
||||
$image_file = $this->article->get('field_image')->entity;
|
||||
$image_uri = ImageStyle::load('medium')->buildUrl($image_file->getFileUri());
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $image_uri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/image', $expected_value), "Teaser image was found (schema:image).");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that article data is exposed using RDFa.
|
||||
*
|
||||
* Two fields are not tested for output here. Changed date is not displayed
|
||||
* on the page, so there is no test for output in node view. Comment count is
|
||||
* displayed in teaser view, so it is tested in the front article tests.
|
||||
*/
|
||||
protected function doArticleRdfaTests() {
|
||||
// Feed the HTML into the parser.
|
||||
$graph = $this->getRdfGraph($this->article->urlInfo());
|
||||
|
||||
// Type.
|
||||
$this->assertEqual($graph->type($this->articleUri), 'schema:Article', 'Article type was found (schema:Article).');
|
||||
|
||||
// Test the properties that are common between pages and articles.
|
||||
$this->assertRdfaCommonNodeProperties($graph, $this->article, "Article");
|
||||
// Test properties that are displayed in both teaser and full mode.
|
||||
$this->assertRdfaArticleProperties($graph, "Article");
|
||||
// Test the comment properties displayed on articles.
|
||||
$this->assertRdfaNodeCommentProperties($graph);
|
||||
|
||||
// @todo Once the image points to the original instead of the processed
|
||||
// image, move this to testArticleProperties().
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->imageUri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/image', $expected_value), "Article image was found (schema:image).");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that page data is exposed using RDFa.
|
||||
*
|
||||
* Two fields are not tested for output here. Changed date is not displayed
|
||||
* on the page, so there is no test for output in node view. Comment count is
|
||||
* displayed in teaser view, so it is tested in the front page tests.
|
||||
*/
|
||||
protected function doPageRdfaTests() {
|
||||
// The standard profile hides the created date on pages. Revert display to
|
||||
// true for testing.
|
||||
// @todo Clean-up standard profile defaults.
|
||||
$node_type = NodeType::load('page');
|
||||
$node_type->setDisplaySubmitted(TRUE);
|
||||
$node_type->save();
|
||||
|
||||
// Feed the HTML into the parser.
|
||||
$graph = $this->getRdfGraph($this->page->urlInfo());
|
||||
|
||||
// Type.
|
||||
$this->assertEqual($graph->type($this->pageUri), 'schema:WebPage', 'Page type was found (schema:WebPage).');
|
||||
|
||||
// Test the properties that are common between pages and articles.
|
||||
$this->assertRdfaCommonNodeProperties($graph, $this->page, "Page");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that user data is exposed on user page.
|
||||
*/
|
||||
protected function doUserRdfaTests() {
|
||||
$this->drupalLogin($this->rootUser);
|
||||
|
||||
// Feed the HTML into the parser.
|
||||
$graph = $this->getRdfGraph($this->adminUser->urlInfo());
|
||||
|
||||
// User type.
|
||||
$this->assertEqual($graph->type($this->authorUri), 'schema:Person', "User type was found (schema:Person) on user page.");
|
||||
|
||||
// User name.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $this->adminUser->label(),
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->authorUri, 'http://schema.org/name', $expected_value), "User name was found (schema:name) on user page.");
|
||||
|
||||
$this->drupalLogout();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that term data is exposed on term page.
|
||||
*/
|
||||
protected function doTermRdfaTests() {
|
||||
// Feed the HTML into the parser.
|
||||
$graph = $this->getRdfGraph($this->term->urlInfo());
|
||||
|
||||
// Term type.
|
||||
$this->assertEqual($graph->type($this->termUri), 'schema:Thing', "Term type was found (schema:Thing) on term page.");
|
||||
|
||||
// Term name.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $this->term->getName(),
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->termUri, 'http://schema.org/name', $expected_value), "Term name was found (schema:name) on term page.");
|
||||
|
||||
// @todo Add test for term description once it is a field:
|
||||
// https://www.drupal.org/node/569434.
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests output for properties held in common between articles and pages.
|
||||
*
|
||||
* @param \EasyRdf_Graph $graph
|
||||
* The EasyRDF graph object.
|
||||
* @param \Drupal\node\NodeInterface $node
|
||||
* The node being displayed.
|
||||
* @param string $message_prefix
|
||||
* The word to use in the test assertion message.
|
||||
*/
|
||||
protected function assertRdfaCommonNodeProperties($graph, NodeInterface $node, $message_prefix) {
|
||||
$uri = $node->url('canonical', array('absolute' => TRUE));
|
||||
|
||||
// Title.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $node->get('title')->value,
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($uri, 'http://schema.org/name', $expected_value), "$message_prefix title was found (schema:name).");
|
||||
|
||||
// Created date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date_iso8601($node->get('created')->value),
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($uri, 'http://schema.org/dateCreated', $expected_value), "$message_prefix created date was found (schema:dateCreated) in teaser.");
|
||||
|
||||
// Body.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $node->get('body')->value,
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($uri, 'http://schema.org/text', $expected_value), "$message_prefix body was found (schema:text) in teaser.");
|
||||
|
||||
// Author.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->authorUri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($uri, 'http://schema.org/author', $expected_value), "$message_prefix author was found (schema:author) in teaser.");
|
||||
|
||||
// Author type.
|
||||
$this->assertEqual($graph->type($this->authorUri), 'schema:Person', "$message_prefix author type was found (schema:Person).");
|
||||
|
||||
// Author name.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $this->adminUser->label(),
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->authorUri, 'http://schema.org/name', $expected_value), "$message_prefix author name was found (schema:name).");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests output for article properties displayed in both view modes.
|
||||
*
|
||||
* @param \EasyRdf_Graph $graph
|
||||
* The EasyRDF graph object.
|
||||
* @param string $message_prefix
|
||||
* The word to use in the test assertion message.
|
||||
*/
|
||||
protected function assertRdfaArticleProperties($graph, $message_prefix) {
|
||||
// Tags.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->termUri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/about', $expected_value), "$message_prefix tag was found (schema:about).");
|
||||
|
||||
// Tag type.
|
||||
// @todo Enable with https://www.drupal.org/node/2072791.
|
||||
//$this->assertEqual($graph->type($this->termUri), 'schema:Thing', 'Tag type was found (schema:Thing).');
|
||||
|
||||
// Tag name.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $this->term->getName(),
|
||||
'lang' => 'en',
|
||||
);
|
||||
// @todo Enable with https://www.drupal.org/node/2072791.
|
||||
//$this->assertTrue($graph->hasProperty($this->termUri, 'http://schema.org/name', $expected_value), "$message_prefix name was found (schema:name).");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests output for comment properties on nodes in full page view mode.
|
||||
*
|
||||
* @param \EasyRdf_Graph $graph
|
||||
* The EasyRDF graph object.
|
||||
*/
|
||||
protected function assertRdfaNodeCommentProperties($graph) {
|
||||
// Relationship between node and comment.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->articleCommentUri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/comment', $expected_value), 'Relationship between node and comment found (schema:comment).');
|
||||
|
||||
// Comment type.
|
||||
$this->assertEqual($graph->type($this->articleCommentUri), 'schema:Comment', 'Comment type was found (schema:Comment).');
|
||||
|
||||
// Comment title.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $this->articleComment->get('subject')->value,
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/name', $expected_value), 'Article comment title was found (schema:name).');
|
||||
|
||||
// Comment created date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date_iso8601($this->articleComment->get('created')->value),
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/dateCreated', $expected_value), 'Article comment created date was found (schema:dateCreated).');
|
||||
|
||||
// Comment body.
|
||||
$text = $this->articleComment->get('comment_body')->value;
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
// There is an extra carriage return in the when parsing comments as
|
||||
// output by Bartik, so it must be added to the expected value.
|
||||
'value' => "$text
|
||||
",
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/text', $expected_value), 'Article comment body was found (schema:text).');
|
||||
|
||||
// Comment uid.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->commenterUri,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/author', $expected_value), 'Article comment author was found (schema:author).');
|
||||
|
||||
// Comment author type.
|
||||
$this->assertEqual($graph->type($this->commenterUri), 'schema:Person', 'Comment author type was found (schema:Person).');
|
||||
|
||||
// Comment author name.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $this->webUser->getUsername(),
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->commenterUri, 'http://schema.org/name', $expected_value), 'Comment author name was found (schema:name).');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comment entity.
|
||||
*
|
||||
* @param int $nid
|
||||
* Node id which will hold the comment.
|
||||
* @param int $uid
|
||||
* User id of the author of the comment. Can be NULL if $contact provided.
|
||||
* @param mixed $contact
|
||||
* Set to NULL for no contact info, TRUE to ignore success checking, and
|
||||
* array of values to set contact info.
|
||||
* @param int $pid
|
||||
* Comment id of the parent comment in a thread.
|
||||
*
|
||||
* @return \Drupal\comment\Entity\Comment
|
||||
* The saved comment.
|
||||
*/
|
||||
protected function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
|
||||
$values = array(
|
||||
'entity_id' => $nid,
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'uid' => $uid,
|
||||
'pid' => $pid,
|
||||
'subject' => $this->randomMachineName(),
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'status' => 1,
|
||||
);
|
||||
if ($contact) {
|
||||
$values += $contact;
|
||||
}
|
||||
|
||||
$comment = entity_create('comment', $values);
|
||||
$comment->save();
|
||||
return $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the EasyRdf_Graph object for a page.
|
||||
*
|
||||
* @param \Drupal\Core\Url $url
|
||||
* The URL object for the page.
|
||||
*
|
||||
* @return \EasyRdf_Graph
|
||||
* The RDF graph object.
|
||||
*/
|
||||
protected function getRdfGraph(Url $url) {
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet($url), 'rdfa', $this->baseUri);
|
||||
return $graph;
|
||||
}
|
||||
}
|
85
core/modules/rdf/src/Tests/TaxonomyAttributesTest.php
Normal file
85
core/modules/rdf/src/Tests/TaxonomyAttributesTest.php
Normal file
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\TaxonomyAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\taxonomy\Tests\TaxonomyTestBase;
|
||||
|
||||
/**
|
||||
* Tests the RDFa markup of Taxonomy terms.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class TaxonomyAttributesTest extends TaxonomyTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf', 'views');
|
||||
|
||||
/**
|
||||
* Vocabulary created for testing purposes.
|
||||
*
|
||||
* @var \Drupal\taxonomy\VocabularyInterface
|
||||
*/
|
||||
protected $vocabulary;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->vocabulary = $this->createVocabulary();
|
||||
|
||||
// RDF mapping - term bundle.
|
||||
rdf_get_mapping('taxonomy_term', $this->vocabulary->id())
|
||||
->setBundleMapping(array('types' => array('skos:Concept')))
|
||||
->setFieldMapping('name', array(
|
||||
'properties' => array('rdfs:label', 'skos:prefLabel'),
|
||||
))
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a random term and ensures the RDF output is correct.
|
||||
*/
|
||||
function testTaxonomyTermRdfaAttributes() {
|
||||
$term = $this->createTerm($this->vocabulary);
|
||||
$term_uri = $term->url('canonical', ['absolute' => TRUE]);
|
||||
|
||||
// Parses the term's page and checks that the RDF output is correct.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
|
||||
$parser->parse($graph, $this->drupalGet('taxonomy/term/' . $term->id()), 'rdfa', $base_uri);
|
||||
|
||||
// Inspects RDF graph output.
|
||||
// Term type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Term type found in RDF output (skos:Concept).');
|
||||
// Term label.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $term->getName(),
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Term label found in RDF output (rdfs:label).');
|
||||
// Term label.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $term->getName(),
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2004/02/skos/core#prefLabel', $expected_value), 'Term label found in RDF output (skos:prefLabel).');
|
||||
|
||||
// @todo Add test for term description once it is a field:
|
||||
// https://www.drupal.org/node/569434.
|
||||
}
|
||||
}
|
112
core/modules/rdf/src/Tests/UserAttributesTest.php
Normal file
112
core/modules/rdf/src/Tests/UserAttributesTest.php
Normal file
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\rdf\Tests\UserAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the RDFa markup of Users.
|
||||
*
|
||||
* @group rdf
|
||||
*/
|
||||
class UserAttributesTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('rdf', 'node');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
rdf_get_mapping('user', 'user')
|
||||
->setBundleMapping(array(
|
||||
'types' => array('sioc:UserAccount'),
|
||||
))
|
||||
->setFieldMapping('name', array(
|
||||
'properties' => array('foaf:name'),
|
||||
))
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if default mapping for user is being used.
|
||||
*
|
||||
* Creates a random user and ensures the default mapping for the user is
|
||||
* being used.
|
||||
*/
|
||||
function testUserAttributesInMarkup() {
|
||||
// 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'));
|
||||
|
||||
$authors = array(
|
||||
$this->drupalCreateUser(array(), $this->randomMachineName(30)),
|
||||
$this->drupalCreateUser(array(), $this->randomMachineName(20)),
|
||||
$this->drupalCreateUser(array(), $this->randomMachineName(5))
|
||||
);
|
||||
|
||||
$this->drupalLogin($user1);
|
||||
|
||||
$this->drupalCreateContentType(array('type' => 'article'));
|
||||
|
||||
/** @var \Drupal\user\UserInterface[] $authors */
|
||||
foreach ($authors as $author) {
|
||||
$account_uri = $author->url('canonical', ['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 = \Drupal::url('<front>', [], ['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' => $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 creates a node.
|
||||
$this->drupalLogin($author);
|
||||
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
|
||||
$this->drupalLogin($user1);
|
||||
|
||||
// Parses the node created by the user.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$base_uri = \Drupal::url('<front>', [], ['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).');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
22
core/modules/rdf/templates/rdf-metadata.html.twig
Normal file
22
core/modules/rdf/templates/rdf-metadata.html.twig
Normal file
|
@ -0,0 +1,22 @@
|
|||
{#
|
||||
/**
|
||||
* @file
|
||||
* Default theme implementation for empty spans with RDF attributes.
|
||||
*
|
||||
* The XHTML+RDFa doctype allows either <span></span> or <span /> syntax to
|
||||
* be used, but for maximum browser compatibility, W3C recommends the
|
||||
* former when serving pages using the text/html media type, see
|
||||
* http://www.w3.org/TR/xhtml1/#C_3.
|
||||
*
|
||||
* Available variables:
|
||||
* - metadata: Each item within corresponds to its own set of attributes,
|
||||
* and therefore, needs its own 'attributes' element.
|
||||
*
|
||||
* @see template_preprocess_rdf_metadata()
|
||||
*
|
||||
* @ingroup themeable
|
||||
*/
|
||||
#}
|
||||
{% for attributes in metadata %}
|
||||
<span{{ attributes.addClass('hidden') }}></span>
|
||||
{% endfor %}
|
|
@ -0,0 +1,8 @@
|
|||
name: 'RDF module conflicting namespaces test'
|
||||
type: module
|
||||
description: 'Test conflicting namespace declaration.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- rdf
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Test the namespace registration functionality.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_rdf_namespaces().
|
||||
*/
|
||||
function rdf_conflicting_namespaces_rdf_namespaces() {
|
||||
return array(
|
||||
'dc' => 'http://purl.org/conflicting/namespace',
|
||||
);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
name: 'RDF module namespaces test'
|
||||
type: module
|
||||
description: 'Test namespace declaration.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- rdf
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Test the namespace registration functionality.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_rdf_namespaces().
|
||||
*/
|
||||
function rdf_test_namespaces_rdf_namespaces() {
|
||||
return array(
|
||||
'foaf' => 'http://xmlns.com/foaf/0.1/',
|
||||
'foaf1' => 'http://xmlns.com/foaf/0.1/',
|
||||
);
|
||||
}
|
|
@ -0,0 +1,149 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\rdf\Unit\RdfMappingConfigEntityUnitTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\rdf\Unit;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\rdf\Entity\RdfMapping;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\rdf\Entity\RdfMapping
|
||||
* @group rdf
|
||||
*/
|
||||
class RdfMappingConfigEntityUnitTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The entity type used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityType;
|
||||
|
||||
/**
|
||||
* The entity manager used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The ID of the type of the entity under test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityTypeId;
|
||||
|
||||
/**
|
||||
* The UUID generator used for testing.
|
||||
*
|
||||
* @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $uuid;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->entityTypeId = $this->randomMachineName();
|
||||
|
||||
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$this->entityType->expects($this->any())
|
||||
->method('getProvider')
|
||||
->will($this->returnValue('entity'));
|
||||
|
||||
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
|
||||
|
||||
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('entity.manager', $this->entityManager);
|
||||
$container->set('uuid', $this->uuid);
|
||||
\Drupal::setContainer($container);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::calculateDependencies
|
||||
*/
|
||||
public function testCalculateDependencies() {
|
||||
$target_entity_type_id = $this->randomMachineName(16);
|
||||
|
||||
$target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$target_entity_type->expects($this->any())
|
||||
->method('getProvider')
|
||||
->will($this->returnValue('test_module'));
|
||||
$values = array('targetEntityType' => $target_entity_type_id);
|
||||
$target_entity_type->expects($this->any())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue('bundle'));
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with($target_entity_type_id)
|
||||
->will($this->returnValue($target_entity_type));
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->will($this->returnValue($this->entityType));
|
||||
|
||||
$entity = new RdfMapping($values, $this->entityTypeId);
|
||||
$dependencies = $entity->calculateDependencies();
|
||||
$this->assertArrayNotHasKey('config', $dependencies);
|
||||
$this->assertContains('test_module', $dependencies['module']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::calculateDependencies
|
||||
*/
|
||||
public function testCalculateDependenciesWithEntityBundle() {
|
||||
$target_entity_type_id = $this->randomMachineName(16);
|
||||
$target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$target_entity_type->expects($this->any())
|
||||
->method('getProvider')
|
||||
->will($this->returnValue('test_module'));
|
||||
$bundle_id = $this->randomMachineName(10);
|
||||
$values = array('targetEntityType' => $target_entity_type_id , 'bundle' => $bundle_id);
|
||||
|
||||
$bundle_entity_type_id = $this->randomMachineName(17);
|
||||
$bundle_entity = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityInterface');
|
||||
$bundle_entity
|
||||
->expects($this->once())
|
||||
->method('getConfigDependencyName')
|
||||
->will($this->returnValue('test_module.type.' . $bundle_id));
|
||||
|
||||
$target_entity_type->expects($this->any())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue($bundle_entity_type_id));
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with($target_entity_type_id)
|
||||
->will($this->returnValue($target_entity_type));
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->will($this->returnValue($this->entityType));
|
||||
|
||||
$storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
|
||||
$storage->expects($this->once())
|
||||
->method('load')
|
||||
->with($bundle_id)
|
||||
->will($this->returnValue($bundle_entity));
|
||||
|
||||
$this->entityManager->expects($this->once())
|
||||
->method('getStorage')
|
||||
->with($bundle_entity_type_id)
|
||||
->will($this->returnValue($storage));
|
||||
|
||||
$entity = new RdfMapping($values, $this->entityTypeId);
|
||||
$dependencies = $entity->calculateDependencies();
|
||||
$this->assertContains('test_module.type.' . $bundle_id, $dependencies['config']);
|
||||
$this->assertContains('test_module', $dependencies['module']);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue