public function AuthcacheFormTestBaseFormIdToken::testBaseFormId in Authenticated User Page Caching (Authcache) 7.2
Test token removal functionality.
File
- modules/
authcache_form/ tests/ authcache_form.test, line 317 - Test classes for Authcache Form module.
Class
- AuthcacheFormTestBaseFormIdToken
- Test base form id token.
Code
public function testBaseFormId() {
$admin = $this
->drupalCreateUser(array(
'administer nodes',
));
$user = $this
->drupalCreateUser();
// Work around #1873606
user_save($user, array(
'roles' => array(
DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
),
));
$forms = array(
'authcache_form_test_form_1' => array(
'callback' => 'authcache_form_test_form',
),
'authcache_form_test_form_2' => array(
'callback' => 'authcache_form_test_form',
),
);
$this->stubmod
->hook('forms', $forms);
$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 measures, base form id should be used for
// generating the form tokens.
$this
->drupalLogin($user);
$this
->drupalGet('authcache-form-test-multiple-forms/authcache_form_test_form_1/authcache_form_test_form_2');
// Assert that both forms use the same form token.
$form_token_values = $this
->xpath('//form//input[@name="form_token"]/@value');
$this
->assertEqual(2, count($form_token_values), 'Two form token fields on the page');
$token_0 = (string) $form_token_values[0];
$token_1 = (string) $form_token_values[1];
$this
->assertTrue($token_0);
$this
->assertTrue($token_1);
$this
->assertIdentical($token_0, $token_1);
$this
->drupalLogout();
// However, form tokens should not be altered for users with uncacheable
// roles.
$this
->drupalLogin($admin);
$this
->drupalGet('authcache-form-test-multiple-forms/authcache_form_test_form_1/authcache_form_test_form_2');
// FIXME: Assert that both forms use different form tokens.
$form_token_values = $this
->xpath('//form//input[@name="form_token"]/@value');
$this
->assertEqual(2, count($form_token_values), 'Two form token fields on the page');
$token_0 = (string) $form_token_values[0];
$token_1 = (string) $form_token_values[1];
$this
->assertTrue($token_0);
$this
->assertTrue($token_1);
$this
->assertNotEqual($token_0, $token_1);
$this
->drupalLogout();
// Now disable the functionality by setting allowed base forms to an empty
// string.
variable_set('authcache_form_base_id_token', '');
$this
->drupalLogin($user);
$this
->drupalGet('authcache-form-test-multiple-forms/authcache_form_test_form_1/authcache_form_test_form_2');
// FIXME: Assert that both forms use different form tokens.
$form_token_values = $this
->xpath('//form//input[@name="form_token"]/@value');
$this
->assertEqual(2, count($form_token_values), 'Two form token fields on the page');
$token_0 = (string) $form_token_values[0];
$token_1 = (string) $form_token_values[1];
$this
->assertTrue($token_0);
$this
->assertTrue($token_1);
$this
->assertNotEqual($token_0, $token_1);
$this
->drupalLogout();
}