View source
<?php
namespace Drupal\Tests\rdf\Functional;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Drupal\Tests\image\Functional\ImageFieldTestBase;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
use Drupal\Tests\rdf\Traits\RdfParsingTrait;
use Drupal\Tests\TestFileCreationTrait;
class ImageFieldAttributesTest extends ImageFieldTestBase {
use RdfParsingTrait;
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected static $modules = [
'rdf',
'image',
];
protected $defaultTheme = 'stark';
protected $baseUri;
protected $fieldName;
protected $file;
protected $node;
protected function setUp() : void {
parent::setUp();
$this->fieldName = 'field_image';
$this
->createImageField($this->fieldName, 'article');
rdf_get_mapping('node', 'article')
->setFieldMapping($this->fieldName, [
'properties' => [
'og:image',
],
'mapping_type' => 'rel',
])
->setBundleMapping([
'types' => [],
])
->save();
$image = current($this
->drupalGetTestFiles('image'));
$nid = $this
->uploadNodeImage($image, $this->fieldName, 'article', $this
->randomMachineName());
$this->node = Node::load($nid);
$this->file = File::load($this->node->{$this->fieldName}->target_id);
$this->baseUri = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])
->toString();
}
public function testNodeTeaser() {
$display_options = [
'type' => 'image',
'settings' => [
'image_style' => 'medium',
'image_link' => 'content',
],
];
$display = \Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article', 'teaser');
$display
->setComponent($this->fieldName, $display_options)
->save();
$node_render_array = \Drupal::entityTypeManager()
->getViewBuilder('node')
->view($this->node, 'teaser');
$html = \Drupal::service('renderer')
->renderRoot($node_render_array);
$node_uri = $this->node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$image_uri = ImageStyle::load('medium')
->buildUrl($this->file
->getFileUri());
$expected_value = [
'type' => 'uri',
'value' => $image_uri,
];
$this
->assertTrue($this
->hasRdfProperty($html, $this->baseUri, $node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).');
$expected_value = [
'type' => 'uri',
'value' => 'http://xmlns.com/foaf/0.1/Image',
];
$this
->assertTrue($this
->hasRdfProperty($html, $this->baseUri, $image_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Term type found in RDF output (skos:Concept).');
}
}