From b8ddc3993449175acd6cb8a36f99d6a2b8004315 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sun, 12 May 2013 23:02:35 +0100
Subject: [PATCH] Issue #1978978 by plopesc, ParisLiakos, dawehner: Convert
 simpletest_test_form() to a Controller.

---
 .../simpletest/Form/SimpletestResultsForm.php |   8 +-
 .../simpletest/Form/SimpletestTestForm.php    | 109 ++++++++++++++++++
 core/modules/simpletest/simpletest.module     |  11 +-
 .../modules/simpletest/simpletest.routing.yml |   7 ++
 ...pletest.pages.inc => simpletest.theme.inc} |  79 -------------
 5 files changed, 124 insertions(+), 90 deletions(-)
 create mode 100644 core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestTestForm.php
 rename core/modules/simpletest/{simpletest.pages.inc => simpletest.theme.inc} (66%)

diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php
index 35be8541a604..844e9efe4086 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestResultsForm.php
@@ -254,15 +254,15 @@ public function submitForm(array &$form, array &$form_state) {
       return;
     }
 
+    $form_execute = array();
     $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);
+    // Submit the simpletest test form to rerun the tests.
+    $simpletest_test_form = new SimpletestTestForm();
+    $simpletest_test_form->submitForm($form_execute, $form_state_execute);
     $form_state['redirect'] = $form_state_execute['redirect'];
   }
 
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestTestForm.php b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestTestForm.php
new file mode 100644
index 000000000000..229211c36691
--- /dev/null
+++ b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestTestForm.php
@@ -0,0 +1,109 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\simpletest\Form\SimpletestTestForm.
+ */
+
+namespace Drupal\simpletest\Form;
+
+use Drupal\Core\Form\FormInterface;
+
+/**
+ * List tests arranged in groups that can be selected and run.
+ */
+class SimpletestTestForm implements FormInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getFormID() {
+    return 'simpletest_test_form';
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildForm(array $form, array &$form_state) {
+    $form['tests'] = array(
+      '#type' => 'details',
+      '#title' => t('Tests'),
+      '#description' => t('Select the test(s) or test group(s) you would like to run, and click <em>Run tests</em>.'),
+    );
+
+    $form['tests']['table'] = array(
+      '#theme' => 'simpletest_test_table',
+    );
+
+    // Generate the list of tests arranged by group.
+    $groups = simpletest_test_get_all();
+    $groups['PHPUnit'] = simpletest_phpunit_get_available_tests();
+    $form_state['storage']['PHPUnit'] = $groups['PHPUnit'];
+
+    foreach ($groups as $group => $tests) {
+      $form['tests']['table'][$group] = array(
+        '#collapsed' => TRUE,
+      );
+
+      foreach ($tests as $class => $info) {
+        $form['tests']['table'][$group][$class] = array(
+          '#type' => 'checkbox',
+          '#title' => $info['name'],
+          '#description' => $info['description'],
+        );
+      }
+    }
+
+    // Operation buttons.
+    $form['tests']['op'] = array(
+      '#type' => 'submit',
+      '#value' => t('Run tests'),
+    );
+    $form['clean'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Clean test environment'),
+      '#description' => t('Remove tables with the prefix "simpletest" and temporary directories that are left over from tests that crashed. This is intended for developers when creating tests.'),
+    );
+    $form['clean']['op'] = array(
+      '#type' => 'submit',
+      '#value' => t('Clean environment'),
+      '#submit' => array('simpletest_clean_environment'),
+    );
+
+    return $form;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateForm(array &$form, array &$form_state) {
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitForm(array &$form, array &$form_state) {
+    // Get list of tests.
+    $tests_list = array();
+    simpletest_classloader_register();
+
+    $phpunit_all = array_keys($form_state['storage']['PHPUnit']);
+
+    foreach ($form_state['values'] as $class_name => $value) {
+      // Since class_exists() will likely trigger an autoload lookup,
+      // we do the fast check first.
+      if ($value === 1 && class_exists($class_name)) {
+        $test_type = in_array($class_name, $phpunit_all) ? 'UnitTest' : 'WebTest';
+        $tests_list[$test_type][] = $class_name;
+      }
+    }
+    if (count($tests_list) > 0 ) {
+      $test_id = simpletest_run_tests($tests_list, 'drupal');
+      $form_state['redirect'] = 'admin/config/development/testing/results/' . $test_id;
+    }
+    else {
+      drupal_set_message(t('No test(s) selected.'), 'error');
+    }
+  }
+
+}
diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index 97caa11386be..06e56e729463 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -34,11 +34,8 @@ function simpletest_help($path, $arg) {
 function simpletest_menu() {
   $items['admin/config/development/testing'] = array(
     'title' => 'Testing',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('simpletest_test_form'),
     'description' => 'Run tests against Drupal core and your modules. These tests help assure that your site code is working as designed.',
-    'access arguments' => array('administer unit tests'),
-    'file' => 'simpletest.pages.inc',
+    'route_name' => 'simpletest_test_form',
     'weight' => -5,
   );
   $items['admin/config/development/testing/list'] = array(
@@ -55,7 +52,7 @@ function simpletest_menu() {
   $items['admin/config/development/testing/results/%'] = array(
     'title' => 'Test result',
     'description' => 'View result of tests.',
-    'router_name' => 'simpletest_result_form',
+    'route_name' => 'simpletest_result_form',
   );
   return $items;
 }
@@ -79,11 +76,11 @@ function simpletest_theme() {
   return array(
     'simpletest_test_table' => array(
       'render element' => 'table',
-      'file' => 'simpletest.pages.inc',
+      'file' => 'simpletest.theme.inc',
     ),
     'simpletest_result_summary' => array(
       'render element' => 'form',
-      'file' => 'simpletest.pages.inc',
+      'file' => 'simpletest.theme.inc',
     ),
   );
 }
diff --git a/core/modules/simpletest/simpletest.routing.yml b/core/modules/simpletest/simpletest.routing.yml
index fc5d8bcf3d14..8436309a6314 100644
--- a/core/modules/simpletest/simpletest.routing.yml
+++ b/core/modules/simpletest/simpletest.routing.yml
@@ -5,6 +5,13 @@ simpletest_settings:
   requirements:
     _permission: 'administer unit tests'
 
+simpletest_test_form:
+  pattern: '/admin/config/development/testing'
+  defaults:
+    _form: '\Drupal\simpletest\Form\SimpletestTestForm'
+  requirements:
+    _permission: 'administer unit tests'
+
 simpletest_result_form:
   pattern: '/admin/config/development/testing/results/{test_id}'
   defaults:
diff --git a/core/modules/simpletest/simpletest.pages.inc b/core/modules/simpletest/simpletest.theme.inc
similarity index 66%
rename from core/modules/simpletest/simpletest.pages.inc
rename to core/modules/simpletest/simpletest.theme.inc
index 99f242b00d36..f7b5e6fd2bbe 100644
--- a/core/modules/simpletest/simpletest.pages.inc
+++ b/core/modules/simpletest/simpletest.theme.inc
@@ -5,58 +5,6 @@
  * Page callbacks for simpletest module.
  */
 
-/**
- * List tests arranged in groups that can be selected and run.
- */
-function simpletest_test_form($form, &$form_state) {
-  $form['tests'] = array(
-    '#type' => 'details',
-    '#title' => t('Tests'),
-    '#description' => t('Select the test(s) or test group(s) you would like to run, and click <em>Run tests</em>.'),
-  );
-
-  $form['tests']['table'] = array(
-    '#theme' => 'simpletest_test_table',
-  );
-
-  // Generate the list of tests arranged by group.
-  $groups = simpletest_test_get_all();
-  $groups['PHPUnit'] = simpletest_phpunit_get_available_tests();
-  $form_state['storage']['PHPUnit'] = $groups['PHPUnit'];
-
-  foreach ($groups as $group => $tests) {
-    $form['tests']['table'][$group] = array(
-      '#collapsed' => TRUE,
-    );
-
-    foreach ($tests as $class => $info) {
-      $form['tests']['table'][$group][$class] = array(
-        '#type' => 'checkbox',
-        '#title' => $info['name'],
-        '#description' => $info['description'],
-      );
-    }
-  }
-
-  // Operation buttons.
-  $form['tests']['op'] = array(
-    '#type' => 'submit',
-    '#value' => t('Run tests'),
-  );
-  $form['clean'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Clean test environment'),
-    '#description' => t('Remove tables with the prefix "simpletest" and temporary directories that are left over from tests that crashed. This is intended for developers when creating tests.'),
-  );
-  $form['clean']['op'] = array(
-    '#type' => 'submit',
-    '#value' => t('Clean environment'),
-    '#submit' => array('simpletest_clean_environment'),
-  );
-
-  return $form;
-}
-
 /**
  * Returns HTML for a test list generated by simpletest_test_form() into a table.
  *
@@ -176,33 +124,6 @@ function theme_simpletest_test_table($variables) {
   }
 }
 
-/**
- * Run selected tests.
- */
-function simpletest_test_form_submit($form, &$form_state) {
-  // Get list of tests.
-  $tests_list = array();
-  simpletest_classloader_register();
-
-  $phpunit_all = array_keys($form_state['storage']['PHPUnit']);
-
-  foreach ($form_state['values'] as $class_name => $value) {
-    // Since class_exists() will likely trigger an autoload lookup,
-    // we do the fast check first.
-    if ($value === 1 && class_exists($class_name)) {
-      $test_type = in_array($class_name, $phpunit_all) ? 'UnitTest' : 'WebTest';
-      $tests_list[$test_type][] = $class_name;
-    }
-  }
-  if (count($tests_list) > 0 ) {
-    $test_id = simpletest_run_tests($tests_list, 'drupal');
-    $form_state['redirect'] = 'admin/config/development/testing/results/' . $test_id;
-  }
-  else {
-    drupal_set_message(t('No test(s) selected.'), 'error');
-  }
-}
-
 /**
  * Returns HTML for the summary status of a simpletest result.
  *
-- 
GitLab