summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2013-04-30 21:25:25 (GMT)
committer Alex Pott2013-04-30 21:25:25 (GMT)
commitb30789561b10ab4f0e601b3f8e9704364f5938b6 (patch)
tree9a0a3fd7fb0d18e413bdd666d777997763ed3a29
parent2784f4886dc1dfb7d09b21a066e37b0f701f6b05 (diff)
Issue #1978982 by RoSk0, ParisLiakos: Convert simpletest_result_form() to a Controller.
-rw-r--r--core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php297
-rw-r--r--core/modules/simpletest/simpletest.module5
-rw-r--r--core/modules/simpletest/simpletest.pages.inc214
-rw-r--r--core/modules/simpletest/simpletest.routing.yml7
4 files changed, 305 insertions, 218 deletions
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php
new file mode 100644
index 0000000..35be854
--- /dev/null
+++ b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php
@@ -0,0 +1,297 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\simpletest\Form\SimpletestResultsForm.
+ */
+
+namespace Drupal\simpletest\Form;
+
+use Drupal\Core\ControllerInterface;
+use Drupal\Core\Database\Connection;
+use Drupal\Core\Form\FormInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Test results form for $test_id.
+ */
+class SimpletestResultsForm implements FormInterface, ControllerInterface {
+
+ /**
+ * Associative array of themed result images keyed by status.
+ *
+ * @var array
+ */
+ protected $statusImageMap;
+
+ /**
+ * The database connection service.
+ *
+ * @var \Drupal\Core\Database\Connection
+ */
+ protected $database;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static($container->get('database'));
+ }
+
+ /**
+ * Constructs a \Drupal\simpletest\Form\SimpletestResultsForm object.
+ *
+ * @param \Drupal\Core\Database\Connection $database
+ * The database connection service.
+ */
+ public function __construct(Connection $database) {
+ $this->database = $database;
+ // Initialize image mapping property.
+ $this->statusImageMap = array(
+ 'pass' => theme('image', array(
+ 'uri' => 'core/misc/watchdog-ok.png',
+ 'width' => 18,
+ 'height' => 18,
+ 'alt' => t('Pass')
+ )),
+ 'fail' => theme('image', array(
+ 'uri' => 'core/misc/watchdog-error.png',
+ 'width' => 18,
+ 'height' => 18,
+ 'alt' => t('Fail')
+ )),
+ 'exception' => theme('image', array(
+ 'uri' => 'core/misc/watchdog-warning.png',
+ 'width' => 18,
+ 'height' => 18,
+ 'alt' => t('Exception')
+ )),
+ 'debug' => theme('image', array(
+ 'uri' => 'core/misc/watchdog-warning.png',
+ 'width' => 18,
+ 'height' => 18,
+ 'alt' => t('Debug')
+ )),
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getFormID() {
+ return 'simpletest_results_form';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(array $form, array &$form_state, $test_id = NULL) {
+ // Make sure there are test results to display and a re-run is not being performed.
+ $results = array();
+
+ if (is_numeric($test_id) && !$results = $this->getResults($test_id)) {
+ drupal_set_message(t('No test results to display.'), 'error');
+ drupal_goto('admin/config/development/testing');
+
+ return $form;
+ }
+
+ // Load all classes and include CSS.
+ $form['#attached']['css'][] = drupal_get_path('module', 'simpletest') . '/simpletest.css';
+
+ // Keep track of which test cases passed or failed.
+ $filter = array(
+ 'pass' => array(),
+ 'fail' => array(),
+ );
+
+ // Summary result widget.
+ $form['result'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Results'),
+ );
+ $form['result']['summary'] = $summary = array(
+ '#theme' => 'simpletest_result_summary',
+ '#pass' => 0,
+ '#fail' => 0,
+ '#exception' => 0,
+ '#debug' => 0,
+ );
+
+ simpletest_classloader_register();
+
+ // Cycle through each test group.
+ $header = array(
+ t('Message'),
+ t('Group'),
+ t('Filename'),
+ t('Line'),
+ t('Function'),
+ array('colspan' => 2, 'data' => t('Status'))
+ );
+ $form['result']['results'] = array();
+ foreach ($results as $group => $assertions) {
+ // Create group details with summary information.
+ $info = call_user_func(array($group, 'getInfo'));
+ $form['result']['results'][$group] = array(
+ '#type' => 'details',
+ '#title' => $info['name'],
+ '#description' => $info['description'],
+ );
+ $form['result']['results'][$group]['summary'] = $summary;
+ $group_summary =& $form['result']['results'][$group]['summary'];
+
+ // Create table of assertions for the group.
+ $rows = array();
+ foreach ($assertions as $assertion) {
+ $row = array();
+ $row[] = $assertion->message;
+ $row[] = $assertion->message_group;
+ $row[] = drupal_basename($assertion->file);
+ $row[] = $assertion->line;
+ $row[] = $assertion->function;
+ $row[] = $this->statusImageMap[$assertion->status];
+
+ $class = 'simpletest-' . $assertion->status;
+ if ($assertion->message_group == 'Debug') {
+ $class = 'simpletest-debug';
+ }
+ $rows[] = array('data' => $row, 'class' => array($class));
+
+ $group_summary['#' . $assertion->status]++;
+ $form['result']['summary']['#' . $assertion->status]++;
+ }
+ $form['result']['results'][$group]['table'] = array(
+ '#theme' => 'table',
+ '#header' => $header,
+ '#rows' => $rows,
+ );
+
+ // Set summary information.
+ $group_summary['#ok'] = $group_summary['#fail'] + $group_summary['#exception'] == 0;
+ $form['result']['results'][$group]['#collapsed'] = $group_summary['#ok'];
+
+ // Store test group (class) as for use in filter.
+ $filter[$group_summary['#ok'] ? 'pass' : 'fail'][] = $group;
+ }
+
+ // Overal summary status.
+ $form['result']['summary']['#ok'] = $form['result']['summary']['#fail'] + $form['result']['summary']['#exception'] == 0;
+
+ // Actions.
+ $form['#action'] = url('admin/config/development/testing/results/re-run');
+ $form['action'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Actions'),
+ '#attributes' => array('class' => array('container-inline')),
+ '#weight' => -11,
+ );
+
+ $form['action']['filter'] = array(
+ '#type' => 'select',
+ '#title' => 'Filter',
+ '#options' => array(
+ 'all' => t('All (@count)', array('@count' => count($filter['pass']) + count($filter['fail']))),
+ 'pass' => t('Pass (@count)', array('@count' => count($filter['pass']))),
+ 'fail' => t('Fail (@count)', array('@count' => count($filter['fail']))),
+ ),
+ );
+ $form['action']['filter']['#default_value'] = ($filter['fail'] ? 'fail' : 'all');
+
+ // Categorized test classes for to be used with selected filter value.
+ $form['action']['filter_pass'] = array(
+ '#type' => 'hidden',
+ '#default_value' => implode(',', $filter['pass']),
+ );
+ $form['action']['filter_fail'] = array(
+ '#type' => 'hidden',
+ '#default_value' => implode(',', $filter['fail']),
+ );
+
+ $form['action']['op'] = array(
+ '#type' => 'submit',
+ '#value' => t('Run tests'),
+ );
+
+ $form['action']['return'] = array(
+ '#type' => 'link',
+ '#title' => t('Return to list'),
+ '#href' => 'admin/config/development/testing',
+ );
+
+ if (is_numeric($test_id)) {
+ simpletest_clean_results_table($test_id);
+ }
+
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function validateForm(array &$form, array &$form_state) {
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitForm(array &$form, array &$form_state) {
+ $pass = $form_state['values']['filter_pass'] ? explode(',', $form_state['values']['filter_pass']) : array();
+ $fail = $form_state['values']['filter_fail'] ? explode(',', $form_state['values']['filter_fail']) : array();
+
+ if ($form_state['values']['filter'] == 'all') {
+ $classes = array_merge($pass, $fail);
+ }
+ elseif ($form_state['values']['filter'] == 'pass') {
+ $classes = $pass;
+ }
+ else {
+ $classes = $fail;
+ }
+
+ if (!$classes) {
+ $form_state['redirect'] = 'admin/config/development/testing';
+ return;
+ }
+
+ $form_state_execute = array('values' => array());
+ foreach ($classes as $class) {
+ $form_state_execute['values'][$class] = 1;
+ }
+
+ // @todo When simpletest_test_form is converted, extend it and remove this.
+ \Drupal::moduleHandler()->loadInclude('simpletest', 'inc', 'simpletest.pages');
+
+ simpletest_test_form_submit(array(), $form_state_execute);
+ $form_state['redirect'] = $form_state_execute['redirect'];
+ }
+
+ /**
+ * Get test results for $test_id.
+ *
+ * @param int $test_id
+ * The test_id to retrieve results of.
+ *
+ * @return array
+ * Array of results grouped by test_class.
+ */
+ protected function getResults($test_id) {
+ $results = $this->database->select('simpletest')
+ ->fields('simpletest')
+ ->condition('test_id', $test_id)
+ ->orderBy('test_class')
+ ->orderBy('message_id')
+ ->execute();
+
+ $test_results = array();
+ foreach ($results as $result) {
+ if (!isset($test_results[$result->test_class])) {
+ $test_results[$result->test_class] = array();
+ }
+ $test_results[$result->test_class][] = $result;
+ }
+
+ return $test_results;
+ }
+
+}
diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index 0a46873..97caa11 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -54,11 +54,8 @@ function simpletest_menu() {
);
$items['admin/config/development/testing/results/%'] = array(
'title' => 'Test result',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('simpletest_result_form', 5),
'description' => 'View result of tests.',
- 'access arguments' => array('administer unit tests'),
- 'file' => 'simpletest.pages.inc',
+ 'router_name' => 'simpletest_result_form',
);
return $items;
}
diff --git a/core/modules/simpletest/simpletest.pages.inc b/core/modules/simpletest/simpletest.pages.inc
index 4fb908d..99f242b 100644
--- a/core/modules/simpletest/simpletest.pages.inc
+++ b/core/modules/simpletest/simpletest.pages.inc
@@ -204,172 +204,6 @@ function simpletest_test_form_submit($form, &$form_state) {
}
/**
- * Test results form for $test_id.
- */
-function simpletest_result_form($form, &$form_state, $test_id) {
- // Make sure there are test results to display and a re-run is not being performed.
- $results = array();
- if (is_numeric($test_id) && !$results = simpletest_result_get($test_id)) {
- drupal_set_message(t('No test results to display.'), 'error');
- drupal_goto('admin/config/development/testing');
- return $form;
- }
-
- // Load all classes and include CSS.
- drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
-
- // Keep track of which test cases passed or failed.
- $filter = array(
- 'pass' => array(),
- 'fail' => array(),
- );
-
- // Summary result widget.
- $form['result'] = array(
- '#type' => 'fieldset',
- '#title' => t('Results'),
- );
- $form['result']['summary'] = $summary = array(
- '#theme' => 'simpletest_result_summary',
- '#pass' => 0,
- '#fail' => 0,
- '#exception' => 0,
- '#debug' => 0,
- );
-
- simpletest_classloader_register();
-
- // Cycle through each test group.
- $header = array(t('Message'), t('Group'), t('Filename'), t('Line'), t('Function'), array('colspan' => 2, 'data' => t('Status')));
- $form['result']['results'] = array();
- foreach ($results as $group => $assertions) {
- // Create group details with summary information.
- $info = call_user_func(array($group, 'getInfo'));
- $form['result']['results'][$group] = array(
- '#type' => 'details',
- '#title' => $info['name'],
- '#description' => $info['description'],
- );
- $form['result']['results'][$group]['summary'] = $summary;
- $group_summary = &$form['result']['results'][$group]['summary'];
-
- // Create table of assertions for the group.
- $rows = array();
- foreach ($assertions as $assertion) {
- $row = array();
- $row[] = $assertion->message;
- $row[] = $assertion->message_group;
- $row[] = drupal_basename($assertion->file);
- $row[] = $assertion->line;
- $row[] = $assertion->function;
- $row[] = simpletest_result_status_image($assertion->status);
-
- $class = 'simpletest-' . $assertion->status;
- if ($assertion->message_group == 'Debug') {
- $class = 'simpletest-debug';
- }
- $rows[] = array('data' => $row, 'class' => array($class));
-
- $group_summary['#' . $assertion->status]++;
- $form['result']['summary']['#' . $assertion->status]++;
- }
- $form['result']['results'][$group]['table'] = array(
- '#theme' => 'table',
- '#header' => $header,
- '#rows' => $rows,
- );
-
- // Set summary information.
- $group_summary['#ok'] = $group_summary['#fail'] + $group_summary['#exception'] == 0;
- $form['result']['results'][$group]['#collapsed'] = $group_summary['#ok'];
-
- // Store test group (class) as for use in filter.
- $filter[$group_summary['#ok'] ? 'pass' : 'fail'][] = $group;
- }
-
- // Overal summary status.
- $form['result']['summary']['#ok'] = $form['result']['summary']['#fail'] + $form['result']['summary']['#exception'] == 0;
-
- // Actions.
- $form['#action'] = url('admin/config/development/testing/results/re-run');
- $form['action'] = array(
- '#type' => 'fieldset',
- '#title' => t('Actions'),
- '#attributes' => array('class' => array('container-inline')),
- '#weight' => -11,
- );
-
- $form['action']['filter'] = array(
- '#type' => 'select',
- '#title' => 'Filter',
- '#options' => array(
- 'all' => t('All (@count)', array('@count' => count($filter['pass']) + count($filter['fail']))),
- 'pass' => t('Pass (@count)', array('@count' => count($filter['pass']))),
- 'fail' => t('Fail (@count)', array('@count' => count($filter['fail']))),
- ),
- );
- $form['action']['filter']['#default_value'] = ($filter['fail'] ? 'fail' : 'all');
-
- // Categorized test classes for to be used with selected filter value.
- $form['action']['filter_pass'] = array(
- '#type' => 'hidden',
- '#default_value' => implode(',', $filter['pass']),
- );
- $form['action']['filter_fail'] = array(
- '#type' => 'hidden',
- '#default_value' => implode(',', $filter['fail']),
- );
-
- $form['action']['op'] = array(
- '#type' => 'submit',
- '#value' => t('Run tests'),
- );
-
- $form['action']['return'] = array(
- '#type' => 'link',
- '#title' => t('Return to list'),
- '#href' => 'admin/config/development/testing',
- );
-
- if (is_numeric($test_id)) {
- simpletest_clean_results_table($test_id);
- }
-
- return $form;
-}
-
-/**
- * Re-run the tests that match the filter.
- */
-function simpletest_result_form_submit($form, &$form_state) {
- $pass = $form_state['values']['filter_pass'] ? explode(',', $form_state['values']['filter_pass']) : array();
- $fail = $form_state['values']['filter_fail'] ? explode(',', $form_state['values']['filter_fail']) : array();
-
- if ($form_state['values']['filter'] == 'all') {
- $classes = array_merge($pass, $fail);
- }
- elseif ($form_state['values']['filter'] == 'pass') {
- $classes = $pass;
- }
- else {
- $classes = $fail;
- }
-
- if (!$classes) {
- $form_state['redirect'] = 'admin/config/development/testing';
- return;
- }
-
- $form_state_execute = array('values' => array());
- foreach ($classes as $class) {
- $form_state_execute['values'][$class] = 1;
- }
-
- simpletest_test_form_submit(array(), $form_state_execute);
- $form_state['redirect'] = $form_state_execute['redirect'];
-}
-
-/**
* Returns HTML for the summary status of a simpletest result.
*
* @param $variables
@@ -382,51 +216,3 @@ function theme_simpletest_result_summary($variables) {
$form = $variables['form'];
return '<div class="simpletest-' . ($form['#ok'] ? 'pass' : 'fail') . '">' . _simpletest_format_summary_line($form) . '</div>';
}
-
-/**
- * Get test results for $test_id.
- *
- * @param $test_id The test_id to retrieve results of.
- * @return Array of results grouped by test_class.
- */
-function simpletest_result_get($test_id) {
- $results = db_select('simpletest')
- ->fields('simpletest')
- ->condition('test_id', $test_id)
- ->orderBy('test_class')
- ->orderBy('message_id')
- ->execute();
-
- $test_results = array();
- foreach ($results as $result) {
- if (!isset($test_results[$result->test_class])) {
- $test_results[$result->test_class] = array();
- }
- $test_results[$result->test_class][] = $result;
- }
- return $test_results;
-}
-
-/**
- * Get the appropriate image for the status.
- *
- * @param $status Status string, either: pass, fail, exception.
- * @return HTML image or false.
- */
-function simpletest_result_status_image($status) {
- // $map does not use drupal_static() as its value never changes.
- static $map;
-
- if (!isset($map)) {
- $map = array(
- 'pass' => theme('image', array('uri' => 'core/misc/watchdog-ok.png', 'width' => 18, 'height' => 18, 'alt' => t('Pass'))),
- 'fail' => theme('image', array('uri' => 'core/misc/watchdog-error.png', 'width' => 18, 'height' => 18, 'alt' => t('Fail'))),
- 'exception' => theme('image', array('uri' => 'core/misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('Exception'))),
- 'debug' => theme('image', array('uri' => 'core/misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('Debug'))),
- );
- }
- if (isset($map[$status])) {
- return $map[$status];
- }
- return FALSE;
-}
diff --git a/core/modules/simpletest/simpletest.routing.yml b/core/modules/simpletest/simpletest.routing.yml
index 7a17bfd..fc5d8bc 100644
--- a/core/modules/simpletest/simpletest.routing.yml
+++ b/core/modules/simpletest/simpletest.routing.yml
@@ -4,3 +4,10 @@ simpletest_settings:
_form: 'Drupal\simpletest\Form\SimpletestSettingsForm'
requirements:
_permission: 'administer unit tests'
+
+simpletest_result_form:
+ pattern: '/admin/config/development/testing/results/{test_id}'
+ defaults:
+ _form: 'Drupal\simpletest\Form\SimpletestResultsForm'
+ requirements:
+ _permission: 'administer unit tests'