You are here

public function PhraseCaptchaTestCase::testAjaxFormRebuild in CAPTCHA Pack 8

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

File

text_captcha/modules/phrase_captcha/src/Test/PhraseCaptchaTestCase.php, line 220

Class

PhraseCaptchaTestCase
Phrase CAPTCHA main test case sensitivity.

Namespace

Drupal\phrase_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' => 'phrase_captcha/Phrase CAPTCHA',
  ])
    ->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.'));
}