Commit 7b79e106 authored by Jakob Perry's avatar Jakob Perry
Browse files

Various Code cleanup before release.

parent 4ca4b0a9
......@@ -492,8 +492,8 @@ function captcha_validate($element, FormStateInterface &$form_state) {
$is_cacheable = (bool) $form_state->getValue('captcha_cacheable', FALSE);
if ($is_cacheable) {
// Completely ignore the captcha_sessions table, since the captcha_sid can get reused by the cache.
// Completely ignore the captcha_sessions table,
// since the captcha_sid can get reused by the cache.
$solution = FALSE;
$captcha_validate = $element['#captcha_validate'];
if (!function_exists($captcha_validate)) {
......
......@@ -9,7 +9,6 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\DrupalKernel;
use Drupal\Core\File;
define('IMAGE_CAPTCHA_ALLOWED_CHARACTERS', 'aAbBCdEeFfGHhijKLMmNPQRrSTtWXYZ23456789');
......
......@@ -6,23 +6,31 @@
height: 30px;
-webkit-animation: image_captcha_refresh_spin 1s linear infinite;
animation: image_captcha_refresh_spin 1s linear infinite;
position: absolute;
left: 0;
right: 0;
margin: auto;
top: 0;
bottom: 0;
position: absolute;
left: 0;
right: 0;
margin: auto;
top: 0;
bottom: 0;
}
/* Safari */
@-webkit-keyframes image_captcha_refresh_spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes image_captcha_refresh_spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.captcha {
......
......@@ -256,7 +256,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
*
* @param array $form
* Form structured array.
* @param Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state structured array.
*/
public function clearCaptchaPlacementCacheSubmit(array $form, FormStateInterface $form_state) {
......
......@@ -31,4 +31,5 @@ class CaptchaTypeFormatter extends ProcessPluginBase {
$type = $row->getSourceProperty('captcha_type');
return $module . '/' . $type;
}
}
......@@ -64,4 +64,5 @@ class CaptchaPoints extends DrupalSqlBase {
$ids['form_id']['type'] = 'string';
return $ids;
}
}
\ No newline at end of file
}
......@@ -85,7 +85,7 @@ $connection->insert('system')
])
->execute();
// Create the Captcha Points D7 Table
// Create the Captcha Points D7 Table.
$connection->schema()->createTable('captcha_points', [
'fields' => [
'form_id' => [
......@@ -108,7 +108,7 @@ $connection->schema()->createTable('captcha_points', [
],
],
'primary key' => [
'form_id'
'form_id',
],
'mysql_character_set' => 'utf8',
]);
......@@ -117,7 +117,7 @@ $connection->insert('captcha_points')
->fields([
'form_id',
'module',
'captcha_type'
'captcha_type',
])
->values([
'form_id' => 'comment_node_article_form',
......@@ -131,7 +131,7 @@ $connection->insert('captcha_points')
])
->execute();
// Create the Captcha Points D7 Table
// Create the Captcha Points D7 Table.
$connection->schema()->createTable('captcha_sessions', [
'fields' => [
'csid' => [
......@@ -148,7 +148,7 @@ $connection->schema()->createTable('captcha_sessions', [
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => 0
'default' => 0,
],
'sid' => [
'type' => 'varchar',
......@@ -193,13 +193,13 @@ $connection->schema()->createTable('captcha_sessions', [
],
],
'primary key' => [
'csid'
'csid',
],
'indexes' => [
'csid_ip' => [
'csid',
'ip_address'
]
'ip_address',
],
],
'mysql_character_set' => 'utf8',
]);
......@@ -253,4 +253,4 @@ $connection->insert('captcha_sessions')
'status' => 0,
'attempts' => 0,
])
->execute();
\ No newline at end of file
->execute();
......@@ -111,7 +111,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
'administration_mode' => TRUE,
];
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH, $edit, t('Save configuration'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this->submitForm($edit, 'Save configuration');
// Create a node with comments enabled.
$node = $this->drupalCreateNode();
......@@ -132,7 +133,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
// Enable Math CAPTCHA.
$edit = ['captchaType' => 'captcha/Math'];
$this->drupalPostForm($this->getUrl(), $edit, t('Save'));
$this->drupalGet($this->getUrl());
$this->submitForm($edit, 'Save');
// Check if returned to original comment form.
$this->assertUrl($add_comment_url, [],
'After setting CAPTCHA with CAPTCHA admin links: should return to original form.', 'CAPTCHA');
......@@ -150,7 +152,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
// Enable Math CAPTCHA.
$edit = ['captchaType' => 'default'];
$this->drupalPostForm($this->getUrl(), $edit, t('Save'));
$this->drupalGet($this->getUrl());
$this->submitForm($edit, 'Save');
// Check if returned to original comment form.
$this->assertEqual($add_comment_url, $this->getUrl(),
......@@ -168,7 +171,7 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
// Disable challenge through CAPTCHA admin links.
$this->drupalGet(Url::fromRoute('entity.captcha_point.disable', ['captcha_point' => self::COMMENT_FORM_ID]));
$this->drupalPostForm(NULL, [], t('Disable'));
$this->submitForm([], 'Disable');
// Check if returned to captcha point list.
global $base_url;
......@@ -204,7 +207,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
// Try to post a comment with wrong answer.
$edit = $this->getCommentFormValues();
$edit['captcha_response'] = 'xx';
$this->drupalPostForm($add_comment_url, $edit, t('Preview'));
$this->drupalGet($add_comment_url);
$this->submitForm($edit, 'Preview');
$this->assertText(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE,
'wrong CAPTCHA should block form submission.', 'CAPTCHA');
}
......@@ -220,7 +224,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
$this->drupalLogin($this->adminUser);
$xss = '<script type="text/javascript">alert("xss")</script>';
$edit = ['description' => $xss];
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH, $edit, 'Save configuration');
$this->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this->submitForm($edit, 'Save configuration');
// Visit user register form and check if JavaScript snippet is there.
$this->drupalLogout();
......@@ -242,7 +247,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
$this->assertNotNull($placement_map, 'CAPTCHA placement cache should be set.');
// Clear the cache.
$this->drupalLogin($this->adminUser);
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH, [], t('Clear the CAPTCHA placement cache'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this->submitForm([], 'Clear the CAPTCHA placement cache');
// Check that the placement cache is unset.
$placement_map = $this->container->get('cache.default')
->get('captcha_placement_map_cache');
......@@ -286,12 +292,14 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
'formId' => $captcha_point_form_id,
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
];
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add', $form_values, t('Save'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
$this->submitForm($form_values, 'Save');
$this->assertText(t('Form ID field is required.'));
// Set CAPTCHA point through admin/user/captcha/captcha/captcha_point.
$form_values['label'] = $label;
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add', $form_values, t('Save'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
$this->submitForm($form_values, 'Save');
$this->assertRaw(t('Captcha Point for %label form was created.', ['%label' => $captcha_point_form_id]));
// Check in database.
......@@ -301,7 +309,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
'Enabled CAPTCHA point should have module and type set');
// Disable CAPTCHA point again.
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/disable', [], t('Disable'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/disable');
$this->submitForm([], 'Disable');
$this->assertRaw(t('Captcha point %label has been disabled.', ['%label' => $label]), 'Disabling of CAPTCHA point');
// Check in database.
......@@ -313,7 +322,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
$form_values = [
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
];
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id, $form_values, t('Save'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id);
$this->submitForm($form_values, 'Save');
$this->assertRaw(t('Captcha Point for %form_id form was updated.', ['%form_id' => $captcha_point_form_id]), 'Saving of CAPTCHA point settings');
// Check in database.
......@@ -322,7 +332,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
'Enabled CAPTCHA point should have module and type set');
// Delete CAPTCHA point.
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete', [], t('Delete'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete');
$this->submitForm([], 'Delete');
$this->assertRaw(t('Captcha point %label has been deleted.', ['%label' => $label]),
'Deleting of CAPTCHA point');
......@@ -347,7 +358,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
'formId' => $captcha_point_form_id,
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
];
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add', $form_values, 'Save');
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
$this->submitForm($form_values, 'Save');
$this->assertRaw(t('Captcha Point for %form_id form was created.', ['%form_id' => $captcha_point_form_id]));
// Switch from admin to non-admin.
......@@ -377,7 +389,8 @@ class CaptchaAdminTest extends CaptchaWebTestBase {
$this->assertEqual($result->captchaType, $captcha_point_module . '/' . $captcha_point_type, 'Enabled CAPTCHA point should have module and type set');
// Delete captcha point.
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete', [], 'Delete');
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete');
$this->submitForm([], 'Delete');
$this->assertRaw(t('Captcha point %label has been deleted.', ['%label' => $label]), 'Disabling of CAPTCHA point');
}
......
......@@ -23,7 +23,8 @@ class CaptchaPersistenceTest extends CaptchaWebTestBase {
$this->drupalLogin($this->adminUser);
// Set persistence.
$edit = ['persistence' => $persistence];
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH, $edit, 'Save configuration');
$this->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this->submitForm($edit, 'Save configuration');
// Log admin out.
$this->drupalLogout();
......@@ -82,7 +83,7 @@ class CaptchaPersistenceTest extends CaptchaWebTestBase {
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this->drupalPostForm(NULL, $edit, $this->t('Log in'), [], self::LOGIN_HTML_FORM_ID);
$this->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
// Check that there was no error message for the CAPTCHA.
$this->assertCaptchaResponseAccepted();
......@@ -92,7 +93,7 @@ class CaptchaPersistenceTest extends CaptchaWebTestBase {
$this->assertPreservedCsid($captcha_sid_initial);
// Post from again.
$this->drupalPostForm(NULL, $edit, $this->t('Log in'), [], self::LOGIN_HTML_FORM_ID);
$this->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
// Check that there was no error message for the CAPTCHA.
$this->assertCaptchaResponseAccepted();
$this->assertPreservedCsid($captcha_sid_initial);
......@@ -116,7 +117,7 @@ class CaptchaPersistenceTest extends CaptchaWebTestBase {
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this->drupalPostForm(NULL, $edit, $this->t('Log in'), [], self::LOGIN_HTML_FORM_ID);
$this->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
// Check that there was no error message for the CAPTCHA.
$this->assertCaptchaResponseAccepted();
// There shouldn't be a CAPTCHA on the new form.
......@@ -153,7 +154,7 @@ class CaptchaPersistenceTest extends CaptchaWebTestBase {
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this->drupalPostForm(NULL, $edit, $this->t('Log in'), [], self::LOGIN_HTML_FORM_ID);
$this->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
// Check that there was no error message for the CAPTCHA.
$this->assertCaptchaResponseAccepted();
// There shouldn't be a CAPTCHA on the new form.
......@@ -195,7 +196,7 @@ class CaptchaPersistenceTest extends CaptchaWebTestBase {
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this->drupalPostForm(NULL, $edit, $this->t('Log in'), [], self::LOGIN_HTML_FORM_ID);
$this->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
// Check that there was no error message for the CAPTCHA.
$this->assertCaptchaResponseAccepted();
// There shouldn't be a CAPTCHA on the new form.
......
......@@ -47,7 +47,7 @@ class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
// Post the form with the solution.
$edit = $this->getCommentFormValues();
$edit['captcha_response'] = $solution;
$this->drupalPostForm(NULL, $edit, t('Preview'));
$this->submitForm($edit, 'Preview');
// Answer should be accepted and further CAPTCHA omitted.
$this->assertCaptchaResponseAccepted();
$this->assertCaptchaPresence(FALSE);
......@@ -60,7 +60,7 @@ class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
$this->assertSession()->hiddenFieldExists("captcha_sid")->setValue((string) $captcha_sid);
$this->assertSession()->hiddenFieldExists("captcha_token")->setValue((string) $captcha_token);
$edit['captcha_response'] = $solution;
$this->drupalPostForm(NULL, $edit, t('Preview'));
$this->submitForm($edit, 'Preview');
// CAPTCHA session reuse attack should be detected.
$this->assertCaptchaSessionIdReuseAttackDetection();
// There should be a CAPTCHA.
......@@ -93,7 +93,7 @@ class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
$edit = $this->getNodeFormValues();
$edit['captcha_response'] = $solution;
// Preview the node.
$this->drupalPostForm(NULL, $edit, t('Preview'));
$this->submitForm($edit, 'Preview');
// Answer should be accepted.
$this->assertCaptchaResponseAccepted();
// Check that there is no CAPTCHA after preview.
......@@ -108,7 +108,7 @@ class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
$this->assertSession()->hiddenFieldExists("captcha_sid")->setValue((string) $captcha_sid);
$this->assertSession()->hiddenFieldExists("captcha_token")->setValue((string) $captcha_token);
$edit['captcha_response'] = $solution;
$this->drupalPostForm(NULL, $edit, t('Preview'));
$this->submitForm($edit, 'Preview');
// CAPTCHA session reuse attack should be detected.
$this->assertCaptchaSessionIdReuseAttackDetection();
// There should be a CAPTCHA.
......@@ -140,7 +140,7 @@ class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
'pass' => $this->normalUser->pass_raw,
'captcha_response' => $solution,
];
$this->drupalPostForm(NULL, $edit, t('Log in'), [], self::LOGIN_HTML_FORM_ID);
$this->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
$this->assertCaptchaResponseAccepted();
$this->assertCaptchaPresence(FALSE);
// If a "log out" link appears on the page, it is almost certainly because
......@@ -157,7 +157,7 @@ class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
$this->assertSession()->hiddenFieldExists("captcha_sid")->setValue((string) $captcha_sid);
$this->assertSession()->hiddenFieldExists("captcha_token")->setValue((string) $captcha_token);
$this->assert('pass', json_encode($edit));
$this->drupalPostForm(NULL, $edit, t('Log in'));
$this->submitForm($edit, 'Log in');
// CAPTCHA session reuse attack should be detected.
$this->assertCaptchaSessionIdReuseAttackDetection();
// There should be a CAPTCHA.
......@@ -182,7 +182,8 @@ class CaptchaSessionReuseAttackTestCase extends CaptchaWebTestBase {
$edit = $this->getCommentFormValues();
$comment_subject = $edit['subject[0][value]'];
$edit['captcha_response'] = 'Test 123';
$this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment');
$this->submitForm($edit, 'Preview');
// Post should be accepted: no warnings,
// no CAPTCHA reuse detection (which could be used by user log in block).
$this->assertCaptchaResponseAccepted();
......
......@@ -44,7 +44,7 @@ class CaptchaTest extends CaptchaWebTestBase {
'pass' => $user->pass_raw,
'captcha_response' => '?',
];
$this->drupalPostForm(NULL, $edit, t('Log in'), [], self::LOGIN_HTML_FORM_ID);
$this->submitForm($edit, 'Log in', self::LOGIN_HTML_FORM_ID);
// Check for error message.
$this->assertText(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, 'CAPTCHA should block user login form', 'CAPTCHA');
......@@ -83,7 +83,8 @@ class CaptchaTest extends CaptchaWebTestBase {
$comment_subject = $edit['subject[0][value]'];
$comment_body = $edit['comment_body[0][value]'];
$edit['captcha_response'] = $captcha_response;
$this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'), [], 'comment-form');
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment');
$this->submitForm($edit, 'Save', 'comment-form');
if ($should_pass) {
// There should be no error message.
......@@ -153,7 +154,8 @@ class CaptchaTest extends CaptchaWebTestBase {
// Preview comment with correct CAPTCHA answer.
$edit = $this->getCommentFormValues();
$edit['captcha_response'] = 'Test 123';
$this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment');
$this->submitForm($edit, 'Preview');
// Check that there is no CAPTCHA after preview.
$this->assertCaptchaPresence(FALSE);
......@@ -180,7 +182,7 @@ class CaptchaTest extends CaptchaWebTestBase {
$edit = $this->getNodeFormValues();
$edit['captcha_response'] = 'Test 123';
$this->drupalGet('node/add/page');
$this->drupalPostForm(NULL, $edit, t('Preview'));
$this->submitForm($edit, 'Preview');
$this->assertCaptchaPresence(FALSE);
}
......@@ -229,7 +231,8 @@ class CaptchaTest extends CaptchaWebTestBase {
];
// Create intentionally long id Captcha Point.
$this->drupalPostForm(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add', $form_values, t('Save'));
$this->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
$this->submitForm($form_values, 'Save');
$this->assertRaw(t('Captcha Point for %label form was created.', ['%label' => $formId]));
// We need to log out to test the captcha.
......
......@@ -18,6 +18,11 @@ class MigrateCaptchaPointsTest extends MigrateDrupal7TestBase {
*/
protected static $modules = ['captcha'];
/**
* The expected captcha points from the Captcha fixture.
*
* @var array[]
*/
protected $captchaPoints = [
[
'form_id' => 'comment_node_article_form',
......@@ -60,13 +65,12 @@ class MigrateCaptchaPointsTest extends MigrateDrupal7TestBase {
/**
* Tests a single captcha point type.
*
* @dataProvider testCaptchaPointDataProvider
*
* @param string $form_id
* The captcha point form id.
* @param string $captcha_type
* The expected captcha type for the config.
* @param $status
* @param bool $status
* The expected status for a captcha type.
*/
protected function assertEntity(string $form_id, string $captcha_type, $status) {
/** @var \Drupal\captcha\CaptchaPointInterface $entity */
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\captcha\Kernel\Migrate\d7;
use Drupal\captcha\CaptchaPointInterface;
use Drupal\captcha\Entity\CaptchaPoint;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
......@@ -18,6 +16,11 @@ class MigrateCaptchaSimpleConfigurationTest extends MigrateDrupal7TestBase {
*/
protected static $modules = ['captcha'];
/**
* The expected configuration from the Captcha fixture.
*
* @var array[]
*/
protected $expectedConfig = [
'captcha.settings' => [
'enabled_default' => 1,
......
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