Creates a node based on default settings.
array $settings: (optional) An associative array of settings for the node, as used in entity_create(). Override the defaults by specifying the key and value in the array, for example:
$this
->drupalCreateNode(array(
'title' => t('Hello, world!'),
'type' => 'article',
));
The following defaults are provided:
$settings['body'][LANGUAGE_NOT_SPECIFIED][0] = array(
'value' => $this
->randomName(32),
'format' => filter_default_format(),
);
Drupal\node\Node The created node entity.
protected function drupalCreateNode(array $settings = array()) {
// Populate defaults array.
$settings += array(
'body' => array(
LANGUAGE_NOT_SPECIFIED => array(
array(),
),
),
'title' => $this
->randomName(8),
'changed' => REQUEST_TIME,
'promote' => NODE_NOT_PROMOTED,
'revision' => 1,
'log' => '',
'status' => NODE_PUBLISHED,
'sticky' => NODE_NOT_STICKY,
'type' => 'page',
'langcode' => LANGUAGE_NOT_SPECIFIED,
);
// Add in comment settings for nodes.
if (module_exists('comment')) {
$settings += array(
'comment' => COMMENT_NODE_OPEN,
);
}
// Use the original node's created time for existing nodes.
if (isset($settings['created']) && !isset($settings['date'])) {
$settings['date'] = format_date($settings['created'], 'custom', 'Y-m-d H:i:s O');
}
// If the node's user uid is not specified manually, use the currently
// logged in user if available, or else the user running the test.
if (!isset($settings['uid'])) {
if ($this->loggedInUser) {
$settings['uid'] = $this->loggedInUser->uid;
}
else {
global $user;
$settings['uid'] = $user->uid;
}
}
// Merge body field value and format separately.
$body = array(
'value' => $this
->randomName(32),
'format' => filter_default_format(),
);
if (empty($settings['body'][$settings['langcode']])) {
$settings['body'][$settings['langcode']][0] = array();
}
$settings['body'][$settings['langcode']][0] += $body;
$node = entity_create('node', $settings);
if (!empty($settings['revision'])) {
$node
->setNewRevision();
}
$node
->save();
// Small hack to link revisions to our test user.
db_update('node_revision')
->fields(array(
'uid' => $node->uid,
))
->condition('vid', $node->vid)
->execute();
return $node;
}