annotate core/modules/rdf/tests/src/Functional/FileFieldAttributesTest.php @ 19:fa3358dc1485 tip

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