You are here

function PaymentWebTestCase::assertEntityPermission in Payment 7

Test an entity's permissions.

Parameters

object $entity: This does not have to be a 'real' entity registered with hook_entity_info().

string $entity_type_title: The entity's human-readable type.

string $callback: The name of the access function, which must accept the operation, the entity and a user account as arguments.

string $operation: The operation to perform on the entity.

array $permissions: Permissions to grant authenticated users before testing their access. Defaults to an empty array.

array $access: An array of arrays with the following items:

  • anonymous (boolean): Whether anonymous users should be able to perform the operation. Defaults to FALSE.
  • root (boolean): Whether the root user (with UID 1) should be able to perform the operation. Defaults to TRUE.
  • authenticated_with_permissions (boolean): Whether authenticated users that have all the required permissions should be able to perform the operation. Defaults to TRUE.
  • authenticated_without_permissions (boolean): Whether authenticated users that do not have all the required permissions should be able to perform the operation. Defaults to FALSE.

Return value

NULL

2 calls to PaymentWebTestCase::assertEntityPermission()
PaymentTestPaymentEntityPermissionWebTestCase::testPaymentEntityPermissions in tests/payment_test/tests/PaymentTestPaymentEntityPermissionWebTestCase.test
PaymentTestPaymentMethodEntityPermissionWebTestCase::testPaymentMethodEntityPermissions in tests/payment_test/tests/PaymentTestPaymentMethodEntityPermissionWebTestCase.test

File

tests/PaymentWebTestCase.test, line 104

Class

PaymentWebTestCase
Provides reusable code for testing payments and payment methods.

Code

function assertEntityPermission($entity, $entity_type_title, $callback, $operation, array $permissions = array(), array $access = array()) {
  $user_access_permissions =& drupal_static('user_access');

  // Create the user accounts.
  $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;

  // Merge in defaults.
  $access += array(
    'anonymous' => FALSE,
    'root' => TRUE,
    'authenticated_with_permissions' => TRUE,
    'authenticated_without_permissions' => FALSE,
  );

  // Test anonymous users.
  $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));

  // // Test UID 1.
  $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));

  // Test authenticated users with all 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));
  }

  // Test authenticated users without all 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],
    )));
  }
}