EntityReferenceRdfaTest.php 2.68 KB
Newer Older
1 2 3
<?php
/**
 * @file
4
 * Contains \Drupal\rdf\Tests\Field\EntityReferenceRdfaTest.
5 6 7 8
 */

namespace Drupal\rdf\Tests\Field;

9
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
10
use Drupal\user\Entity\Role;
11
use Drupal\user\RoleInterface;
12 13 14

/**
 * Tests the RDFa output of the entity reference field formatter.
15 16
 *
 * @group rdf
17 18 19
 */
class EntityReferenceRdfaTest extends FieldRdfaTestBase {

20 21
  use EntityReferenceTestTrait;

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  /**
   * {@inheritdoc}
   */
  protected $fieldType = 'entity_reference';

  /**
   * The entity type used in this test.
   *
   * @var string
   */
  protected $entityType = 'entity_test';

  /**
   * The bundle used in this test.
   *
   * @var string
   */
  protected $bundle = 'entity_test';

  /**
   * The term for testing.
   *
   * @var \Drupal\taxonomy\Entity\Term
   */
46
  protected $targetEntity;
47 48 49 50

  /**
   * {@inheritdoc}
   */
51
  public static $modules = ['text', 'filter'];
52

53
  protected function setUp() {
54 55 56 57
    parent::setUp();

    $this->installEntitySchema('entity_test_rev');

58 59
    // Give anonymous users permission to view test entities.
    $this->installConfig(array('user'));
60
    Role::load(RoleInterface::ANONYMOUS_ID)
61 62 63
      ->grantPermission('view test entity')
      ->save();

64
    $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
65 66 67 68 69 70 71 72

    // Add the mapping.
    $mapping = rdf_get_mapping('entity_test', 'entity_test');
    $mapping->setFieldMapping($this->fieldName, array(
      'properties' => array('schema:knows'),
    ))->save();

    // Create the entity to be referenced.
73 74 75
    $this->targetEntity = $this->container->get('entity_type.manager')
      ->getStorage($this->entityType)
      ->create(array('name' => $this->randomMachineName()));
76
    $this->targetEntity->save();
77 78

    // Create the entity that will have the entity reference field.
79 80 81
    $this->entity = $this->container->get('entity_type.manager')
      ->getStorage($this->entityType)
      ->create(array('name' => $this->randomMachineName()));
82
    $this->entity->save();
83
    $this->entity->{$this->fieldName}->entity = $this->targetEntity;
84 85 86 87 88 89 90
    $this->uri = $this->getAbsoluteUri($this->entity);
  }

  /**
   * Tests all the entity reference formatters.
   */
  public function testAllFormatters() {
91
    $entity_uri = $this->getAbsoluteUri($this->targetEntity);
92 93 94 95 96 97 98 99

    // Tests the label formatter.
    $this->assertFormatterRdfa(array('type' => 'entity_reference_label'), 'http://schema.org/knows', array('value' => $entity_uri, 'type' => 'uri'));
    // Tests the entity formatter.
    $this->assertFormatterRdfa(array('type' => 'entity_reference_entity_view'), 'http://schema.org/knows', array('value' => $entity_uri, 'type' => 'uri'));
  }

}