Skip to content
Snippets Groups Projects
Commit fe058228 authored by dpi's avatar dpi
Browse files

Added registration access kernel tests.

Added kernel test base.
Added kernel test trait.
parent 08314d84
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,6 @@ namespace Drupal\rng\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\rng\Entity\EventType;
use Drupal\rng\Entity\RegistrationType;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\rng\Entity\Registration;
......@@ -19,6 +18,8 @@ use Drupal\rng\Entity\Registration;
*/
abstract class RNGTestBase extends WebTestBase {
use RNGTestTrait;
public static $modules = array('rng');
/**
......@@ -82,22 +83,6 @@ abstract class RNGTestBase extends WebTestBase {
return $entity;
}
/**
* Create and save a registration type entity.
*
* @return \Drupal\rng\Entity\RegistrationType
* A registration type entity
*/
function createRegistrationType() {
$registration_type = RegistrationType::create([
'id' => 'registration_type_a',
'label' => 'Registration Type A',
'description' => 'Description for registration type a',
]);
$registration_type->save();
return $registration_type;
}
/**
* Creates and saves a registration entity.
*
......
<?php
/**
* @file
* Definition of Drupal\rng\Tests\RNGTestTrait.
*/
namespace Drupal\rng\Tests;
use Drupal\rng\Entity\RegistrationType;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\rng\RegistrationTypeInterface;
use Drupal\rng\Entity\EventType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\rng\Entity\Registration;
use Drupal\rng\EventManagerInterface;
use Drupal\rng\Entity\EventTypeRule;
trait RNGTestTrait {
/**
* Create and save a registration type entity.
*
* @return \Drupal\rng\RegistrationTypeInterface
* A registration type entity
*/
protected function createRegistrationType() {
$registration_type = RegistrationType::create([
'id' => 'registration_type_a',
'label' => 'Registration Type A',
'description' => 'Description for registration type a',
]);
$registration_type->save();
return $registration_type;
}
/**
* Creates an event type config.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityInterface
* An entity type.
*
* @return \Drupal\rng\EventTypeInterface
* An event type config.
*/
protected function createEventType($entity_type_id, $bundle) {
$event_type = EventType::create([
'label' => 'Event Type A',
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'mirror_operation_to_event_manage' => 'update',
]);
$event_type->save();
return $event_type;
}
/**
* Create an event.
*
* @return \Drupal\rng\EventMetaInterface
*/
protected function createEvent($values = []) {
$event = EntityTest::create($values + [
EventManagerInterface::FIELD_REGISTRATION_TYPE => $this->registrationType->id(),
EventManagerInterface::FIELD_STATUS => TRUE,
EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS => 0,
]);
$event->save();
return $this->eventManager->getMeta($event);
}
/**
* Create a registration and add an identity as a registrant.
*
* @param \Drupal\Core\Entity\EntityInterface $event
* An event entity
* @param \Drupal\rng\RegistrationTypeInterface $registration_type
* A registration type.
* @param \Drupal\Core\Entity\EntityInterface[] $identities
* An array of identities.
*
* @return \Drupal\rng\RegistrationInterface
* A saved registration
*/
protected function createRegistration(EntityInterface $event, RegistrationTypeInterface $registration_type, array $identities) {
$registration = Registration::create([
'type' => $registration_type->id(),
]);
foreach ($identities as $identity) {
$registration->addIdentity($identity);
}
$registration
->setEvent($event)
->save();
return $registration;
}
/**
* Create rules for an event type.
*
* @param array $roles
* An array of role ID to add access.
* @param array $operations
* An array of operations. Value is boolean whether to grant, key can be
* any of 'create', 'view', 'update', 'delete'.
*/
protected function createRules($roles = [], $operations = []) {
$rule = EventTypeRule::create([
'trigger' => 'rng_event.register',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'machine_name' => 'user_role',
]);
$rule->setCondition('role', [
'id' => 'rng_user_role',
'roles' => $roles,
]);
$rule->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => $operations,
],
]);
$rule->save();
}
}
\ No newline at end of file
<?php
/**
* @file
* Contains \Drupal\Tests\rng\Kernel\RNGKernelTestBase.
*/
namespace Drupal\Tests\rng\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\rng\Tests\RNGTestTrait;
/**
* Base class for RNG unit tests.
*/
abstract class RNGKernelTestBase extends KernelTestBase {
use RNGTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['rng', 'user', 'field', 'dynamic_entity_reference', 'unlimited_number', 'courier', 'text'];
}
<?php
/**
* @file
* Contains \Drupal\Tests\rng\Kernel\RegistrationAccessTest.
*/
namespace Drupal\Tests\rng\Kernel;
use Drupal\simpletest\UserCreationTrait;
use Drupal\rng\EventManagerInterface;
use Drupal\rng\Entity\EventTypeRule;
/**
* Tests ability to register for events..
*
* @group rng
*/
class RegistrationAccessTest extends RNGKernelTestBase {
use UserCreationTrait {
createUser as drupalCreateUser;
}
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['system', 'entity_test'];
/**
* The RNG event manager.
*
* @var \Drupal\rng\EventManagerInterface
*/
protected $eventManager;
/**
* @var \Drupal\rng\RegistrationTypeInterface
*/
protected $registrationType;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->eventManager = \Drupal::service('rng.event_manager');
$this->installEntitySchema('entity_test');
$this->installEntitySchema('registration');
$this->installEntitySchema('registrant');
$this->installEntitySchema('rng_rule');
$this->installEntitySchema('rng_rule_component');
$this->installEntitySchema('user');
$this->installConfig('rng');
$this->installSchema('system', array('sequences'));
$this->registrationType = $this->createRegistrationType();
$this->createEventType('entity_test', 'entity_test');
}
/**
* Test register self
*/
public function testRegisterSelf() {
$event_meta = $this->createEvent();
$user1 = $this->drupalCreateUser(['rng register self']);
$this->setCurrentUser($user1);
$this->createRules([], ['create' => TRUE]);
$this->assertTrue($event_meta->identitiesCanRegister('user', [$user1->id()]));
}
/**
* Test register self with no default rules.
*/
public function testRegisterSelfNoDefaultRules() {
$event_meta = $this->createEvent();
$user1 = $this->drupalCreateUser(['rng register self']);
$this->setCurrentUser($user1);
$this->assertFalse($event_meta->identitiesCanRegister('user', [$user1->id()]));
}
/**
* Test register self rule with no roles.
*
* No roles = All roles.
*/
public function testRegisterSelfRuleNoRoles() {
$event_meta = $this->createEvent();
$user1 = $this->drupalCreateUser(['rng register self']);
$this->setCurrentUser($user1);
$this->createRules([], ['create' => TRUE]);
$this->assertTrue($event_meta->identitiesCanRegister('user', [$user1->id()]));
}
/**
* Test register self rule a role the user does not have.
*/
public function testRegisterSelfRuleRoleAlternative() {
$event_meta = $this->createEvent();
$role1 = $this->createRole([]);
$user1 = $this->drupalCreateUser(['rng register self']);
$this->setCurrentUser($user1);
$this->createRules([$role1 => $role1], ['create' => TRUE]);
$this->assertFalse($event_meta->identitiesCanRegister('user', [$user1->id()]));
}
/**
* Test register self no permission
*/
public function testRegisterSelfNoPermission() {
$event_meta = $this->createEvent();
// Need to create a dummy role otherwise 'authenticated' is used.
$role1 = $this->createRole([]);
$user1 = $this->drupalCreateUser();
$this->setCurrentUser($user1);
$this->createRules([$role1 => $role1], ['create' => TRUE]);
$this->assertFalse($event_meta->identitiesCanRegister('user', [$user1->id()]));
}
/**
* Test register self no duplicates.
*/
public function testRegisterSelfNoDuplicates() {
$event_meta = $this->createEvent([
EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS => 0,
]);
$this->createRules([], ['create' => TRUE]);
$user1 = $this->drupalCreateUser(['rng register self']);
$this->setCurrentUser($user1);
$this->assertTrue($event_meta->identitiesCanRegister('user', [$user1->id()]));
$this->createRegistration($event_meta->getEvent(), $this->registrationType, [$user1]);
$this->assertFalse($event_meta->identitiesCanRegister('user', [$user1->id()]));
}
/**
* Test register self duplicates allowed
*/
public function testRegisterSelfWithDuplicates() {
$event_meta = $this->createEvent([
EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS => 1,
]);
$this->createRules([], ['create' => TRUE]);
$user1 = $this->drupalCreateUser(['rng register self']);
$this->setCurrentUser($user1);
$this->assertTrue($event_meta->identitiesCanRegister('user', [$user1->id()]));
$this->createRegistration($event_meta->getEvent(), $this->registrationType, [$user1]);
$this->assertTrue($event_meta->identitiesCanRegister('user', [$user1->id()]));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment