You are here

public function RulesTestCase::testRulesCrud in Rules 7.2

Same name and namespace in other branches
  1. 8.3 d7-tests/rules_test_case.test \RulesTestCase::testRulesCRUD()

Tests CRUD functionality.

File

tests/rules.test, line 249
Rules tests.

Class

RulesTestCase
Rules test cases.

Code

public function testRulesCrud() {
  $rule = $this
    ->createTestRule();
  $rule
    ->integrityCheck()
    ->save('test');
  $this
    ->assertEqual(TRUE, $rule->active, 'Rule is active.');
  $this
    ->assertEqual(0, $rule->weight, 'Rule weight is zero.');
  $results = entity_load('rules_config', array(
    'test',
  ));
  $rule2 = array_pop($results);
  $this
    ->assertEqual($rule->id, $rule2->id, 'Rule created and loaded');
  $this
    ->assertEqual(get_class($rule2), get_class($rule), 'Class properly instantiated.');
  $rule2
    ->execute();

  // Update.
  $rule2
    ->save();

  // Make sure all rule elements are still here.
  $it = new RecursiveIteratorIterator($rule2
    ->conditions(), RecursiveIteratorIterator::SELF_FIRST);
  $this
    ->assertEqual(iterator_count($it), 8, 'Iterated over all conditions and condition containers');
  $it = new RecursiveIteratorIterator($rule2
    ->conditions());
  $this
    ->assertEqual(iterator_count($it), 6, 'Iterated over all conditions');
  $this
    ->assertEqual(iterator_count($rule2
    ->actions()), 1, 'Iterated over all actions');

  // Delete.
  $rule2
    ->delete();
  $this
    ->assertEqual(entity_load('rules_config', FALSE, array(
    'id' => $rule->id,
  )), array(), 'Deleted.');

  // Tests CRUD for tags - making sure the tags are stored properly..
  $rule = $this
    ->createTestRule();
  $tag = $this
    ->randomString();
  $rule->tags = array(
    $tag,
  );
  $rule
    ->save();
  $result = db_select('rules_tags')
    ->fields('rules_tags', array(
    'tag',
  ))
    ->condition('id', $rule->id)
    ->execute();
  $this
    ->assertEqual($result
    ->fetchField(), $tag, 'Associated tag has been saved.');

  // Try updating.
  $rule->tags = array(
    $this
      ->randomName(),
    $this
      ->randomName(),
  );
  $rule
    ->integrityCheck()
    ->save();
  $result = db_select('rules_tags')
    ->fields('rules_tags', array(
    'tag',
  ))
    ->condition('id', $rule->id)
    ->execute()
    ->fetchCol();
  $this
    ->assertTrue(in_array($rule->tags[0], $result) && in_array($rule->tags[1], $result), 'Updated associated tags.');

  // Try loading multiple rules by tags.
  $rule2 = $this
    ->createTestRule();
  $rule2->tags = array(
    $this
      ->randomName(),
  );
  $rule2
    ->save();
  $loaded = entity_load('rules_config', FALSE, array(
    'tags' => array(
      $rule->tags[0],
      $rule2->tags[0],
    ),
  ));
  $this
    ->assertTrue($loaded[$rule->id]->id == $rule->id && $loaded[$rule2->id]->id == $rule2->id, 'Loading configs by tags');

  // Try deleting.
  $rule
    ->delete();
  $result = db_select('rules_tags')
    ->fields('rules_tags', array(
    'tag',
  ))
    ->condition('id', $rule->id)
    ->execute();
  $this
    ->assertEqual($result
    ->fetchField(), FALSE, 'Deleted associated tags.');
}