You are here

public function LostCharacterCaptchaTestCase::testAjaxFormRebuild in CAPTCHA Pack 8

Tests that the Lost Character CAPTCHA is not changed on AJAX form rebuilds.

File

text_captcha/modules/lost_character_captcha/src/Tests/LostCharacterCaptchaTestCase.php, line 220

Class

LostCharacterCaptchaTestCase
Tests Lost character CAPTCHA main test case sensitivity.

Namespace

Drupal\lost_character_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' => 'lost_character_captcha/Lost characters',
  ])
    ->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.'));
}