Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\rdf\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\KernelTests\KernelTestBase;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Tests RDFa attribute generation from RDF mapping.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @group rdf
|
Chris@0
|
11 */
|
Chris@0
|
12 class RdfaAttributesTest extends KernelTestBase {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Modules to enable.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var array
|
Chris@0
|
18 */
|
Chris@0
|
19 public static $modules = ['rdf'];
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Test attribute creation for mappings which use 'property'.
|
Chris@0
|
23 */
|
Chris@0
|
24 public function testProperty() {
|
Chris@0
|
25 $properties = ['dc:title'];
|
Chris@0
|
26
|
Chris@0
|
27 $mapping = ['properties' => $properties];
|
Chris@0
|
28 $expected_attributes = ['property' => $properties];
|
Chris@0
|
29
|
Chris@0
|
30 $this->_testAttributes($expected_attributes, $mapping);
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Test attribute creation for mappings which use 'datatype'.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function testDatatype() {
|
Chris@0
|
37 $properties = ['foo:bar1'];
|
Chris@0
|
38 $datatype = 'foo:bar1type';
|
Chris@0
|
39
|
Chris@0
|
40 $mapping = [
|
Chris@0
|
41 'datatype' => $datatype,
|
Chris@0
|
42 'properties' => $properties,
|
Chris@0
|
43 ];
|
Chris@0
|
44 $expected_attributes = [
|
Chris@0
|
45 'datatype' => $datatype,
|
Chris@0
|
46 'property' => $properties,
|
Chris@0
|
47 ];
|
Chris@0
|
48
|
Chris@0
|
49 $this->_testAttributes($expected_attributes, $mapping);
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Test attribute creation for mappings which override human-readable content.
|
Chris@0
|
54 */
|
Chris@0
|
55 public function testDatatypeCallback() {
|
Chris@0
|
56 $properties = ['dc:created'];
|
Chris@0
|
57 $datatype = 'xsd:dateTime';
|
Chris@0
|
58
|
Chris@0
|
59 $date = 1252750327;
|
Chris@18
|
60 $iso_date = $this->container->get('date.formatter')->format($date, 'custom', 'c', 'UTC');
|
Chris@0
|
61
|
Chris@0
|
62 $mapping = [
|
Chris@0
|
63 'datatype' => $datatype,
|
Chris@0
|
64 'properties' => $properties,
|
Chris@18
|
65 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
|
Chris@0
|
66 ];
|
Chris@0
|
67 $expected_attributes = [
|
Chris@0
|
68 'datatype' => $datatype,
|
Chris@0
|
69 'property' => $properties,
|
Chris@0
|
70 'content' => $iso_date,
|
Chris@0
|
71 ];
|
Chris@0
|
72
|
Chris@18
|
73 $this->_testAttributes($expected_attributes, $mapping, ['value' => $date]);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Test attribute creation for mappings which use data converters.
|
Chris@0
|
78 */
|
Chris@0
|
79 public function testDatatypeCallbackWithConverter() {
|
Chris@0
|
80 $properties = ['schema:interactionCount'];
|
Chris@0
|
81
|
Chris@0
|
82 $data = "23";
|
Chris@0
|
83 $content = "UserComments:23";
|
Chris@0
|
84
|
Chris@0
|
85 $mapping = [
|
Chris@0
|
86 'properties' => $properties,
|
Chris@0
|
87 'datatype_callback' => [
|
Chris@0
|
88 'callable' => 'Drupal\rdf\SchemaOrgDataConverter::interactionCount',
|
Chris@0
|
89 'arguments' => ['interaction_type' => 'UserComments'],
|
Chris@0
|
90 ],
|
Chris@0
|
91 ];
|
Chris@0
|
92 $expected_attributes = [
|
Chris@0
|
93 'property' => $properties,
|
Chris@0
|
94 'content' => $content,
|
Chris@0
|
95 ];
|
Chris@0
|
96
|
Chris@0
|
97 $this->_testAttributes($expected_attributes, $mapping, $data);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Test attribute creation for mappings which use 'rel'.
|
Chris@0
|
102 */
|
Chris@0
|
103 public function testRel() {
|
Chris@0
|
104 $properties = ['sioc:has_creator', 'dc:creator'];
|
Chris@0
|
105
|
Chris@0
|
106 $mapping = [
|
Chris@0
|
107 'properties' => $properties,
|
Chris@0
|
108 'mapping_type' => 'rel',
|
Chris@0
|
109 ];
|
Chris@0
|
110 $expected_attributes = ['rel' => $properties];
|
Chris@0
|
111
|
Chris@0
|
112 $this->_testAttributes($expected_attributes, $mapping);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Helper function to test attribute generation.
|
Chris@0
|
117 *
|
Chris@0
|
118 * @param array $expected_attributes
|
Chris@0
|
119 * The expected return of rdf_rdfa_attributes.
|
Chris@0
|
120 * @param array $field_mapping
|
Chris@0
|
121 * The field mapping to merge into the RDF mapping config.
|
Chris@0
|
122 * @param mixed $data
|
Chris@0
|
123 * The data to pass into the datatype callback, if specified.
|
Chris@0
|
124 */
|
Chris@0
|
125 protected function _testAttributes($expected_attributes, $field_mapping, $data = NULL) {
|
Chris@0
|
126 $mapping = rdf_get_mapping('node', 'article')
|
Chris@0
|
127 ->setFieldMapping('field_test', $field_mapping)
|
Chris@0
|
128 ->getPreparedFieldMapping('field_test');
|
Chris@0
|
129 $attributes = rdf_rdfa_attributes($mapping, $data);
|
Chris@0
|
130 ksort($expected_attributes);
|
Chris@0
|
131 ksort($attributes);
|
Chris@0
|
132 $this->assertEqual($expected_attributes, $attributes);
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 }
|