Last updated May 18, 2012. Created on May 18, 2012.
Edited by linclark. Log in to edit this page.

To ensure that changes to field formatter code don't break the microdata integration, you may want to write an automated test. Microdata module provides a base class, MicrodataFieldTestCase, which can be extended to test your field.

Three abstract methods need to be implemented for the setUp function to run: getFields, getInstances, and getMapping. Once setUp has installed these, your test method can create a new node and use the parseMicrodata() function to extract an object. The test should look at properties on this object to make sure they have the expected values.

getFields()

First the fields need to be declared.

Example:

  protected function getFields() {
    $fields = array(
      // Text field.
      array(
        'field_name' => 'text_test_field',
        'type' => 'text',
      ),
    );
    return $fields
  }

getInstances()

Then an instance needs to be attached to a test content type. By default the test uses the 'article' type, but this property can be overriden in the extending class.

Example:

  protected function getInstances() {
    $instances = array(
      // Text instance.
      array(
        'field_name' => 'text_test_field',
        'entity_type' => $this->entityType,
        'bundle' => $this->bundleType,
        'display' => array(
          'teaser' => array(
            'type' => 'text_default',
          ),
        ),
      ),
    );
    return $instances;
  }

getMapping()

Finally, a microdata mapping needs to be added for the field. Minimally, the '#itemprop' array attribute should be used to indicate the relationship between the entity and the field.

Example:

  protected function getMapping() {
    $mapping = array(
      $this->entityType => array(
        $this->bundleType => array(
          '#itemtype' => array('http://schema.org/Movie'),
          // Text field.
          'text_field_test' => array(
            '#itemprop' => array('genre'),
          ),
        ),
      ),
    );
    return $mapping;
  }

In cases of compound fields, where the field has its own properties such as AddressField, mapping arrays should also be included for those properties, keyed by the property name (for example, 'locality' in the AddressField below).

Example of compound field with a property:

    $mapping = array(
      $this->entityType => array(
        $this->bundleType => array(
          'addressfield_test' => array(
            '#is_item' => TRUE,
            '#itemtype' => array('http://schema.org/PostalAddress'),
            '#itemprop' => array('address'),
            'locality' => array(
              '#itemprop' => array('addressLocality'),
            ),
          ),
        ),
      ),
    );

Testing the microdata

The MicrodataFieldTestCase setUp function installs the fields and mappings. The a test needs to be written to create a node with values and test those values.

The parseMicrodata function can be used to extract microdata once the test node is posted. Then assertions can test whether the extracted microdata item matches the entity.

Example:

  public function testAttributesInMarkup() {
    $text = $this->randomName();

    $node = $this->drupalCreateNode(array('type' => $this->bundleType, 'promote' => 1));
    $edit["{'text_field_test'}[und][0][value]"] = $text;
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

    // Get the microdata result for the page.
    $md = $this->parseMicrodata();

    // Get the entity as a microdata item.
    $item = $md->items[0];

    // Test text field.
    $text_itemprop = 'genre';
    $this->assertEqual($text, $item->properties[$text_itemprop][0], 'Itemprop is placed on text field.');
  }

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.