3 namespace Drupal\Tests\rdf\Functional;
5 use Drupal\file\Tests\FileFieldTestBase;
6 use Drupal\file\Entity\File;
9 * Tests the RDFa markup of filefields.
13 class FileFieldAttributesTest extends FileFieldTestBase {
20 public static $modules = ['rdf', 'file'];
23 * The name of the file field used in the test.
30 * The file object used in the test.
32 * @var \Drupal\file\FileInterface
37 * The node object used in the test.
39 * @var \Drupal\node\NodeInterface
43 protected function setUp() {
45 $node_storage = $this->container->get('entity.manager')->getStorage('node');
46 $this->fieldName = strtolower($this->randomMachineName());
48 $type_name = 'article';
49 $this->createFileField($this->fieldName, 'node', $type_name);
51 // Set the teaser display to show this field.
52 entity_get_display('node', 'article', 'teaser')
53 ->setComponent($this->fieldName, ['type' => 'file_default'])
56 // Set the RDF mapping for the new field.
57 $mapping = rdf_get_mapping('node', 'article');
58 $mapping->setFieldMapping($this->fieldName, ['properties' => ['rdfs:seeAlso'], 'mapping_type' => 'rel'])->save();
60 $test_file = $this->getTestFile('text');
62 // Create a new node with the uploaded file.
63 $nid = $this->uploadNodeFile($test_file, $this->fieldName, $type_name);
65 $node_storage->resetCache([$nid]);
66 $this->node = $node_storage->load($nid);
67 $this->file = File::load($this->node->{$this->fieldName}->target_id);
71 * Tests if file fields in teasers have correct resources.
73 * Ensure that file fields have the correct resource as the object in RDFa
74 * when displayed as a teaser.
76 public function testNodeTeaser() {
78 $node_render_array = entity_view_multiple([$this->node], 'teaser');
79 $html = \Drupal::service('renderer')->renderRoot($node_render_array);
81 // Parses front page where the node is displayed in its teaser form.
82 $parser = new \EasyRdf_Parser_Rdfa();
83 $graph = new \EasyRdf_Graph();
84 $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
85 $parser->parse($graph, $html, 'rdfa', $base_uri);
87 $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
88 $file_uri = file_create_url($this->file->getFileUri());
90 // Node relation to attached file.
95 $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).');
96 $this->drupalGet('node');