View source
<?php
namespace Drupal\Tests\rdf\Kernel;
use Drupal\KernelTests\KernelTestBase;
class RdfaAttributesTest extends KernelTestBase {
public static $modules = [
'rdf',
];
public function testProperty() {
$properties = [
'dc:title',
];
$mapping = [
'properties' => $properties,
];
$expected_attributes = [
'property' => $properties,
];
$this
->_testAttributes($expected_attributes, $mapping);
}
public function testDatatype() {
$properties = [
'foo:bar1',
];
$datatype = 'foo:bar1type';
$mapping = [
'datatype' => $datatype,
'properties' => $properties,
];
$expected_attributes = [
'datatype' => $datatype,
'property' => $properties,
];
$this
->_testAttributes($expected_attributes, $mapping);
}
public function testDatatypeCallback() {
$properties = [
'dc:created',
];
$datatype = 'xsd:dateTime';
$date = 1252750327;
$iso_date = $this->container
->get('date.formatter')
->format($date, 'custom', 'c', 'UTC');
$mapping = [
'datatype' => $datatype,
'properties' => $properties,
'datatype_callback' => [
'callable' => 'Drupal\\rdf\\CommonDataConverter::dateIso8601Value',
],
];
$expected_attributes = [
'datatype' => $datatype,
'property' => $properties,
'content' => $iso_date,
];
$this
->_testAttributes($expected_attributes, $mapping, [
'value' => $date,
]);
}
public function testDatatypeCallbackWithConverter() {
$properties = [
'schema:interactionCount',
];
$data = "23";
$content = "UserComments:23";
$mapping = [
'properties' => $properties,
'datatype_callback' => [
'callable' => 'Drupal\\rdf\\SchemaOrgDataConverter::interactionCount',
'arguments' => [
'interaction_type' => 'UserComments',
],
],
];
$expected_attributes = [
'property' => $properties,
'content' => $content,
];
$this
->_testAttributes($expected_attributes, $mapping, $data);
}
public function testRel() {
$properties = [
'sioc:has_creator',
'dc:creator',
];
$mapping = [
'properties' => $properties,
'mapping_type' => 'rel',
];
$expected_attributes = [
'rel' => $properties,
];
$this
->_testAttributes($expected_attributes, $mapping);
}
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);
}
}