diff --git a/acquia_spi/acquia_spi.api.php b/acquia_spi/acquia_spi.api.php index bae4a6b..e52f922 100644 --- a/acquia_spi/acquia_spi.api.php +++ b/acquia_spi/acquia_spi.api.php @@ -33,3 +33,33 @@ function hook_acquia_spi_get() { ); return $data; } + +/** + * Include data to be sent to Acquia Insight as part of the SPI process. This + * data will be stored on Acquia's servers in an unencrypted database, so be + * careful not to send sensitive information in any field. + * + * @return + * An array of user-contributed test data keyed by unique identifier. + * - (string) description: Detailed information regarding test, its impact, and other relevant details. + * - (string) solved_message: The message to display when the test has succeeded. + * - (string) failed_message: The message to display when the test has failed. + * - (boolean) solved: A flag indicating whether or not the test was successful. + * - (string) fix_details: Information on how to fix or resolve the test if failed. + * - (string) category: The category to place the test within. Must be either 'performance', 'security, or 'best_practices'. + * - (int) severity: The priority level of the custom test. Must be either 0, 1, 2, 4, 8, 16, 32, 64, or 128. Higher severities impact the Insight score proportionally. + * + */ +function hook_acquia_spi_test() { + return array( + 'example' => array( + 'description' => 'This example test is useful.', + 'solved_message' => 'The test was successful', + 'failed_message' => 'The test has failed', + 'solved' => TRUE, + 'fix_details' => 'Please resolve this issue using this fix information.', + 'category' => 'best_practices', + 'severity' => 0, + ), + ); +} diff --git a/acquia_spi/acquia_spi.drush.inc b/acquia_spi/acquia_spi.drush.inc index 7d6690c..d9b1652 100644 --- a/acquia_spi/acquia_spi.drush.inc +++ b/acquia_spi/acquia_spi.drush.inc @@ -1,4 +1,5 @@ 'Write JSON encoded SPI data to spi.json in current directory.', ), ); + $items['spi-validate'] = array( + 'callback' => 'drush_acquia_spi_custom_test_validate', + 'description' => dt('Perform a validation check on any modules with Acquia SPI custom tests.'), + 'aliases' => array('spi-ctv'), + ); return $items; } @@ -84,3 +90,62 @@ function _acquia_spi_drush_get() { } return acquia_spi_get(); } + +/** + * A command callback and drush wrapper for custom test validation. + * + */ +function drush_acquia_spi_custom_test_validate() { + + $pass = array(); + $failure = array(); + + $modules = module_implements('acquia_spi_test'); + drush_print_r(dt("Acquia SPI custom tests were detected in: @modules \n", array('@modules' => implode(', ', $modules)))); + + foreach (module_implements('acquia_spi_test') as $module) { + $function = $module . '_acquia_spi_test'; + if (function_exists($function)) { + $result = acquia_spi_test_validate($function()); + + if (!$result['result']) { + $failure[] = $module; + drush_print_r(dt("[FAILED] Validation failed for '@module' and has been logged.", array('@module' => $module))); + $custom_data[$module] = $result; + + foreach ($result['failure'] as $test_name => $test_failures) { + foreach ($test_failures as $test_param => $test_value) { + $variables = array( + '!module_name' => $module, + '@message' => $test_value['message'], + '!param_name' => $test_param, + '!test_name' => $test_name, + '!value' => $test_value['value'], + ); + drush_print_r(dt("[DETAILS] @message for parameter '!param_name'; current value '!value'. (Test !test_name in module !module_name)", $variables)); + watchdog('acquia spi test', "Custom test validation failed: @message for parameter '!param_name'; current value '!value'. (Test '!test_name' in module '!module_name')", $variables, WATCHDOG_WARNING); + } + } + } + else { + $pass[] = $module; + drush_print_r(dt("[PASSED] Validation passed for '@module.'", array('@module' => $module))); + } + } + drush_print_r(""); + } + + $message = 'Validation checks completed.'; + if (count($pass) > 0) { + $variables['@passes'] = implode(', ', $pass); + $variables['@pass_count'] = count($pass); + $message .= "\n@pass_count module(s) passed validation: @passes."; + } + if (count($failure) > 0) { + $variables['@failures'] = implode(', ', $failure); + $variables['@fail_count'] = count($failure); + $message .= "\n@fail_count module(s) failed validation: @failures."; + } + + drush_print_r(dt($message, $variables)); +} diff --git a/acquia_spi/acquia_spi.install b/acquia_spi/acquia_spi.install index af22339..9d74130 100644 --- a/acquia_spi/acquia_spi.install +++ b/acquia_spi/acquia_spi.install @@ -75,6 +75,32 @@ function acquia_spi_requirements($phase) { 'description' => t($description, array('!interval' => $interval, '!config-page' => $config_url, '!spi-send' => url('system/acquia-spi-send', array('query' => array('destination' => 'admin/reports/status', 'key' => $key))))), ); } + + // User-contributed tests status. + $modules = module_implements('acquia_spi_test'); + if (!empty($modules)) { + $result = acquia_spi_test_status(); + + if (!empty($result)) { + $modules = implode(', ', array_keys($result)); + $description = 'Custom tests within the following module(s) have failed validation and will not be sent: %modules.
Please check your error logs for more information regarding how to pass validation.'; + $value = 'Failed'; + $severity = REQUIREMENT_ERROR; + } + else { + $modules = implode(', ', $modules); + $description = 'Custom test data is structured properly and is sending from: %modules'; + $value = 'Passed'; + $severity = REQUIREMENT_OK; + } + + $requirements['acquia_spi_test'] = array( + 'title' => t('Acquia Network SPI User-Contributed Tests'), + 'description' => t($description, array('%modules' => $modules)), + 'value' => t($value), + 'severity' => $severity, + ); + } break; } diff --git a/acquia_spi/acquia_spi.module b/acquia_spi/acquia_spi.module index 33c0231..fbc46c3 100644 --- a/acquia_spi/acquia_spi.module +++ b/acquia_spi/acquia_spi.module @@ -505,6 +505,13 @@ function acquia_spi_get() { if (!empty($security_review_results)) { $additional_data['security_review'] = $security_review_results['security_review']; } + + // Collect all user-contributed custom tests that pass validation. + $custom_tests_results = acquia_spi_test_collect(); + if (!empty($custom_tests_results)) { + $additional_data['custom_tests'] = $custom_tests_results; + } + $spi_data = module_invoke_all('acquia_spi_get'); if (!empty($spi_data)) { foreach ($spi_data as $name => $data) { @@ -598,6 +605,164 @@ function _acquia_spi_security_review_compatible() { } /** + * Collects all user-contributed test results that pass validation. + * + * @return array $custom_data + * An associative array containing properly formatted user-contributed tests. + * + */ +function acquia_spi_test_collect() { + + $custom_data = array(); + + // Collect all custom data provided by hook_insight_custom_data(). + $collections = module_invoke_all('acquia_spi_test'); + + foreach ($collections as $test_name => $test_params) { + $result = acquia_spi_test_validate(array($test_name => $test_params)); + + if ($result['result']) { + $custom_data[$test_name] = $test_params; + } + } + + return $custom_data; +} + +/** + * Implements hook_modules_enabled(). + * + */ +function acquia_spi_test_modules_enabled($modules) { + + foreach ($modules as $module) { + if (function_exists($module . '_acquia_spi_test')) { + drupal_set_message(t("A new invokation of hook_acquia_spi_test() has been detected in @module.", array('@module' => $module))); + watchdog('acquia spi test', "A new invokation of hook_acquia_spi_test() has been detected in @module.", array('@module' => $module)); + } + } +} + + +/** + * Determines the status of all user-contributed tests and logs any failures to + * a tracking table. + * + * @return array $custom_data + * An associative array containing any tests which failed validation. + * + */ +function acquia_spi_test_status() { + + $custom_data = array(); + + // Iterate through modules which contain hook_acquia_spi_test(). + foreach (module_implements('acquia_spi_test') as $module) { + $function = $module . '_acquia_spi_test'; + if (function_exists($function)) { + $result = acquia_spi_test_validate($function()); + + if (!$result['result']) { + $custom_data[$module] = $result; + + foreach ($result['failure'] as $test_name => $test_failures) { + foreach ($test_failures as $test_param => $test_value) { + $variables = array( + '!module_name' => $module, + '@message' => $test_value['message'], + '!param_name' => $test_param, + '!test_name' => $test_name, + '!value' => $test_value['value'], + ); + watchdog('acquia spi test', "Custom test validation failed: @message for parameter '!param_name'; current value '!value'. (Test '!test_name' in module '!module_name')", $variables, WATCHDOG_WARNING); + } + } + } + } + } + + return $custom_data; +} + +/** + * Validates data from callbacks. + * + * @param array $collection + * An associative array containing a collection of user-contributed tests. + * + * @return array + * An associative array containing the validation result of the given tests, + * along with any failed parameters. + * + */ +function acquia_spi_test_validate($collection) { + + $result = TRUE; + $check_result_value = array(); + + // Load valid categories and severities. + $categories = array('performance', 'security', 'best_practices'); + $severities = array(0, 1, 2, 4, 8, 16, 32, 64, 128); + + foreach ($collection as $machine_name => $tests) { + foreach ($tests as $check_name => $check_value) { + $fail_value = ''; + $message = ''; + + $check_name = strtolower($check_name); + $check_value = (is_string($check_value)) ? strtolower($check_value) : $check_value; + + // Validate the data inputs for each check. + switch ($check_name) { + case 'category': + if (!is_string($check_value) || !in_array($check_value, $categories)) { + $type = gettype($check_value); + $fail_value = "$check_value ($type)"; + $message = 'Value must be a string and one of ' . implode(', ', $categories); + } + break; + + case 'solved': + if (!is_bool($check_value)) { + $type = gettype($check_value); + $fail_value = "$check_value ($type)"; + $message = 'Value must be a boolean'; + } + break; + + case 'severity': + if (!is_int($check_value) || !in_array($check_value, $severities)) { + $type = gettype($check_value); + $fail_value = "$check_value ($type)"; + $message = 'Value must be an integer and set to one of ' . implode(', ', $severities); + } + break; + + default: + if (!is_string($check_value)) { + $type = gettype($check_value); + $fail_value = "$check_value ($type)"; + $message = 'Value must be a string'; + } + break; + } + + if (!empty($fail_value) && !empty($message)) { + $check_result_value['failed'][$machine_name][$check_name]['value'] = $fail_value; + $check_result_value['failed'][$machine_name][$check_name]['message'] = $message; + } + } + } + + // If there were any failures, the test has failed. Into exile it must go. + if (!empty($check_result_value)) { + $result = FALSE; + } + + return array('result' => $result, 'failure' => (isset($check_result_value['failed'])) ? $check_result_value['failed'] : array()); +} + +/** * Attempt to determine the version of Drupal being used. * Note, there is better information on this in the common.inc file. *