You are here

function DomainHookTest::testDomainHooks in Domain Access 7.3

File

tests/domain.test, line 307
Simpletest for Domain Access.

Class

DomainHookTest

Code

function testDomainHooks() {

  // hook_domain_load() adds 'testvar' => TRUE to the domain array.
  $domain = domain_default(TRUE);
  $this
    ->assertTrue(!empty($domain['testvar']), t('hook_domain_load() fired correctly.'));

  // hook_domain_insert() sets sitename to 'foobar' instead of 'testfoo'.
  $result = $this
    ->domainCreateDomains(array(
    'testfoo',
  ));
  $new_domain = $result['testfoo'];
  $this
    ->assertTrue($new_domain['sitename'] == 'foobar', t('hook_domain_insert() fired correctly.'));

  // hook_domain_update() re-saves that domain and set it's sitename back to 'testfoo'.
  $updated_domain = domain_save($new_domain, $new_domain);
  $this
    ->assertTrue($updated_domain['sitename'] == 'testfoo', t('hook_domain_update() fired correctly.'));

  // hook_domain_cron() should run for each domain, setting our static to the name of the domain.
  // Get the domain list.
  $domains = domain_domains(TRUE);

  // Run the hook for each active domain.
  foreach ($domains as $domain) {
    domain_set_domain($domain['domain_id'], TRUE);
    module_invoke('domain_test', 'domain_cron', $domain);
    $value = domain_test_get();
    $this
      ->assertTrue($value == $domain['sitename'], t('Domain cron fired for %domain.', array(
      '%domain' => $domain['subdomain'],
    )));
  }

  // Reset the active domain.
  domain_reset_domain(TRUE);

  // domain_reassign() sets a static value to 'domain_editor' and changes
  // editors from domain 1 to domain 2. It also calls a hook that our
  // test module uses to set a variable to 'domain_editor'.
  $old_domain = domain_lookup(1);
  $new_domain = domain_lookup(2);
  $count = db_query("SELECT COUNT(domain_id) FROM {domain_editor} WHERE domain_id = :domain_id", array(
    ':domain_id' => $old_domain['domain_id'],
  ))
    ->fetchField();
  $count2 = db_query("SELECT COUNT(domain_id) FROM {domain_editor} WHERE domain_id = :domain_id", array(
    ':domain_id' => $new_domain['domain_id'],
  ))
    ->fetchField();
  $total = $count + $count2;
  domain_reassign($old_domain, $new_domain, 'domain_editor');
  $value = domain_test_get();
  $this
    ->assertTrue($value == 'domain_editor', t('hook_domain_reassign() fired correctly.'));
  $count = db_query("SELECT COUNT(domain_id) FROM {domain_editor} WHERE domain_id = :domain_id", array(
    ':domain_id' => $old_domain['domain_id'],
  ))
    ->fetchField();
  $count2 = db_query("SELECT COUNT(domain_id) FROM {domain_editor} WHERE domain_id = :domain_id", array(
    ':domain_id' => $new_domain['domain_id'],
  ))
    ->fetchField();
  $this
    ->assertTrue($count == 0, t('No editors remain on domain 1.'));
  $this
    ->assertTrue($total > 0 && $count2 == $total, t('Editors reassigned to domain 2.'));

  // hook_domain_delete() sets a static value to 'delete' and removes the domain.
  domain_delete($updated_domain);
  $value = domain_test_get();
  $this
    ->assertTrue($value == 'delete', t('hook_domain_delete() fired correctly.'));
  $deleted_domain = domain_lookup($updated_domain['domain_id'], NULL, TRUE);
  $this
    ->assertTrue($deleted_domain == -1, t('Domain deleted successfully.'));

  // hook_domain_validate_alter() allows any domain to be valid.
  $return = domain_validate('thisshouldfail');
  $this
    ->assertTrue(empty($return), t('hook_domain_validate_alter() fired correctly.'));

  // TODO: abstract these test functions?
  // hook_domain_bootstrap_lookup() will let us adjust the domain lookup.
  // Here we read the domain 'local.test' as an alias for the default domain.
  // However, this function can't really be registered, so we have to fake it a bit.
  $new_domain = $this
    ->domainCreateDomains(array(
    'newtest',
  ));
  $test_domain = array(
    'subdomain' => 'local.test',
  );
  $modules = _domain_bootstrap_modules();
  $modules[] = 'domain_test';
  $args[] = $test_domain;
  foreach ($modules as $module) {
    $function = $module . '_domain_bootstrap_lookup';
    if (function_exists($function)) {
      $result = call_user_func_array($function, $args);
      if (isset($result) && is_array($result)) {
        $return = array_merge_recursive($return, $result);
      }
      elseif (isset($result)) {
        $return[] = $result;
      }
    }
  }
  $this
    ->assertTrue($new_domain['newtest']['domain_id'] == $return['domain_id'], t('hook_domain_bootstrap_lookup() fired correctly.'));

  // hook_domain_bootstrap_full() fires after a domain is determined.
  // This hook does not allow a return value, so we modify the $_domain global.
  // However, this function can't really be registered, so we have to fake it a bit.
  $args = array(
    $return,
  );
  foreach ($modules as $module) {
    $function = $module . '_domain_bootstrap_full';
    if (function_exists($function)) {
      call_user_func_array($function, $args);
    }
  }
  $return = domain_get_domain();
  $this
    ->assertTrue(!empty($return['test_full']), t('hook_domain_bootstrap_full() fired correctly.'));

  // hook_domain_batch() should add a 'domain_test' array element.
  $batch = domain_batch_actions();
  $this
    ->assertTrue(isset($batch['domain_test']), t('hook_domain_batch() fired correctly.'));

  // hook_domain_batch_alter() changes values in the batch options.
  $this
    ->assertTrue($batch['maintenance_mode']['#form']['#title'] == t('Test reset value'), t('hook_domain_batch_alter() fired correctly.'));
}