View source
<?php
class ModuleGrantsEntityTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Module grants entity test cases',
'description' => 'Test module grants entity module\'s integration with entity module.',
'group' => 'Module grants',
);
}
function setUp() {
parent::setUp(array(
'entity',
'module_grants',
'module_grants_entity',
'module_grants_entity_test',
'module_grants_test_color',
'module_grants_test_number',
'module_grants_test_animal',
));
node_access_rebuild();
}
function testNodeViewAllAccess() {
$node = $this
->drupalCreateNode(array(
'title' => "Test node dog blue three",
'type' => 'article',
));
variable_set('module_grants_lenient', true);
$this
->assertEntityViewAllAccess(array(), false);
$this
->assertEntityViewAllAccess(array(
'dog',
), false);
$this
->assertEntityViewAllAccess(array(
'dog',
'blue',
), false);
$this
->assertEntityViewAllAccess(array(
'dog',
'blue',
'three',
), false);
variable_set('module_grants_lenient', false);
$this
->assertEntityViewAllAccess(array(), false);
$this
->assertEntityViewAllAccess(array(
'dog',
), false);
$this
->assertEntityViewAllAccess(array(
'dog',
'blue',
), false);
$this
->assertEntityViewAllAccess(array(
'dog',
'blue',
'three',
), false);
_module_grants_test_add_view_all(module_grants_test_animal_get_grants(), 1000);
_module_grants_test_add_view_all(module_grants_test_color_get_grants(), 1001);
_module_grants_test_add_view_all(module_grants_test_number_get_grants(), 1002);
variable_set('module_grants_lenient', true);
$this
->assertEntityViewAllAccess(array(), false);
$this
->assertEntityViewAllAccess(array(
'view all pets',
), false);
$this
->assertEntityViewAllAccess(array(
'view all pets',
'view all color',
), false);
$this
->assertEntityViewAllAccess(array(
'view all pets',
'view all color',
'view all number',
), true);
variable_set('module_grants_lenient', false);
$this
->assertEntityViewAllAccess(array(), false);
$this
->assertEntityViewAllAccess(array(
'view all pets',
), false);
$this
->assertEntityViewAllAccess(array(
'view all pets',
'view all color',
), false);
$this
->assertEntityViewAllAccess(array(
'view all pets',
'view all color',
'view all number',
), true);
}
function assertEntityViewAllAccess($permissions, $access) {
$permissions += array(
'access content',
);
$account = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($account);
$msg = t("User with permissions @permissions @access to entity_access('view', 'node').", array(
'@permissions' => implode(',', $permissions),
'@access' => $access ? 'has access' : 'has no access',
));
$this
->drupalGet('entity_access_test_page');
$this
->assertResponse(200, "Entity access test page is displayed");
$this
->assertNoText('Exception', "No database exception");
if ($access) {
$this
->assertText("Yes", $msg);
}
else {
$this
->assertText('No', $msg);
}
}
function testViewAllNodesFunction() {
_module_grants_test_add_view_all(module_grants_test_animal_get_grants(), 1000);
_module_grants_test_add_view_all(module_grants_test_color_get_grants(), 1001);
_module_grants_test_add_view_all(module_grants_test_number_get_grants(), 1002);
$node = $this
->drupalCreateNode(array(
'title' => "Test node dog blue three",
'type' => 'article',
));
variable_set('module_grants_lenient', true);
$this
->assertViewAllNodesFunction(array(), false);
$this
->assertViewAllNodesFunction(array(
'view all pets',
), false);
$this
->assertViewAllNodesFunction(array(
'view all pets',
'view all color',
), false);
$this
->assertViewAllNodesFunction(array(
'view all pets',
'view all color',
'view all number',
), true);
variable_set('module_grants_lenient', false);
$this
->assertViewAllNodesFunction(array(), false);
$this
->assertViewAllNodesFunction(array(
'view all pets',
), false);
$this
->assertViewAllNodesFunction(array(
'view all pets',
'view all color',
), false);
$this
->assertViewAllNodesFunction(array(
'view all pets',
'view all color',
'view all number',
), true);
}
function assertViewAllNodesFunction($permissions, $access) {
$permissions += array(
'access content',
);
$account = $this
->drupalCreateUser($permissions);
$msg = t("For user with permissions @permissions, _module_grants_node_access_view_all_nodes returns @access.", array(
'@permissions' => implode(',', $permissions),
'@access' => $access ? 'true' : 'false',
));
$this
->assertEqual($access, _module_grants_node_access_view_all_nodes($account), $msg);
}
}