NodeSaveTest.php 5.92 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
  /**
   * A normal logged in user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $webUser;

26 27 28 29 30 31 32
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('node_test');

33
  protected function setUp() {
34 35
    parent::setUp();

36 37 38
    // 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);
39
    $this->webUser = $web_user;
40 41 42
  }

  /**
43 44
   * Checks whether custom node IDs are saved properly during an import operation.
   *
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);
54
    $title = $this->randomMachineName(8);
55 56
    $node = array(
      'title' => $title,
57
      'body' => array(array('value' => $this->randomMachineName(32))),
58
      'uid' => $this->webUser->id(),
59 60 61
      'type' => 'article',
      'nid' => $test_nid,
    );
62
    /** @var \Drupal\node\NodeInterface $node */
63
    $node = entity_create('node', $node);
64
    $node->enforceIsNew();
65 66

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

    $node->save();
    // Test the import.
71
    $node_by_nid = Node::load($test_nid);
72
    $this->assertTrue($node_by_nid, 'Node load by node ID.');
73 74

    $node_by_title = $this->drupalGetNodeByTitle($title);
75
    $this->assertTrue($node_by_title, 'Node load by node title.');
76 77 78
  }

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

    entity_create('node', $edit)->save();
    $node = $this->drupalGetNodeByTitle($edit['title']);
91 92
    $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.');
93 94

    // Store the timestamps.
95
    $created = $node->getCreatedTime();
96 97 98

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

101
    // Programmatically set the timestamps using hook_ENTITY_TYPE_presave().
102 103 104 105
    $node->title = 'testing_node_presave';

    $node->save();
    $node = $this->drupalGetNodeByTitle('testing_node_presave', TRUE);
106 107
    $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.');
108 109 110

    // Programmatically set the timestamps on the node.
    $edit = array(
111
      'uid' => $this->webUser->id(),
112
      'type' => 'article',
113
      'title' => $this->randomMachineName(8),
114 115 116 117 118 119
      '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']);
120 121
    $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.');
122 123

    // Update the timestamps.
124
    $node->setCreatedTime(979534800);
125 126 127 128
    $node->changed = 280299600;

    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
129 130
    $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.');
131 132 133
  }

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

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

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

    // Test the static node load cache to be cleared.
161
    $node = Node::load($node->id());
162
    $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
163
  }
164 165 166 167

  /**
   * Tests saving a node on node insert.
   *
168 169 170
   * 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.
171 172 173 174
   *
   * @see node_test_node_insert()
   */
  function testNodeSaveOnInsert() {
175
    // node_test_node_insert() triggers a save on insert if the title equals
176 177
    // 'new'.
    $node = $this->drupalCreateNode(array('title' => 'new'));
178
    $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');
179
  }
180
}