You are here

class RegistrationAPITestCase in Entity Registration 8.2

Same name in this branch
  1. 8.2 tests/registration.test \RegistrationAPITestCase
  2. 8.2 src/RegistrationAPITestCase.php \Drupal\registration\RegistrationAPITestCase

Hierarchy

Expanded class hierarchy of RegistrationAPITestCase

File

src/RegistrationAPITestCase.php, line 4

Namespace

Drupal\registration
View source
class RegistrationAPITestCase extends RegistrationTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Registration API',
      'description' => 'Test hooks provided by Registration.',
      'group' => 'Registration',
    );
  }
  function setUp() {
    parent::setUp(array(
      'registration_test_api',
    ));
    $this
      ->setUpEntity();
  }

  /**
   * Test hook_registration_access().
   */
  function testHookAccess() {
    $account = $this
      ->drupalCreateUser();
    $crud = array(
      'create',
      'view',
      'update',
      'delete',
    );
    $registration_values = array(
      'registrant_id' => $account->uid,
    );

    // Test hook.
    $registration = $this
      ->createRegistration($registration_values);
    $random = $this
      ->randomString();
    $registration->hook_registration_access = $random;
    $this
      ->assertEqual($random, \Drupal::moduleHandler()
      ->invoke('registration_test_api', 'registration_access', [
      'view',
      $registration,
      $account,
    ]), t('Manually invoke hook_registration_access()'), 'Registration');

    // Default access (none).
    foreach ($crud as $op) {
      $registration = $this
        ->createRegistration();
      $this
        ->assertFalse(entity_access($op, 'registration', $registration, $account), t('User cannot @op registration.', array(
        '@op' => $op,
      )), 'Registration');
    }

    // Force allow access.
    foreach ($crud as $op) {
      $registration = $this
        ->createRegistration($registration_values);
      $registration->hook_registration_access = TRUE;
      $this
        ->assertTrue(entity_access($op, 'registration', $registration, $account), t('User can @op registration.', array(
        '@op' => $op,
      )), 'Registration');
    }
  }

  /**
   * Test hook_registration_status().
   */
  function testHookStatus() {

    // Testing host status, no hook.
    $this
      ->setHostEntitySettings(array(
      'status' => 1,
    ));
    $this
      ->assertTrue(registration_status($this->host_entity_type, $this->host_entity_id, TRUE), t('Host entity status is open.'), 'Registration');

    // Host main status is opened, hook closes.
    \Drupal::configFactory()
      ->getEditable('registration.settings')
      ->set('registration_test_api_registration_status_alter', FALSE)
      ->save();
    $this
      ->assertFalse(registration_status($this->host_entity_type, $this->host_entity_id, TRUE), t('Host entity status is open, hook overrides'), 'Registration');

    // Hook should still be invoked if main status is closed.
    $this
      ->setHostEntitySettings(array(
      'status' => 0,
    ));
    \Drupal::configFactory()
      ->getEditable('registration.settings')
      ->set('registration_test_api_registration_status_alter', TRUE)
      ->save();
    $this
      ->assertTrue(registration_status($this->host_entity_type, $this->host_entity_id, TRUE), t('Host entity status is closed, hook overrides.'), 'Registration');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RegistrationAPITestCase::getInfo public static function
RegistrationAPITestCase::setUp function
RegistrationAPITestCase::testHookAccess function Test hook_registration_access().
RegistrationAPITestCase::testHookStatus function Test hook_registration_status().
RegistrationTestCase::createRegistration function Create a Registration programmatically.
RegistrationTestCase::entityLastId function
RegistrationTestCase::loadRegistration function Loads a registration from the database, avoiding the cache.
RegistrationTestCase::resetRegistration function Reset session cache of registration entities.
RegistrationTestCase::setHostEntitySettings function
RegistrationTestCase::setUpEntity function