class-wp-unittest-factory-for-blog.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Unit test factory for sites on a multisite network.
  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_Site 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_Blog extends WP_UnitTest_Factory_For_Thing {
  13. public function __construct( $factory = null ) {
  14. global $current_site, $base;
  15. parent::__construct( $factory );
  16. $this->default_generation_definitions = array(
  17. 'domain' => $current_site->domain,
  18. 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
  19. 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
  20. 'network_id' => $current_site->id,
  21. );
  22. }
  23. /**
  24. * Creates a site object.
  25. *
  26. * @param array $args Arguments for the site object.
  27. *
  28. * @return int|WP_Error The site ID on success, WP_Error object on failure.
  29. */
  30. public function create_object( $args ) {
  31. global $wpdb;
  32. // Map some arguments for backward compatibility with `wpmu_create_blog()` previously used here.
  33. if ( isset( $args['site_id'] ) ) {
  34. $args['network_id'] = $args['site_id'];
  35. unset( $args['site_id'] );
  36. }
  37. if ( isset( $args['meta'] ) ) {
  38. // The `$allowed_data_fields` matches the one used in `wpmu_create_blog()`.
  39. $allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
  40. foreach ( $args['meta'] as $key => $value ) {
  41. // Promote allowed keys to top-level arguments, add others to the options array.
  42. if ( in_array( $key, $allowed_data_fields, true ) ) {
  43. $args[ $key ] = $value;
  44. } else {
  45. $args['options'][ $key ] = $value;
  46. }
  47. }
  48. unset( $args['meta'] );
  49. }
  50. // Temporary tables will trigger DB errors when we attempt to reference them as new temporary tables.
  51. $suppress = $wpdb->suppress_errors();
  52. $blog = wp_insert_site( $args );
  53. $wpdb->suppress_errors( $suppress );
  54. // Tell WP we're done installing.
  55. wp_installing( false );
  56. return $blog;
  57. }
  58. /**
  59. * Updates a site object. Not implemented.
  60. *
  61. * @param int $blog_id ID of the site to update.
  62. * @param array $fields The fields to update.
  63. *
  64. * @return void
  65. */
  66. public function update_object( $blog_id, $fields ) {}
  67. /**
  68. * Retrieves a site by a given ID.
  69. *
  70. * @param int $blog_id ID of the site to retrieve.
  71. *
  72. * @return WP_Site|null The site object on success, null on failure.
  73. */
  74. public function get_object_by_id( $blog_id ) {
  75. return get_site( $blog_id );
  76. }
  77. }