You are here

public function RulesTestDataCase::testDataSaving in Rules 7.2

Same name and namespace in other branches
  1. 8.3 d7-tests/rules_test_data_case.test \RulesTestDataCase::testDataSaving()

Tests intelligently saving data.

File

tests/rules.test, line 984
Rules tests.

Class

RulesTestDataCase
Test rules data wrappers.

Code

public function testDataSaving() {
  $node = $this
    ->drupalCreateNode();
  $state = new RulesState(rule());
  $state
    ->addVariable('node', $node, array(
    'type' => 'node',
  ));
  $wrapper = $state
    ->get('node');
  $node->title = 'test';
  $wrapper
    ->set($node);
  $state
    ->saveChanges('node', $wrapper, FALSE);
  $this
    ->assertFalse($this
    ->drupalGetNodeByTitle('test'), 'Changes have not been saved.');
  $state
    ->saveChanges('node', $wrapper, TRUE);
  $this
    ->assertTrue($this
    ->drupalGetNodeByTitle('test'), 'Changes have been saved.');

  // Test skipping saving.
  $state
    ->addVariable('node2', $node, array(
    'type' => 'node',
    'skip save' => TRUE,
  ));
  $wrapper = $state
    ->get('node2');
  $node->title = 'test2';
  $wrapper
    ->set($node);
  $state
    ->saveChanges('node2', $wrapper, TRUE);
  $this
    ->assertFalse($this
    ->drupalGetNodeByTitle('test2'), 'Changes have not been saved.');

  // Try saving a non-entity wrapper, which should result in saving the
  // parent entity containing the property.
  $wrapper = $state
    ->get('node');
  $wrapper->title
    ->set('test3');
  $state
    ->saveChanges('node:title', $wrapper, TRUE);
  $this
    ->assertTrue($this
    ->drupalGetNodeByTitle('test3'), 'Parent entity has been saved.');
}