You are here

public function CaptchaAdminTest::testCaptchaPointAdministrationByNonAdmin in CAPTCHA 8

Method for testing the CAPTCHA point administration.

File

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

Class

CaptchaAdminTest
Tests CAPTCHA admin settings.

Namespace

Drupal\Tests\captcha\Functional

Code

public function testCaptchaPointAdministrationByNonAdmin() {

  // First add a CAPTCHA point (as admin).
  $captcha_point_form_id = 'form_' . strtolower($this
    ->randomMachineName(32));
  $captcha_point_module = 'captcha';
  $captcha_point_type = 'Math';
  $label = 'TEST_2';
  $this
    ->drupalLogin($this->adminUser);
  $form_values = [
    'label' => $label,
    '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, $this
    ->t('Save'));
  $this
    ->assertSession()
    ->responseContains($this
    ->t('Captcha Point for %form_id form was created.', [
    '%form_id' => $captcha_point_form_id,
  ]));

  // Switch from admin to non-admin.
  $this
    ->drupalLogin($this->normalUser);

  // Try to set CAPTCHA point
  // through admin/user/captcha/captcha/captcha_point.
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points');
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('You are not authorized to access this page.'), 'Non admin should not be able to set a CAPTCHA point');

  // Try to disable the CAPTCHA point.
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/disable');
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('You are not authorized to access this page.'), 'Non admin should not be able to disable a CAPTCHA point');

  // Try to delete the CAPTCHA point.
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete');
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('You are not authorized to access this page.'), 'Non admin should not be able to delete a CAPTCHA point');

  // Switch from nonadmin to admin again.
  $this
    ->drupalLogin($this->adminUser);

  // Check if original CAPTCHA point still exists 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([], 'Delete');
  $this
    ->assertSession()
    ->responseContains($this
    ->t('Captcha point %label has been deleted.', [
    '%label' => $label,
  ]), 'Disabling of CAPTCHA point');
}