annotate core/modules/rdf/tests/src/Functional/FileFieldAttributesTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\rdf\Functional;
Chris@0 4
Chris@0 5 use Drupal\file\Tests\FileFieldTestBase;
Chris@0 6 use Drupal\file\Entity\File;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the RDFa markup of filefields.
Chris@0 10 *
Chris@0 11 * @group rdf
Chris@0 12 */
Chris@0 13 class FileFieldAttributesTest extends FileFieldTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Modules to enable.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $modules = ['rdf', 'file'];
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The name of the file field used in the test.
Chris@0 24 *
Chris@0 25 * @var string
Chris@0 26 */
Chris@0 27 protected $fieldName;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The file object used in the test.
Chris@0 31 *
Chris@0 32 * @var \Drupal\file\FileInterface
Chris@0 33 */
Chris@0 34 protected $file;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The node object used in the test.
Chris@0 38 *
Chris@0 39 * @var \Drupal\node\NodeInterface
Chris@0 40 */
Chris@0 41 protected $node;
Chris@0 42
Chris@0 43 protected function setUp() {
Chris@0 44 parent::setUp();
Chris@0 45 $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@0 46 $this->fieldName = strtolower($this->randomMachineName());
Chris@0 47
Chris@0 48 $type_name = 'article';
Chris@0 49 $this->createFileField($this->fieldName, 'node', $type_name);
Chris@0 50
Chris@0 51 // Set the teaser display to show this field.
Chris@0 52 entity_get_display('node', 'article', 'teaser')
Chris@0 53 ->setComponent($this->fieldName, ['type' => 'file_default'])
Chris@0 54 ->save();
Chris@0 55
Chris@0 56 // Set the RDF mapping for the new field.
Chris@0 57 $mapping = rdf_get_mapping('node', 'article');
Chris@0 58 $mapping->setFieldMapping($this->fieldName, ['properties' => ['rdfs:seeAlso'], 'mapping_type' => 'rel'])->save();
Chris@0 59
Chris@0 60 $test_file = $this->getTestFile('text');
Chris@0 61
Chris@0 62 // Create a new node with the uploaded file.
Chris@0 63 $nid = $this->uploadNodeFile($test_file, $this->fieldName, $type_name);
Chris@0 64
Chris@0 65 $node_storage->resetCache([$nid]);
Chris@0 66 $this->node = $node_storage->load($nid);
Chris@0 67 $this->file = File::load($this->node->{$this->fieldName}->target_id);
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Tests if file fields in teasers have correct resources.
Chris@0 72 *
Chris@0 73 * Ensure that file fields have the correct resource as the object in RDFa
Chris@0 74 * when displayed as a teaser.
Chris@0 75 */
Chris@0 76 public function testNodeTeaser() {
Chris@0 77 // Render the teaser.
Chris@0 78 $node_render_array = entity_view_multiple([$this->node], 'teaser');
Chris@0 79 $html = \Drupal::service('renderer')->renderRoot($node_render_array);
Chris@0 80
Chris@0 81 // Parses front page where the node is displayed in its teaser form.
Chris@0 82 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 83 $graph = new \EasyRdf_Graph();
Chris@0 84 $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
Chris@0 85 $parser->parse($graph, $html, 'rdfa', $base_uri);
Chris@0 86
Chris@0 87 $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
Chris@0 88 $file_uri = file_create_url($this->file->getFileUri());
Chris@0 89
Chris@0 90 // Node relation to attached file.
Chris@0 91 $expected_value = [
Chris@0 92 'type' => 'uri',
Chris@0 93 'value' => $file_uri,
Chris@0 94 ];
Chris@0 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).');
Chris@0 96 $this->drupalGet('node');
Chris@0 97 }
Chris@0 98
Chris@0 99 }