FieldRdfaTestBase.php 3.53 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
   */
  protected $entity;

41 42 43 44 45 46 47
  /**
   * TRUE if verbose debugging is enabled.
   *
   * @var bool
   */
  protected $debug = TRUE;

48 49 50 51 52
  /**
   * Modules to enable.
   *
   * @var array
   */
53 54 55 56 57 58
  public static $modules = array('rdf');

  /**
   * @var string
   */
  protected $testValue;
59

60 61 62 63 64 65 66
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->installSchema('system', array('router'));
67
    \Drupal::service('router.builder')->rebuild();
68 69
  }

70 71 72
  /**
   * Helper function to test the formatter's RDFa.
   *
73 74 75 76 77
   * @param array $formatter
   *   An associative array describing the formatter to test and its settings
   *   containing:
   *   - type: The machine name of the field formatter to test.
   *   - settings: The settings of the field formatter to test.
78 79
   * @param string $property
   *   The property that should be found.
80 81 82 83 84 85 86
   * @param array $expected_rdf_value
   *   An associative array describing the expected value of the property
   *   containing:
   *   - value: The actual value of the string or URI.
   *   - type: The type of RDF value, e.g. 'literal' for a string, or 'uri'.
   *   Defaults to 'literal'.
   *   - datatype: (optional) The datatype of the value (e.g. xsd:dateTime).
87
   */
88 89 90
  protected function assertFormatterRdfa($formatter, $property, $expected_rdf_value) {
    $expected_rdf_value += array('type' => 'literal');

91 92
    // The field formatter will be rendered inside the entity. Set the field
    // formatter in the entity display options before rendering the entity.
93
    entity_get_display('entity_test', 'entity_test', 'default')
94
      ->setComponent($this->fieldName, $formatter)
95 96 97 98
      ->save();
    $build = entity_view($this->entity, 'default');
    $output = drupal_render($build);
    $graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa');
99 100 101 102 103 104 105 106

    // If verbose debugging is turned on, display the HTML and parsed RDF
    // in the results.
    if ($this->debug) {
      debug($output);
      debug($graph->toRdfPhp());
    }

107
    $this->assertTrue($graph->hasProperty($this->uri, $property, $expected_rdf_value), "Formatter {$formatter['type']} exposes data correctly for {$this->fieldType} fields.");
108 109 110 111 112 113
  }

  /**
   * Creates the field for testing.
   */
  protected function createTestField() {
114
    entity_create('field_storage_config', array(
115
      'name' => $this->fieldName,
116
      'entity_type' => 'entity_test',
117 118
      'type' => $this->fieldType,
    ))->save();
119
    entity_create('field_instance_config', array(
120
      'entity_type' => 'entity_test',
121
      'field_name' => $this->fieldName,
122
      'bundle' => 'entity_test',
123 124 125 126 127 128
    ))->save();
  }

  /**
   * Gets the absolute URI of an entity.
   *
129
   * @param \Drupal\Core\Entity\ContentEntityBase $entity
130 131 132 133 134 135
   *   The entity for which to generate the URI.
   *
   * @return string
   *   The absolute URI.
   */
  protected function getAbsoluteUri($entity) {
136
    return $entity->url('canonical', array('absolute' => TRUE));
137
  }
138

139
}