View source
<?php
namespace Drupal\Tests\rate\Kernel;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\RoleInterface;
class RateTypeAccessTest extends KernelTestBase {
use UserCreationTrait {
createUser as drupalCreateUser;
createRole as drupalCreateRole;
createAdminRole as drupalCreateAdminRole;
}
use StringTranslationTrait;
public static $modules = [
'user',
'system',
'votingapi',
'rate',
];
protected static $configSchemaCheckerExclusions = [
'views.view.rate_results',
];
protected $accessHandler;
protected $loggedInUser;
protected $anonymousUser;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('user');
$this
->installConfig('rate');
$this->accessHandler = $this->container
->get('entity_type.manager')
->getAccessControlHandler('vote_type');
$this
->config('user.role.' . RoleInterface::AUTHENTICATED_ID)
->set('permissions', [])
->save();
$this
->drupalCreateUser();
$this->loggedInUser = $this
->drupalCreateUser([
'view rate results page',
]);
$this->anonymousUser = $this
->drupalCreateUser([]);
}
public function testRateTypeAccess() {
$rate_types = [
'updown',
'fivestar',
];
$vote_type_storage = $this->container
->get('entity_type.manager')
->getStorage('vote_type');
$vote_type_storage
->create([
'id' => 'fake',
'label' => 'Fake vote type',
'value_type' => 'points',
'description' => 'A fake vote type for testing purposes.',
])
->save();
foreach ($rate_types as $rate_type) {
$vote_type = $vote_type_storage
->load($rate_type);
$this
->assertTrue($this->accessHandler
->access($vote_type, 'view', $this->loggedInUser), 'Logged in user can see vote of type ' . $rate_type);
$this
->assertFalse($this->accessHandler
->access($vote_type, 'view', $this->anonymousUser), 'Anonymous user cannot see vote of type ' . $rate_type);
}
$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());
}
}