DatabaseSeedersTable.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Tests\DatabaseSeeders;
  3. use StatonLab\TripalTestSuite\Database\Seeder;
  4. class UsersTableSeeder extends Seeder
  5. {
  6. /**
  7. * Whether to run the seeder automatically before
  8. * starting our tests and destruct them automatically
  9. * once the tests are completed.
  10. *
  11. * If you set this to false, you can run the seeder
  12. * from your tests directly using UsersTableSeeder::seed()
  13. * which returns an instance of the class the you can use
  14. * to run the down() method whenever required.
  15. *
  16. * @var bool
  17. */
  18. public $auto_run = true;
  19. /**
  20. * The users that got created.
  21. * We save this here to have them easily deleted
  22. * in the down() method.
  23. *
  24. * @var array
  25. */
  26. protected $users = [];
  27. /**
  28. * Seeds the database with users.
  29. */
  30. public function up()
  31. {
  32. $new_user = [
  33. 'name' => 'test user',
  34. 'pass' => 'secret',
  35. 'mail' => 'test@example.com',
  36. 'status' => 1,
  37. 'init' => 'Email',
  38. 'roles' => [
  39. DRUPAL_AUTHENTICATED_RID => 'authenticated user',
  40. ],
  41. ];
  42. // The first parameter is sent blank so a new user is created.
  43. $this->users[] = user_save(new \stdClass(), $new_user);
  44. }
  45. /**
  46. * Cleans up the database from the created users.
  47. */
  48. public function down()
  49. {
  50. foreach ($this->users as $user) {
  51. user_delete($user->uid);
  52. }
  53. }
  54. }