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

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

namespace Drupal\node\Tests;

10 11
use Drupal\node\Entity\Node;

12
/**
13 14 15
 * Tests $node->save() for saving content.
 *
 * @group node
16 17 18
 */
class NodeSaveTest extends NodeTestBase {

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

26
  protected function setUp() {
27 28
    parent::setUp();

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

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

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

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

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

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

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

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

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

94
    // Programmatically set the timestamps using hook_ENTITY_TYPE_presave().
95 96 97 98
    $node->title = 'testing_node_presave';

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

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

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

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

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

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

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

    // Test the static node load cache to be cleared.
154
    $node = Node::load($node->id());
155
    $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
156
  }
157 158 159 160

  /**
   * Tests saving a node on node insert.
   *
161 162 163
   * This test ensures that a node has been fully saved when
   * hook_ENTITY_TYPE_insert() is invoked, so that the node can be saved again
   * in a hook implementation without errors.
164 165 166 167
   *
   * @see node_test_node_insert()
   */
  function testNodeSaveOnInsert() {
168
    // node_test_node_insert() triggers a save on insert if the title equals
169 170
    // 'new'.
    $node = $this->drupalCreateNode(array('title' => 'new'));
171
    $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');
172
  }
173
}