LinkFieldRdfaTest.php 5.2 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
<?php
/**
 * @file
 * Contains \Drupal\rdf\Tests\Field\LinkFieldRdfaTest.
 */

namespace Drupal\rdf\Tests\Field;

use Drupal\rdf\Tests\Field\FieldRdfaTestBase;

/**
 * Tests the placement of RDFa in link field formatters.
 */
class LinkFieldRdfaTest extends FieldRdfaTestBase {

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

  /**
   * {@inheritdoc}
   */
  public static $modules = array('link', 'text');

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name'  => 'Field formatter: link',
      'description'  => 'Tests RDFa output by link field formatters.',
      'group' => 'RDF',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->createTestField();

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

  }

  /**
   * Tests all formatters with link to external page.
   */
  public function testAllFormattersExternal() {
    // Set up test values.
    $this->testValue = 'http://test.me/foo/bar/neque/porro/quisquam/est/qui-dolorem?foo/bar/neque/porro/quisquam/est/qui-dolorem';
    $this->entity = entity_create('entity_test', array());
    $this->entity->{$this->fieldName}->url = $this->testValue;

    // Set up the expected result.
    $expected_rdf = array(
      'value' => $this->testValue,
      'type' => 'uri',
    );

    $this->runTestAllFormatters($expected_rdf, 'external');
  }

  /**
   * Tests all formatters with link to internal page.
   */
  public function testAllFormattersInternal() {
    // Set up test values.
    $this->testValue = 'admin';
    $this->entity = entity_create('entity_test', array());
    $this->entity->{$this->fieldName}->url = $this->testValue;

    // Set up the expected result.
    // AssertFormatterRdfa looks for a full path.
    $expected_rdf = array(
      'value' => $this->uri . '/' . $this->testValue,
      'type' => 'uri',
    );

    $this->runTestAllFormatters($expected_rdf, 'internal');
  }

  /**
   * Tests all formatters with link to frontpage.
   */
  public function testAllFormattersFront() {
    // Set up test values.
    $this->testValue = '<front>';
    $this->entity = entity_create('entity_test', array());
    $this->entity->{$this->fieldName}->url = $this->testValue;

    // Set up the expected result.
    $expected_rdf = array(
      'value' =>  $this->uri . '/',
      'type' => 'uri',
    );

    $this->runTestAllFormatters($expected_rdf, 'front');
  }

  /**
   * Helper function to test all link formatters.
   */
  public function runTestAllFormatters($expected_rdf, $type = NULL) {

    // Test the link formatter: trim at 80, no other settings.
    $formatter = array(
      'type' => $type . ' link',
      'settings' => array(
        'trim_length' => 80,
        'url_only' => FALSE,
        'url_plain' => FALSE,
        'rel' => '',
        'target' => '',
      ),
    );
    $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link formatter: trim at 40, nofollow, new window.
    $formatter = array(
      'type' => $type . ' link',
      'settings' => array(
        'trim_length' => 40,
        'url_only' => FALSE,
        'url_plain' => FALSE,
        'rel' => 'nofollow',
        'target' => '_blank',
      ),
    );
    $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link formatter: trim at 40, URL only (not plaintext) nofollow,
    // new window.
    $formatter = array(
      'type' => $type . ' link',
      'settings' => array(
        'trim_length' => 40,
        'url_only' => TRUE,
        'url_plain' => FALSE,
        'rel' => 'nofollow',
        'target' => '_blank',
      ),
    );
    $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link_separate formatter: trim at 40, nofollow, new window.
    $formatter = array(
      'type' => $type . ' link_separate',
      'settings' => array(
        'trim_length' => 40,
        'rel' => 'nofollow',
        'target' => '_blank',
      ),
    );
    $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Change the expected value here to literal. When formatted as plaintext
    // then the RDF is expecting a 'literal' not a 'uri'.
    $expected_rdf = array(
      'value' => $this->testValue,
      'type' => 'literal',
    );
    // Test the link formatter: trim at 20, url only (as plaintext.)
    $formatter = array(
      'type' => $type . ' link',
      'settings' => array(
        'trim_length' => 20,
        'url_only' => TRUE,
        'url_plain' => TRUE,
        'rel' => '0',
        'target' => '0',
      ),
    );
    $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link formatter: do not trim, url only (as plaintext.)
    $formatter = array(
      'type' => $type . ' link',
      'settings' => array(
        'trim_length' => 0,
        'url_only' => TRUE,
        'url_plain' => TRUE,
        'rel' => '0',
        'target' => '0',
      ),
    );
    $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
  }

}