You are here

public function EntityMetadataNodeCreateAccessTestCase::testNodeMetadataCreateAccess in Entity API 7

Addresses the special case of 'create' access for nodes.

File

./entity.test, line 1225
Entity CRUD API tests.

Class

EntityMetadataNodeCreateAccessTestCase
Test user permissions for node creation.

Code

public function testNodeMetadataCreateAccess() {

  // Create some users. One with super-powers, one with create perms,
  // and one with no perms, and a different one to author the node.
  $admin_account = $this
    ->drupalCreateUser(array(
    'bypass node access',
  ));
  $creator_account = $this
    ->drupalCreateUser(array(
    'create page content',
  ));
  $auth_only_account = $this
    ->drupalCreateUser(array());
  $node_author_account = $this
    ->drupalCreateUser(array());

  // Make a node object with Entity API (contrib)
  $settings = array(
    'uid' => $node_author_account->uid,
    'type' => 'page',
    'title' => $this
      ->randomName(32),
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          $this
            ->randomName(64),
        ),
      ),
    ),
  );
  $node = entity_create('node', $settings);

  // Test the populated wrapper.
  $wrapper = entity_metadata_wrapper('node', $node);
  $this
    ->assertTrue($wrapper
    ->entityAccess('create', $admin_account), 'Create access allowed for ADMIN, for populated wrapper.');
  $this
    ->assertTrue($wrapper
    ->entityAccess('create', $creator_account), 'Create access allowed for CREATOR, for populated wrapper.');
  $this
    ->assertFalse($wrapper
    ->entityAccess('create', $auth_only_account), 'Create access denied for USER, for populated wrapper.');

  // Test entity_acces().
  $this
    ->assertTrue(entity_access('create', 'node', $node, $admin_account), 'Create access allowed for ADMIN, for entity_access().');
  $this
    ->assertTrue(entity_access('create', 'node', $node, $creator_account), 'Create access allowed for CREATOR, for entity_access().');
  $this
    ->assertFalse(entity_access('create', 'node', $node, $auth_only_account), 'Create access denied for USER, for entity_access().');
}