diff --git a/core/modules/dblog/tests/src/Functional/DbLogTest.php b/core/modules/dblog/tests/src/Functional/DbLogTest.php index 0756315ee5..878b809655 100644 --- a/core/modules/dblog/tests/src/Functional/DbLogTest.php +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php @@ -738,7 +738,7 @@ public function testTemporaryUser() { $this->assertText('Dblog test log message'); // Delete the user. - user_delete($tempuser->id()); + $tempuser->delete(); $this->drupalGet('user/' . $tempuser_uid); $this->assertResponse(404); diff --git a/core/modules/user/tests/src/Kernel/UserDeleteTest.php b/core/modules/user/tests/src/Kernel/UserDeleteTest.php index a015d70483..928c095ec2 100644 --- a/core/modules/user/tests/src/Kernel/UserDeleteTest.php +++ b/core/modules/user/tests/src/Kernel/UserDeleteTest.php @@ -53,7 +53,9 @@ public function testUserDeleteMultiple() { // We should be able to load one of the users. $this->assertNotNull(User::load($user_a->id())); // Delete the users. - user_delete_multiple($uids); + $storage = $this->container->get('entity_type.manager')->getStorage('user'); + $users = $storage->loadMultiple($uids); + $storage->delete($users); // Test if the roles assignments are deleted. $query = $connection->select('user__roles', 'r'); $roles_after_deletion = $query diff --git a/core/modules/user/tests/src/Kernel/UserLegacyTest.php b/core/modules/user/tests/src/Kernel/UserLegacyTest.php index cdf4e9751f..46abae4aa4 100644 --- a/core/modules/user/tests/src/Kernel/UserLegacyTest.php +++ b/core/modules/user/tests/src/Kernel/UserLegacyTest.php @@ -27,6 +27,7 @@ class UserLegacyTest extends KernelTestBase { protected function setUp() { parent::setUp(); $this->installEntitySchema('user'); + $this->installSchema('user', ['users_data']); } /** @@ -59,4 +60,28 @@ public function testUserView() { $this->assertEquals(4, count(user_view_multiple($entities))); } + /** + * Tests that user_delete throws a deprecation error. + * + * @expectedDeprecation user_delete() is deprecated in drupal:8.8.0. Use the user entity's delete method to delete the user. See https://www.drupal.org/node/3051463 + */ + public function testUserDelete() { + User::create(['name' => 'foo', 'uid' => 10])->save(); + user_delete(10); + $this->assert(NULL === User::load(10), "User has been deleted"); + } + + /** + * Tests that user_delete_multiple throws a deprecation error. + * + * @expectedDeprecation user_delete_multiple() is deprecated in drupal:8.8.0. Use the entity storage system to delete the users. See https://www.drupal.org/node/3051463 + */ + public function testUserDeleteMultiple() { + User::create(['name' => 'foo', 'uid' => 10])->save(); + User::create(['name' => 'bar', 'uid' => 11])->save(); + user_delete_multiple([10, 11]); + $this->assert(NULL === User::load(10), "User 10 has been deleted"); + $this->assert(NULL === User::load(11), "User 11 has been deleted"); + } + } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 2afbee3660..772c4e23b9 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -864,8 +864,17 @@ function user_cancel_methods() { * * @param int $uid * A user ID. + * + * @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use the user + * entity's delete method to delete the user. + * @code + * Drupal::entityTypeManager->getStorage('user')->load($uid)->delete(); + * @endcode + * + * @see https://www.drupal.org/node/3051463 */ function user_delete($uid) { + @trigger_error("user_delete() is deprecated in drupal:8.8.0. Use the user entity's delete method to delete the user. See https://www.drupal.org/node/3051463", E_USER_DEPRECATED); user_delete_multiple([$uid]); } @@ -875,13 +884,19 @@ function user_delete($uid) { * @param int[] $uids * An array of user IDs. * - * @see hook_ENTITY_TYPE_predelete() - * @see hook_ENTITY_TYPE_delete() + * @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use the + * entity storage system to delete the users. + * @code + * $storage_handler = \Drupal::entityTypeManager()->getStorage('user'); + * $users = $storage_handler->loadMultiple($uids); + * $storage_handler->delete($users); + * @endcode + * + * @see https://www.drupal.org/node/3051463 */ function user_delete_multiple(array $uids) { - $user_storage = \Drupal::entityTypeManager()->getStorage('user'); - $users = $user_storage->loadMultiple($uids); - $user_storage->delete($users); + @trigger_error("user_delete_multiple() is deprecated in drupal:8.8.0. Use the entity storage system to delete the users. See https://www.drupal.org/node/3051463", E_USER_DEPRECATED); + entity_delete_multiple('user', $uids); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php index df9880714f..a3566fdd6c 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php @@ -524,7 +524,7 @@ public function testUserHooks() { ]); $GLOBALS['entity_crud_hook_test'] = []; - user_delete($account->id()); + $account->delete(); $this->assertHookMessageOrder([ 'entity_crud_hook_test_user_predelete called', diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php index a472facc98..ca502d4425 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php @@ -34,6 +34,21 @@ protected function setUp() { $this->installEntitySchema('entity_test_rev'); } + /** + * Tests that entity_delete_multiple triggers an error. + * + * @expectedDeprecation entity_delete_multiple is deprecated in drupal:8.0.0 and will be removed in drupal:9.0.0. Use the entity storage's delete() method to delete multiple entities. @see https://www.drupal.org/node/3051072 + * + * @throws \Drupal\Core\Entity\EntityStorageException + */ + public function testEntityDeleteMultiple() { + EntityTest::create(['name' => 'published entity'])->save(); + EntityTest::create(['name' => 'published entity'])->save(); + $this->assertCount(2, \Drupal::entityTypeManager()->getStorage('entity_test')->loadMultiple()); + entity_delete_multiple('entity_test', [1, 2]); + $this->assertCount(0, \Drupal::entityTypeManager()->getStorage('entity_test')->loadMultiple()); + } + /** * @expectedDeprecation entity_load_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's loadMultiple() method. See https://www.drupal.org/node/2266845 * @expectedDeprecation entity_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's load() method. See https://www.drupal.org/node/2266845 @@ -121,19 +136,4 @@ public function testEntityView() { $this->assertEquals(4, count(entity_view_multiple($entities, 'default'))); } - /** - * Tests that entity_delete_multiple triggers an error. - * - * @expectedDeprecation entity_delete_multiple is deprecated in drupal:8.0.0 and will be removed in drupal:9.0.0. Use the entity storage's delete() method to delete multiple entities. @see https://www.drupal.org/node/3051072 - * - * @throws \Drupal\Core\Entity\EntityStorageException - */ - public function testEntityDeleteMultiple() { - EntityTest::create(['name' => 'published entity'])->save(); - EntityTest::create(['name' => 'published entity'])->save(); - $this->assertCount(2, \Drupal::entityTypeManager()->getStorage('entity_test')->loadMultiple()); - entity_delete_multiple('entity_test', [1, 2]); - $this->assertCount(0, \Drupal::entityTypeManager()->getStorage('entity_test')->loadMultiple()); - } - }