1<?php 2 3namespace Drupal\Tests\rdf\Traits; 4 5use Drupal\Core\Url; 6 7/** 8 * Override \EasyRdf_ParsedUri for PHP 7.4 compatibility. 9 * 10 * @todo https://www.drupal.org/project/drupal/issues/3110972 Remove this work 11 * around. 12 */ 13class_alias('\Drupal\Tests\rdf\Traits\EasyRdf_ParsedUri', '\EasyRdf_ParsedUri'); 14 15/** 16 * Defines a trait for parsing RDF properties from HTML. 17 */ 18trait RdfParsingTrait { 19 20 /** 21 * Checks if a html document contains a resource with a given property value. 22 * 23 * @param string $html 24 * The HTML to parse. 25 * @param string $base_uri 26 * The base URI for the html being parsed. 27 * @param string $resource 28 * The URI of the resource which should have the given property. 29 * @param string $property 30 * The property being tested. 31 * @param array $value 32 * The expected value. This should include the following keys: 33 * - type: one of literal, uri and bnode 34 * - value: the expected value 35 * - datatype: the expected datatype in URI format - e.g. 36 * - http://www.w3.org/2001/XMLSchema#integer 37 * - http://www.w3.org/2001/XMLSchema#dateTime 38 * - lang: language code of the property. 39 * 40 * @return bool 41 * TRUE if the property exists with the given value. 42 */ 43 protected function hasRdfProperty($html, $base_uri, $resource, $property, array $value) { 44 $parser = new \EasyRdf_Parser_Rdfa(); 45 $graph = new \EasyRdf_Graph(); 46 $parser->parse($graph, $html, 'rdfa', $base_uri); 47 48 return $graph->hasProperty($resource, $property, $value); 49 } 50 51 /** 52 * Checks if a html document contains a resource with a given property value. 53 * 54 * @param string $html 55 * The HTML to parse. 56 * @param string $base_uri 57 * The base URI for the html being parsed. 58 * @param string $resource 59 * The URI of the resource which should have the given property. 60 * @param string $parent_property 61 * The parent property being tested. 62 * @param string $child_property 63 * The child property being tested. 64 * @param array $value 65 * The expected value. This should include the following keys: 66 * - type: one of literal, uri and bnode 67 * - value: the expected value 68 * - datatype: the expected datatype in URI format - e.g. 69 * - http://www.w3.org/2001/XMLSchema#integer 70 * - http://www.w3.org/2001/XMLSchema#dateTime 71 * - lang: language code of the property. 72 * 73 * @return bool 74 * TRUE if the property exists with the given value. 75 */ 76 protected function hasRdfChildProperty($html, $base_uri, $resource, $parent_property, string $child_property, array $value) { 77 $parser = new \EasyRdf_Parser_Rdfa(); 78 $graph = new \EasyRdf_Graph(); 79 $parser->parse($graph, $html, 'rdfa', $base_uri); 80 $node = $graph->get($resource, $parent_property); 81 return $graph->hasProperty($node, $child_property, $value); 82 } 83 84 /** 85 * Counts the number of resources of the provided type. 86 * 87 * @param \Drupal\Core\Url $url 88 * URL of the document. 89 * @param string $base_uri 90 * The base URI for the html being parsed. 91 * @param string $type 92 * Type of resource to count. 93 * 94 * @return int 95 * The number of resources of the provided type. 96 */ 97 protected function getElementByRdfTypeCount(Url $url, $base_uri, $type) { 98 $parser = new \EasyRdf_Parser_Rdfa(); 99 $graph = new \EasyRdf_Graph(); 100 $parser->parse($graph, $this->drupalGet($url), 'rdfa', $base_uri); 101 return count($graph->allOfType($type)); 102 } 103 104 /** 105 * Gets type of RDF Element. 106 * 107 * @param \Drupal\Core\Url $url 108 * URL of the document. 109 * @param string $base_uri 110 * The base URI for the html being parsed. 111 * @param string $resource_uri 112 * The URI of the resource from where to get element. 113 * 114 * @return string|null 115 * The type of resource or NULL if the resource has no type. 116 */ 117 protected function getElementRdfType(Url $url, $base_uri, $resource_uri) { 118 $parser = new \EasyRdf_Parser_Rdfa(); 119 $graph = new \EasyRdf_Graph(); 120 $parser->parse($graph, $this->drupalGet($url), 'rdfa', $base_uri); 121 return $graph->type($resource_uri); 122 } 123 124 /** 125 * Checks if RDF Node property is blank. 126 * 127 * @param string $html 128 * The HTML to parse. 129 * @param string $base_uri 130 * The base URI for the html being parsed. 131 * @param string $resource_uri 132 * The URI of the resource which should have the given property. 133 * @param string $property 134 * The property being tested. 135 * 136 * @return bool 137 * TRUE if the given property is blank. 138 */ 139 protected function rdfElementIsBlankNode($html, $base_uri, $resource_uri, $property) { 140 $parser = new \EasyRdf_Parser_Rdfa(); 141 $graph = new \EasyRdf_Graph(); 142 $parser->parse($graph, $html, 'rdfa', $base_uri); 143 return $graph->get($resource_uri, $property)->isBnode(); 144 } 145 146} 147