diff --git a/userpoints_role/userpoints_role.info b/userpoints_role/userpoints_role.info
index 7c57d7b..9dc4365 100644
--- a/userpoints_role/userpoints_role.info
+++ b/userpoints_role/userpoints_role.info
@@ -3,3 +3,4 @@ description = Users join/leave roles as they earn/lose certain points threshold,
 package = Userpoints
 dependencies[] = userpoints
 core = 7.x
+files[]=userpoints_role.test
diff --git a/userpoints_role/userpoints_role.module b/userpoints_role/userpoints_role.module
index e8aad24..592c301 100644
--- a/userpoints_role/userpoints_role.module
+++ b/userpoints_role/userpoints_role.module
@@ -224,7 +224,7 @@ function _userpoints_role_update_roles($transaction) {
       // Only continue if the role has points assigned and the category
       // equals the category of the points transaction.
       if ($role_point['points'] > 0 && $role_point['tid'] == $transaction['tid']) {
-        if ($points < $role_point['points']) {
+        if ($points >= $role_point['points']) {
           // Assign the role to the user.
           userpoints_role_join($transaction['uid'], $role_point);
         }
@@ -339,6 +339,6 @@ function _userpoints_role_load_role_data($rid, $name) {
     'rid'    => $rid,
     'name'   => $name,
     'points' => (int)variable_get(USERPOINTS_ROLE . $rid, 0),
-    'tid'    => variable_get(USERPOINTS_ROLE . $rid .'_tid', 0),
+    'tid'    => (int)variable_get(USERPOINTS_ROLE . $rid .'_tid', 0),
   );
 }
diff --git a/userpoints_role/userpoints_role.test b/userpoints_role/userpoints_role.test
new file mode 100644
index 0000000..012acd8
--- /dev/null
+++ b/userpoints_role/userpoints_role.test
@@ -0,0 +1,119 @@
+<?php
+
+/**
+ * API Tests.
+ */
+class UserpointsRoleTestCase extends UserpointsBaseTestCase {
+
+  protected $admin_user;
+
+  /**
+   * Implements getInfo().
+   */
+  function getInfo() {
+    return array(
+        'name' => t('Userpoints Role'),
+        'description' => t('Tests the userpoints role functionality'),
+        'group' => t('Userpoints'),
+    );
+  }
+
+  /**
+   * Install userpoints module and create users.
+   */
+  function setUp() {
+    parent::setUp('userpoints', 'userpoints_role');
+
+    // Create an administrator account.
+    $this->admin_user = $this->drupalCreateUser(array('administer userpoints', 'administer users', 'administer permissions'));
+  }
+
+  function createRole() {
+    $edit = array(
+      'name' => $role_name = $this->randomName(),
+    );
+    $this->drupalPost('admin/people/permissions/roles', $edit, t('Add role'));
+    $this->assertText(t('The role has been added.'));
+    $this->assertText($role_name);
+
+    $roles = user_roles(TRUE);
+
+    return array(array_search($role_name, $roles), $role_name);
+  }
+
+  function testTwoRoles() {
+    $this->drupalLogin($this->admin_user);
+
+    // Create two roles.
+    list($first_role_rid, $first_role_name) = $this->createRole();
+    list($second_role_rid, $second_role_name) = $this->createRole();
+
+    // Check that the settings for the defined roles show up.
+    $this->drupalGet('admin/config/people/userpoints/settings');
+    $this->assertText(t('Points for role @role', array('@role' => $first_role_name)));
+    $this->assertText(t('Points for role @role', array('@role' => $second_role_name)));
+
+    $edit = array(
+      'userpoints_role_' . $first_role_rid => 10,
+      'userpoints_role_' . $second_role_rid => 50,
+    );
+    $this->drupalPost(NULL, $edit, t('Save configuration'));
+
+    $user1 = $this->drupalCreateUser();
+
+    // Grant 5 points, nothing should happen yet.
+    userpoints_userpointsapi(array(
+      'points' => 5,
+      'uid' => $user1->uid,
+    ));
+    $this->assertEqual(5, userpoints_get_current_points($user1->uid));
+
+    $user1 = user_load($user1->uid, TRUE);
+    $this->assertFalse(isset($user1->roles[$first_role_rid]));
+    $this->assertFalse(isset($user1->roles[$second_role_rid]));
+
+    // Grant 7 more points, the user shoud now have the first role.
+    userpoints_userpointsapi(array(
+      'points' => 7,
+      'uid' => $user1->uid,
+    ));
+    $this->assertEqual(12, userpoints_get_current_points($user1->uid));
+
+    $user1 = user_load($user1->uid, TRUE);
+    $this->assertTrue(isset($user1->roles[$first_role_rid]));
+    $this->assertFalse(isset($user1->roles[$second_role_rid]));
+
+    // Remove 3 points, role should be removed again.
+    userpoints_userpointsapi(array(
+      'points' => -3,
+      'uid' => $user1->uid,
+    ));
+    $this->assertEqual(9, userpoints_get_current_points($user1->uid));
+
+    $user1 = user_load($user1->uid, TRUE);
+    $this->assertFalse(isset($user1->roles[$first_role_rid]));
+    $this->assertFalse(isset($user1->roles[$second_role_rid]));
+
+    // Grant 41 points, the user should now have both roles.
+    userpoints_userpointsapi(array(
+      'points' => 41,
+      'uid' => $user1->uid,
+    ));
+    $this->assertEqual(50, userpoints_get_current_points($user1->uid));
+
+    $user1 = user_load($user1->uid, TRUE);
+    $this->assertTrue(isset($user1->roles[$first_role_rid]));
+    $this->assertTrue(isset($user1->roles[$second_role_rid]));
+
+   // Remove 1 point, second role should be removed again.
+    userpoints_userpointsapi(array(
+      'points' => -1,
+      'uid' => $user1->uid,
+    ));
+    $this->assertEqual(49, userpoints_get_current_points($user1->uid));
+
+    $user1 = user_load($user1->uid, TRUE);
+    $this->assertTrue(isset($user1->roles[$first_role_rid]));
+    $this->assertFalse(isset($user1->roles[$second_role_rid]));
+  }
+}
