You are here

public function WordListCaptchaTestCase::testAjaxFormRebuild in CAPTCHA Pack 8

Tests that the Word List CAPTCHA is not changed on AJAX form rebuilds.

File

text_captcha/modules/word_list_captcha/src/Test/WordListCaptchaTestCase.php, line 220

Class

WordListCaptchaTestCase
Word list CAPTCHA main test case sensitivity.

Namespace

Drupal\word_list_captcha\Tests

Code

public function testAjaxFormRebuild() {

  // Setup captcha point for user edit form.
  \Drupal::entityManager()
    ->getStorage('captcha_point')
    ->create([
    'id' => 'user_form',
    'formId' => 'user_form',
    'status' => TRUE,
    'captchaType' => 'word_list_captcha/Pick the unrelated word',
  ])
    ->save();

  // Add multiple text field on user edit form.
  $field_storage_config = FieldStorageConfig::create([
    'field_name' => 'field_texts',
    'type' => 'string',
    'entity_type' => 'user',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_storage_config
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage_config,
    'bundle' => 'user',
  ])
    ->save();
  entity_get_form_display('user', 'user', 'default')
    ->setComponent('field_texts', [
    'type' => 'string_textfield',
    'weight' => 10,
  ])
    ->save();

  // Create and login a user.
  $user = $this
    ->drupalCreateUser([]);
  $this
    ->drupalLogin($user);

  // On edit form, add another item and save.
  $this
    ->drupalGet("user/{$user->id()}/edit");
  $this
    ->drupalPostAjaxForm(NULL, [], 'field_texts_add_more');
  $this
    ->drupalPostForm(NULL, [
    'captcha_response' => $this
      ->getMathCaptchaSolutionFromForm('user-form'),
  ], t('Save'));

  // No error.
  $this
    ->assertText(t('The changes have been saved.'));
}