RdfaMarkupTest.php 7.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\rdf\Tests\RdfaMarkupTest.
 */

namespace Drupal\rdf\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests RDFa markup generation.
 */
class RdfaMarkupTest extends WebTestBase {
16 17 18 19 20 21 22 23

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

24 25 26 27 28 29 30 31 32 33 34
  protected $profile = 'standard';

  public static function getInfo() {
    return array(
      'name' => 'RDFa markup',
      'description' => 'Test RDFa markup generation.',
      'group' => 'RDF',
    );
  }

  /**
35
   * Tests rdf_rdfa_attributes().
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
   */
  function testDrupalRdfaAttributes() {
    // Same value as the one in the HTML tag (no callback function).
    $expected_attributes = array(
      'property' => array('dc:title'),
    );
    $mapping = rdf_mapping_load('test_entity', 'test_bundle');
    $attributes = rdf_rdfa_attributes($mapping['title']);
    ksort($expected_attributes);
    ksort($attributes);
    $this->assertEqual($expected_attributes, $attributes);

    // Value different from the one in the HTML tag (callback function).
    $date = 1252750327;
    $isoDate = date('c', $date);
    $expected_attributes = array(
      'datatype' => 'xsd:dateTime',
      'property' => array('dc:created'),
      'content' => $isoDate,
    );
    $mapping = rdf_mapping_load('test_entity', 'test_bundle');
    $attributes = rdf_rdfa_attributes($mapping['created'], $date);
    ksort($expected_attributes);
    ksort($attributes);
    $this->assertEqual($expected_attributes, $attributes);

    // Same value as the one in the HTML tag with datatype.
    $expected_attributes = array(
      'datatype' => 'foo:bar1type',
      'property' => array('foo:bar1'),
    );
    $mapping = rdf_mapping_load('test_entity', 'test_bundle');
    $attributes = rdf_rdfa_attributes($mapping['foobar1']);
    ksort($expected_attributes);
    ksort($attributes);
    $this->assertEqual($expected_attributes, $attributes);

    // ObjectProperty mapping (rel).
    $expected_attributes = array(
      'rel' => array('sioc:has_creator', 'dc:creator'),
    );
    $mapping = rdf_mapping_load('test_entity', 'test_bundle');
    $attributes = rdf_rdfa_attributes($mapping['foobar_objproperty1']);
    ksort($expected_attributes);
    ksort($attributes);
    $this->assertEqual($expected_attributes, $attributes);

    // Inverse ObjectProperty mapping (rev).
    $expected_attributes = array(
      'rev' => array('sioc:reply_of'),
    );
    $mapping = rdf_mapping_load('test_entity', 'test_bundle');
    $attributes = rdf_rdfa_attributes($mapping['foobar_objproperty2']);
    ksort($expected_attributes);
    ksort($attributes);
    $this->assertEqual($expected_attributes, $attributes);
  }

  /**
95 96
   * Tests if file fields in teasers have correct resources.
   *
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
   * Ensure that file fields have the correct resource as the object in RDFa
   * when displayed as a teaser.
   */
  function testAttributesInMarkupFile() {
    // Create a user to post the image.
    $admin_user = $this->drupalCreateUser(array('edit own article content', 'revert revisions', 'administer content types'));
    $this->drupalLogin($admin_user);

    $langcode = LANGUAGE_NOT_SPECIFIED;
    $bundle_name = "article";

    $field_name = 'file_test';
    $field = array(
      'field_name' => $field_name,
      'type' => 'file',
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => $bundle_name,
      'display' => array(
        'teaser' => array(
          'type' => 'file_default',
        ),
      ),
    );
    field_create_instance($instance);

    // Set the RDF mapping for the new field.
    $rdf_mapping = rdf_mapping_load('node', $bundle_name);
    $rdf_mapping += array($field_name => array('predicates' => array('rdfs:seeAlso'), 'type' => 'rel'));
    $rdf_mapping_save = array('mapping' => $rdf_mapping, 'type' => 'node', 'bundle' => $bundle_name);
    rdf_mapping_save($rdf_mapping_save);

    // Get the test file that simpletest provides.
    $file = current($this->drupalGetTestFiles('text'));

    // Prepare image variables.
    $image_field = "field_image";
    // Get the test image that simpletest provides.
    $image = current($this->drupalGetTestFiles('image'));

    // Create an array for drupalPost with the field names as the keys and
141
    // the URIs for the test files as the values.
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
    $edit = array("files[" . $field_name . "_" . $langcode . "_0]" => drupal_realpath($file->uri),
                  "files[" . $image_field . "_" . $langcode . "_0]" => drupal_realpath($image->uri));

    // Create node and save, then edit node to upload files.
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

    // Get filenames and nid for comparison with HTML output.
    $file_filename = $file->filename;
    $image_filename = $image->filename;
    $nid = $node->nid;
    // Navigate to front page, where node is displayed in teaser form.
    $this->drupalGet('node');

    // We only check to make sure that the resource attribute contains '.txt'
    // instead of the full file name because the filename is altered on upload.
    $file_rel = $this->xpath('//div[contains(@about, :node-uri)]//div[contains(@rel, "rdfs:seeAlso") and contains(@resource, ".txt")]', array(
      ':node-uri' => 'node/' . $nid,
    ));
    $this->assertTrue(!empty($file_rel), t('Attribute \'rel\' set on file field. Attribute \'resource\' is also set.'));
    $image_rel = $this->xpath('//div[contains(@about, :node-uri)]//div[contains(@rel, "rdfs:seeAlso") and contains(@resource, :image)]//img[contains(@typeof, "foaf:Image")]', array(
      ':node-uri' => 'node/' . $nid,
      ':image' => $image_filename,
    ));

    $this->assertTrue(!empty($image_rel), t('Attribute \'rel\' set on image field. Attribute \'resource\' is also set.'));

    // Edits the node to add tags.
    $tag1 = $this->randomName(8);
    $tag2 = $this->randomName(8);
    $edit = array();
    $edit['field_tags[' . LANGUAGE_NOT_SPECIFIED . ']'] = "$tag1, $tag2";
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
    // Ensures the RDFa markup for the relationship between the node and its
    // tags is correct.
    $term_rdfa_meta = $this->xpath('//div[@about=:node-url and contains(@typeof, "sioc:Item") and contains(@typeof, "foaf:Document")]//ul[@class="links"]/li[@rel="dc:subject"]/a[@typeof="skos:Concept" and text()=:term-name]', array(
      ':node-url' => url('node/' . $node->nid),
      ':term-name' => $tag1,
    ));
    $this->assertTrue(!empty($term_rdfa_meta), t('Property dc:subject is present for the tag1 field item.'));
    $term_rdfa_meta = $this->xpath('//div[@about=:node-url and contains(@typeof, "sioc:Item") and contains(@typeof, "foaf:Document")]//ul[@class="links"]/li[@rel="dc:subject"]/a[@typeof="skos:Concept" and text()=:term-name]', array(
      ':node-url' => url('node/' . $node->nid),
      ':term-name' => $tag2,
    ));
    $this->assertTrue(!empty($term_rdfa_meta), t('Property dc:subject is present for the tag2 field item.'));
  }
}