View source
<?php
class PaymentWebTestCase extends DrupalWebTestCase {
function setUp(array $modules = array()) {
$this->profile = 'testing';
parent::setUp($modules);
}
static function paymentCreate($uid, PaymentMethod $payment_method = NULL) {
$payment_method = $payment_method ? $payment_method : new PaymentMethodUnavailable();
$payment = new Payment(array(
'currency_code' => 'XXX',
'description' => 'This is the payment description',
'finish_callback' => 'payment_test_finish_callback',
'method' => $payment_method,
'uid' => $uid,
));
$payment
->setLineItem(new PaymentLineItem(array(
'name' => 'foo',
'amount' => 1.0,
'tax_rate' => 0.1,
)));
return $payment;
}
static function paymentMethodCreate($uid, PaymentMethodController $controller = NULL) {
$name = self::randomName();
$controller = $controller ? $controller : payment_method_controller_load('PaymentMethodControllerUnavailable');
$payment_method = new PaymentMethod(array(
'controller' => $controller,
'controller_data' => $controller->controller_data_defaults,
'name' => $name,
'title' => $name,
'uid' => $uid,
));
return $payment_method;
}
function assertEntityPermission($entity, $entity_type_title, $callback, $operation, array $permissions = array(), array $access = array()) {
$user_access_permissions =& drupal_static('user_access');
$anonymous = drupal_anonymous_user();
$root = drupal_anonymous_user();
$root->uid = 1;
$authenticated = drupal_anonymous_user();
$authenticated->uid = 2;
$comment = $entity && isset($entity->uid) ? ' with UID ' . $entity->uid : NULL;
$access += array(
'anonymous' => FALSE,
'root' => TRUE,
'authenticated_with_permissions' => TRUE,
'authenticated_without_permissions' => FALSE,
);
$can = $access['anonymous'] ? 'can' : 'cannot';
$this
->assertEqual($callback($operation, $entity, $anonymous), $access['anonymous'], "An anonymous user {$can} perform operation <strong>{$operation}</strong> on a <strong>{$entity_type_title}</strong>{$comment} without permission(s) " . $this
->permissionLabel($permissions));
$can = $access['root'] ? 'can' : 'cannot';
$this
->assertEqual($callback($operation, $entity, $root), $access['root'], "The root user (UID 1) {$can} perform operation <strong>{$operation}</strong> on a <strong>{$entity_type_title}</strong>{$comment} without permission(s) " . $this
->permissionLabel($permissions));
if ($permissions) {
$user_access_permissions[2] = array_fill_keys($permissions, TRUE);
$can = $access['authenticated_with_permissions'] ? 'can' : 'cannot';
$this
->assertEqual($callback($operation, $entity, $authenticated), $access['authenticated_with_permissions'], "An authenticated user (UID 2) {$can} perform operation <strong>{$operation}</strong> on a <strong>{$entity_type_title}</strong>{$comment} with permission(s) " . $this
->permissionLabel($permissions));
}
foreach ($permissions as $i => $permission) {
$assert_permissions = $permissions;
unset($assert_permissions[$i]);
$user_access_permissions[2] = array_fill_keys($assert_permissions, TRUE);
$can = $access['authenticated_without_permissions'] ? 'can' : 'cannot';
$operation = $operation;
$this
->assertFalse($callback($operation, $entity, $authenticated), "An authenticated user (UID 2) {$can} perform operation <strong>{$operation}</strong> on a <strong>{$entity_type_title}</strong>{$comment} without permission " . $this
->permissionLabel(array(
$permissions[$i],
)));
}
}
function permissionLabel(array $permissions) {
$info =& drupal_static(__CLASS__ . '_permission');
if (is_null($info)) {
$info = module_invoke_all('permission');
}
$labels = array();
foreach ($permissions as $permission) {
$labels[] = '<strong>' . $info[$permission]['title'] . ' (' . $permission . ')</strong>';
}
return implode(', ', $labels);
}
}