You are here

public function AuthcacheFormTestTokenDeferral::testTokenDefer in Authenticated User Page Caching (Authcache) 7.2

Test token deferral functionality.

File

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

Class

AuthcacheFormTestTokenDeferral
Test defer form tokens

Code

public function testTokenDefer() {
  $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 markup substitution.
  $clients = array(
    'authcache_form_test' => array(
      'title' => 'Authcache form test stub',
      'enabled' => TRUE,
    ),
  );
  $this->stubmod
    ->hook('authcache_p13n_client', $clients);
  $id = $this
    ->randomName(8);
  $markup = '<span ' . drupal_attributes(array(
    'id' => $id,
  )) . '></span>';
  HookStub::on('theme_authcache_p13n_fragment__authcache_form_test', $markup);

  // 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
    ->assertEqual(0, count($this
    ->xpath('//span[@id=:id]', array(
    ':id' => $id,
  ))));
  $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
    ->assertEqual(1, count($this
    ->xpath('//span[@id=:id]', array(
    ':id' => $id,
  ))));
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::never());
  $this
    ->drupalLogout();

  // Test form token retrieval.
  $url = authcache_p13n_request_get_callback('frag/form-token', 'authcache-form-test-form');
  $headers = array(
    'X-Authcache: 1',
  );
  $this
    ->drupalGet($GLOBALS['base_root'] . $url['path'], $url['options'], $headers);
  $this
    ->assertField('form_token');
}