I am creating a test class using the framework of the core Testing module for a module that is supposed to autoexpire nodes some time after they've been created.

Nodes have two timestamp fields: changed and created. I've tried to use the following code inside my test class (subclass of DrupalWebTestCase) to post an article that is supposed to have been created some time ago:

  // Post an article.
  $edit = array();
  $edit["title"] = $this->randomName(8);
  $edit["body[und][0][value]"] = $this->randomName(16);
  $edit['changed'] = 1397700000; // This is accepted.
  $edit['created'] = 1397700000; // This fails.
  $this->drupalPost('node/add/article', $edit, t('Save'));

The above code snippet produces the following error message when the test is run:

Failed to set field created to 1397700000

Do anyone know how to explicitly set the created field to some specified Unix timestamp date for a node created inside a test class?


2 Answers 2


When you access node create form using admin role created value is exposed using Authored Information...

enter image description here

So, You can able to set created date by creating user with administrator role for testing...

Please see implementation below..


 * Tests the functionality of the Simpletest example content type.
class SimpletestExampleTestCase extends DrupalWebTestCase {
  protected $privileged_user;

  public static function getInfo() {
    // Note: getInfo() strings are not translated with t().
    return array(
        'name' => 'Simpletest Example',
        'description' => 'Ensure that the simpletest_example content type provided functions properly.',
        'group' => 'Examples',
  public function setUp() {
    // Enable any modules required for the test. This should be an array of
    // module names.
    // Create and log in our privileged user.
    $user = $this->drupalCreateAdminUser();

  public function drupalCreateAdminUser(array $permissions = array()) {
    $roles = user_roles();
    $index = array_search('administrator', $roles);
    $user = $this->drupalCreateUser($permissions);
    $user->roles[$index] = 'administrator';
    return user_save($user);

   * Tests creation of a Simpletest example node.
  public function testSimpleTestExampleCreate() {
    // Create node to edit.
    $edit = array();
    $edit['title'] = $this->randomName(8);
    $edit["body[und][0][value]"] = $this->randomName(16);
    $edit["date"] = "2012-04-19 22:54:38 +0530";
    $this->drupalPost('node/add/article', $edit, t('Save'));
    $this->assertText(t('Simpletest Example Node Type @title has been created.', array('@title' => $edit['title'])));

Here is the output

enter image description here

  • 1
    Nice approach, but I don't think limiting creation of nodes to the admin role would make this a fair test
    – Clive
    Commented Apr 19, 2014 at 17:37
  • 2
    @Clive, It will do for me :-). I don't care where they come from - I just want them to be old. But your solution looks OK as well, I'll keep it in mind if I need something more general one day. Impressed by the speed of you guys! Commented Apr 19, 2014 at 17:39
  • 1
    Agree with you clive :-) Just incase if some one want to set created date using admin role or where above field is exposed in create content type form..
    – Anil Sagar
    Commented Apr 19, 2014 at 17:40
  • @FreeRadical I've been off for a few days, my brain was crying out for something interesting to get stuck into ;)
    – Clive
    Commented Apr 19, 2014 at 17:50

The value for created isn't exposed to the node form, it comes directly from node_object_prepare(). So there's no way to override it using form values unfortunately.

The only way to adjust the created timestamp when it comes through a node form submission is to implement hook_node_presave(). The easiest way to do this would be for your test to require a companion module, which implements that hook.

There is precedent for this: the core node tests include the node_test module, which implements hook_node_presave(), so NodeSaveTestCase::testTimestamps() can test that created dates can be overridden...

function node_test_node_presave($node) {
  if ($node->title == 'testing_node_presave') {
    // Sun, 19 Nov 1978 05:00:00 GMT
    $node->created = 280299600;
    // Drupal 1.0 release.
    $node->changed = 979534800;

After creating the module you just need to declare it as a dependency in the setUp method, e.g.


And you should be able to test properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.