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