public function MicrodataCoreFieldsMarkupTestCase::testAttributesInMarkup in Microdata 7
Tests the placement of attributes in field markup.
File
- ./
microdata.test, line 344 - Tests for microdata.module.
Class
Code
public function testAttributesInMarkup() {
global $base_url;
// Set test values.
$title = $this
->randomName();
$body = $this
->randomName();
$text = $this
->randomName();
$number = 23;
// Get the test image that simpletest provides.
$image = current($this
->drupalGetTestFiles('image'));
// Create node and save, then edit node to upload files.
$node = $this
->drupalCreateNode(array(
'type' => $this->bundleType,
'promote' => 1,
));
// Add title value.
$edit["title"] = $title;
// Add body value.
$edit["body[und][0][value]"] = $body;
// Add text field value.
$edit["{$this->textFieldName}[und][0][value]"] = $text;
// Add image field value.
$edit['files[' . $this->imageFieldName . '_' . $this->langcode . '_0]'] = drupal_realpath($image->uri);
// Add number field values.
$edit["{$this->listFieldName}[und][2]"] = TRUE;
$edit["{$this->listFieldName}[und][3]"] = TRUE;
// Add number field value.
$edit["{$this->numberFieldName}[und][0][value]"] = $number;
// Add tags field value.
$edit["{$this->taxFieldName}[und]"] = 'funny, monkey';
// Add keywords.
$edit["{$this->keywordsFieldName}[und]"] = 'foo, bar';
$this
->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Get the microdata result for the page.
$md = $this
->parseMicrodata();
// Get the item and the mapping to check tests.
$item = $md->items[0];
$full_mapping = $this
->getMapping();
$mapping = $full_mapping[$this->entityType][$this->bundleType];
// Test itemtype.
$itemtype = $mapping['#itemtype'][0];
$this
->assertTrue($itemtype == $item->type[0] && count($item->type) == 1, 'Itemtype is placed correctly.');
// Test item id.
$itemid = $base_url . '/node/' . $node->nid;
$this
->assertTrue($itemid == $item->id, 'Itemid is placed correctly.');
// Test title.
$title_itemprop = $mapping['title']['#itemprop'][0];
$this
->assertEqual($title, $item->properties[$title_itemprop][0], 'Itemprop is placed on title.');
// Test body.
$body_itemprop = $mapping['body']['value']['#itemprop'][0];
// Use trim here because the HTML input field adds a new line.
$this
->assertEqual($body, trim($item->properties[$body_itemprop][0]), 'Itemprop is placed on body field (text_formatted value).');
// Test text field.
$text_itemprop = $mapping[$this->textFieldName]['#itemprop'][0];
$this
->assertEqual($text, $item->properties[$text_itemprop][0], 'Itemprop is placed on text field.');
// Test image field.
$image_itemprop = $mapping[$this->imageFieldName]['#itemprop'][0];
// We don't have the full HTTP URI of the image, so we just check that the
// property value contains the image filename.
$this
->assertTrue(strpos($item->properties[$image_itemprop][0], $image->filename), t('Itemprop is placed on image field.'));
// Test list field.
$list_itemprop = $mapping[$this->listFieldName]['#itemprop'][0];
$this
->assertTrue(count($item->properties[$list_itemprop]) == 2, t('Itemprops are placed on list.'));
// Test number field.
$number_itemprop = $mapping[$this->numberFieldName]['#itemprop'][0];
$this
->assertEqual($number, $item->properties[$number_itemprop][0], t('Itemprop is placed on number field.'));
// The taxonomy tags multiple values are wrapped with itemscope and the url
// and term name are mapped.
$tax_itemprop = $mapping[$this->taxFieldName]['#itemprop'][0];
$this
->assertTrue(count($item->properties[$tax_itemprop]) == 2, t('Itemprop placed on taxonomy tags.'));
$this
->assertTrue($item->properties[$tax_itemprop][0]->properties['name'][0] == 'funny', t('Itemprop placed correctly for taxonomy tags.'));
$this
->assertTrue(is_object($item->properties[$tax_itemprop][0]), t('Taxonomy tags handled as items.'));
// The keywords taxonomy term entity is not handled as an item.
$keywords_itemprop = $mapping[$this->keywordsFieldName]['#itemprop'][0];
$this
->assertTrue(count($item->properties[$keywords_itemprop]) == 2, t('Itemprop placed on both keywords.'));
$this
->assertTrue($item->properties[$keywords_itemprop][0] == 'foo', t('Itemprop placed correctly for keywords.'));
$this
->assertTrue(is_string($item->properties[$keywords_itemprop][0]), t('Keywords taxonomy tags handled as strings.'));
// Test teaser.
$this
->drupalGet('node');
// Get the new microdata result for the teaser page.
$md = $this
->parseMicrodata();
$item = $md->items[0];
// The body summary is mapped to property 'description'.
$summary_itemprop = $mapping['body']['summary']['#itemprop'][0];
$this
->assertEqual($body, trim($item->properties[$summary_itemprop][0]), t('Itemprop is placed on body summary.'));
// The url is mapped to property 'url'.
$url_itemprop = 'url';
$this
->assertEqual('node/' . $node->nid, trim($item->properties[$url_itemprop][0]), t('Schema.org url property is placed.'));
}