You are here

public function SimpletestTestForm::buildForm in SimpleTest 8.3

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/SimpletestTestForm.php, line 65

Class

SimpletestTestForm
List tests arranged in groups that can be selected and run.

Namespace

Drupal\simpletest\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Run tests'),
    '#tableselect' => TRUE,
    '#button_type' => 'primary',
  ];
  $form['clean'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Clean test environment'),
    '#description' => $this
      ->t('Remove tables with the prefix "test" followed by digits and temporary directories that are left over from tests that crashed. This is intended for developers when creating tests.'),
    '#weight' => 200,
  ];
  $form['clean']['op'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Clean environment'),
    '#submit' => [
      'simpletest_clean_environment',
    ],
  ];

  // Do not needlessly re-execute a full test discovery if the user input
  // already contains an explicit list of test classes to run.
  $user_input = $form_state
    ->getUserInput();
  if (!empty($user_input['tests'])) {
    return $form;
  }

  // JavaScript-only table filters.
  $form['filters'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'table-filter',
        'js-show',
      ],
    ],
  ];
  $form['filters']['text'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Search'),
    '#size' => 30,
    '#placeholder' => $this
      ->t('Enter test name…'),
    '#attributes' => [
      'class' => [
        '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'] = [
    '#cache' => [
      'keys' => [
        'simpletest_ui_table',
      ],
      'contexts' => [
        'test_discovery',
      ],
    ],
    '#type' => 'table',
    '#id' => 'simpletest-form-table',
    '#tableselect' => TRUE,
    '#header' => [
      [
        'data' => $this
          ->t('Test'),
        'class' => [
          'simpletest-test-label',
        ],
      ],
      [
        'data' => $this
          ->t('Description'),
        'class' => [
          'simpletest-test-description',
        ],
      ],
    ],
    '#empty' => $this
      ->t('No tests to display.'),
    '#attached' => [
      'library' => [
        'simpletest/drupal.simpletest',
      ],
    ],
  ];

  // Define the images used to expand/collapse the test groups.
  $image_collapsed = [
    '#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 = [
    '#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),
  ];

  // Generate the list of tests arranged by group.
  $groups = $this->testDiscovery
    ->getTestClasses();
  foreach ($groups as $group => $tests) {
    $form['tests'][$group] = [
      '#attributes' => [
        'class' => [
          'simpletest-group',
        ],
      ],
    ];

    // Make the class name safe for output on the page by replacing all
    // non-word/decimal characters with a dash (-).
    $group_class = 'module-' . strtolower(trim(preg_replace("/[^\\w\\d]/", "-", $group)));

    // Override tableselect column with custom selector for this group.
    // This group-select-all checkbox is injected via JavaScript.
    $form['tests'][$group]['select'] = [
      '#wrapper_attributes' => [
        'id' => $group_class,
        'class' => [
          'simpletest-group-select-all',
        ],
      ],
    ];
    $form['tests'][$group]['title'] = [
      // Expand/collapse image.
      '#prefix' => '<div class="simpletest-image" id="simpletest-test-group-' . $group_class . '"></div>',
      '#markup' => '<label for="' . $group_class . '-group-select-all">' . $group . '</label>',
      '#wrapper_attributes' => [
        'class' => [
          'simpletest-group-label',
        ],
      ],
    ];
    $form['tests'][$group]['description'] = [
      '#markup' => '&nbsp;',
      '#wrapper_attributes' => [
        'class' => [
          'simpletest-group-description',
        ],
      ],
    ];

    // Cycle through each test within the current group.
    foreach ($tests as $class => $info) {
      $form['tests'][$class] = [
        '#attributes' => [
          'class' => [
            $group_class . '-test',
            'js-hide',
          ],
        ],
      ];
      $form['tests'][$class]['title'] = [
        '#type' => 'label',
        '#title' => '\\' . $info['name'],
        '#wrapper_attributes' => [
          'class' => [
            'simpletest-test-label',
            'table-filter-text-source',
          ],
        ],
      ];
      $form['tests'][$class]['description'] = [
        '#prefix' => '<div class="description">',
        '#plain_text' => $info['description'],
        '#suffix' => '</div>',
        '#wrapper_attributes' => [
          'class' => [
            'simpletest-test-description',
            'table-filter-text-source',
          ],
        ],
      ];
    }
  }
  return $form;
}