Same name in other branches
  1. 8.x-1.x tests/src/Kernel/EntityCloneAccessTest.php \Drupal\Tests\entity_clone\Kernel\EntityCloneAccessTest::testCloneAccess()

Tests that users need to have the correct permissions to clone an entity.

File

tests/src/Kernel/EntityCloneAccessTest.php, line 63

Class

EntityCloneAccessTest
Tests entity clone access.

Namespace

Drupal\Tests\entity_clone\Kernel

Code

public function testCloneAccess() {
    $node = Node::create([
        'type' => 'page',
        'title' => 'My node to clone',
        'status' => TRUE,
    ]);
    $node->save();
    $user_no_permissions = $this->createUser([
        'access content',
    ]);
    $user_that_can_create = $this->createUser([
        'access content',
        'create page content',
    ]);
    $user_that_can_clone = $this->createUser([
        'access content',
        'clone node entity',
    ]);
    $user_that_can_do_both = $this->createUser([
        'access content',
        'clone node entity',
        'create page content',
    ]);
    $url = $node->toUrl('clone-form');
    $access_control_handler = $this->container
        ->get('entity_type.manager')
        ->getAccessControlHandler('node');
    // The user without permissions can view the content but cannot clone.
    $this->assertTrue($access_control_handler->access($node, 'view', $user_no_permissions));
    $this->assertFalse($access_control_handler->access($node, 'clone', $user_no_permissions));
    $this->assertFalse($url->access($user_no_permissions));
    // The user that can create content, cannot clone.
    $this->assertTrue($access_control_handler->createAccess('page', $user_that_can_create));
    $this->assertFalse($access_control_handler->access($node, 'clone', $user_that_can_create));
    $this->assertFalse($url->access($user_that_can_create));
    // The user that has clone permissions, but cannot create content, cannot
    // clone.
    $this->assertFalse($access_control_handler->createAccess('page', $user_that_can_clone));
    $this->assertFalse($access_control_handler->access($node, 'clone', $user_that_can_clone));
    $this->assertFalse($url->access($user_that_can_clone));
    // The user that can do both, can clone.
    $this->assertTrue($access_control_handler->createAccess('page', $user_that_can_do_both));
    $this->assertTrue($access_control_handler->access($node, 'clone', $user_that_can_do_both));
    $this->assertTrue($url->access($user_that_can_do_both));
}