annotate core/modules/rdf/src/Tests/ImageFieldAttributesTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\rdf\Tests;
Chris@0 4
Chris@0 5 use Drupal\image\Entity\ImageStyle;
Chris@0 6 use Drupal\image\Tests\ImageFieldTestBase;
Chris@0 7 use Drupal\node\Entity\Node;
Chris@0 8 use Drupal\file\Entity\File;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Tests the RDFa markup of imagefields.
Chris@0 12 *
Chris@0 13 * @group rdf
Chris@0 14 */
Chris@0 15 class ImageFieldAttributesTest extends ImageFieldTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Modules to enable.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 public static $modules = ['rdf', 'image'];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The name of the image field used in the test.
Chris@0 26 *
Chris@0 27 * @var string
Chris@0 28 */
Chris@0 29 protected $fieldName;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * The file object used in the test.
Chris@0 33 *
Chris@0 34 * @var \Drupal\file\FileInterface
Chris@0 35 */
Chris@0 36 protected $file;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * The node object used in the test.
Chris@0 40 *
Chris@0 41 * @var \Drupal\node\NodeInterface
Chris@0 42 */
Chris@0 43 protected $node;
Chris@0 44
Chris@0 45 protected function setUp() {
Chris@0 46 parent::setUp();
Chris@0 47
Chris@0 48 $this->fieldName = 'field_image';
Chris@0 49
Chris@0 50 // Create the image field.
Chris@0 51 $this->createImageField($this->fieldName, 'article');
Chris@0 52
Chris@0 53 // Set the RDF mapping for the new field.
Chris@0 54 rdf_get_mapping('node', 'article')
Chris@0 55 ->setFieldMapping($this->fieldName, [
Chris@0 56 'properties' => ['og:image'],
Chris@0 57 'mapping_type' => 'rel',
Chris@0 58 ])
Chris@0 59 ->setBundleMapping(['types' => []])
Chris@0 60 ->save();
Chris@0 61
Chris@0 62 // Get the test image that simpletest provides.
Chris@0 63 $image = current($this->drupalGetTestFiles('image'));
Chris@0 64
Chris@0 65 // Save a node with the image.
Chris@0 66 $nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName());
Chris@0 67 $this->node = Node::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 that image fields in teasers have correct resources.
Chris@0 73 */
Chris@0 74 public function testNodeTeaser() {
Chris@0 75 // Set the display options for the teaser.
Chris@0 76 $display_options = [
Chris@0 77 'type' => 'image',
Chris@0 78 'settings' => ['image_style' => 'medium', 'image_link' => 'content'],
Chris@0 79 ];
Chris@0 80 $display = entity_get_display('node', 'article', 'teaser');
Chris@0 81 $display->setComponent($this->fieldName, $display_options)
Chris@0 82 ->save();
Chris@0 83
Chris@0 84 // Render the teaser.
Chris@0 85 $node_render_array = node_view($this->node, 'teaser');
Chris@0 86 $html = \Drupal::service('renderer')->renderRoot($node_render_array);
Chris@0 87
Chris@0 88 // Parse the teaser.
Chris@0 89 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 90 $graph = new \EasyRdf_Graph();
Chris@0 91 $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
Chris@0 92 $parser->parse($graph, $html, 'rdfa', $base_uri);
Chris@0 93
Chris@0 94 // Construct the node and image URIs for testing.
Chris@0 95 $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
Chris@0 96 $image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri());
Chris@0 97
Chris@0 98 // Test relations from node to image.
Chris@0 99 $expected_value = [
Chris@0 100 'type' => 'uri',
Chris@0 101 'value' => $image_uri,
Chris@0 102 ];
Chris@0 103 $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).');
Chris@0 104
Chris@0 105 // Test image type.
Chris@0 106 $expected_value = [
Chris@0 107 'type' => 'uri',
Chris@0 108 'value' => 'http://xmlns.com/foaf/0.1/Image',
Chris@0 109 ];
Chris@0 110 $this->assertTrue($graph->hasProperty($image_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Image type found in RDF output (foaf:Image).');
Chris@0 111 }
Chris@0 112
Chris@0 113 }