You are here

public function AuthcacheFormTestTokenRemoval::testTokenRemoval in Authenticated User Page Caching (Authcache) 7.2

Test token removal functionality.

File

modules/authcache_form/tests/authcache_form.test, line 438
Test classes for Authcache Form module.

Class

AuthcacheFormTestTokenRemoval
Test form token removal method

Code

public function testTokenRemoval() {
  $user = $this
    ->drupalCreateUser();

  // Work around #1873606
  user_save($user, array(
    'roles' => array(
      DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
    ),
  ));
  $form = array(
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Test'),
    ),
  );
  $this->stubmod
    ->hook('form', $form);

  // Setup authcache roles.
  variable_set('authcache_roles', $user->roles + array(
    DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
  ));

  // Without any additional mesures, form should be submittable by anonymous
  // users but not by authenticated users.
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('authcache-form-test-form');
  $this
    ->assertNoField('form_token');
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::never());
  $this
    ->drupalLogin($user);
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('authcache-form-test-form');
  $this
    ->assertField('form_token');
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::once());
  $this
    ->drupalLogout();

  // Now setup authcache and authcache_form for authenticated users.
  variable_set('authcache_form_notoken', 'authcache_form_test_form');
  variable_set('authcache_form_notoken_roles', array(
    'custom' => TRUE,
    'roles' => $user->roles,
  ));

  // Form should be submittable by both, anonymous and authenticated users
  // and form_token should not be present anymore on form for authenticated
  // users.
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('authcache-form-test-form');
  $this
    ->assertNoField('form_token');
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::never());
  $this
    ->drupalLogin($user);
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('authcache-form-test-form');
  $this
    ->assertNoField('form_token');
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::never());
  $this
    ->drupalLogout();
}