You are here

public function NodeSaveTest::testDeterminingChanges in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeSaveTest.php \Drupal\Tests\node\Functional\NodeSaveTest::testDeterminingChanges()

Tests node presave and static node load cache.

This test determines changes in hook_ENTITY_TYPE_presave() and verifies that the static node load cache is cleared upon save.

File

core/modules/node/tests/src/Functional/NodeSaveTest.php, line 147

Class

NodeSaveTest
Tests $node->save() for saving content.

Namespace

Drupal\Tests\node\Functional

Code

public function testDeterminingChanges() {

  // Initial creation.
  $node = Node::create([
    'uid' => $this->webUser
      ->id(),
    'type' => 'article',
    'title' => 'test_changes',
  ]);
  $node
    ->save();

  // Update the node without applying changes.
  $node
    ->save();
  $this
    ->assertEqual($node
    ->label(), 'test_changes', 'No changes have been determined.');

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

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