'page', 'title' => $this->randomName(32), 'body' => array(LANGUAGE_NONE => array(array($this->randomName(64)))), ); $this->drupalCreateNode($settings); // Using drupalCreateUser runs tests we don't want. $edit = array( 'name' => $this->randomName(32), 'mail' => $this->randomName(32) . '@example.com', 'roles' => array(DRUPAL_AUTHENTICATED_RID), 'status' => 1, ); user_save(NULL, $edit); } } /** * Helper function to create domains. * * @param $subdomains * An array of subdomains to create. * @param $base * A base domain string, in the format 'example.com'. * * @return * An array of created domains. */ public function domainCreateDomains($subdomains = array('one', 'two', 'three'), $base = NULL) { $setup = drupal_map_assoc($subdomains); $base_domain = $base; if (empty($base_domain) && !empty($_SERVER['HTTP_HOST'])) { $base_domain = $_SERVER['HTTP_HOST']; } if (empty($base_domain)) { $base_domain = 'example.com'; } $i = 0; $domains = array(); foreach ($setup as $subdomain) { $record = array( 'sitename' => $subdomain, 'subdomain' => $subdomain . '.' . $base_domain, 'valid' => 1, 'is_default' => 0, 'weight' => $i++, 'scheme' => 'http', ); $domains[$subdomain] = domain_save($record, $record); } return $domains; } public function tearDown() { parent::tearDown(); } } class DomainInstallTest extends DomainTestCase { public static function getInfo() { return array( 'name' => 'Check install routine', 'description' => 'Create primary domain record and verify its consistency in the database.', 'group' => 'Domain Access', ); } // If the default domain is not in the database, the sitename will be default sitename. public function testDomainInstall() { $domain = db_query("SELECT sitename FROM {domain} WHERE is_default = 1")->fetch(); $this->assertTrue($domain->sitename == 'TestDomainSitename', t('Default domain created successfully')); } // Check the existence of our default grant. function testDomainGrantAll() { domain_set_default_grant(TRUE); $check = db_query_range("SELECT 1 FROM {node_access} WHERE realm = :realm AND gid = :gid", 0, 1, array( ':realm' => 'domain_all', ':gid' => 0, ) )->fetchField(); $this->assertTrue($check > 0, t('Domain all grant stored correctly !check.', array('!check' => $check))); } function testDomainEnable() { // Delete records so we can test the installer. db_query("TRUNCATE {domain_access}"); db_query("TRUNCATE {domain_editor}"); // Check nodes. $nid = db_query_range("SELECT n.nid FROM {node} n INNER JOIN {domain_access} da ON n.nid = da.nid", 0, 1)->fetchField(); $this->assertTrue(empty($nid), t('Initial nodes have no domain data.')); // Check users. $uid = db_query_range("SELECT u.uid FROM {users} u INNER JOIN {domain_editor} de ON u.uid = de.uid", 0, 1)->fetchField(); $this->assertTrue(empty($uid), t('Initial users have no domain data.')); // Run domain_enable. domain_enable(); // Check nodes. $nid = db_query_range("SELECT n.nid FROM {node} n INNER JOIN {domain_access} da ON n.nid = da.nid", 0, 1)->fetchField(); $this->assertTrue(!empty($nid), t('Nodes have added domain data.')); // Check users. $uid = db_query_range("SELECT u.uid FROM {users} u INNER JOIN {domain_editor} de ON u.uid = de.uid", 0, 1)->fetchField(); $this->assertTrue(!empty($uid), t('Users have added domain data.')); } } class DomainCreateTest extends DomainTestCase { public static function getInfo() { return array( 'name' => 'Create domains', 'description' => 'Create random domains and verify consistency in the database.', 'group' => 'Domain Access', ); } // Create some domains and verify they match what we expect. function testDomainCreate() { $setup = $this->domainCreateDomains(); $result = db_query("SELECT sitename, subdomain FROM {domain} WHERE is_default = 0 ORDER BY weight"); foreach ($result as $record) { $domains[$record->sitename] = array('sitename' => $record->sitename, 'subdomain' => $record->subdomain); } foreach ($domains as $sitename => $domain) { $this->assertTrue($domains[$sitename]['sitename'] == $setup[$sitename]['sitename'], t('Domain %subdomain created successfully', array('%subdomain' => $domain['subdomain']))); } // Delete one of the domains. domain_delete($setup['three']); $deleted_domain = domain_lookup($setup['three']['domain_id'], NULL, TRUE); $this->assertTrue($deleted_domain == -1, t('Domain %domain deleted successfully.', array('%domain' => $setup['three']['subdomain']))); } // Try to create badly formed domains. function testDomainValidate() { $tests = array( 'localhost' => array(), 'nodotsinstring' => array(t('At least one dot (.) is required, except for localhost.')), 'example.com:2000:1' => array(t('Only one colon (:) is allowed.')), 'example.com:foo' => array(t('The port protocol must be an integer.')), '.example.com' => array(t('The domain must not begin with a dot (.)')), 'example.com.' => array(t('The domain must not end with a dot (.)')), '$.example.com' => array(t('Only alphanumeric characters, dashes, and a colon are allowed.')), 'EXAMPLE.com' => array(t('Only lower-case characters are allowed.')), ); foreach ($tests as $subdomain => $error) { $return = domain_validate($subdomain); if ($subdomain == 'localhost' && empty($return)) { $return = TRUE; } $this->assertTrue(!empty($return), t('!message', array('!message' => !empty($error) ? $error[0] : t('localhost is an allowed domain string.')))); } // Test for the non-alphanumeric character override. global $conf; $conf['domain_allow_non_ascii'] = TRUE; $return = domain_validate('$%#!.com'); $this->assertTrue(empty($return), t('ASCII character override value allowed.')); } } class DomainHookTest extends DomainTestCase { public static function getInfo() { return array( 'name' => 'Test domain hooks', 'description' => 'Fire domain hooks and test responses.', 'group' => 'Domain Access', ); } // On setup, install our test module. function setUp() { parent::setUp(array('domain_test')); } 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() resaves 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); // 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 = '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.')); } } class DomainSwitchingTest extends DomainTestCase { public static function getInfo() { return array( 'name' => 'Domain switching', 'description' => 'Ensure that programmtic domain switching works.', 'group' => 'Domain Access', ); } // On setup, install our test module. function setUp() { parent::setUp(); } // Test domain creation and switching. function testDomainSwtiching() { $_domain = domain_get_domain(); // Create some domains. $result = $this->domainCreateDomains(); // Are we currently on the primary domain? $default_id = domain_default_id(); $this->assertTrue($_domain['domain_id'] == $default_id, t('Primary domain loaded initially.')); // Get the domain list. $domains = domain_domains(TRUE); // Switch to each active domain. foreach ($domains as $domain) { domain_set_domain($domain['domain_id'], TRUE); $active = domain_get_domain(); $this->assertTrue($active['domain_id'] == $domain['domain_id'], t('Switched to domain %domain.', array('%domain' => $domain['subdomain']))); } // Reset the active domain. domain_reset_domain(TRUE); $active = domain_get_domain(); $this->assertTrue($active['domain_id'] == $_domain['domain_id'], t('Switched back to initial domain.')); } }