3 namespace Drupal\Tests\rdf\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
8 * Tests RDFa attribute generation from RDF mapping.
12 class RdfaAttributesTest extends KernelTestBase {
19 public static $modules = ['rdf'];
22 * Test attribute creation for mappings which use 'property'.
24 public function testProperty() {
25 $properties = ['dc:title'];
27 $mapping = ['properties' => $properties];
28 $expected_attributes = ['property' => $properties];
30 $this->_testAttributes($expected_attributes, $mapping);
34 * Test attribute creation for mappings which use 'datatype'.
36 public function testDatatype() {
37 $properties = ['foo:bar1'];
38 $datatype = 'foo:bar1type';
41 'datatype' => $datatype,
42 'properties' => $properties,
44 $expected_attributes = [
45 'datatype' => $datatype,
46 'property' => $properties,
49 $this->_testAttributes($expected_attributes, $mapping);
53 * Test attribute creation for mappings which override human-readable content.
55 public function testDatatypeCallback() {
56 $properties = ['dc:created'];
57 $datatype = 'xsd:dateTime';
60 $iso_date = date('c', $date);
63 'datatype' => $datatype,
64 'properties' => $properties,
65 'datatype_callback' => ['callable' => 'date_iso8601'],
67 $expected_attributes = [
68 'datatype' => $datatype,
69 'property' => $properties,
70 'content' => $iso_date,
73 $this->_testAttributes($expected_attributes, $mapping, $date);
78 * Test attribute creation for mappings which use data converters.
80 public function testDatatypeCallbackWithConverter() {
81 $properties = ['schema:interactionCount'];
84 $content = "UserComments:23";
87 'properties' => $properties,
88 'datatype_callback' => [
89 'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
90 'arguments' => ['interaction_type' => 'UserComments'],
93 $expected_attributes = [
94 'property' => $properties,
95 'content' => $content,
98 $this->_testAttributes($expected_attributes, $mapping, $data);
102 * Test attribute creation for mappings which use 'rel'.
104 public function testRel() {
105 $properties = ['sioc:has_creator', 'dc:creator'];
108 'properties' => $properties,
109 'mapping_type' => 'rel',
111 $expected_attributes = ['rel' => $properties];
113 $this->_testAttributes($expected_attributes, $mapping);
117 * Helper function to test attribute generation.
119 * @param array $expected_attributes
120 * The expected return of rdf_rdfa_attributes.
121 * @param array $field_mapping
122 * The field mapping to merge into the RDF mapping config.
124 * The data to pass into the datatype callback, if specified.
126 protected function _testAttributes($expected_attributes, $field_mapping, $data = NULL) {
127 $mapping = rdf_get_mapping('node', 'article')
128 ->setFieldMapping('field_test', $field_mapping)
129 ->getPreparedFieldMapping('field_test');
130 $attributes = rdf_rdfa_attributes($mapping, $data);
131 ksort($expected_attributes);
133 $this->assertEqual($expected_attributes, $attributes);