NodeSaveTest.php 5.82 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
    $this->assertEqual($node->getOwnerId(), $this->webUser->id());
67 68 69

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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