You are here

public function AsciiArtCaptchaTestCase::testAjaxFormRebuild in CAPTCHA Pack 8

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

File

ascii_art_captcha/src/Tests/AsciiArtCaptchaTestCase.php, line 220

Class

AsciiArtCaptchaTestCase
Tests Ascii art CAPTCHA main test case sensitivity.

Namespace

Drupal\ascii_art_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' => 'ascii_art_captcha/ASCII art 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.'));
}