class-wp-unittest-factory-for-user.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Unit test factory for users.
  4. *
  5. * Note: The below @method notations are defined solely for the benefit of IDEs,
  6. * as a way to indicate expected return values from the given factory methods.
  7. *
  8. * @method int create( $args = array(), $generation_definitions = null )
  9. * @method WP_User create_and_get( $args = array(), $generation_definitions = null )
  10. * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
  11. */
  12. class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
  13. public function __construct( $factory = null ) {
  14. parent::__construct( $factory );
  15. $this->default_generation_definitions = array(
  16. 'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
  17. 'user_pass' => 'password',
  18. 'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
  19. );
  20. }
  21. /**
  22. * Inserts an user.
  23. *
  24. * @param array $args The user data to insert.
  25. *
  26. * @return int|WP_Error The user ID on success, WP_Error object on failure.
  27. */
  28. public function create_object( $args ) {
  29. return wp_insert_user( $args );
  30. }
  31. /**
  32. * Updates the user data.
  33. *
  34. * @param int $user_id ID of the user to update.
  35. * @param array $fields The user data to update.
  36. *
  37. * @return int|WP_Error The user ID on success, WP_Error object on failure.
  38. */
  39. public function update_object( $user_id, $fields ) {
  40. $fields['ID'] = $user_id;
  41. return wp_update_user( $fields );
  42. }
  43. /**
  44. * Retrieves the user for a given ID.
  45. *
  46. * @param int $user_id ID of the user ID to retrieve.
  47. *
  48. * @return WP_User The user object.
  49. */
  50. public function get_object_by_id( $user_id ) {
  51. return new WP_User( $user_id );
  52. }
  53. }