function RulesTestCase::testRuleImportExport

Same name in other branches
  1. 8.x-3.x d7-tests/rules_test_case.test \RulesTestCase::testRuleImportExport()

Tests importing and exporting a rule.

File

tests/rules.test, line 821

Class

RulesTestCase
Rules test cases.

Code

public function testRuleImportExport() {
    $rule = rule(array(
        'nid' => array(
            'type' => 'integer',
        ),
    ));
    $rule->name = "rules_export_test";
    $rule->action('rules_action_load_node')
        ->action('drupal_message', array(
        'message' => 'Title: [node_loaded:title]',
    ));
    $export = '{ "rules_export_test" : {
    "PLUGIN" : "rule",
    "REQUIRES" : [ "rules_test", "rules" ],
    "USES VARIABLES" : { "nid" : { "type" : "integer" } },
    "DO" : [
      { "rules_action_load_node" : { "PROVIDE" : { "node_loaded" : { "node_loaded" : "Loaded content" } } } },
      { "drupal_message" : { "message" : "Title: [node_loaded:title]" } }
    ]
  }
}';
    $this->assertEqual($export, $rule->export(), 'Rule has been exported correctly.');
    // Test importing a rule which makes use of almost all features.
    $export = _rules_export_get_test_export();
    $rule = rules_import($export);
    $this->assertTrue(!empty($rule) && $rule->integrityCheck(), 'Rule has been imported.');
    // Test loading the same export provided as default rule.
    $rule = rules_config_load('rules_export_test');
    $this->assertTrue(!empty($rule) && $rule->integrityCheck(), 'Export has been provided in code.');
    // Export it and make sure the same export is generated again.
    $this->assertEqual($export, $rule->export(), 'Export of imported rule equals original export.');
    // Now try importing a rule set.
    $export = '{ "rules_test_set" : {
    "LABEL" : "Test set",
    "PLUGIN" : "rule set",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "node" : { "label" : "Test node", "type" : "node" } },
    "RULES" : [
      { "RULE" : {
          "IF" : [ { "NOT data_is" : { "data" : [ "node:title" ], "value" : "test" } } ],
          "DO" : [ { "data_set" : { "data" : [ "node:title" ], "value" : "test" } } ],
          "LABEL" : "Test Rule"
        }
      },
      { "RULE" : {
          "DO" : [ { "drupal_message" : { "message" : "hi" } } ],
          "LABEL" : "Test Rule 2"
        }
      }
    ]
  }
}';
    $set = rules_import($export);
    $this->assertTrue(!empty($set) && $set->integrityCheck(), 'Rule set has been imported.');
    // Export it and make sure the same export is generated again.
    $this->assertEqual($export, $set->export(), 'Export of imported rule set equals original export.');
    // Try executing the imported rule set.
    $node = $this->drupalCreateNode();
    $set->execute($node);
    $this->assertEqual($node->title, 'test', 'Imported rule set has been executed.');
    RulesLog::logger()->checkLog();
    // Try import / export for a rule component providing a variable.
    $rule = rule(array(
        'number' => array(
            'type' => 'integer',
            'label' => 'Number',
            'parameter' => FALSE,
        ),
    ), array(
        'number',
    ));
    $rule->action('data_set', array(
        'data:select' => 'number',
        'value' => 3,
    ));
    $rule->name = 'rules_test_provides';
    $export = '{ "rules_test_provides" : {
    "PLUGIN" : "rule",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "number" : { "type" : "integer", "label" : "Number", "parameter" : false } },
    "DO" : [ { "data_set" : { "data" : [ "number" ], "value" : 3 } } ],
    "PROVIDES VARIABLES" : [ "number" ]
  }
}';
    $this->assertEqual($export, $rule->export(), 'Rule 2 has been exported correctly.');
    $imported_rule = rules_import($rule->export());
    $this->assertTrue(!empty($imported_rule) && $imported_rule->integrityCheck(), 'Rule 2 has been imported.');
    $this->assertEqual($export, $imported_rule->export(), 'Export of imported rule 2 equals original export.');
    // Test importing a negated condition component.
    $export = '{ "rules_negated_component" : {
    "LABEL" : "negated_component",
    "PLUGIN" : "or",
    "REQUIRES" : [ "rules" ],
    "NOT OR" : [ { "data_is_empty" : { "data" : [ "site:slogan" ] } } ]
  }
}';
    $or = rules_import($export);
    $this->assertTrue($or->integrityCheck() && $or->isNegated(), 'Negated condition component imported.');
}