API page: https://api.drupal.org/api/drupal/core%21modules%21simpletest%21lib%21Dr...

Enter a descriptive title (above) relating to protected function WebTestBase::drupalCreateNode, then describe the problem you have found:

The documentation and code for WebTestBase::drupalCreateNode() says that you can set $settings['changed'] to a value and it will be set on the node.

This is not correct. See Node::preSave() -- it always sets the value of changed to the request time, which in a test is always when you started the test (constant for all nodes created during the test, and nothing you can do about it as far as I can tell).

We should just take the 'changed' attribute out of the documentation and code of WebTestBase, unless we are allowed to fix Node::preSave() so that it only sets 'changed' if it wasn't already set to a value.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Yep, see #722688: Allow programmatic setting of node->changed and #322759: Allow hook_node_presave() to alter other node fields for historical issues about this.

We needed to test the sorting of this for /admin/content, and to work around that we had to do this:

class NodeAdminTest {
  function testContentAdminSort() {
    $this->drupalLogin($this->admin_user);

    $changed = REQUEST_TIME;
    foreach (array('dd', 'aa', 'DD', 'bb', 'cc', 'CC', 'AA', 'BB') as $prefix) {
      $changed += 1000;
      $node = $this->drupalCreateNode(array('title' => $prefix . $this->randomName(6)));
      db_update('node_field_data')
        ->fields(array('changed' => $changed))
        ->condition('nid', $node->id())
        ->execute();
    }
jhodgdon’s picture

Status: Active » Needs review
FileSize
919 bytes

Here is a simple patch that just removes 'changed' from the docs and code, since it (at least currently) has no effect.

Status: Needs review » Needs work

The last submitted patch, 2: 2213411-simple-fix.patch, failed testing.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
933 bytes

I guess that patch was made on an old repo. Here's a new one.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Yep, makes sense.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Ok, then.

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.