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

namespace Drupal\rdf\Tests\Field;

use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
10
use Drupal\Core\Field\FieldDefinitionInterface;
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 37 38 39
use Drupal\Core\Language\Language;

/**
 * Tests the RDFa output of the taxonomy term reference field formatter.
 */
class TaxonomyTermReferenceRdfaTest extends FieldRdfaTestBase {

  /**
   * {@inheritdoc}
   */
  protected $fieldType = 'taxonomy_term_reference';

  /**
   * The term for testing.
   *
   * @var \Drupal\taxonomy\Entity\Term
   */
  protected $term;

  /**
   * The URI of the term for testing.
   *
   * @var string
   */
  protected $termUri;

  /**
   * {@inheritdoc}
   */
40
  public static $modules = array('taxonomy', 'options', 'text', 'filter');
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

  public static function getInfo() {
    return array(
      'name' => 'Field formatter: taxonomy term reference',
      'description' => 'Tests RDFa output by taxonomy term reference field formatters.',
      'group' => 'RDF',
    );
  }

  public function setUp() {
    parent::setUp();

    $this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));

    $vocabulary = entity_create('taxonomy_vocabulary', array(
      'name' => $this->randomName(),
      'vid' => drupal_strtolower($this->randomName()),
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
    ));
    $vocabulary->save();

62
    entity_create('field_config', array(
63
      'name' => $this->fieldName,
64
      'entity_type' => 'entity_test',
65
      'type' => 'taxonomy_term_reference',
66
      'cardinality' => FieldDefinitionInterface::CARDINALITY_UNLIMITED,
67 68 69 70 71 72 73 74 75
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $vocabulary->id(),
            'parent' => 0,
          ),
        ),
      ),
    ))->save();
76
    entity_create('field_instance_config', array(
77
      'entity_type' => 'entity_test',
78
      'field_name' => $this->fieldName,
79
      'bundle' => 'entity_test',
80 81 82 83 84 85 86 87 88 89
    ))->save();

    $this->term = entity_create('taxonomy_term', array(
      'name' => $this->randomName(),
      'vid' => $vocabulary->id(),
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
    ));
    $this->term->save();

    // Add the mapping.
90
    $mapping = rdf_get_mapping('entity_test', 'entity_test');
91 92 93 94 95
    $mapping->setFieldMapping($this->fieldName, array(
      'properties' => array('schema:about'),
    ))->save();

    // Set up test values.
96
    $this->entity = entity_create('entity_test');
97 98 99 100 101 102 103 104
    $this->entity->{$this->fieldName}->target_id = $this->term->id();
    $this->entity->save();
    $this->uri = $this->getAbsoluteUri($this->entity);
  }

  /**
   * Tests the plain formatter.
   */
105 106
  public function testAllFormatters() {
    // Tests the plain formatter.
107
    $this->assertFormatterRdfa(array('type' => 'taxonomy_term_reference_plain'), 'http://schema.org/about', array('value' => $this->term->getName(), 'type' => 'literal'));
108
    // Tests the link formatter.
109
    $term_uri = $this->getAbsoluteUri($this->term);
110
    $this->assertFormatterRdfa(array('type'=>'taxonomy_term_reference_link'), 'http://schema.org/about', array('value' => $term_uri, 'type' => 'uri'));
111 112 113
  }

}