/** * {@inheritdoc} */ protected function generateElements(array $values) { $num = $values['num']; $kill = $values['kill']; $pass = $values['pass']; $age = $values['time_range']; $roles = $values['roles']; $url = parse_url($GLOBALS['base_url']); if ($kill) { $uids = db_select('users', 'u')->fields('u', array('uid'))->condition('uid', 1, '>')->execute()->fetchAllAssoc('uid'); user_delete_multiple(array_keys($uids)); $this->setMessage(\Drupal::translation()->formatPlural(count($uids), '1 user deleted', '@count users deleted.')); } if ($num > 0) { $names = array(); while (count($names) < $num) { //@todo add suport for devel_generate_word(mt_rand(6, 12)) in a class method $name = $this->generateWord(mt_rand(6, 12)); $names[$name] = ''; } if (empty($roles)) { $roles = array(DRUPAL_AUTHENTICATED_RID); } foreach ($names as $name => $value) { $edit = array('uid' => NULL, 'name' => $name, 'pass' => $pass, 'mail' => $name . '@example.com', 'status' => 1, 'created' => REQUEST_TIME - mt_rand(0, $age), 'roles' => array_combine($roles, $roles), 'devel_generate' => TRUE); $account = entity_create('user', $edit); // Populate all fields with sample values. $this->populateFields($account); $account->save(); } } $this->setMessage(t('!num_users created.', array('!num_users' => format_plural($num, '1 user', '@count users')))); }
/** * Test deleting multiple users. */ function testUserDeleteMultiple() { // Create a few users with permissions, so roles will be created. $user_a = $this->drupalCreateUser(array('access user profiles')); $user_b = $this->drupalCreateUser(array('access user profiles')); $user_c = $this->drupalCreateUser(array('access user profiles')); $uids = array($user_a->id(), $user_b->id(), $user_c->id()); // These users should have a role $query = db_select('user__roles', 'r'); $roles_created = $query->fields('r', array('entity_id'))->condition('entity_id', $uids, 'IN')->countQuery()->execute()->fetchField(); $this->assertTrue($roles_created > 0, 'Role assignments created for new users and deletion of role assignments can be tested'); // We should be able to load one of the users. $this->assertTrue(User::load($user_a->id()), 'User is created and deletion of user can be tested'); // Delete the users. user_delete_multiple($uids); // Test if the roles assignments are deleted. $query = db_select('user__roles', 'r'); $roles_after_deletion = $query->fields('r', array('entity_id'))->condition('entity_id', $uids, 'IN')->countQuery()->execute()->fetchField(); $this->assertTrue($roles_after_deletion == 0, 'Role assignments deleted along with users'); // Test if the users are deleted, User::load() will return NULL. $this->assertNull(User::load($user_a->id()), format_string('User with id @uid deleted.', array('@uid' => $user_a->id()))); $this->assertNull(User::load($user_b->id()), format_string('User with id @uid deleted.', array('@uid' => $user_b->id()))); $this->assertNull(User::load($user_c->id()), format_string('User with id @uid deleted.', array('@uid' => $user_c->id()))); }
/** * {@inheritdoc} */ public function delete(array $entities) { return user_delete_multiple(array_map(function (UserInterface $entity) { return $entity->id(); }, $entities)); }
/** * Delete entities. This is a copy of entity_delete_multiple() function in * entity.module since entity module may not be present. * * @param string $entity_type * Entity type. * @param int $min_entity_id * Minimum entity id over which all entities will be deleted. * * @return bool * TRUE if entities got deleted and FALSE otherwise. */ public static function deleteEntities($entity_type, $min_entity_id) { $query = new \EntityFieldQuery(); $results = $query->entityCondition('entity_type', $entity_type)->entityCondition('entity_id', $min_entity_id, '>')->execute(); if (isset($results[$entity_type])) { $entity_ids = array_keys($results[$entity_type]); $info = entity_get_info($entity_type); if (isset($info['deletion callback'])) { foreach ($entity_ids as $id) { $info['deletion callback']($id); } } elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) { entity_get_controller($entity_type)->delete($entity_ids); } else { if ($entity_type == 'node') { node_delete_multiple($entity_ids); } elseif ($entity_type == 'user') { user_delete_multiple($entity_ids); } elseif ($entity_type == 'taxonomy_term') { foreach ($entity_ids as $entity_id) { taxonomy_term_delete($entity_id); } } elseif ($entity_type == 'comment') { foreach ($entity_ids as $entity_id) { comment_delete($entity_id); } } return FALSE; } } }
// Trucate tables that need to truncated plain and simple // Get the current schema, order it by table name. /* $schema = drupal_get_schema(); ksort($schema); foreach ($schema as $table => $data) { if (substr($table, 0, 5) == 'cache' || $table == 'contact_importer_log' || $table == 'feeds_item' || $table == 'feeds_log' || $table == 'feeds_source' || $table == 'flood' || $table == 'history' || $table == 'sessions' || $table == 'watchdog') { //print ("delete table = $table \n"); //db_trucate("$table"); db_query("truncate table $table"); } } */ /* drop table authmap_bak; drop table role_bak; drop table sessions_bak; drop table users_bak; */ // Pick all users not in the role 'Test Users' and delete them $query = "select uid from {users} where uid not in (0,1,90,1815,2093,2129,2136,3743,19735,19933,157606) limit 0,2000"; $result = db_query($query); $users_to_delete = array(); foreach ($result as $record) { array_push($users_to_delete, $record->uid); } user_delete_multiple($users_to_delete); // Delete subscriptions info of all ANON users // All AUTH users not in 'Test Users' role are alrady dead