How do I post a node that has its created field set to the past in a test class? 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-&gt;randomName(8);
  $edit["body[und][0][value]"] = $this-&gt;randomName(16);
  $edit['changed'] = 1397700000; // This is accepted.
  $edit['created'] = 1397700000; // This fails.
  $this-&gt;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?
When you access node create form using admin role created value is exposed using Authored Information...



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

Please see implementation below..

&lt;?php


/**
 * 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' =&gt; 'Simpletest Example',
        'description' =&gt; 'Ensure that the simpletest_example content type provided functions properly.',
        'group' =&gt; 'Examples',
    );
  }
  public function setUp() {
    // Enable any modules required for the test. This should be an array of
    // module names.
    parent::setUp(array('simpletest_example'));
    // Create and log in our privileged user.
    $user = $this-&gt;drupalCreateAdminUser();
    $this-&gt;drupalLogin($user);
  }



  public function drupalCreateAdminUser(array $permissions = array()) {
    $roles = user_roles();
    $index = array_search('administrator', $roles);
    $user = $this-&gt;drupalCreateUser($permissions);
    $user-&gt;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-&gt;randomName(8);
    $edit["body[und][0][value]"] = $this-&gt;randomName(16);
    $edit["date"] = "2012-04-19 22:54:38 +0530";
    $this-&gt;drupalPost('node/add/article', $edit, t('Save'));
    $this-&gt;assertText(t('Simpletest Example Node Type @title has been created.', array('@title' =&gt; $edit['title'])));
  }
}


Here is the output