function FieldTestPermissionsExample::testFieldnoteViewPerms

Test view permissions.

File

field_permission_example/tests/field_permission_example.test, line 457

Class

FieldTestPermissionsExample

Code

public function testFieldnoteViewPerms() {
    // We create two sets of content so we can get a few
    // test cases out of the way.
    $view_own_content = $this->randomName(23);
    $view_any_content = $this->randomName(23);
    $view_own_node = $this->createFieldContentForUser(NULL, $view_own_content);
    // Get the type of the node so we can create another one.
    $node_type = node_type_load($view_own_node->type);
    $view_any_node = $this->createFieldContentForUser(NULL, $view_any_content, $node_type);
    // There should be a node now, with some lovely content, but it's the wrong
    // user for the view-own test.
    $view_own_account = $this->drupalCreateUser(array(
        'view own fieldnote',
    ));
    debug("Created user with 'view own fieldnote' permission.");
    // Now change the user id for the test node.
    $view_own_node = node_load($view_own_node->nid);
    $view_own_node->uid = $view_own_account->uid;
    node_save($view_own_node);
    $view_own_node = node_load($view_own_node->nid);
    $this->assertTrue($view_own_node->uid == $view_own_account->uid, 'New user assigned to node.');
    // Now we want to look at the page with the field and
    // check that we can see it.
    $this->drupalLogin($view_own_account);
    $this->drupalGet('node/' . $view_own_node->nid);
    // Check that the field content is present.
    $output_strings = $this->xpath("//div[contains(@class,'stickynote')]/text()");
    $this->assertEqual((string) reset($output_strings), $view_own_content);
    debug("'view own fieldnote' can view own field.");
    // This account shouldn't be able to see the field on the
    // 'view any' node.
    $this->drupalGet('node/' . $view_any_node->nid);
    // Check that the field content is not present.
    $output_strings = $this->xpath("//div[contains(@class,'stickynote')]/text()");
    $this->assertNotEqual((string) reset($output_strings), $view_any_content);
    debug("'view own fieldnote' cannot view other field.");
    // Now, to test for 'view any fieldnote' we create another user
    // with that permission, and try to look at the same node.
    $view_any_account = $this->drupalCreateUser(array(
        'view any fieldnote',
    ));
    debug("Created user with 'view any fieldnote' permission.");
    $this->drupalLogin($view_any_account);
    // This account should be able to see the field on the
    // 'view any' node.
    $this->drupalGet('node/' . $view_any_node->nid);
    // Check that the field content is present.
    $output_strings = $this->xpath("//div[contains(@class,'stickynote')]/text()");
    $this->assertEqual((string) reset($output_strings), $view_any_content);
    debug("'view any fieldnote' can view other field.");
}