public function EntityMetadataTestCase::testEntityMetadataWrapper in Entity API 7
Creates a user and a node, then tests getting the properties.
File
- ./
entity.test, line 745 - Entity CRUD API tests.
Class
- EntityMetadataTestCase
- Tests metadata wrappers.
Code
public function testEntityMetadataWrapper() {
$account = $this
->drupalCreateUser();
// For testing sanitizing give the user a malicious user name.
$account = user_save($account, array(
'name' => '<b>BadName</b>',
));
$title = '<b>Is it bold?<b>';
$body[LANGUAGE_NONE][0] = array(
'value' => '<b>The body & nothing.</b>',
'summary' => '<b>The body.</b>',
);
$node = $this
->drupalCreateNode(array(
'uid' => $account->uid,
'name' => $account->name,
'body' => $body,
'title' => $title,
'summary' => '',
'type' => 'page',
));
// First test without sanitizing.
$wrapper = entity_metadata_wrapper('node', $node);
$this
->assertEqual('<b>Is it bold?<b>', $wrapper->title
->value(), 'Getting a field value.');
$this
->assertEqual($node->title, $wrapper->title
->raw(), 'Getting a raw property value.');
// Test chaining.
$this
->assertEqual($account->mail, $wrapper->author->mail
->value(), 'Testing chained usage.');
$this
->assertEqual($account->name, $wrapper->author->name
->value(), 'Testing chained usage with callback and sanitizing.');
// Test sanitized output.
$options = array(
'sanitize' => TRUE,
);
$this
->assertEqual(check_plain('<b>Is it bold?<b>'), $wrapper->title
->value($options), 'Getting sanitized field.');
$this
->assertEqual(filter_xss($node->name), $wrapper->author->name
->value($options), 'Getting sanitized property with getter callback.');
// Test getting an not existing property.
try {
echo $wrapper->dummy;
$this
->fail('Getting an not existing property.');
} catch (EntityMetadataWrapperException $e) {
$this
->pass('Getting an not existing property.');
}
// Test setting.
$wrapper->author = 0;
$this
->assertEqual(0, $wrapper->author->uid
->value(), 'Setting a property.');
try {
$wrapper->url = 'dummy';
$this
->fail('Setting an unsupported property.');
} catch (EntityMetadataWrapperException $e) {
$this
->pass('Setting an unsupported property.');
}
// Test value validation.
$this
->assertFalse($wrapper->author->name
->validate(array(
3,
)), 'Validation correctly checks for valid data types.');
try {
$wrapper->author->mail = 'foo';
$this
->fail('An invalid mail address has been set.');
} catch (EntityMetadataWrapperException $e) {
$this
->pass('Setting an invalid mail address throws exception.');
}
// Test unsetting a required property.
try {
$wrapper->author = NULL;
$this
->fail('The required node author has been unset.');
} catch (EntityMetadataWrapperException $e) {
$this
->pass('Unsetting the required node author throws an exception.');
}
// Test setting a referenced entity by id.
$wrapper->author
->set($GLOBALS['user']->uid);
$this
->assertEqual($wrapper->author
->getIdentifier(), $GLOBALS['user']->uid, 'Get the identifier of a referenced entity.');
$this
->assertEqual($wrapper->author->uid
->value(), $GLOBALS['user']->uid, 'Successfully set referenced entity using the identifier.');
// Set by object.
$wrapper->author
->set($GLOBALS['user']);
$this
->assertEqual($wrapper->author->uid
->value(), $GLOBALS['user']->uid, 'Successfully set referenced entity using the entity.');
// Test getting by the field API processed values like the node body.
$body_value = $wrapper->body->value;
$this
->assertEqual("<p>The body & nothing.</p>\n", $body_value
->value(), "Getting processed value.");
$this
->assertEqual("The body & nothing.\n", $body_value
->value(array(
'decode' => TRUE,
)), "Decoded value.");
$this
->assertEqual("<b>The body & nothing.</b>", $body_value
->raw(), "Raw body returned.");
// Test getting the summary.
$this
->assertEqual("<p>The body.</p>\n", $wrapper->body->summary
->value(), "Getting body summary.");
$wrapper->body
->set(array(
'value' => "<b>The second body.</b>",
));
$this
->assertEqual("<p>The second body.</p>\n", $wrapper->body->value
->value(), "Setting a processed field value and reading it again.");
$this
->assertEqual($node->body[LANGUAGE_NONE][0]['value'], "<b>The second body.</b>", 'Update appears in the wrapped entity.');
$this
->assert(isset($node->body[LANGUAGE_NONE][0]['safe_value']), 'Formatted text has been processed.');
// Test translating the body on an English node.
locale_add_language('de');
$body['en'][0] = array(
'value' => '<b>English body.</b>',
'summary' => '<b>The body.</b>',
);
$node = $this
->drupalCreateNode(array(
'body' => $body,
'language' => 'en',
'type' => 'page',
));
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper
->language('de');
$languages = language_list();
$this
->assertEqual($wrapper
->getPropertyLanguage(), $languages['de'], 'Wrapper language has been set to German');
$this
->assertEqual($wrapper->body->value
->value(), "<p>English body.</p>\n", 'Language fallback on default language.');
// Set a German text using the wrapper.
$wrapper->body
->set(array(
'value' => "<b>Der zweite Text.</b>",
));
$this
->assertEqual($wrapper->body->value
->value(), "<p>Der zweite Text.</p>\n", 'German body set and retrieved.');
$wrapper
->language(LANGUAGE_NONE);
$this
->assertEqual($wrapper->body->value
->value(), "<p>English body.</p>\n", 'Default language text is still there.');
// Test iterator.
$type_info = entity_get_property_info('node');
$this
->assertFalse(array_diff_key($type_info['properties'], iterator_to_array($wrapper
->getIterator())), 'Iterator is working.');
foreach ($wrapper as $property) {
$this
->assertTrue($property instanceof EntityMetadataWrapper, 'Iterate over wrapper properties.');
}
// Test setting a new node.
$node->title = 'foo';
$wrapper
->set($node);
$this
->assertEqual($wrapper->title
->value(), 'foo', 'Changed the wrapped node.');
// Test getting options lists.
$this
->assertEqual($wrapper->type
->optionsList(), node_type_get_names(), 'Options list returned.');
// Test making use of a generic 'entity' reference property the
// 'entity_test' module provides. The property defaults to the node author.
$this
->assertEqual($wrapper->reference->uid
->value(), $wrapper->author
->getIdentifier(), 'Used generic entity reference property.');
// Test updating a property of the generic entity reference.
$wrapper->reference->name
->set('foo');
$this
->assertEqual($wrapper->reference->name
->value(), 'foo', 'Updated property of generic entity reference');
// For testing, just point the reference to the node itself now.
$wrapper->reference
->set($wrapper);
$this
->assertEqual($wrapper->reference->nid
->value(), $wrapper
->getIdentifier(), 'Correctly updated the generic entity referenced property.');
// Test saving and deleting.
$wrapper
->save();
$wrapper
->delete();
$return = node_load($wrapper
->getIdentifier());
$this
->assertFalse($return, "Node has been successfully deleted.");
// Ensure changing the bundle changes available wrapper properties.
$wrapper->type
->set('article');
$this
->assertTrue(isset($wrapper->field_tags), 'Changing bundle changes available wrapper properties.');
// Test labels.
$user = $this
->drupalCreateUser();
user_save($user, array(
'roles' => array(),
));
$wrapper->author = $user->uid;
$this
->assertEqual($wrapper
->label(), $node->title, 'Entity label returned.');
$this
->assertEqual($wrapper->author->roles[0]
->label(), t('authenticated user'), 'Label from options list returned');
$this
->assertEqual($wrapper->author->roles
->label(), t('authenticated user'), 'Label for a list from options list returned');
}