FieldRdfaTestBase.php 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php
/**
 * @file
 * Contains \Drupal\rdf\Tests\Field\FieldRdfaTestBase.
 */

namespace Drupal\rdf\Tests\Field;

use Drupal\field\Tests\FieldUnitTestBase;

abstract class FieldRdfaTestBase extends FieldUnitTestBase {

  /**
   * The machine name of the field type to test.
   *
   * @var string
   */
  protected $fieldType;

  /**
   * The name of the field to create for testing.
   *
   * @var string
   */
  protected $fieldName = 'field_test';

  /**
   * The URI to identify the entity.
   *
   * @var string
   */
  protected $uri = 'http://ex.com';

  /**
   * The entity to render for testing.
   *
37
   * @var \Drupal\Core\Entity\ContentEntityBase
38 39 40 41 42 43 44 45 46 47
   */
  protected $entity;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('rdf');

48 49 50 51 52 53 54
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->installSchema('system', array('router'));
55
    \Drupal::service('router.builder')->rebuild();
56 57
  }

58 59 60 61 62 63 64 65 66 67 68
  /**
   * Helper function to test the formatter's RDFa.
   *
   * @param string $formatter
   *   The machine name of the formatter to test.
   * @param string $property
   *   The property that should be found.
   * @param string $value
   *   The expected value of the property.
   * @param string $object_type
   *   The object's type, either 'uri' or 'literal'.
69 70
   * @param string $datatype
   *   The data type of the property.
71
   */
72
  protected function assertFormatterRdfa($formatter, $property, $value, $object_type = 'literal', $datatype = '') {
73 74
    // The field formatter will be rendered inside the entity. Set the field
    // formatter in the entity display options before rendering the entity.
75
    entity_get_display('entity_test', 'entity_test', 'default')
76 77 78 79 80
      ->setComponent($this->fieldName, array('type' => $formatter))
      ->save();
    $build = entity_view($this->entity, 'default');
    $output = drupal_render($build);
    $graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa');
81 82 83 84
    $expected_value = array(
      'type' => $object_type,
      'value' => $value,
    );
85 86 87
    if ($datatype) {
      $expected_value['datatype'] = $datatype;
    }
88 89 90 91 92 93 94
    $this->assertTrue($graph->hasProperty($this->uri, $property, $expected_value), "Formatter $formatter exposes data correctly for {$this->fieldType} fields.");
  }

  /**
   * Creates the field for testing.
   */
  protected function createTestField() {
95
    entity_create('field_config', array(
96
      'name' => $this->fieldName,
97
      'entity_type' => 'entity_test',
98 99
      'type' => $this->fieldType,
    ))->save();
100
    entity_create('field_instance_config', array(
101
      'entity_type' => 'entity_test',
102
      'field_name' => $this->fieldName,
103
      'bundle' => 'entity_test',
104 105 106 107 108 109
    ))->save();
  }

  /**
   * Gets the absolute URI of an entity.
   *
110
   * @param \Drupal\Core\Entity\ContentEntityBase $entity
111 112 113 114 115 116
   *   The entity for which to generate the URI.
   *
   * @return string
   *   The absolute URI.
   */
  protected function getAbsoluteUri($entity) {
117
    return $entity->url('canonical', array('absolute' => TRUE));
118
  }
119

120
}