NodeSaveTest.php 5.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Definition of Drupal\node\Tests\NodeSaveTest.
 */

namespace Drupal\node\Tests;

/**
11
 * Tests node save related functionality, including import-save.
12 13 14
 */
class NodeSaveTest extends NodeTestBase {

15 16 17 18 19 20 21
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('node_test');

22 23 24 25 26 27 28 29 30
  public static function getInfo() {
    return array(
      'name' => 'Node save',
      'description' => 'Test $node->save() for saving content.',
      'group' => 'Node',
    );
  }

  function setUp() {
31 32
    parent::setUp();

33 34 35 36 37 38 39
    // Create a user that is allowed to post; we'll use this to test the submission.
    $web_user = $this->drupalCreateUser(array('create article content'));
    $this->drupalLogin($web_user);
    $this->web_user = $web_user;
  }

  /**
40 41
   * Checks whether custom node IDs are saved properly during an import operation.
   *
42 43 44 45 46 47 48 49 50 51 52 53
   * Workflow:
   *  - first create a piece of content
   *  - save the content
   *  - check if node exists
   */
  function testImport() {
    // Node ID must be a number that is not in the database.
    $max_nid = db_query('SELECT MAX(nid) FROM {node}')->fetchField();
    $test_nid = $max_nid + mt_rand(1000, 1000000);
    $title = $this->randomName(8);
    $node = array(
      'title' => $title,
54
      'body' => array(array('value' => $this->randomName(32))),
55
      'uid' => $this->web_user->id(),
56 57 58
      'type' => 'article',
      'nid' => $test_nid,
    );
59
    /** @var \Drupal\node\NodeInterface $node */
60
    $node = entity_create('node', $node);
61
    $node->enforceIsNew();
62 63

    // Verify that node_submit did not overwrite the user ID.
64
    $this->assertEqual($node->getOwnerId(), $this->web_user->id(), 'Function node_submit() preserves user ID');
65 66 67 68

    $node->save();
    // Test the import.
    $node_by_nid = node_load($test_nid);
69
    $this->assertTrue($node_by_nid, 'Node load by node ID.');
70 71

    $node_by_title = $this->drupalGetNodeByTitle($title);
72
    $this->assertTrue($node_by_title, 'Node load by node title.');
73 74 75
  }

  /**
76
   * Verifies accuracy of the "created" and "changed" timestamp functionality.
77 78 79 80
   */
  function testTimestamps() {
    // Use the default timestamps.
    $edit = array(
81
      'uid' => $this->web_user->id(),
82 83 84 85 86 87
      'type' => 'article',
      'title' => $this->randomName(8),
    );

    entity_create('node', $edit)->save();
    $node = $this->drupalGetNodeByTitle($edit['title']);
88 89
    $this->assertEqual($node->getCreatedTime(), REQUEST_TIME, 'Creating a node sets default "created" timestamp.');
    $this->assertEqual($node->getChangedTime(), REQUEST_TIME, 'Creating a node sets default "changed" timestamp.');
90 91

    // Store the timestamps.
92
    $created = $node->getCreatedTime();
93 94 95

    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
96
    $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');
97 98 99 100 101 102

    // Programmatically set the timestamps using hook_node_presave.
    $node->title = 'testing_node_presave';

    $node->save();
    $node = $this->drupalGetNodeByTitle('testing_node_presave', TRUE);
103 104
    $this->assertEqual($node->getCreatedTime(), 280299600, 'Saving a node uses "created" timestamp set in presave hook.');
    $this->assertEqual($node->getChangedTime(), 979534800, 'Saving a node uses "changed" timestamp set in presave hook.');
105 106 107

    // Programmatically set the timestamps on the node.
    $edit = array(
108
      'uid' => $this->web_user->id(),
109 110 111 112 113 114 115 116
      'type' => 'article',
      'title' => $this->randomName(8),
      'created' => 280299600, // Sun, 19 Nov 1978 05:00:00 GMT
      'changed' => 979534800, // Drupal 1.0 release.
    );

    entity_create('node', $edit)->save();
    $node = $this->drupalGetNodeByTitle($edit['title']);
117 118
    $this->assertEqual($node->getCreatedTime(), 280299600, 'Creating a node uses user-set "created" timestamp.');
    $this->assertNotEqual($node->getChangedTime(), 979534800, 'Creating a node does not use user-set "changed" timestamp.');
119 120

    // Update the timestamps.
121
    $node->setCreatedTime(979534800);
122 123 124 125
    $node->changed = 280299600;

    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
126 127
    $this->assertEqual($node->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.');
    $this->assertNotEqual($node->getChangedTime(), 280299600, 'Updating a node does not use user-set "changed" timestamp.');
128 129 130
  }

  /**
131 132 133 134
   * Tests node presave and static node load cache.
   *
   * This test determines changes in hook_node_presave() and verifies that the
   * static node load cache is cleared upon save.
135 136 137 138
   */
  function testDeterminingChanges() {
    // Initial creation.
    $node = entity_create('node', array(
139
      'uid' => $this->web_user->id(),
140 141 142 143 144 145 146
      'type' => 'article',
      'title' => 'test_changes',
    ));
    $node->save();

    // Update the node without applying changes.
    $node->save();
147
    $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.');
148 149 150 151 152 153 154

    // Apply changes.
    $node->title = 'updated';
    $node->save();

    // The hook implementations node_test_node_presave() and
    // node_test_node_update() determine changes and change the title.
155
    $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.');
156 157

    // Test the static node load cache to be cleared.
158
    $node = node_load($node->id());
159
    $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
160
  }
161 162 163 164 165 166 167 168 169 170 171

  /**
   * Tests saving a node on node insert.
   *
   * This test ensures that a node has been fully saved when hook_node_insert()
   * is invoked, so that the node can be saved again in a hook implementation
   * without errors.
   *
   * @see node_test_node_insert()
   */
  function testNodeSaveOnInsert() {
172
    // node_test_node_insert() triggers a save on insert if the title equals
173 174
    // 'new'.
    $node = $this->drupalCreateNode(array('title' => 'new'));
175
    $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');
176
  }
177
}