You are here

public function CaptchaAdminTest::testCaptchaPointAdministration in CAPTCHA 8

Method for testing the CAPTCHA point administration.

File

tests/src/Functional/CaptchaAdminTest.php, line 281

Class

CaptchaAdminTest
Tests CAPTCHA admin settings.

Namespace

Drupal\Tests\captcha\Functional

Code

public function testCaptchaPointAdministration() {

  // Generate CAPTCHA point data:
  // Drupal form ID should consist of lowercase alphanumerics and underscore).
  $captcha_point_form_id = 'form_' . strtolower($this
    ->randomMachineName(32));

  // The Math CAPTCHA by the CAPTCHA module is always available,
  // so let's use it.
  $captcha_point_module = 'captcha';
  $captcha_point_type = 'Math';

  // Log in as admin.
  $this
    ->drupalLogin($this->adminUser);
  $label = 'TEST';

  // Try and set CAPTCHA point without the #required label. Should fail.
  $form_values = [
    'formId' => $captcha_point_form_id,
    'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
  ];
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
  $this
    ->submitForm($form_values, 'Save');
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('Form ID field is required.'));

  // Set CAPTCHA point through admin/user/captcha/captcha/captcha_point.
  $form_values['label'] = $label;
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
  $this
    ->submitForm($form_values, $this
    ->t('Save'));
  $this
    ->assertSession()
    ->responseContains($this
    ->t('Captcha Point for %label form was created.', [
    '%label' => $captcha_point_form_id,
  ]));

  // Check in database.

  /* @var CaptchaPoint result */
  $result = $this
    ->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
  $this
    ->assertEquals($result->captchaType, $captcha_point_module . '/' . $captcha_point_type, 'Enabled CAPTCHA point should have module and type set');

  // Disable CAPTCHA point again.
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/disable');
  $this
    ->submitForm([], $this
    ->t('Disable'));
  $this
    ->assertSession()
    ->responseContains($this
    ->t('Captcha point %label has been disabled.', [
    '%label' => $label,
  ]), 'Disabling of CAPTCHA point');

  // Check in database.
  $result = $this
    ->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
  $this
    ->assertInstanceOf(CaptchaPoint::class, $result, 'Disabled CAPTCHA point should be in database');
  $this
    ->assertFalse($result
    ->status());

  // Set CAPTCHA point via admin/user/captcha/captcha/captcha_point/$form_id.
  $form_values = [
    'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
  ];
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id);
  $this
    ->submitForm($form_values, $this
    ->t('Save'));
  $this
    ->assertSession()
    ->responseContains($this
    ->t('Captcha Point for %form_id form was updated.', [
    '%form_id' => $captcha_point_form_id,
  ]), 'Saving of CAPTCHA point settings');

  // Check in database.
  $result = $this
    ->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
  $this
    ->assertEquals($result->captchaType, $captcha_point_module . '/' . $captcha_point_type, 'Enabled CAPTCHA point should have module and type set');

  // Delete CAPTCHA point.
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete');
  $this
    ->submitForm([], $this
    ->t('Delete'));
  $this
    ->assertSession()
    ->responseContains($this
    ->t('Captcha point %label has been deleted.', [
    '%label' => $label,
  ]), 'Deleting of CAPTCHA point');
  $result = $this
    ->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
  $this
    ->assertNull($result, 'Deleted CAPTCHA point should not be in database');
}