diff --git a/core/modules/simpletest/simpletest.api.php b/core/modules/simpletest/simpletest.api.php index d4397de3d8..9b9014e5ad 100644 --- a/core/modules/simpletest/simpletest.api.php +++ b/core/modules/simpletest/simpletest.api.php @@ -40,7 +40,7 @@ function hook_simpletest_alter(&$groups) { * This hook is only invoked by the Simpletest UI form runner. It will not be * invoked by run-tests.sh or the phpunit tool. * - * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Convert + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Convert * your test to a PHPUnit-based one and implement test listeners. * * @see https://www.drupal.org/node/2934242 @@ -56,7 +56,7 @@ function hook_test_group_started() { * This hook is only invoked by the Simpletest UI form runner. It will not be * invoked by run-tests.sh or the phpunit tool. * - * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Convert + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Convert * your test to a PHPUnit-based one and implement test listeners. * * @see https://www.drupal.org/node/2934242 @@ -78,7 +78,7 @@ function hook_test_group_finished() { * * @see \Drupal\simpletest\WebTestBase::results() * - * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Convert + * @deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Convert * your test to a PHPUnit-based one and implement test listeners. * * @see https://www.drupal.org/node/2934242 diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index be417175e9..d0fb80579c 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -138,7 +138,9 @@ function simpletest_run_tests($test_list) { unset($test_list['phpunit']); } - $test_id = db_insert('simpletest_test_id') + // Use the database service so that we can inject a database for testing this + // function. + $test_id = \Drupal::database()->insert('simpletest_test_id') ->useDefaults(['test_id']) ->execute(); diff --git a/core/modules/simpletest/tests/modules/simpletest_deprecation_test/src/Tests/InnocuousTest.php b/core/modules/simpletest/tests/modules/simpletest_deprecation_test/src/Tests/InnocuousTest.php new file mode 100644 index 0000000000..0a3b3f8f75 --- /dev/null +++ b/core/modules/simpletest/tests/modules/simpletest_deprecation_test/src/Tests/InnocuousTest.php @@ -0,0 +1,38 @@ +getMockBuilder(Insert::class) + ->disableOriginalConstructor() + ->setMethods(['execute', 'useDefaults']) + ->getMock(); + $insert->expects($this->any()) + ->method('useDefaults') + ->willReturn($insert); + $insert->expects($this->any()) + ->method('execute') + // Return an arbitrary test ID. + ->willReturn(__METHOD__); + + $connection = $this->getMockBuilder(Connection::class) + ->disableOriginalConstructor() + ->setMethods(['insert']) + ->getMockForAbstractClass(); + $connection->expects($this->once()) + ->method('insert') + ->willReturn($insert); + + // Mock public stream wrapper enough for simpletest_run_tests(). + $public = $this->getMockBuilder(PublicStream::class) + ->disableOriginalConstructor() + // Mock all methods to do nothing and return NULL. + ->setMethods([]) + ->getMock(); + + // Set up the container. + $this->container->set('database', $connection); + $this->container->set('stream_wrapper.public', $public); + + // Make sure our mocked database is in use by expecting a test ID that is + // __METHOD__. + $this->assertEquals(__METHOD__, simpletest_run_tests([static::class])); + } + + /** + * @expectedDeprecation The deprecated hook hook_test_finished() is implemented in these functions: simpletest_deprecation_test_test_finished(). Convert your test to a PHPUnit-based one and implement test listeners. See https://www.drupal.org/node/2934242 + */ + public function testHookTestFinished() { + // Mock test_discovery. + $discovery = $this->getMockBuilder(TestDiscovery::class) + ->disableOriginalConstructor() + ->setMethods(['registerTestNamespaces']) + ->getMock(); + $discovery->expects($this->any()) + ->method('registerTestNamespaces') + ->willReturn([]); + + // Mock renderer. + $renderer = $this->getMockBuilder(Renderer::class) + ->disableOriginalConstructor() + ->setMethods(['render']) + ->getMock(); + // We don't care what the rendered batch elements look like. + $renderer->expects($this->any()) + ->method('render') + ->willReturn(''); + + // Set up the container. + $this->container->set('test_discovery', $discovery); + $this->container->set('renderer', $renderer); + + // A mock batch. + $context = []; + + // InnocuousTest is a WebTestBase test class which passes and never touches + // the database. + _simpletest_batch_operation([InnocuousTest::class], __METHOD__, $context); + } + }