factory.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. class WP_UnitTest_Factory {
  3. /**
  4. * @var WP_UnitTest_Factory_For_Post
  5. */
  6. public $post;
  7. /**
  8. * @var WP_UnitTest_Factory_For_Attachment
  9. */
  10. public $attachment;
  11. /**
  12. * @var WP_UnitTest_Factory_For_Comment
  13. */
  14. public $comment;
  15. /**
  16. * @var WP_UnitTest_Factory_For_User
  17. */
  18. public $user;
  19. /**
  20. * @var WP_UnitTest_Factory_For_Term
  21. */
  22. public $term;
  23. /**
  24. * @var WP_UnitTest_Factory_For_Term
  25. */
  26. public $category;
  27. /**
  28. * @var WP_UnitTest_Factory_For_Term
  29. */
  30. public $tag;
  31. /**
  32. * @var WP_UnitTest_Factory_For_Blog
  33. */
  34. public $blog;
  35. function __construct() {
  36. $this->post = new WP_UnitTest_Factory_For_Post( $this );
  37. $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
  38. $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
  39. $this->user = new WP_UnitTest_Factory_For_User( $this );
  40. $this->term = new WP_UnitTest_Factory_For_Term( $this );
  41. $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
  42. $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
  43. if ( is_multisite() )
  44. $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
  45. }
  46. }
  47. class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
  48. function __construct( $factory = null ) {
  49. parent::__construct( $factory );
  50. $this->default_generation_definitions = array(
  51. 'post_status' => 'publish',
  52. 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
  53. 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
  54. 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
  55. 'post_type' => 'post'
  56. );
  57. }
  58. function create_object( $args ) {
  59. return wp_insert_post( $args );
  60. }
  61. function update_object( $post_id, $fields ) {
  62. $fields['ID'] = $post_id;
  63. return wp_update_post( $fields );
  64. }
  65. function get_object_by_id( $post_id ) {
  66. return get_post( $post_id );
  67. }
  68. }
  69. class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
  70. function create_object( $file, $parent = 0, $args = array() ) {
  71. return wp_insert_attachment( $args, $file, $parent );
  72. }
  73. }
  74. class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
  75. function __construct( $factory = null ) {
  76. parent::__construct( $factory );
  77. $this->default_generation_definitions = array(
  78. 'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
  79. 'user_pass' => 'password',
  80. 'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
  81. );
  82. }
  83. function create_object( $args ) {
  84. return wp_insert_user( $args );
  85. }
  86. function update_object( $user_id, $fields ) {
  87. $fields['ID'] = $user_id;
  88. return wp_update_user( $fields );
  89. }
  90. function get_object_by_id( $user_id ) {
  91. return new WP_User( $user_id );
  92. }
  93. }
  94. class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
  95. function __construct( $factory = null ) {
  96. parent::__construct( $factory );
  97. $this->default_generation_definitions = array(
  98. 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
  99. 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
  100. 'comment_approved' => 1,
  101. 'comment_content' => 'This is a comment'
  102. );
  103. }
  104. function create_object( $args ) {
  105. return wp_insert_comment( $this->addslashes_deep( $args ) );
  106. }
  107. function update_object( $comment_id, $fields ) {
  108. $fields['comment_ID'] = $comment_id;
  109. return wp_update_comment( $this->addslashes_deep( $fields ) );
  110. }
  111. function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
  112. $args['comment_post_ID'] = $post_id;
  113. return $this->create_many( $count, $args, $generation_definitions );
  114. }
  115. function get_object_by_id( $comment_id ) {
  116. return get_comment( $comment_id );
  117. }
  118. }
  119. class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
  120. function __construct( $factory = null ) {
  121. global $current_site, $base;
  122. parent::__construct( $factory );
  123. $this->default_generation_definitions = array(
  124. 'domain' => $current_site->domain,
  125. 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
  126. 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
  127. 'site_id' => $current_site->id,
  128. );
  129. }
  130. function create_object( $args ) {
  131. global $wpdb;
  132. $meta = isset( $args['meta'] ) ? $args['meta'] : array();
  133. $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
  134. // temp tables will trigger db errors when we attempt to reference them as new temp tables
  135. $suppress = $wpdb->suppress_errors();
  136. $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
  137. $wpdb->suppress_errors( $suppress );
  138. return $blog;
  139. }
  140. function update_object( $blog_id, $fields ) {}
  141. function get_object_by_id( $blog_id ) {
  142. return get_blog_details( $blog_id, false );
  143. }
  144. }
  145. class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
  146. private $taxonomy;
  147. const DEFAULT_TAXONOMY = 'post_tag';
  148. function __construct( $factory = null, $taxonomy = null ) {
  149. parent::__construct( $factory );
  150. $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
  151. $this->default_generation_definitions = array(
  152. 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
  153. 'taxonomy' => $this->taxonomy,
  154. 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
  155. );
  156. }
  157. function create_object( $args ) {
  158. $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
  159. $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
  160. if ( is_wp_error( $term_id_pair ) )
  161. return $term_id_pair;
  162. return $term_id_pair['term_id'];
  163. }
  164. function update_object( $term, $fields ) {
  165. $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
  166. if ( is_object( $term ) )
  167. $taxonomy = $term->taxonomy;
  168. $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
  169. return $term_id_pair['term_id'];
  170. }
  171. function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
  172. return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
  173. }
  174. function get_object_by_id( $term_id ) {
  175. return get_term( $term_id, $this->taxonomy );
  176. }
  177. }
  178. abstract class WP_UnitTest_Factory_For_Thing {
  179. var $default_generation_definitions;
  180. var $factory;
  181. /**
  182. * Creates a new factory, which will create objects of a specific Thing
  183. *
  184. * @param object $factory Global factory that can be used to create other objects on the system
  185. * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
  186. * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
  187. * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
  188. */
  189. function __construct( $factory, $default_generation_definitions = array() ) {
  190. $this->factory = $factory;
  191. $this->default_generation_definitions = $default_generation_definitions;
  192. }
  193. abstract function create_object( $args );
  194. abstract function update_object( $object, $fields );
  195. function create( $args = array(), $generation_definitions = null ) {
  196. if ( is_null( $generation_definitions ) )
  197. $generation_definitions = $this->default_generation_definitions;
  198. $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
  199. $created = $this->create_object( $generated_args );
  200. if ( !$created || is_wp_error( $created ) )
  201. return $created;
  202. if ( $callbacks ) {
  203. $updated_fields = $this->apply_callbacks( $callbacks, $created );
  204. $save_result = $this->update_object( $created, $updated_fields );
  205. if ( !$save_result || is_wp_error( $save_result ) )
  206. return $save_result;
  207. }
  208. return $created;
  209. }
  210. function create_and_get( $args = array(), $generation_definitions = null ) {
  211. $object_id = $this->create( $args, $generation_definitions );
  212. return $this->get_object_by_id( $object_id );
  213. }
  214. abstract function get_object_by_id( $object_id );
  215. function create_many( $count, $args = array(), $generation_definitions = null ) {
  216. $results = array();
  217. for ( $i = 0; $i < $count; $i++ ) {
  218. $results[] = $this->create( $args, $generation_definitions );
  219. }
  220. return $results;
  221. }
  222. function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
  223. $callbacks = array();
  224. if ( is_null( $generation_definitions ) )
  225. $generation_definitions = $this->default_generation_definitions;
  226. foreach( array_keys( $generation_definitions ) as $field_name ) {
  227. if ( !isset( $args[$field_name] ) ) {
  228. $generator = $generation_definitions[$field_name];
  229. if ( is_scalar( $generator ) )
  230. $args[$field_name] = $generator;
  231. elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
  232. $callbacks[$field_name] = $generator;
  233. } elseif ( is_object( $generator ) )
  234. $args[$field_name] = $generator->next();
  235. else
  236. return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
  237. }
  238. }
  239. return $args;
  240. }
  241. function apply_callbacks( $callbacks, $created ) {
  242. $updated_fields = array();
  243. foreach( $callbacks as $field_name => $generator ) {
  244. $updated_fields[$field_name] = $generator->call( $created );
  245. }
  246. return $updated_fields;
  247. }
  248. function callback( $function ) {
  249. return new WP_UnitTest_Factory_Callback_After_Create( $function );
  250. }
  251. function addslashes_deep($value) {
  252. if ( is_array( $value ) ) {
  253. $value = array_map( array( $this, 'addslashes_deep' ), $value );
  254. } elseif ( is_object( $value ) ) {
  255. $vars = get_object_vars( $value );
  256. foreach ($vars as $key=>$data) {
  257. $value->{$key} = $this->addslashes_deep( $data );
  258. }
  259. } elseif ( is_string( $value ) ) {
  260. $value = addslashes( $value );
  261. }
  262. return $value;
  263. }
  264. }
  265. class WP_UnitTest_Generator_Sequence {
  266. var $next;
  267. var $template_string;
  268. function __construct( $template_string = '%s', $start = 1 ) {
  269. $this->next = $start;
  270. $this->template_string = $template_string;
  271. }
  272. function next() {
  273. $generated = sprintf( $this->template_string , $this->next );
  274. $this->next++;
  275. return $generated;
  276. }
  277. }
  278. class WP_UnitTest_Factory_Callback_After_Create {
  279. var $callback;
  280. function __construct( $callback ) {
  281. $this->callback = $callback;
  282. }
  283. function call( $object ) {
  284. return call_user_func( $this->callback, $object );
  285. }
  286. }