diff --git a/config/optional/captcha/monitoring.sensor_config.captcha_count.yml b/config/optional/captcha/monitoring.sensor_config.captcha_count.yml new file mode 100644 index 0000000..1f7c90b --- /dev/null +++ b/config/optional/captcha/monitoring.sensor_config.captcha_count.yml @@ -0,0 +1,22 @@ +id: captcha_failed_count +label: 'Captcha failed Count' +description: 'Monitors the number of failed attempts for Captcha' +category: Captcha +plugin_id: database_aggregator +value_label: Attempt(s) +value_type: 'number' +status: TRUE +caching_time: 3600 +settings: + table: 'captcha_sessions' + conditions: + - + field: 'status' + value: '0' + - + field: 'attempts' + value: '0' + operator: '>' +dependencies: + module: + - captcha diff --git a/monitoring.info.yml b/monitoring.info.yml index 0ff1283..07f9797 100644 --- a/monitoring.info.yml +++ b/monitoring.info.yml @@ -9,7 +9,6 @@ test_dependencies: - simplenews - elysia_cron - payment - - captcha simplytest_dependencies: - monitoring_demo diff --git a/monitoring.install b/monitoring.install index 9f94d2d..6f723f1 100644 --- a/monitoring.install +++ b/monitoring.install @@ -68,27 +68,6 @@ function monitoring_install() { } } - // Declares captcha faild attempts count sensor. - if (\Drupal::moduleHandler()->moduleExists('captcha')) { - $sensor = SensorConfig::create(array( - 'id' => 'captcha_failed_count', - 'label' => 'Captcha failed Count', - 'description' => 'Monitors the number of failed attempts for Captcha', - 'plugin_id' => 'database_aggregator', - 'value_label' => 'Attempt(s)', - 'value_type' => 'number', - 'category' => 'Captcha', - 'status' => TRUE, - 'settings' => array( - 'table' => 'captcha_sessions', - 'conditions' => array( - array('field' => 'status', 'value' => 0), - ), - ), - )); - $sensor->save(); - } - // Declares Watchdog events sensors. $severities = monitoring_event_severities(); foreach (RfcLogLevel::getLevels() as $level => $name) { diff --git a/src/Tests/MonitoringCaptchaTest.php b/src/Tests/MonitoringCaptchaTest.php index c7bc021..dcac531 100644 --- a/src/Tests/MonitoringCaptchaTest.php +++ b/src/Tests/MonitoringCaptchaTest.php @@ -28,15 +28,21 @@ class MonitoringCaptchaTest extends MonitoringTestBase { parent::setUp(); // Add captcha.inc file. module_load_include('inc', 'captcha'); - // Set a CAPTCHA on login form. - captcha_set_form_id_setting('user_login_form', 'captcha/Math'); } /** * {@inheritdoc} */ public function testCaptchaSensor() { + // Create user and test log in without CAPTCHA. $user = $this->drupalCreateUser(); + $this->drupalLogin($user); + // Log out again. + $this->drupalLogout(); + + // Set a CAPTCHA on login form. + captcha_set_form_id_setting('user_login_form', 'captcha/Math'); + // Try to log in, with invalid captcha answer which should fail. $edit = array( 'name' => $user->getUsername(), @@ -47,6 +53,11 @@ class MonitoringCaptchaTest extends MonitoringTestBase { // Run sensor and get the message. $result = $this->runSensor('captcha_failed_count')->getMessage(); + + // Assert the number of failed attempts. $this->assertEqual($result, '1 attempt(s)'); + + // Assert the total number of entries in captcha_sessions table. + $this->assertEqual(db_query('SELECT COUNT (*) FROM {captcha_sessions}')->fetchField(), 3); } }