You are here

public function RngEventAccessTest::testComponentAccessCache in RNG - Events and Registrations 8.2

Same name and namespace in other branches
  1. 8 src/Tests/RngEventAccessTest.php \Drupal\rng\Tests\RngEventAccessTest::testComponentAccessCache()
  2. 3.x src/Tests/RngEventAccessTest.php \Drupal\rng\Tests\RngEventAccessTest::testComponentAccessCache()

Test access from event rules.

Ensure if these rules change they invalidate caches.

File

src/Tests/RngEventAccessTest.php, line 55

Class

RngEventAccessTest
Tests event access.

Namespace

Drupal\rng\Tests

Code

public function testComponentAccessCache() {
  $event = EntityTest::create([
    EventManagerInterface::FIELD_REGISTRATION_TYPE => $this->registration_type
      ->id(),
    EventManagerInterface::FIELD_STATUS => TRUE,
  ]);
  $event
    ->save();
  $register_link = Url::fromRoute('rng.event.entity_test.register.type_list', [
    'entity_test' => $event
      ->id(),
  ]);
  $register_link_str = $register_link
    ->toString();
  $event_meta = $this->eventManager
    ->getMeta($event);
  $this
    ->assertEqual(0, count($event_meta
    ->getRules(NULL, FALSE, TRUE)), 'There are zero rules');

  // Set rules via API to set a baseline.
  $rule = Rule::create([
    'event' => [
      'entity' => $event,
    ],
    'trigger_id' => 'rng_event.register',
    'status' => TRUE,
  ]);
  $component = RuleComponent::create()
    ->setType('condition')
    ->setPluginId('rng_user_role')
    ->setConfiguration([
    'roles' => [],
  ]);
  $rule
    ->addComponent($component);
  $component = RuleComponent::create()
    ->setType('action')
    ->setPluginId('registration_operations')
    ->setConfiguration([
    'registration_operations' => [
      'create' => FALSE,
    ],
  ]);
  $rule
    ->addComponent($component);
  $rule
    ->save();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $user_registrant = $this
    ->drupalCreateUser([
    'rng register self',
    'view test entity',
    'administer entity_test content',
  ]);
  $roles = $user_registrant
    ->getRoles(TRUE);
  $this
    ->drupalLogin($user_registrant);
  $this
    ->drupalGet($event
    ->toUrl());
  $this
    ->assertResponse(200);

  // Register tab is cached, ensure it is missing.
  $this
    ->assertSession()
    ->linkByHrefNotExists($register_link_str);
  $this
    ->drupalGet($register_link);
  $this
    ->assertResponse(403);
  $user_manager = $this
    ->drupalCreateUser([
    'administer entity_test content',
  ]);
  $this
    ->drupalLogin($user_manager);

  // Set conditions so registrant user can register
  // Use UI because component form should invalidate tags.
  $conditions = $rule
    ->getConditions();
  $edit = [
    'roles[' . $roles[0] . ']' => TRUE,
  ];
  $this
    ->drupalPostForm($conditions[0]
    ->toUrl(), $edit, t('Save'));
  $actions = $rule
    ->getActions();
  $edit = [
    'operations[create]' => TRUE,
  ];
  $this
    ->drupalPostForm($actions[0]
    ->toUrl(), $edit, t('Save'));
  $this
    ->drupalLogin($user_registrant);
  $this
    ->drupalGet($event
    ->toUrl());
  $this
    ->assertResponse(200);

  // Register tab is cached, ensure it is exposed.
  // If this fails, then the register tab is still cached to previous rules.
  $this
    ->assertSession()
    ->linkByHrefExists($register_link_str);
  $this
    ->drupalGet($register_link);
  $this
    ->assertResponse(200);
}