<?php

/**
 * @file
 * Tests for rdfa.module.
 */

/**
 * Tests for RDFa 1.1 prefix attribute.
 */
class RdfaPrefixAttributeTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'RDFa 1.1 prefix attribute',
      'description' => 'Test implementation of hook_rdf_namespaces() by rdfa.module.',
      'group' => 'RDFa',
    );
  }

  function setUp() {
    parent::setUp('rdf', 'rdfa');
  }

  /**
   * Tests the new RDFa prefix attribute.
   */
  function testRdfaPrefix() {
    // Fetches the front page, builds and XML document and extract namespaces.
    $this->drupalGet('');
    $xml = new SimpleXMLElement($this->content);
    $ns = $xml->getDocNamespaces();

    // xmlns should no longer be used to define prefixes.
    $this->assertTrue(empty($ns['foaf']), t('Prefixes are not declared using xmlns.'));

    // Ensures the foaf prefix is defined in the @prefix attribute.
    $prefix = $this->xpath("/html[contains(@prefix, 'foaf: http://xmlns.com/foaf/0.1/')]");
    $this->assertTrue(!empty($prefix), t('foaf: prefix definition found in RDFa 1.1 @prefix attribute.'));
  }
}

/**
 * Tests for removal of empty datatype attribute on node and comment title.
 */
class RdfaEmptyDatatypeAttributeTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'RDFa 1.1 empty datatype attribute removal',
      'description' => 'Test that rdfa.module is able to remove the empty datatype attribute on node and comment title.',
      'group' => 'RDFa',
    );
  }

  function setUp() {
    parent::setUp('rdf', 'rdfa');
  }

  /**
   * Tests the removal of empty datatype attribute on node and comment title.
   */
  function testEmptyDatatypeAttribute() {
    // Creates a node and a comment.
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => TRUE));
    $comment = (object) array(
      'cid' => NULL,
      'nid' => $node->nid,
      'node_type' => $node->type,
      'pid' => 0,
      'uid' => 1,
      'status' => COMMENT_PUBLISHED,
      'subject' => $this->randomName(),
      'comment_body' => array(LANGUAGE_NONE => array($this->randomName())),
    );
    comment_save($comment);
    $this->drupalGet('node/' . $node->nid);

    // Finds comment title and checks for empty datatype attributes.
    $title_element = $this->xpath('//div[contains(@class, "comment")]//*[@property="dc:title" and not(@datatype)]');
    $empty_datatype_element = $this->xpath('//*[@datatype=""]');
    $this->assertTrue(!empty($title_element) && empty($empty_datatype_element), t('No empty datatype attribute was found on comment title.'));
    // Ensures xml:lang is not used and lang is used instead.
    $title_element = $this->xpath('//*[@class="username" and @typeof="sioc:UserAccount" and @lang=""]');
    // There should be two usernames, one for the node and one for the comment.
    $this->assertTrue(count($title_element) == 2, t('The lang attribute is used in username markup.'));
    $this->assertNoRaw('xml:lang=""', 'xml:lang="" not used anywhere on the page');

    // Finds node title on frontpage and checks for empty datatype attributes.
    $this->drupalGet('');
    $title_element = $this->xpath('//div[contains(@class, "node")]//*[@property="dc:title" and not(@datatype)]');
    $empty_datatype_element = $this->xpath('//*[@datatype=""]');
    $this->assertTrue(!empty($title_element) && empty($empty_datatype_element), t('No empty datatype attribute was found on node title.'));
    // Ensures xml:lang is not used and lang is used instead.
    $title_element = $this->xpath('//*[@class="username" and @typeof="sioc:UserAccount" and @lang=""]');
    // There should be only one username for the node teaser.
    $this->assertTrue(count($title_element) == 1, t('The lang attribute is used in username markup.'));
    $this->assertNoRaw('xml:lang=""', 'xml:lang="" not used anywhere on the page');
  }
}

/**
 * Tests for RDFa taxonomy markup.
 */
class RdfaTaxonomyMarkupTestCase extends TaxonomyWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'RDFa taxonomy markup for plain text term',
      'description' => 'Test the presence of the RDFa taxonomy markup (plain text term).',
      'group' => 'RDFa',
    );
  }

  function setUp() {
    parent::setUp('rdf', 'rdfa');
    // Create two taxonomy terms.
    $this->vocabulary = $this->createVocabulary();
    $this->term1 = $this->createTerm($this->vocabulary);
    $this->term2 = $this->createTerm($this->vocabulary);

    $this->web_user = $this->drupalCreateUser(array('create article content', 'edit own article content'));
    $this->drupalLogin($this->web_user);
  }

  /**
   * Test taxonomy markup generated by RDFa module.
   */
  function testTaxonomyTermPlainRdfaMarkup() {
    $field_name = 'taxonomy_' . $this->vocabulary->machine_name;
    $field = array(
      'field_name' => $field_name,
      'type' => 'taxonomy_term_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $this->vocabulary->machine_name,
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);

    $instance = array(
      'field_name' => $field_name,
      'bundle' => 'article',
      'entity_type' => 'node',
      'widget' => array(
        'type' => 'options_select',
      ),
      'display' => array(
        'default' => array(
          'type' => 'taxonomy_term_reference_plain',
        ),
      ),
    );
    field_create_instance($instance);

    // Set the RDF mapping for the test taxonomy field.
    $rdf_mapping = rdf_mapping_load('node', 'article');
    $rdf_mapping += array($field_name => array('predicates' => array('schema:about'), 'type' => 'rel'));
    $rdf_mapping_save = array('mapping' => $rdf_mapping, 'type' => 'node', 'bundle' => 'article');
    rdf_mapping_save($rdf_mapping_save);

    // Create node with term1.
    $edit = array();
    $node = $this->drupalCreateNode(array('type' => 'article'));
    $edit[$field_name . '[' . LANGUAGE_NONE . '][]'] = $this->term1->tid;
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

    // Check if the RDFa property attribute is present and make sure there is no
    // rel attribute.
    $file_rel = $this->xpath('//div[contains(@class, :class-taxonomy)]//li[@property=:property and not(contains(@rel, :rel)) and text()=:term_name]', array(
      ':class-taxonomy' => 'field-name-taxonomy-' . $this->vocabulary->machine_name,
      ':property' => 'schema:about',
      ':rel' => 'schema:about',
      ':term_name' => $this->term1->name,
    ));
    $this->assertTrue(!empty($file_rel), "Taxonomy term annotated as literal in RDFa.");
  }
}
