You are here

public function AuthcacheCommentTest::testCommentAttributionWithFormCache in Authenticated User Page Caching (Authcache) 7.2

Test that comments get properly attributed when the form cache is in use.

Bug: https://www.drupal.org/node/2307555

File

modules/authcache_comment/authcache_comment.test, line 489
Test cases for the Authcache Comment module.

Class

AuthcacheCommentTest
Tests for markup substitution.

Code

public function testCommentAttributionWithFormCache() {
  $this->stubmod
    ->hook('authcache_p13n_client', array(
    'authcache_p13n_test' => array(
      'title' => t('Test Client'),
      'enabled' => TRUE,
    ),
  ));

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

  // Add a file-field to the comment entity. This will trigger the form cache
  // because the file field widget uses Ajax.
  $field = array(
    'field_name' => 'field_document',
    'type' => 'file',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_document',
    'entity_type' => 'comment',
    'label' => 'Document',
    'bundle' => 'comment_node_article',
    'settings' => array(
      'file_directory' => 'field/document',
      'file_extensions' => 'txt doc docx pdf ods odt xls xlsx',
      'max_filesize' => '2MB',
      'title_field' => '',
    ),
    'widget' => array(
      'type' => 'file_generic',
      'weight' => -4,
    ),
    'display' => array(
      'default' => array(
        'type' => 'file_default',
        'weight' => 10,
      ),
      'teaser' => array(
        'type' => 'file_default',
        'weight' => 10,
      ),
    ),
  );
  field_create_instance($instance);

  // Create a clone of member1 with the same set of roles.
  $cloned_member1 = $this
    ->drupalCreateUser();
  $cloned_member1->roles = $this->member1->roles;
  user_save($cloned_member1);
  $this
    ->drupalLogin($this->member1);

  // M1: Visit the node in order to warm the form cache.
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('node/' . $this->node->nid);
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::never());

  // Record the current page content and url.
  $content = $this
    ->drupalGetContent();
  $url = $this
    ->getUrl();

  // M2: Post a comment.
  $this
    ->drupalLogin($cloned_member1);

  // Simulate a cache-hit by restoring the content and url from member1.
  $this
    ->drupalSetContent($content, $url);

  // Compute CSRF token and supply it via the extra_post parameter.
  $token = $this
    ->drupalGetToken('comment_form');
  $extra_post = '&form_token=' . urlencode($token);
  $this
    ->postComment(NULL, $this
    ->randomName(8), '', NULL, $extra_post);

  // Ensure that we are not running into CSRF protection.
  $this
    ->assertNoText('The form has become outdated.');

  // Ensure that the comment is attributed to the clone of member1.
  $this
    ->assertText($cloned_member1->name);
  $this
    ->assertNoText($this->member1->name);
  $this
    ->drupalLogout();
}