class FileFieldAttributesTest
Tests the RDFa markup of filefields.
@group rdf @group legacy
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\file\Functional\FileFieldTestBase uses \Drupal\Tests\file\Functional\FileFieldCreationTrait, \Drupal\Tests\TestFileCreationTrait implements \Drupal\Tests\BrowserTestBase
- class \Drupal\Tests\rdf\Functional\FileFieldAttributesTest uses \Drupal\Tests\rdf\Traits\RdfParsingTrait implements \Drupal\Tests\file\Functional\FileFieldTestBase
- class \Drupal\Tests\file\Functional\FileFieldTestBase uses \Drupal\Tests\file\Functional\FileFieldCreationTrait, \Drupal\Tests\TestFileCreationTrait implements \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of FileFieldAttributesTest
File
-
core/
modules/ rdf/ tests/ src/ Functional/ FileFieldAttributesTest.php, line 16
Namespace
Drupal\Tests\rdf\FunctionalView source
class FileFieldAttributesTest extends FileFieldTestBase {
use RdfParsingTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'rdf',
'file',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* URI of the front page of the Drupal site.
*
* @var string
*/
protected $baseUri;
/**
* The name of the file field used in the test.
*
* @var string
*/
protected $fieldName;
/**
* The file object used in the test.
*
* @var \Drupal\file\FileInterface
*/
protected $file;
/**
* The node object used in the test.
*
* @var \Drupal\node\NodeInterface
*/
protected $node;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$this->fieldName = strtolower($this->randomMachineName());
$type_name = 'article';
$this->createFileField($this->fieldName, 'node', $type_name);
// Set the teaser display to show this field.
\Drupal::service('entity_display.repository')->getViewDisplay('node', 'article', 'teaser')
->setComponent($this->fieldName, [
'type' => 'file_default',
])
->save();
// Set the RDF mapping for the new field.
$mapping = rdf_get_mapping('node', 'article');
$mapping->setFieldMapping($this->fieldName, [
'properties' => [
'rdfs:seeAlso',
],
'mapping_type' => 'rel',
])
->save();
$test_file = $this->getTestFile('text');
// Create a new node with the uploaded file.
$nid = $this->uploadNodeFile($test_file, $this->fieldName, $type_name);
$node_storage->resetCache([
$nid,
]);
$this->node = $node_storage->load($nid);
$this->file = File::load($this->node->{$this->fieldName}->target_id);
// Prepares commonly used URIs.
$this->baseUri = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])->toString();
}
/**
* Tests if file fields in teasers have correct resources.
*
* Ensure that file fields have the correct resource as the object in RDFa
* when displayed as a teaser.
*/
public function testNodeTeaser() {
// Render the teaser.
$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();
$file_uri = $this->file
->createFileUrl(FALSE);
// Node relation to attached file.
$expected_value = [
'type' => 'uri',
'value' => $file_uri,
];
$this->assertTrue($this->hasRdfProperty($html, $this->baseUri, $node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).');
$this->drupalGet('node');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.