You are here

userpoints_rules.test in User Points 7.2

Tests for Userpoints Services integration.

File

userpoints_rules/userpoints_rules.test
View source
<?php

/**
 * @file
 * Tests for Userpoints Services integration.
 */
class UserpointsRulesTestCase extends DrupalWebTestCase {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Rules integration'),
      'description' => t('Tests the rules events and actions.'),
      'group' => t('Userpoints'),
      'dependencies' => array(
        'rules',
      ),
    );
  }
  public function setUp() {
    parent::setUp(array(
      'userpoints',
      'entity',
      'rules',
      'userpoints_rules',
    ));
  }

  /**
   * Test the rules actions exposed by this module.
   */
  public function testActions() {
    $user = $this
      ->drupalCreateUser(array(
      'view userpoints',
      'view userpoints transactions',
    ));
    $this
      ->createActionRules();
    $this
      ->drupalLogin($user);
    $this
      ->drupalGet('myuserpoints');
    $this
      ->clickLink(t('view'));

    // Get the transaction id and load the transaction.
    preg_match('|myuserpoints\\/transaction\\/([0-9]+)\\/view|', $this
      ->getURL(), $matches);
    $txn_id = $matches[1];
    $transaction = userpoints_transaction_load($txn_id);

    // Assert a few properties.
    $this
      ->assertEqual(10, $transaction
      ->getPoints());
    $this
      ->assertTrue($transaction
      ->isApproved());
    $this
      ->assertTrue($transaction
      ->getExpiryDate() > REQUEST_TIME);
    $this
      ->assertEqual('userpoints_rules_login_test', $transaction
      ->getOperation());

    // Verify that the reason can be clicked and points to the user profile.
    $this
      ->clickLink(t('Granted points for logging in.'));
    $this
      ->assertEqual(url('user/' . $user->uid, array(
      'absolute' => TRUE,
    )), $this
      ->getUrl());
    $this
      ->drupalLogout();
  }

  /**
   * Test the before and after events and the setter and getter callbacks.
   */
  public function testEvents() {
    $user = $this
      ->drupalCreateUser(array(
      'view userpoints',
    ));
    $this
      ->createEventRules();
    $transaction = userpoints_grant_points('userpoints_rules_trigger_before_rule', 10, 'userpoints', $user->uid);
    $transaction
      ->save();

    // Verify that the changes defined in the rules have been done.
    $this
      ->assertTrue($transaction
      ->isDeclined(), t('Transaction has been marked as declined.'));
    $this
      ->assertEqual('Transaction was declined through rules.', $transaction
      ->getDescription(), t('Transaction description has been set.'));

    // Verify that the changes have been persisted in the database.
    $transaction = userpoints_transaction_load($transaction
      ->getTxnId());
    $this
      ->assertTrue($transaction
      ->isDeclined(), t('Transaction has been marked as declined.'));
    $this
      ->assertEqual('Transaction was declined through rules.', $transaction
      ->getDescription(), t('Transaction description has been set.'));
    $transaction = userpoints_grant_points('userpoints_rules_trigger_after_rule', 15, 'userpoints', $user->uid)
      ->pending();
    $transaction
      ->save();

    // Verify that the changes defined in the rules have been done.
    $transaction = userpoints_transaction_load($transaction
      ->getTxnId());
    $this
      ->assertTrue($transaction
      ->isApproved(), t('Transaction has been marked as approved.'));
    $this
      ->assertEqual('Transaction was set to pending through rules.', $transaction
      ->getReference(), t('Transaction reference has been set.'));
  }

  /**
   * Set up the rules required for the tests.
   */
  protected function createEventRules() {
    $before_rule = '{ "rules_userpoints_transaction_before_test_rule" : {
        "LABEL" : "Userpoints Transaction Before Test rule",
        "PLUGIN" : "reaction rule",
        "REQUIRES" : [ "rules", "userpoints_rules" ],
        "ON" : [ "userpoints_event_points_awarded_before" ],
        "IF" : [
          { "data_is" : {
              "data" : [ "userpoints-transaction:operation" ],
              "value" : "userpoints_rules_trigger_before_rule"
            }
          }
        ],
        "DO" : [
          { "data_set" : { "data" : [ "userpoints-transaction:status" ], "value" : "2" } },
          { "data_set" : {
              "data" : [ "userpoints-transaction:description" ],
              "value" : "Transaction was declined through rules."
            }
          }
        ]
      }
    }';
    $after_rule = '{ "rules_userpoints_transaction_after_test_rule" : {
        "LABEL" : "Userpoints Transaction After Test rule",
        "PLUGIN" : "reaction rule",
        "REQUIRES" : [ "rules", "userpoints_rules" ],
        "ON" : [ "userpoints_transaction_insert" ],
        "IF" : [
          { "data_is" : {
              "data" : [ "userpoints-transaction:operation" ],
              "value" : "userpoints_rules_trigger_after_rule"
            }
          }
        ],
        "DO" : [
          { "data_set" : { "data" : [ "userpoints-transaction:status" ], "value" : "0" } },
          { "data_set" : {
              "data" : [ "userpoints-transaction:reference" ],
              "value" : "Transaction was set to pending through rules."
            }
          }
        ]
      }
    }';

    // Save the rules.
    $rule = rules_import($before_rule);
    $rule
      ->save();
    $rule = rules_import($after_rule);
    $rule
      ->save();
    drupal_static_reset('rules_get_cache');
    cache_clear_all('*', 'cache_rules', TRUE);
  }
  function createActionRules() {
    $grant_action = '{ "rules_userpoints_rules_grant_points_test" : {
        "LABEL" : "Userpoints Rules grant points test",
        "PLUGIN" : "reaction rule",
        "REQUIRES" : [ "rules", "userpoints_rules" ],
        "ON" : [ "user_login" ],
        "DO" : [
          { "userpoints_action_grant_points" : {
              "type" : "userpoints",
              "user" : [ "account" ],
              "points" : "10",
              "tid" : "0",
              "entity" : [ "account" ],
              "description" : "Granted points for logging in.",
              "operation" : "userpoints_rules_login_test",
              "reference" : "Userpoints Rules Test",
              "display" : 1,
              "moderate" : "approved",
              "expirydate" : "+1 year"
            }
          }
        ]
      }
    }';

    // Save the rules.
    $rule = rules_import($grant_action);
    $rule
      ->save();
    drupal_static_reset('rules_get_cache');
    cache_clear_all('*', 'cache_rules', TRUE);
  }

}

Classes

Namesort descending Description
UserpointsRulesTestCase @file Tests for Userpoints Services integration.