function DomainUnitTest::testDomainGetNodeDomains in Domain Access 7.3
File
- tests/
domain.test, line 473 - Simpletest for Domain Access.
Class
Code
function testDomainGetNodeDomains() {
$count = db_query("SELECT COUNT(domain_id) FROM {domain}")
->fetchField();
$this
->assertTrue($count == 1, t('Primary domain created.'));
// Reset the default domain.
$domain = domain_default(TRUE);
// By design, there should be 10 nodes created with default domain assignment.
$count = db_query("SELECT COUNT(nid) FROM {node}")
->fetchField();
$this
->assertTrue($count == 10, t('Ten initial nodes created.'));
$count = db_query("SELECT COUNT(nid) FROM {domain_access} WHERE realm = 'domain_id'")
->fetchField();
$this
->assertTrue($count == 10, t('Initial nodes are assigned to the default domain.'));
$count = db_query("SELECT COUNT(nid) FROM {domain_access} WHERE realm = 'domain_site'")
->fetchField();
$this
->assertTrue($count == 10, t('Initial nodes are assigned to all affiliates.'));
// Reset the {node_access} table.
node_access_rebuild();
// Create three more domains.
$this
->domainCreateDomains();
$count = db_query("SELECT COUNT(domain_id) FROM {domain}")
->fetchField();
$this
->assertTrue($count == 4, t('Three new domains created.'));
// Reset the domains.
$domains = domain_domains(TRUE);
// Fetch the existing node ids.
$node_ids = array_keys(db_query("SELECT nid FROM {node}")
->FetchAllAssoc('nid'));
// On node load, these should all be assigned to domain_site and domain_id = 1.
$nodes = node_load_multiple($node_ids);
// Check domains.
$set = TRUE;
// TODO: This could be more elegant.
foreach ($nodes as $node) {
if (!isset($node->domains)) {
$set = FALSE;
}
if (empty($node->domains)) {
$set = FALSE;
}
if (empty($node->domains[1])) {
$set = FALSE;
}
if (count($node->domains) > 1) {
$set = FALSE;
}
if (!$set) {
break;
}
}
$this
->assertTrue(!empty($set), t('All nodes return proper domain assignment.'));
// Check domain_site.
$set = TRUE;
foreach ($nodes as $node) {
if (!isset($node->domain_site)) {
$set = FALSE;
}
if (empty($node->domain_site)) {
$set = FALSE;
}
if (!$set) {
break;
}
}
$this
->assertTrue(!empty($set), t('All nodes return proper affiliate assignment.'));
// Check subdomains.
$set = TRUE;
foreach ($nodes as $node) {
if (!isset($node->subdomains)) {
$set = FALSE;
}
if (empty($node->subdomains)) {
$set = FALSE;
}
if (empty($node->subdomains[0])) {
$set = FALSE;
}
if ($node->subdomains[0] != t('All affiliates')) {
$set = FALSE;
}
if ($node->subdomains[1] != $domain['sitename']) {
$set = FALSE;
}
if (count($node->subdomains) > 2) {
$set = FALSE;
}
if (!$set) {
break;
}
}
$this
->assertTrue(!empty($set), t('All nodes return proper subdomain string.'));
// Now, delete half the node information.
$delete_ids = array_chunk($node_ids, 5);
db_delete('domain_access')
->condition('nid', $delete_ids[0], 'IN')
->execute();
// By design, there should be 5 nodes left with default domain assignment.
$count = db_query("SELECT COUNT(nid) FROM {domain_access} WHERE realm = 'domain_id'")
->fetchField();
$this
->assertTrue($count == 5, t('Five nodes are assigned to the default domain.'));
$count = db_query("SELECT COUNT(nid) FROM {domain_access} WHERE realm = 'domain_site'")
->fetchField();
$this
->assertTrue($count == 5, t('Five nodes are assigned to all affiliates.'));
// Test editor permissions.
// Add an editor role.
$this->permissions = array(
'access content',
'create page content',
);
$this->editor_role = $this
->drupalCreateRole($this->permissions);
// Using drupalCreateUser runs tests we don't want.
$edit = array(
'name' => $this
->randomName(32),
'mail' => $this
->randomName(32) . '@example.com',
'roles' => drupal_map_assoc(array(
DRUPAL_AUTHENTICATED_RID,
$this->editor_role,
)),
'status' => 1,
'pass' => 'fubar',
'domain_user' => array(
domain_default_id() => domain_default_id(),
),
);
$test_user = user_save(NULL, $edit);
// Check that a normal user can view the nodes from the primary domain.
$this
->assertTrue(TRUE, '<strong>' . t('No special permissions. User should have view access to all nodes.' . '</strong>'));
foreach ($nodes as $node) {
foreach (array(
'view',
'update',
'delete',
) as $op) {
$access = node_access($op, $node, $test_user);
if ($op == 'view') {
$this
->assertTrue($access, t('User has proper access to %op domain content.', array(
'%op' => $op,
)));
}
else {
$this
->assertFalse($access, t('User denied access to %op domain content.', array(
'%op' => $op,
)));
}
}
}
// Test queries for node access.
$result = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('n.status', 1)
->addTag('node_access')
->addMetaData('account', $test_user)
->execute()
->fetchCol();
$this
->assertTrue(count($result) == 10, t('Node access query returns correctly.'));
// Test editor permissions.
$perms = array(
'edit domain content',
'delete domain content',
'view unpublished domain content',
);
user_role_grant_permissions($this->editor_role, $perms);
$test_user = user_load($test_user->uid, TRUE);
drupal_static_reset('node_access');
$count = 0;
$this
->assertTrue(TRUE, '<strong>' . t('All nodes assigned to user domain. User can edit, delete and view unpublished. User should have access to all nodes.' . '</strong>'));
foreach ($nodes as $node) {
foreach (array(
'view',
'update',
'delete',
) as $op) {
$node->status = 0;
$access = node_access($op, $node, $test_user);
$this
->assertTrue($access, t('User has proper access to %op domain content.', array(
'%op' => $op,
)));
}
$count++;
// This save routine removes the 'domain_site' grant and assigns some
// content to a domain the user cannot access.
if ($count > 5) {
$node->domains = array(
4 => 4,
);
}
else {
$node->domains = array(
domain_default_id() => domain_default_id(),
);
}
$node->domain_site = 0;
node_save($node);
}
drupal_static_reset('node_access');
$count = 0;
$this
->assertTrue(TRUE, '<strong>' . t('Some nodes assigned to user domain. User can edit, delete and view unpublished.User should have access to domain nodes.' . '</strong>'));
foreach ($nodes as $node) {
$count++;
foreach (array(
'view',
'update',
'delete',
) as $op) {
$access = node_access($op, $node, $test_user);
if ($count <= 5) {
$this
->assertTrue($access, t('User has proper access to %op domain content.', array(
'%op' => $op,
)));
}
else {
$this
->assertFalse($access, t('User denied access to %op non-domain content.', array(
'%op' => $op,
)));
}
}
}
// Test queries for node access.
$result = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('n.status', 1)
->addTag('node_access')
->addMetaData('account', $test_user)
->execute()
->fetchCol();
$this
->assertTrue(count($result) == 0, t('Node access query returns correctly.'));
// Test queries for node access.
$result = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('n.status', 0)
->addTag('node_access')
->addMetaData('account', $test_user)
->execute()
->fetchCol();
$this
->assertTrue(count($result) == 5, t('Node access query returns correctly.'));
// Now take away the domain permissions to content.
user_role_revoke_permissions($this->editor_role, $perms);
$test_user = user_load($test_user->uid, TRUE);
drupal_static_reset('node_access');
$this
->assertTrue(TRUE, '<strong>' . t('User has no editing permissions. User should have access to no nodes.' . '</strong>'));
foreach ($nodes as $node) {
foreach (array(
'view',
'update',
'delete',
) as $op) {
$access = node_access($op, $node, $test_user);
$this
->assertFalse($access, t('User denied access to %op all domain content.', array(
'%op' => $op,
)));
}
}
// Test queries for node access.
$result = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('n.status', 1)
->addTag('node_access')
->addMetaData('account', $test_user)
->execute()
->fetchCol();
$this
->assertTrue(count($result) == 0, t('Node access query returns correctly.'));
// Test queries for node access.
$result = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('n.status', 0)
->addTag('node_access')
->addMetaData('account', $test_user)
->execute()
->fetchCol();
$this
->assertTrue(count($result) == 0, t('Node access query returns correctly.'));
// Test the dynamic extension of permissions via user settings.
$roles = variable_get('domain_roles', array());
// Reset the domains.
$domains = domain_domains(TRUE);
// Delete the user's domains.
db_delete('domain_editor')
->condition('uid', $test_user->uid)
->execute();
$test_user = user_load($test_user->uid, TRUE);
// Check that user has no domains.
$this
->assertTrue(empty($test_user->domain_user), t('User is not assigned to any domains.'));
// Give all users dynamic access to the default domain.
$roles = array(
DRUPAL_AUTHENTICATED_RID => array(
DOMAIN_ALL => domain_default_machine_name(),
),
);
variable_set('domain_roles', $roles);
variable_set('domain_add_roles', 0);
// Now reset the user and test again.
$test_user = user_load($test_user->uid, TRUE);
$this
->assertTrue(!empty($test_user->domain_user), t('User is assigned to domains.'));
$this
->assertTrue(!empty($test_user->domain_user[domain_default_id()]), t('User is dynamically assigned to the default domain.'));
// TODO: Expand this testing logic.
}