<?php

/**
 * @file
 * Role assignment product feature tests.
 */

/**
 * Tests the role purchase functionality.
 */
class UbercartRolesTestCase extends UbercartTestHelper {

  public static function getInfo() {
    return array(
      'name' => 'Roles',
      'description' => 'Ensures that the purchase of roles functions correctly.',
      'group' => 'Ubercart',
    );
  }

  function setUp() {
    $modules = array('uc_payment', 'uc_payment_pack', 'uc_roles');
    $permissions = array();
    parent::setUp($modules, $permissions);
  }

  function testRolePurchaseCheckout() {
    // Add role assignment to the test product.
    $rid = $this->drupalCreateRole(array('access content'));
    $this->drupalLogin($this->adminUser);
    $this->drupalPost('node/' . $this->product->nid . '/edit/features', array('feature' => 'role'), t('Add'));
    $this->drupalPost(NULL, array('uc_roles_role' => $rid), t('Save feature'));

    // Check out with the test product.
    $this->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
    $order = $this->checkout();
    uc_payment_enter($order->order_id, 'other', $order->order_total);

    // Test that the role was granted.
    $account = user_load($order->uid);
    $this->assertTrue(isset($account->roles[$rid]), 'Existing user was granted role.');

    // Test that the email is correct.
    $mail = $this->findMail('/Ubercart: ' . preg_quote($account->roles[$rid]) . ' role granted/');
  }
}
