You are here

public function ReferenceAccessTest::testReferenceAccess in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/tests/src/Kernel/ReferenceAccessTest.php \Drupal\Tests\bibcite_entity\Kernel\ReferenceAccessTest::testReferenceAccess()

Runs basic tests for reference_access function.

File

modules/bibcite_entity/tests/src/Kernel/ReferenceAccessTest.php, line 72

Class

ReferenceAccessTest
Tests basic reference_access functionality.

Namespace

Drupal\Tests\bibcite_entity\Kernel

Code

public function testReferenceAccess() {

  // Ensures user without any reference permission can do nothing.
  $web_user1 = $this
    ->drupalCreateUser([]);
  $reference1 = $this
    ->createReference();
  $this
    ->assertReferenceCreateAccess($reference1
    ->bundle(), FALSE, $web_user1);
  $this
    ->assertReferenceAccess([
    'view' => FALSE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $reference1, $web_user1);

  // Ensures user with bibcite_reference action permissions can do everything.
  $web_user2 = $this
    ->drupalCreateUser([
    'create bibcite_reference',
    'view bibcite_reference',
    'edit any bibcite_reference',
    'delete any bibcite_reference',
  ]);
  $this
    ->assertReferenceCreateAccess($reference1
    ->bundle(), TRUE, $web_user2);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $reference1, $web_user2);
  $web_user3 = $this
    ->drupalCreateUser([
    'view bibcite_reference',
  ]);

  // User cannot create reference without permission.
  $this
    ->assertReferenceCreateAccess($reference1
    ->bundle(), FALSE, $web_user3);

  // User can 'edit/delete own reference', but another user cannot.
  $web_user4 = $this
    ->drupalCreateUser([
    'create bibcite_reference',
    'view bibcite_reference',
    'edit own bibcite_reference',
    'delete own bibcite_reference',
  ]);
  $web_user5 = $this
    ->drupalCreateUser([
    'view bibcite_reference',
    'edit own bibcite_reference',
    'delete own bibcite_reference',
  ]);
  $reference2 = $this
    ->createReference([
    'uid' => $web_user4
      ->id(),
  ]);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $reference2, $web_user4);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $reference2, $web_user5);

  // User should not be able to edit or delete references they do not own.
  $reference3 = $this
    ->createReference([]);
  $web_user6 = $this
    ->drupalCreateUser([
    'create bibcite_reference',
    'view bibcite_reference',
    'edit own bibcite_reference',
    'delete own bibcite_reference',
  ]);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $reference3, $web_user6);

  // User can 'edit/delete own reference',
  // another user can 'edit/delete any reference'.
  $web_user7 = $this
    ->drupalCreateUser([
    'create bibcite_reference',
    'view bibcite_reference',
    'edit own bibcite_reference',
    'delete own bibcite_reference',
  ]);
  $web_user8 = $this
    ->drupalCreateUser([
    'view bibcite_reference',
    'edit any bibcite_reference',
    'delete any bibcite_reference',
  ]);
  $reference4 = $this
    ->createReference([
    'uid' => $web_user7
      ->id(),
  ]);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $reference4, $web_user7);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $reference4, $web_user8);

  // Test create/edit/delete BUNDLE.
  $web_user9 = $this
    ->drupalCreateUser([
    'create miscellaneous bibcite_reference',
    'view bibcite_reference',
    'edit any book bibcite_reference',
    'delete any book bibcite_reference',
  ]);
  $this
    ->assertReferenceCreateAccess('miscellaneous', TRUE, $web_user9);
  $this
    ->assertReferenceCreateAccess('book', FALSE, $web_user9);
  $web_user10 = $this
    ->drupalCreateUser([
    'create book bibcite_reference',
    'view bibcite_reference',
    'edit any miscellaneous bibcite_reference',
    'delete any miscellaneous bibcite_reference',
  ]);
  $this
    ->assertReferenceCreateAccess('miscellaneous', FALSE, $web_user10);
  $this
    ->assertReferenceCreateAccess('book', TRUE, $web_user10);
  $reference5 = $this
    ->createReference([
    'type' => 'miscellaneous',
    'uid' => $web_user9
      ->id(),
  ]);
  $reference6 = $this
    ->createReference([
    'type' => 'book',
    'uid' => $web_user10
      ->id(),
  ]);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $reference5, $web_user9);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $reference5, $web_user10);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $reference6, $web_user9);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $reference6, $web_user10);
  $web_user11 = $this
    ->drupalCreateUser([
    'create book bibcite_reference',
    'view bibcite_reference',
    'edit own book bibcite_reference',
    'delete own book bibcite_reference',
  ]);
  $this
    ->assertReferenceCreateAccess('miscellaneous', FALSE, $web_user11);
  $this
    ->assertReferenceCreateAccess('book', TRUE, $web_user11);
  $web_user12 = $this
    ->drupalCreateUser([
    'create book bibcite_reference',
    'view bibcite_reference',
    'edit own book bibcite_reference',
    'delete own book bibcite_reference',
  ]);
  $this
    ->assertReferenceCreateAccess('miscellaneous', FALSE, $web_user12);
  $this
    ->assertReferenceCreateAccess('book', TRUE, $web_user12);
  $reference7 = $this
    ->createReference([
    'type' => 'book',
    'uid' => $web_user11
      ->id(),
  ]);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $reference7, $web_user11);
  $this
    ->assertReferenceAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $reference7, $web_user12);
}