Pull merge.
[yaffs-website] / web / core / modules / simpletest / simpletest.module
index 7d72aa44bcd55fa88bb096e5ec29da8356d07bfa..35fdd92cc19d1e4fb67ad894ca593cfcf7c37347 100644 (file)
@@ -13,6 +13,7 @@ use Drupal\simpletest\TestBase;
 use Drupal\Core\Test\TestDatabase;
 use Drupal\simpletest\TestDiscovery;
 use Drupal\Tests\Listeners\SimpletestUiPrinter;
+use PHPUnit\Framework\TestCase;
 use Symfony\Component\Process\PhpExecutableFinder;
 use Drupal\Core\Test\TestStatus;
 
@@ -284,8 +285,15 @@ function simpletest_phpunit_xml_filepath($test_id) {
  *
  * @return string
  *   The path to core's phpunit.xml.dist configuration file.
+ *
+ * @deprecated in Drupal 8.4.x for removal before Drupal 9.0.0. PHPUnit test
+ *   runners should change directory into core/ and then run the phpunit tool.
+ *   See simpletest_phpunit_run_command() for an example.
+ *
+ * @see simpletest_phpunit_run_command()
  */
 function simpletest_phpunit_configuration_filepath() {
+  @trigger_error('The ' . __FUNCTION__ . ' function is deprecated since version 8.4.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
   return \Drupal::root() . '/core/phpunit.xml.dist';
 }
 
@@ -337,7 +345,7 @@ function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpun
   }
   else {
     // Double escape namespaces so they'll work in a regexp.
-    $escaped_test_classnames = array_map(function($class) {
+    $escaped_test_classnames = array_map(function ($class) {
       return addslashes($class);
     }, $unescaped_test_classnames);
 
@@ -355,7 +363,7 @@ function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpun
 
   // exec in a subshell so that the environment is isolated when running tests
   // via the simpletest UI.
-  $ret = exec(join($command, " "), $output, $status);
+  $ret = exec(implode(" ", $command), $output, $status);
 
   chdir($old_cwd);
   putenv('SIMPLETEST_DB=');
@@ -381,12 +389,12 @@ function simpletest_phpunit_command() {
 
   // The file in Composer's bin dir is a *nix link, which does not work when
   // extracted from a tarball and generally not on Windows.
-  $command = $vendor_dir . '/phpunit/phpunit/phpunit';
+  $command = escapeshellarg($vendor_dir . '/phpunit/phpunit/phpunit');
   if (substr(PHP_OS, 0, 3) == 'WIN') {
     // On Windows it is necessary to run the script using the PHP executable.
     $php_executable_finder = new PhpExecutableFinder();
     $php = $php_executable_finder->find();
-    $command = $php . ' -f ' . escapeshellarg($command) . ' --';
+    $command = $php . ' -f ' . $command . ' --';
   }
   return $command;
 }
@@ -395,7 +403,7 @@ function simpletest_phpunit_command() {
  * Implements callback_batch_operation().
  */
 function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
-  simpletest_classloader_register();
+  \Drupal::service('test_discovery')->registerTestNamespaces();
   // Get working values.
   if (!isset($context['sandbox']['max'])) {
     // First iteration: initialize working values.
@@ -412,7 +420,7 @@ function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
 
   // Perform the next test.
   $test_class = array_shift($test_list);
-  if (is_subclass_of($test_class, \PHPUnit_Framework_TestCase::class)) {
+  if (is_subclass_of($test_class, TestCase::class)) {
     $phpunit_results = simpletest_run_phpunit_tests($test_id, [$test_class]);
     simpletest_process_phpunit_results($phpunit_results);
     $test_results[$test_class] = simpletest_summarize_phpunit_result($phpunit_results)[$test_class];
@@ -437,20 +445,20 @@ function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
       '#theme' => 'simpletest_result_summary',
       '#label' => t($test_results[$class]['#name'] . ':'),
     ];
-    array_unshift($items, drupal_render($class_test_result));
+    array_unshift($items, \Drupal::service('renderer')->render($class_test_result));
   }
   $context['message'] = t('Processed test @num of @max - %test.', ['%test' => $info['name'], '@num' => $max - $size, '@max' => $max]);
   $overall_results = $test_results + [
     '#theme' => 'simpletest_result_summary',
     '#label' => t('Overall results:'),
   ];
-  $context['message'] .= drupal_render($overall_results);
+  $context['message'] .= \Drupal::service('renderer')->render($overall_results);
 
   $item_list = [
     '#theme' => 'item_list',
     '#items' => $items,
   ];
-  $context['message'] .= drupal_render($item_list);
+  $context['message'] .= \Drupal::service('renderer')->render($item_list);
 
   // Save working values for the next iteration.
   $context['sandbox']['tests'] = $test_list;
@@ -467,7 +475,7 @@ function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
  */
 function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
   if ($success) {
-    drupal_set_message(t('The test run finished in @elapsed.', ['@elapsed' => $elapsed]));
+    \Drupal::messenger()->addStatus(t('The test run finished in @elapsed.', ['@elapsed' => $elapsed]));
   }
   else {
     // Use the test_id passed as a parameter to _simpletest_batch_operation().
@@ -479,8 +487,8 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
     list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
     simpletest_log_read($test_id, $last_prefix, $last_test_class);
 
-    drupal_set_message(t('The test run did not successfully finish.'), 'error');
-    drupal_set_message(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'), 'warning');
+    \Drupal::messenger()->addError(t('The test run did not successfully finish.'));
+    \Drupal::messenger()->addWarning(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'));
   }
   \Drupal::moduleHandler()->invokeAll('test_group_finished');
 }
@@ -579,6 +587,7 @@ function simpletest_log_read($test_id, $database_prefix, $test_class) {
  *   instead.
  */
 function simpletest_test_get_all($extension = NULL, array $types = []) {
+  @trigger_error('The ' . __FUNCTION__ . ' function is deprecated in version 8.3.x and will be removed in 9.0.0. Use \Drupal::service(\'test_discovery\')->getTestClasses($extension, $types) instead.', E_USER_DEPRECATED);
   return \Drupal::service('test_discovery')->getTestClasses($extension, $types);
 }
 
@@ -589,6 +598,7 @@ function simpletest_test_get_all($extension = NULL, array $types = []) {
  *   \Drupal::service('test_discovery')->registerTestNamespaces() instead.
  */
 function simpletest_classloader_register() {
+  @trigger_error('The ' . __FUNCTION__ . ' function is deprecated in version 8.3.x and will be removed in 9.0.0. Use \Drupal::service(\'test_discovery\')->registerTestNamespaces() instead.', E_USER_DEPRECATED);
   \Drupal::service('test_discovery')->registerTestNamespaces();
 }
 
@@ -648,37 +658,34 @@ function simpletest_clean_environment() {
   simpletest_clean_temporary_directories();
   if (\Drupal::config('simpletest.settings')->get('clear_results')) {
     $count = simpletest_clean_results_table();
-    drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 test result.', 'Removed @count test results.'));
+    \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 test result.', 'Removed @count test results.'));
   }
   else {
-    drupal_set_message(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
+    \Drupal::messenger()->addWarning(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
   }
-
-  // Detect test classes that have been added, renamed or deleted.
-  \Drupal::cache()->delete('simpletest');
-  \Drupal::cache()->delete('simpletest_phpunit');
 }
 
 /**
  * Removes prefixed tables from the database from crashed tests.
  */
 function simpletest_clean_database() {
+  $schema = Database::getConnection()->schema();
   $tables = db_find_tables('test%');
   $count = 0;
   foreach ($tables as $table) {
     // Only drop tables which begin wih 'test' followed by digits, for example,
     // {test12345678node__body}.
     if (preg_match('/^test\d+.*/', $table, $matches)) {
-      db_drop_table($matches[0]);
+      $schema->dropTable($matches[0]);
       $count++;
     }
   }
 
   if ($count > 0) {
-    drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
+    \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
   }
   else {
-    drupal_set_message(t('No leftover tables to remove.'));
+    \Drupal::messenger()->addStatus(t('No leftover tables to remove.'));
   }
 }
 
@@ -701,10 +708,10 @@ function simpletest_clean_temporary_directories() {
   }
 
   if ($count > 0) {
-    drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
+    \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
   }
   else {
-    drupal_set_message(t('No temporary directories to remove.'));
+    \Drupal::messenger()->addStatus(t('No temporary directories to remove.'));
   }
 }
 
@@ -764,9 +771,10 @@ function simpletest_mail_alter(&$message) {
  * @param $phpunit_xml_file
  *   Path to the PHPUnit XML file.
  *
- * @return array[]
+ * @return array[]|null
  *   The results as array of rows in a format that can be inserted into
- *   {simpletest}.
+ *   {simpletest}. If the phpunit_xml_file does not have any contents then the
+ *   function will return NULL.
  */
 function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
   $contents = @file_get_contents($phpunit_xml_file);
@@ -786,7 +794,7 @@ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
  *
  * @param \SimpleXMLElement $element
  *   The PHPUnit xml to search for test cases.
- * @param \SimpleXMLElement $suite
+ * @param \SimpleXMLElement $parent
  *   (Optional) The parent of the current element. Defaults to NULL.
  *
  * @return array
@@ -847,6 +855,7 @@ function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcas
 
   $attributes = $testcase->attributes();
 
+  $function = $attributes->class . '->' . $attributes->name . '()';
   $record = [
     'test_id' => $test_id,
     'test_class' => (string) $attributes->class,
@@ -854,9 +863,11 @@ function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcas
     'message' => $message,
     // @todo: Check on the proper values for this.
     'message_group' => 'Other',
-    'function' => $attributes->class . '->' . $attributes->name . '()',
+    'function' => $function,
     'line' => $attributes->line ?: 0,
-    'file' => $attributes->file,
+    // There are situations when the file will not be present because a PHPUnit
+    // @requires has caused a test to be skipped.
+    'file' => $attributes->file ?: $function,
   ];
   return $record;
 }