comparison core/modules/simpletest/simpletest.module @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
401 401
402 /** 402 /**
403 * Implements callback_batch_operation(). 403 * Implements callback_batch_operation().
404 */ 404 */
405 function _simpletest_batch_operation($test_list_init, $test_id, &$context) { 405 function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
406 simpletest_classloader_register(); 406 \Drupal::service('test_discovery')->registerTestNamespaces();
407 // Get working values. 407 // Get working values.
408 if (!isset($context['sandbox']['max'])) { 408 if (!isset($context['sandbox']['max'])) {
409 // First iteration: initialize working values. 409 // First iteration: initialize working values.
410 $test_list = $test_list_init; 410 $test_list = $test_list_init;
411 $context['sandbox']['max'] = count($test_list); 411 $context['sandbox']['max'] = count($test_list);
473 /** 473 /**
474 * Implements callback_batch_finished(). 474 * Implements callback_batch_finished().
475 */ 475 */
476 function _simpletest_batch_finished($success, $results, $operations, $elapsed) { 476 function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
477 if ($success) { 477 if ($success) {
478 drupal_set_message(t('The test run finished in @elapsed.', ['@elapsed' => $elapsed])); 478 \Drupal::messenger()->addStatus(t('The test run finished in @elapsed.', ['@elapsed' => $elapsed]));
479 } 479 }
480 else { 480 else {
481 // Use the test_id passed as a parameter to _simpletest_batch_operation(). 481 // Use the test_id passed as a parameter to _simpletest_batch_operation().
482 $test_id = $operations[0][1][1]; 482 $test_id = $operations[0][1][1];
483 483
485 // class that was run from. Use the information to read the lgo file 485 // class that was run from. Use the information to read the lgo file
486 // in case any fatal errors caused the test to crash. 486 // in case any fatal errors caused the test to crash.
487 list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id); 487 list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
488 simpletest_log_read($test_id, $last_prefix, $last_test_class); 488 simpletest_log_read($test_id, $last_prefix, $last_test_class);
489 489
490 drupal_set_message(t('The test run did not successfully finish.'), 'error'); 490 \Drupal::messenger()->addError(t('The test run did not successfully finish.'));
491 drupal_set_message(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'), 'warning'); 491 \Drupal::messenger()->addWarning(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'));
492 } 492 }
493 \Drupal::moduleHandler()->invokeAll('test_group_finished'); 493 \Drupal::moduleHandler()->invokeAll('test_group_finished');
494 } 494 }
495 495
496 /** 496 /**
585 * @deprecated in Drupal 8.3.x, for removal before 9.0.0 release. Use 585 * @deprecated in Drupal 8.3.x, for removal before 9.0.0 release. Use
586 * \Drupal::service('test_discovery')->getTestClasses($extension, $types) 586 * \Drupal::service('test_discovery')->getTestClasses($extension, $types)
587 * instead. 587 * instead.
588 */ 588 */
589 function simpletest_test_get_all($extension = NULL, array $types = []) { 589 function simpletest_test_get_all($extension = NULL, array $types = []) {
590 @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);
590 return \Drupal::service('test_discovery')->getTestClasses($extension, $types); 591 return \Drupal::service('test_discovery')->getTestClasses($extension, $types);
591 } 592 }
592 593
593 /** 594 /**
594 * Registers test namespaces of all extensions and core test classes. 595 * Registers test namespaces of all extensions and core test classes.
595 * 596 *
596 * @deprecated in Drupal 8.3.x for removal before 9.0.0 release. Use 597 * @deprecated in Drupal 8.3.x for removal before 9.0.0 release. Use
597 * \Drupal::service('test_discovery')->registerTestNamespaces() instead. 598 * \Drupal::service('test_discovery')->registerTestNamespaces() instead.
598 */ 599 */
599 function simpletest_classloader_register() { 600 function simpletest_classloader_register() {
601 @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);
600 \Drupal::service('test_discovery')->registerTestNamespaces(); 602 \Drupal::service('test_discovery')->registerTestNamespaces();
601 } 603 }
602 604
603 /** 605 /**
604 * Generates a test file. 606 * Generates a test file.
654 function simpletest_clean_environment() { 656 function simpletest_clean_environment() {
655 simpletest_clean_database(); 657 simpletest_clean_database();
656 simpletest_clean_temporary_directories(); 658 simpletest_clean_temporary_directories();
657 if (\Drupal::config('simpletest.settings')->get('clear_results')) { 659 if (\Drupal::config('simpletest.settings')->get('clear_results')) {
658 $count = simpletest_clean_results_table(); 660 $count = simpletest_clean_results_table();
659 drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 test result.', 'Removed @count test results.')); 661 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 test result.', 'Removed @count test results.'));
660 } 662 }
661 else { 663 else {
662 drupal_set_message(t('Clear results is disabled and the test results table will not be cleared.'), 'warning'); 664 \Drupal::messenger()->addWarning(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
663 } 665 }
664
665 // Detect test classes that have been added, renamed or deleted.
666 \Drupal::cache()->delete('simpletest');
667 \Drupal::cache()->delete('simpletest_phpunit');
668 } 666 }
669 667
670 /** 668 /**
671 * Removes prefixed tables from the database from crashed tests. 669 * Removes prefixed tables from the database from crashed tests.
672 */ 670 */
673 function simpletest_clean_database() { 671 function simpletest_clean_database() {
672 $schema = Database::getConnection()->schema();
674 $tables = db_find_tables('test%'); 673 $tables = db_find_tables('test%');
675 $count = 0; 674 $count = 0;
676 foreach ($tables as $table) { 675 foreach ($tables as $table) {
677 // Only drop tables which begin wih 'test' followed by digits, for example, 676 // Only drop tables which begin wih 'test' followed by digits, for example,
678 // {test12345678node__body}. 677 // {test12345678node__body}.
679 if (preg_match('/^test\d+.*/', $table, $matches)) { 678 if (preg_match('/^test\d+.*/', $table, $matches)) {
680 db_drop_table($matches[0]); 679 $schema->dropTable($matches[0]);
681 $count++; 680 $count++;
682 } 681 }
683 } 682 }
684 683
685 if ($count > 0) { 684 if ($count > 0) {
686 drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.')); 685 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
687 } 686 }
688 else { 687 else {
689 drupal_set_message(t('No leftover tables to remove.')); 688 \Drupal::messenger()->addStatus(t('No leftover tables to remove.'));
690 } 689 }
691 } 690 }
692 691
693 /** 692 /**
694 * Finds all leftover temporary directories and removes them. 693 * Finds all leftover temporary directories and removes them.
707 } 706 }
708 } 707 }
709 } 708 }
710 709
711 if ($count > 0) { 710 if ($count > 0) {
712 drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.')); 711 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
713 } 712 }
714 else { 713 else {
715 drupal_set_message(t('No temporary directories to remove.')); 714 \Drupal::messenger()->addStatus(t('No temporary directories to remove.'));
716 } 715 }
717 } 716 }
718 717
719 /** 718 /**
720 * Clears the test result tables. 719 * Clears the test result tables.
854 $pass = FALSE; 853 $pass = FALSE;
855 } 854 }
856 855
857 $attributes = $testcase->attributes(); 856 $attributes = $testcase->attributes();
858 857
858 $function = $attributes->class . '->' . $attributes->name . '()';
859 $record = [ 859 $record = [
860 'test_id' => $test_id, 860 'test_id' => $test_id,
861 'test_class' => (string) $attributes->class, 861 'test_class' => (string) $attributes->class,
862 'status' => $pass ? 'pass' : 'fail', 862 'status' => $pass ? 'pass' : 'fail',
863 'message' => $message, 863 'message' => $message,
864 // @todo: Check on the proper values for this. 864 // @todo: Check on the proper values for this.
865 'message_group' => 'Other', 865 'message_group' => 'Other',
866 'function' => $attributes->class . '->' . $attributes->name . '()', 866 'function' => $function,
867 'line' => $attributes->line ?: 0, 867 'line' => $attributes->line ?: 0,
868 'file' => $attributes->file, 868 // There are situations when the file will not be present because a PHPUnit
869 // @requires has caused a test to be skipped.
870 'file' => $attributes->file ?: $function,
869 ]; 871 ];
870 return $record; 872 return $record;
871 } 873 }