You are here

public function RateTypeAccessTest::testRateTypeAccess in Rate 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/RateTypeAccessTest.php \Drupal\Tests\rate\Kernel\RateTypeAccessTest::testRateTypeAccess()

Tests access handling for different rate types.

File

tests/src/Kernel/RateTypeAccessTest.php, line 94

Class

RateTypeAccessTest
Tests access control for vote_types defined in the Rate module.

Namespace

Drupal\Tests\rate\Kernel

Code

public function testRateTypeAccess() {

  // The following rate types are defined by the Rate module.
  // In rate_vote_type_access(), permission to view vote types with these
  // IDs is granted for users with the 'view rate results page' permission.
  $rate_types = [
    'updown',
    'fivestar',
  ];
  $vote_type_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('vote_type');

  // Create fake vote type.
  $vote_type_storage
    ->create([
    'id' => 'fake',
    'label' => 'Fake vote type',
    'value_type' => 'points',
    'description' => 'A fake vote type for testing purposes.',
  ])
    ->save();

  // Test each of the vote types that are defined by the Rate module.
  foreach ($rate_types as $rate_type) {
    $vote_type = $vote_type_storage
      ->load($rate_type);

    // Confirm that the logged_user can access the vote type info.
    $this
      ->assertTrue($this->accessHandler
      ->access($vote_type, 'view', $this->loggedInUser), 'Logged in user can see vote of type ' . $rate_type);

    // Confirm that the anonymous_user cannot access the vote type info.
    $this
      ->assertFalse($this->accessHandler
      ->access($vote_type, 'view', $this->anonymousUser), 'Anonymous user cannot see vote of type ' . $rate_type);
  }

  // Confirm that neither user may access the fake vote type,
  // which is not in the list of vote types provided by the Rate module.
  $fake_vote_type = $vote_type_storage
    ->load('fake');
  $this
    ->assertFalse($this->accessHandler
    ->access($fake_vote_type, 'view', $this->loggedInUser), 'Logged in user cannot see vote of type ' . $fake_vote_type
    ->id());
  $this
    ->assertFalse($this->accessHandler
    ->access($fake_vote_type, 'view', $this->anonymousUser), 'Anonymous user cannot see vote of type ' . $fake_vote_type
    ->id());
}