Move into nested docroot

This commit is contained in:
Rob Davies 2017-02-13 15:31:17 +00:00
parent 83a0d3a149
commit c8b70abde9
13405 changed files with 0 additions and 0 deletions

View file

@ -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

View file

@ -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',
);
}

View file

@ -0,0 +1,8 @@
name: 'RDF module namespaces test'
type: module
description: 'Test namespace declaration.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- rdf

View file

@ -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/',
);
}

View file

@ -0,0 +1,111 @@
<?php
namespace Drupal\Tests\rdf\Kernel;
use Drupal\KernelTests\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.');
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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 = EntityTest::create(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'));
}
}

View file

@ -0,0 +1,51 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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 = EntityTest::create(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));
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\field\Tests\EntityReference\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 = ['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 = $this->container->get('entity_type.manager')
->getStorage($this->entityType)
->create(array('name' => $this->randomMachineName()));
$this->targetEntity->save();
// Create the entity that will have the entity reference field.
$this->entity = $this->container->get('entity_type.manager')
->getStorage($this->entityType)
->create(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'));
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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 = EntityTest::create();
$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));
}
}

View file

@ -0,0 +1,189 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
abstract class FieldRdfaTestBase extends FieldKernelTestBase {
/**
* 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 = FALSE;
/**
* 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) {
print_r($output);
print_r($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()) {
FieldStorageConfig::create(array(
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => $this->fieldType,
))->save();
FieldConfig::create([
'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;
}
}
}

View file

@ -0,0 +1,184 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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 = EntityTest::create(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 = EntityTest::create(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 = EntityTest::create(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);
}
}

View file

@ -0,0 +1,204 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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__items") and @content]');
$this->assertFalse($result);
}
/**
* Tests the integer formatter with settings.
*/
public function testIntegerFormatterWithSettings() {
\Drupal::service('theme_handler')->install(['classy']);
\Drupal::service('theme_handler')->setDefault('classy');
$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__items") and @content]');
$this->assertFalse($result);
}
/**
* Tests the float formatter with settings.
*/
public function testFloatFormatterWithSettings() {
\Drupal::service('theme_handler')->install(['classy']);
\Drupal::service('theme_handler')->setDefault('classy');
$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__items") and @content]');
$this->assertFalse($result);
}
/**
* Tests the float formatter with a scale. Scale is exercised.
*/
public function testFloatFormatterWithScaleExercised() {
\Drupal::service('theme_handler')->install(['classy']);
\Drupal::service('theme_handler')->setDefault('classy');
$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__items") and @content]');
$this->assertFalse($result);
}
/**
* Tests the decimal formatter with settings.
*/
public function testDecimalFormatterWithSettings() {
\Drupal::service('theme_handler')->install(['classy']);
\Drupal::service('theme_handler')->setDefault('classy');
$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 = EntityTest::create(array());
$this->entity->{$this->fieldName}->value = $testValue;
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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';
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:text'),
))->save();
// Set up test entity.
$this->entity = EntityTest::create();
$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));
}
}

View file

@ -0,0 +1,69 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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 = EntityTest::create(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);
}
}

View file

@ -0,0 +1,73 @@
<?php
namespace Drupal\Tests\rdf\Kernel\Field;
use Drupal\entity_test\Entity\EntityTest;
/**
* 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 = EntityTest::create();
$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));
}
}

View file

@ -0,0 +1,136 @@
<?php
namespace Drupal\Tests\rdf\Kernel;
use Drupal\KernelTests\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);
}
}

View file

@ -0,0 +1,126 @@
<?php
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(NULL));
$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()->getDependencies();
$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);
$target_entity_type->expects($this->any())
->method('getBundleConfigDependency')
->will($this->returnValue(array('type' => 'config', 'name' => 'test_module.type.' . $bundle_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));
$entity = new RdfMapping($values, $this->entityTypeId);
$dependencies = $entity->calculateDependencies()->getDependencies();
$this->assertContains('test_module.type.' . $bundle_id, $dependencies['config']);
$this->assertContains('test_module', $dependencies['module']);
}
}