You are here

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

Test functionality for form-build-id.

File

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

Class

AuthcacheFormTestTokenDeferral
Test defer form tokens

Code

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

  // Work around #1873606
  user_save($user, array(
    'roles' => array(
      DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
    ),
  ));
  $build_id = 'simpletest-' . $this
    ->randomName(16);
  $form = array(
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Test'),
    ),
    '#build_id' => $build_id,
  );
  $this->stubmod
    ->hook('cached_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-cached-form');
  $this
    ->assertNoField('form_token');
  $this
    ->assertField('form_build_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-cached-form');
  $this
    ->assertField('form_token');
  $this
    ->assertField('form_build_id');
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::once());
  $this
    ->drupalLogout();

  // Now setup markup substitution.
  variable_set('authcache_form_roles', $user->roles + array(
    DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
  ));
  $clients = array(
    'authcache_form_test' => array(
      'title' => 'Authcache form test stub',
      'enabled' => TRUE,
    ),
  );
  $this->stubmod
    ->hook('authcache_p13n_client', $clients);
  $class = $this
    ->randomName(8);
  $markup = '<span ' . drupal_attributes(array(
    'class' => array(
      $class,
    ),
  )) . '></span>';
  HookStub::on('theme_authcache_p13n_fragment__authcache_form_test', $markup);

  // With only markup-substitution, caching is still cancelled for
  // authenticated users. Cached forms require the Cache Object API.
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('authcache-form-test-cached-form');
  $this
    ->assertNoField('form_token');
  $this
    ->assertField('form_build_id');
  $this
    ->assertEqual(0, count($this
    ->xpath('//span[@class=:class]', array(
    ':class' => $class,
  ))));
  $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-cached-form');
  $this
    ->assertField('form_token');
  $this
    ->assertField('form_build_id');
  $this
    ->assertEqual(0, count($this
    ->xpath('//span[@class=:class]', array(
    ':class' => $class,
  ))));
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::once());
  $this
    ->drupalLogout();

  // Setup cache object API.
  module_enable(array(
    'cacheobject',
  ));
  variable_set('cache_class_cache_form', 'CacheObjectAPIWrapper');
  variable_set('cacheobject_class_cache_form', 'DrupalDatabaseCache');
  $this
    ->resetAll();

  // Test with Cache Object API.
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('authcache-form-test-cached-form');
  $this
    ->assertNoField('form_token');
  $this
    ->assertField('form_build_id');
  $this
    ->assertEqual(0, count($this
    ->xpath('//span[@class=:class]', array(
    ':class' => $class,
  ))));
  $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-cached-form');
  $this
    ->assertNoField('form_token');
  $this
    ->assertField('form_build_id');
  $this
    ->assertEqual(1, count($this
    ->xpath('//span[@class=:class]', array(
    ':class' => $class,
  ))));
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::never());
  $this
    ->drupalLogout();
}