View source
<?php
namespace Drupal\simpletest\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SimpletestTestForm extends FormBase {
protected $renderer;
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'));
}
public function __construct(RendererInterface $renderer) {
$this->renderer = $renderer;
}
public function getFormId() {
return 'simpletest_test_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Run tests'),
'#tableselect' => TRUE,
'#button_type' => 'primary',
);
$form['clean'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Clean test environment'),
'#description' => $this
->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.'),
'#weight' => 200,
);
$form['clean']['op'] = array(
'#type' => 'submit',
'#value' => $this
->t('Clean environment'),
'#submit' => array(
'simpletest_clean_environment',
),
);
$user_input = $form_state
->getUserInput();
if (!empty($user_input['tests'])) {
return $form;
}
$form['filters'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'table-filter',
'js-show',
),
),
);
$form['filters']['text'] = array(
'#type' => 'search',
'#title' => $this
->t('Search'),
'#size' => 30,
'#placeholder' => $this
->t('Enter test name…'),
'#attributes' => array(
'class' => array(
'table-filter-text',
),
'data-table' => '#simpletest-test-form',
'autocomplete' => 'off',
'title' => $this
->t('Enter at least 3 characters of the test name or description to filter by.'),
),
);
$form['tests'] = array(
'#type' => 'table',
'#id' => 'simpletest-form-table',
'#tableselect' => TRUE,
'#header' => array(
array(
'data' => $this
->t('Test'),
'class' => array(
'simpletest-test-label',
),
),
array(
'data' => $this
->t('Description'),
'class' => array(
'simpletest-test-description',
),
),
),
'#empty' => $this
->t('No tests to display.'),
'#attached' => array(
'library' => array(
'simpletest/drupal.simpletest',
),
),
);
$image_collapsed = array(
'#theme' => 'image',
'#uri' => 'core/misc/menu-collapsed.png',
'#width' => '7',
'#height' => '7',
'#alt' => $this
->t('Expand'),
'#title' => $this
->t('Expand'),
'#suffix' => '<a href="#" class="simpletest-collapse">(' . $this
->t('Expand') . ')</a>',
);
$image_extended = array(
'#theme' => 'image',
'#uri' => 'core/misc/menu-expanded.png',
'#width' => '7',
'#height' => '7',
'#alt' => $this
->t('Collapse'),
'#title' => $this
->t('Collapse'),
'#suffix' => '<a href="#" class="simpletest-collapse">(' . $this
->t('Collapse') . ')</a>',
);
$form['tests']['#attached']['drupalSettings']['simpleTest']['images'] = [
(string) $this->renderer
->renderPlain($image_collapsed),
(string) $this->renderer
->renderPlain($image_extended),
];
$groups = simpletest_test_get_all();
foreach ($groups as $group => $tests) {
$form['tests'][$group] = array(
'#attributes' => array(
'class' => array(
'simpletest-group',
),
),
);
$group_class = 'module-' . strtolower(trim(preg_replace("/[^\\w\\d]/", "-", $group)));
$form['tests'][$group]['select'] = array(
'#wrapper_attributes' => array(
'id' => $group_class,
'class' => array(
'simpletest-group-select-all',
),
),
);
$form['tests'][$group]['title'] = array(
'#prefix' => '<div class="simpletest-image" id="simpletest-test-group-' . $group_class . '"></div>',
'#markup' => '<label for="' . $group_class . '-group-select-all">' . $group . '</label>',
'#wrapper_attributes' => array(
'class' => array(
'simpletest-group-label',
),
),
);
$form['tests'][$group]['description'] = array(
'#markup' => ' ',
'#wrapper_attributes' => array(
'class' => array(
'simpletest-group-description',
),
),
);
foreach ($tests as $class => $info) {
$form['tests'][$class] = array(
'#attributes' => array(
'class' => array(
$group_class . '-test',
'js-hide',
),
),
);
$form['tests'][$class]['title'] = array(
'#type' => 'label',
'#title' => '\\' . $info['name'],
'#wrapper_attributes' => array(
'class' => array(
'simpletest-test-label',
'table-filter-text-source',
),
),
);
$form['tests'][$class]['description'] = array(
'#prefix' => '<div class="description">',
'#plain_text' => $info['description'],
'#suffix' => '</div>',
'#wrapper_attributes' => array(
'class' => array(
'simpletest-test-description',
'table-filter-text-source',
),
),
);
}
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
global $base_url;
simpletest_classloader_register();
$user_input = $form_state
->getUserInput();
if ($form_state
->isValueEmpty('tests') && !empty($user_input['tests'])) {
$form_state
->setValue('tests', $user_input['tests']);
}
$tests_list = array();
foreach ($form_state
->getValue('tests') as $class_name => $value) {
if ($value === $class_name) {
if (is_subclass_of($class_name, 'PHPUnit_Framework_TestCase')) {
$test_type = 'phpunit';
}
else {
$test_type = 'simpletest';
}
$tests_list[$test_type][] = $class_name;
}
}
if (!empty($tests_list)) {
putenv('SIMPLETEST_BASE_URL=' . $base_url);
$test_id = simpletest_run_tests($tests_list, 'drupal');
$form_state
->setRedirect('simpletest.result_form', array(
'test_id' => $test_id,
));
}
}
}