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