NodeSaveTest.php 5.73 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 12 13
 * Tests $node->save() for saving content.
 *
 * @group node
14 15 16
 */
class NodeSaveTest extends NodeTestBase {

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

24
  function setUp() {
25 26
    parent::setUp();

27 28 29 30 31 32 33
    // 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;
  }

  /**
34 35
   * Checks whether custom node IDs are saved properly during an import operation.
   *
36 37 38 39 40 41 42 43 44 45 46 47
   * 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,
48
      'body' => array(array('value' => $this->randomName(32))),
49
      'uid' => $this->web_user->id(),
50 51 52
      'type' => 'article',
      'nid' => $test_nid,
    );
53
    /** @var \Drupal\node\NodeInterface $node */
54
    $node = entity_create('node', $node);
55
    $node->enforceIsNew();
56 57

    // Verify that node_submit did not overwrite the user ID.
58
    $this->assertEqual($node->getOwnerId(), $this->web_user->id(), 'Function node_submit() preserves user ID');
59 60 61 62

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

    $node_by_title = $this->drupalGetNodeByTitle($title);
66
    $this->assertTrue($node_by_title, 'Node load by node title.');
67 68 69
  }

  /**
70
   * Verifies accuracy of the "created" and "changed" timestamp functionality.
71 72 73 74
   */
  function testTimestamps() {
    // Use the default timestamps.
    $edit = array(
75
      'uid' => $this->web_user->id(),
76 77 78 79 80 81
      'type' => 'article',
      'title' => $this->randomName(8),
    );

    entity_create('node', $edit)->save();
    $node = $this->drupalGetNodeByTitle($edit['title']);
82 83
    $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.');
84 85

    // Store the timestamps.
86
    $created = $node->getCreatedTime();
87 88 89

    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
90
    $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');
91 92 93 94 95 96

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

    $node->save();
    $node = $this->drupalGetNodeByTitle('testing_node_presave', TRUE);
97 98
    $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.');
99 100 101

    // Programmatically set the timestamps on the node.
    $edit = array(
102
      'uid' => $this->web_user->id(),
103 104 105 106 107 108 109 110
      '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']);
111 112
    $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.');
113 114

    // Update the timestamps.
115
    $node->setCreatedTime(979534800);
116 117 118 119
    $node->changed = 280299600;

    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
120 121
    $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.');
122 123 124
  }

  /**
125 126 127 128
   * 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.
129 130 131 132
   */
  function testDeterminingChanges() {
    // Initial creation.
    $node = entity_create('node', array(
133
      'uid' => $this->web_user->id(),
134 135 136 137 138 139 140
      'type' => 'article',
      'title' => 'test_changes',
    ));
    $node->save();

    // Update the node without applying changes.
    $node->save();
141
    $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.');
142 143 144 145 146 147 148

    // 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.
149
    $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.');
150 151

    // Test the static node load cache to be cleared.
152
    $node = node_load($node->id());
153
    $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
154
  }
155 156 157 158 159 160 161 162 163 164 165

  /**
   * 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() {
166
    // node_test_node_insert() triggers a save on insert if the title equals
167 168
    // 'new'.
    $node = $this->drupalCreateNode(array('title' => 'new'));
169
    $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');
170
  }
171
}