Commit ddd7a67a authored by Berdir's avatar Berdir

Started fixing some admin tests.

parent 3061e629
......@@ -2,9 +2,8 @@ name: CAPTCHA
type: module
description: Provides the CAPTCHA API for adding challenges to arbitrary forms.
package: Spam control
version: 8.x
core: 8.x
configure: admin/config/people/captcha
configure: captcha_settings
dependencies:
- node
......@@ -292,15 +292,15 @@ function template_preprocess_captcha(&$variables) {
*/
function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$account = \Drupal::currentUser();
$config = \Drupal::config('captcha.settings');
// Visitor does not have permission to skip CAPTCHAs.
module_load_include('inc', 'captcha');
if (!$account->hasPermission('skip CAPTCHA')) {
// Visitor does not have permission to skip CAPTCHAs.
module_load_include('inc', 'captcha');
/* @var CaptchaPoint $captcha_point */
$captcha_point = \Drupal::entityManager()->getStorage('captcha_point')->load($form_id);
if ($captcha_point) {
module_load_include('inc', 'captcha');
// Build CAPTCHA form element.
$captcha_element = array(
'#type' => 'captcha',
......@@ -308,7 +308,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
);
// Add a CAPTCHA description if required.
if (\Drupal::config('captcha.settings')->get('add_captcha_description')) {
if ($config->get('add_captcha_description')) {
$captcha_element['#description'] = _captcha_get_description();
}
......@@ -317,11 +317,9 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
_captcha_insert_captcha_element($form, $captcha_placement, $captcha_element);
}
}
elseif (\Drupal::config('captcha.settings')->get('administration_mode', FALSE)
&& $account->hasPermission('administer CAPTCHA settings')
&& (\Drupal::service('router.admin_context')->isAdminRoute() || \Drupal::config('captcha.settings')->get('allow_on_admin_pages', FALSE))) {
elseif ($config->get('administration_mode') && $account->hasPermission('administer CAPTCHA settings')
&& (!\Drupal::service('router.admin_context')->isAdminRoute() || $config->get('allow_on_admin_pages'))) {
// Add CAPTCHA administration tools.
module_load_include('inc', 'captcha');
/* var $captcha_point CaptchaPoint */
$captcha_point = captcha_get_form_id_setting($form_id);
......@@ -345,8 +343,8 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
'#title' => t('Enabled challenge'),
'#markup' => t('%type (<a href="@change">change</a>, <a href="@disable">disable</a>)', array(
'%type' => $captcha_point->getCaptchaType(),
'@change' => url("admin/config/people/captcha/captcha-points/$form_id", array('query' => drupal_get_destination())),
'@disable' => url("admin/config/people/captcha/captcha-points/$form_id/disable", array('query' => drupal_get_destination())),
'@change' => $captcha_point->url('edit-form', array('query' => drupal_get_destination())),
//'@disable' => url("admin/config/people/captcha/captcha-points/$form_id/disable", array('query' => drupal_get_destination())),
)),
);
// // Add an example challenge with solution.
......@@ -374,8 +372,8 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
$captcha_element['add_captcha'] = array(
'#markup' => \Drupal::l(
t('Place a CAPTCHA here for untrusted users.'),
Url::fromRoute('captcha_point.edit', array('captcha_point' => $form_id), array(
'query' => drupal_get_destination(),
Url::fromRoute('captcha_point.add', [], array(
'query' => drupal_get_destination() + array('form_id' => $form_id),
))
),
);
......
......@@ -24,10 +24,17 @@ class CaptchaPointForm extends EntityForm {
/* @var CaptchaPointInterface $captchaPoint */
$captcha_point = $this->entity;
// Support to set a default form_id through a query argument.
$request = \Drupal::request();
if ($captcha_point->isNew() && !$captcha_point->id() && $request->query->has('form_id')) {
$captcha_point->set('formId', $request->query->get('form_id'));
$captcha_point->set('label', $request->query->get('form_id'));
}
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this->t('Form ID'),
'#default_value' => $captcha_point->id(),
'#default_value' => $captcha_point->label(),
);
$form['formId'] = array(
......@@ -58,13 +65,13 @@ class CaptchaPointForm extends EntityForm {
$captcha_point = $this->entity;
$status = $captcha_point->save();
if ($status) {
drupal_set_message($this->t('Captcha Point for %label form was saved.', array(
if ($status == SAVED_NEW) {
drupal_set_message($this->t('Captcha Point for %label form was created.', array(
'%label' => $captcha_point->label(),
)));
}
else {
drupal_set_message($this->t('Captcha Point for %label form was not saved.', array(
drupal_set_message($this->t('Captcha Point for %label form was updated.', array(
'%label' => $captcha_point->label(),
)));
}
......
......@@ -118,7 +118,7 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
// Enable CAPTCHA administration links.
$edit = array(
'captcha_administration_mode' => TRUE,
'administration_mode' => TRUE,
);
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH, $edit, t('Save configuration'));
......@@ -141,7 +141,7 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
$this->clickLink(t('Place a CAPTCHA here for untrusted users.'));
// Enable Math CAPTCHA.
$edit = array('captcha_type' => 'captcha/Math');
$edit = array('captchaType' => 'captcha/Math');
$this->drupalPostForm($this->getUrl(), $edit, t('Save'));
// Check if returned to original comment form.
......@@ -160,7 +160,7 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
$this->clickLink(t('change'));
// Enable Math CAPTCHA.
$edit = array('captcha_type' => 'default');
$edit = array('captchaType' => 'default');
$this->drupalPostForm($this->getUrl(), $edit, t('Save'));
// Check if returned to original comment form.
......@@ -270,11 +270,10 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
* Object with mysql query result.
*/
protected function getCaptchaPointSettingFromDatabase($form_id) {
$result = db_query(
"SELECT * FROM {captcha_points} WHERE form_id = :form_id",
array(':form_id' => $form_id)
)->fetchObject();
return $result;
$ids = \Drupal::entityQuery('captcha_point')
->condition('formId', $form_id)
->execute();
return $ids ? CaptchaPoint::load(reset($ids)) : NULL;
}
/**
......@@ -294,10 +293,10 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
// Set CAPTCHA point through admin/user/captcha/captcha/captcha_point.
$form_values = array(
'captcha_point_form_id' => $captcha_point_form_id,
'captcha_type' => $captcha_point_module . '/' . $captcha_point_type,
'formId' => $captcha_point_form_id,
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
);
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha/captcha_point', $form_values, t('Save'));
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha/captcha_point/add', $form_values, t('Save'));
$this->assertText(t('Saved CAPTCHA point settings.'),
'Saving of CAPTCHA point settings');
......@@ -321,7 +320,7 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
// Set CAPTCHA point via admin/user/captcha/captcha/captcha_point/$form_id.
$form_values = array(
'captcha_type' => $captcha_point_module . '/' . $captcha_point_type,
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
);
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha/captcha_point/' . $captcha_point_form_id, $form_values, t('Save'));
$this->assertText(t('Saved CAPTCHA point settings.'),
......@@ -353,15 +352,14 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
$captcha_point_module = 'captcha';
$captcha_point_type = 'Math';
$form_values = array(
'captcha_point_form_id' => $captcha_point_form_id,
'captcha_type' => $captcha_point_module . '/' . $captcha_point_type,
'formId' => $captcha_point_form_id,
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
);
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha/captcha_point/', $form_values, 'Save');
$this->assertText(t('Saved CAPTCHA point settings.'),
'Saving of CAPTCHA point settings');
// Switch from admin to non-admin.
$this->drupalGet(url('logout', array('absolute' => TRUE)));
$this->drupalLogin($this->normalUser);
// Try to set CAPTCHA point
......@@ -387,7 +385,6 @@ class CaptchaAdminTestCase extends CaptchaBaseWebTestCase {
'Non admin should not be able to delete a CAPTCHA point');
// Switch from nonadmin to admin again.
$this->drupalGet(url('logout', array('absolute' => TRUE)));
$this->drupalLogin($this->adminUser);
// Check if original CAPTCHA point still exists in database.
......
......@@ -19,6 +19,8 @@
// TODO: refactor the 'comment_body[0][value]' stuff.
namespace Drupal\captcha\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
......@@ -93,9 +95,10 @@ abstract class CaptchaBaseWebTestCase extends WebTestBase {
// Open comment for page content type.
$this->container->get('comment.manager')->addDefaultField('node', 'page');
// @TODO do we need this in Drupal8?
// Put comments on page nodes on a separate page.
// variable_set('comment_form_location_page', COMMENT_FORM_SEPARATE_PAGE);
$comment_field = FieldConfig::loadByName('node', 'page', 'comment');
$comment_field->settings['form_location'] = CommentItemInterface::FORM_SEPARATE_PAGE;
$comment_field->save();
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment