You are here

public function FieldTestPermissionsExample::testFieldnoteEditPerms in Examples for Developers 7

Test edit permissions.

Note that this is mostly identical to testFieldnoteViewPerms() and could probably be refactored.

File

field_permission_example/tests/field_permission_example.test, line 521
Tests for Field Permission Example.

Class

FieldTestPermissionsExample

Code

public function testFieldnoteEditPerms() {

  // We create two sets of content so we can get a few
  // test cases out of the way.
  $edit_own_content = $this
    ->randomName(23);
  $edit_any_content = $this
    ->randomName(23);
  $edit_own_node = $this
    ->createFieldContentForUser(NULL, $edit_own_content);

  // Get the type of the node so we can create another one.
  $node_type = node_type_load($edit_own_node->type);
  $edit_any_node = $this
    ->createFieldContentForUser(NULL, $edit_any_content, $node_type);
  $edit_own_account = $this
    ->drupalCreateUser(array(
    'edit own ' . $node_type->name . ' content',
    'edit own fieldnote',
  ));
  debug("Created user with 'edit own fieldnote' permission.");

  // Now change the user id for the test node.
  $edit_own_node = node_load($edit_own_node->nid);
  $edit_own_node->uid = $edit_own_account->uid;
  node_save($edit_own_node);
  $edit_own_node = node_load($edit_own_node->nid);
  $this
    ->assertTrue($edit_own_node->uid == $edit_own_account->uid, 'New edit test user assigned to node.');

  // Now we want to look at the page with the field and
  // check that we can see it.
  $this
    ->drupalLogin($edit_own_account);
  $this
    ->drupalGet('node/' . $edit_own_node->nid . '/edit');
  $this
    ->assertText($edit_own_content, "'edit own fieldnote' can edit own fieldnote.");

  // This account shouldn't be able to edit the field on the
  // 'edit any' node.
  $this
    ->drupalGet('node/' . $edit_any_node->nid . '/edit');
  $this
    ->assertNoText($edit_any_content, "'edit own fieldnote' can not edit any fieldnote.");

  // Now, to test for 'edit any fieldnote' we create another user
  // with that permission, and try to edit at the same node.
  // We have to add the ability to edit any node content, as well
  // or Drupal will deny us access to the page.
  $edit_any_account = $this
    ->drupalCreateUser(array(
    'edit any ' . $node_type->name . ' content',
    'edit any fieldnote',
  ));
  debug("Created user with 'edit any fieldnote' permission.");
  $this
    ->drupalLogin($edit_any_account);

  // This account should be able to see the field on the
  // 'edit any' node.
  $this
    ->drupalGet('node/' . $edit_any_node->nid . '/edit');
  $this
    ->assertText($edit_any_content, "'edit any fieldnote' can edit any fieldnote.");
}