Ticket #44521: 44521.diff
File 44521.diff, 15.1 KB (added by , 7 years ago) |
---|
-
tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php
3 3 class WP_UnitTest_Factory_Callback_After_Create { 4 4 var $callback; 5 5 6 /** 7 * WP_UnitTest_Factory_Callback_After_Create constructor. 8 * 9 * @param string $callback A callback function. 10 */ 6 11 function __construct( $callback ) { 7 12 $this->callback = $callback; 8 13 } 9 14 15 /** 16 * Calls the set callback on given object. 17 * 18 * @param object $object The object to apply the callback on. 19 * 20 * @return object The possibly altered object. 21 */ 10 22 function call( $object ) { 11 23 return call_user_func( $this->callback, $object ); 12 24 } -
tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php
12 12 * @type string $file Path of the attached file. 13 13 * } 14 14 * @param int $legacy_parent Deprecated. 15 * @param array $legacy_args Deprecated 15 * @param array $legacy_args Deprecated. 16 * 17 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. 16 18 */ 17 19 function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) { 18 20 // Backward compatibility for legacy argument format. … … 33 35 return wp_insert_attachment( $r, $r['file'], $r['post_parent'] ); 34 36 } 35 37 38 /** 39 * Saves an attachment. 40 * 41 * @param string $file The file name to create attachment object for. 42 * @param int $parent The post id to attach the file to. 43 * 44 * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. 45 */ 36 46 function create_upload_object( $file, $parent = 0 ) { 37 47 $contents = file_get_contents( $file ); 38 48 $upload = wp_upload_bits( basename( $file ), null, $contents ); -
tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php
23 23 ); 24 24 } 25 25 26 /** 27 * Creates a blog object. 28 * 29 * @param array $args Arguments for the site object. 30 * 31 * @return int|WP_Error Returns WP_Error object on failure, the site ID on success. 32 */ 26 33 function create_object( $args ) { 27 34 global $wpdb; 28 35 $meta = isset( $args['meta'] ) ? $args['meta'] : array( 'public' => 1 ); … … 38 45 return $blog; 39 46 } 40 47 48 /** 49 * Updates a blog object. Not implemented. 50 * 51 * @param int $blog_id The blog id to update. 52 * @param array $fields The fields to update. 53 * 54 * @return void 55 */ 41 56 function update_object( $blog_id, $fields ) {} 42 57 58 /** 59 * Retrieves a site by given blog id. 60 * 61 * @param int $blog_id The blog id to retrieve. 62 * 63 * @return null|WP_Site The site object or null if not found. 64 */ 43 65 function get_object_by_id( $blog_id ) { 44 66 return get_site( $blog_id ); 45 67 } -
tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php
22 22 ); 23 23 } 24 24 25 /** 26 * Inserts a comment. 27 * 28 * @param array $args The comment details. 29 * 30 * @return false|int The comment's ID on success, false on failure. 31 */ 25 32 function create_object( $args ) { 26 33 return wp_insert_comment( $this->addslashes_deep( $args ) ); 27 34 } 28 35 36 /** 37 * Updates a comment. 38 * 39 * @param int $comment_id The comment id. 40 * @param array $fields The comment details. 41 * 42 * @return int When updated 1, not update 0. 43 */ 29 44 function update_object( $comment_id, $fields ) { 30 45 $fields['comment_ID'] = $comment_id; 31 46 return wp_update_comment( $this->addslashes_deep( $fields ) ); 32 47 } 33 48 49 /** 50 * Creates multiple comments on given post. 51 * 52 * @param int $post_id The post id to create comments for. 53 * @param int $count Total amount of comments to create. 54 * @param array $args The comment details. 55 * @param null $generation_definitions Default values. 56 * 57 * @return int[] Array with the comment ids. 58 */ 34 59 function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) { 35 60 $args['comment_post_ID'] = $post_id; 36 61 return $this->create_many( $count, $args, $generation_definitions ); 37 62 } 38 63 64 /** 65 * Returns a comment. 66 * 67 * @param int $comment_id The comment id. 68 * 69 * @return null|WP_Comment WP_Comment when found, null when not found. 70 */ 39 71 function get_object_by_id( $comment_id ) { 40 72 return get_comment( $comment_id ); 41 73 } -
tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php
23 23 ); 24 24 } 25 25 26 /** 27 * Creates a post object. 28 * 29 * @param array $args Array with elements for the post. 30 * 31 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. 32 */ 26 33 function create_object( $args ) { 27 34 return wp_insert_post( $args ); 28 35 } 29 36 37 /** 38 * Updates an existing post object. 39 * 40 * @param int $post_id The post id to update. 41 * @param array $fields Post data. 42 * 43 * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. 44 */ 30 45 function update_object( $post_id, $fields ) { 31 46 $fields['ID'] = $post_id; 32 47 return wp_update_post( $fields ); 33 48 } 34 49 50 /** 51 * Retrieves a object by an id. 52 * 53 * @param int $post_id The post id to update. 54 * 55 * @return null|WP_Post WP_Post on success or null on failure. 56 */ 35 57 function get_object_by_id( $post_id ) { 36 58 return get_post( $post_id ); 37 59 } -
tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php
24 24 ); 25 25 } 26 26 27 /** 28 * Creates a term object. 29 * 30 * @param array $args Array or string of arguments for inserting a term. 31 * 32 * @return array|WP_Error 33 */ 27 34 function create_object( $args ) { 28 35 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args ); 29 36 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args ); … … 33 40 return $term_id_pair['term_id']; 34 41 } 35 42 43 /** 44 * Updates the term. 45 * 46 * @param int|object $term The term to update. 47 * @param array|string $fields The context in which to relate the term to the object. 48 * 49 * @return int The term id. 50 */ 36 51 function update_object( $term, $fields ) { 37 52 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields ); 38 53 if ( is_object( $term ) ) { … … 42 57 return $term_id_pair['term_id']; 43 58 } 44 59 60 /** 61 * Attach terms on the given post. 62 * 63 * @param int $post_id The Post ID. 64 * @param string|array $terms An array of terms to set for the post, or a string of terms 65 * separated by commas. Hierarchical taxonomies must always pass IDs rather 66 * than names so that children with the same names but different parents 67 * aren't confused. 68 * @param string $taxonomy Taxonomy name. 69 * @param bool $append Optional. If true, don't delete existing terms, just add on. If false, 70 * replace the terms with the new terms. Default true. 71 * 72 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. 73 */ 45 74 function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { 46 75 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); 47 76 } 48 77 49 78 /** 50 * @return array|null|WP_Error|WP_Term 79 * Create a term and returns it as a object. 80 * 81 * @param array $args Array or string of arguments for inserting a term. 82 * @param null $generation_definitions The default values. 83 * 84 * @return null|WP_Error|WP_Term WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure. 51 85 */ 52 86 function create_and_get( $args = array(), $generation_definitions = null ) { 53 87 $term_id = $this->create( $args, $generation_definitions ); … … 55 89 return get_term( $term_id, $taxonomy ); 56 90 } 57 91 92 /** 93 * Retrieves the term by given term id. 94 * 95 * @param int $term_id The term id to retrieve. 96 * 97 * @return null|WP_Error|WP_Term WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure. 98 */ 58 99 function get_object_by_id( $term_id ) { 59 100 return get_term( $term_id, $this->taxonomy ); 60 101 } -
tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php
21 21 $this->default_generation_definitions = $default_generation_definitions; 22 22 } 23 23 24 /** 25 * Creates an object. 26 * 27 * @param array $args The arguments. 28 * 29 * @return mixed The result. Can be anything. 30 */ 24 31 abstract function create_object( $args ); 32 33 /** 34 * Updates an existing object. 35 * 36 * @param int $object The object id. 37 * @param array $fields The values to update. 38 * 39 * @return mixed The result. Can be anything. 40 */ 25 41 abstract function update_object( $object, $fields ); 26 42 43 /** 44 * Creates an object. 45 * 46 * @param array $args Optional. The arguments for the object to create. Default is empty array. 47 * @param null $generation_definitions Optional. The default values for the object. Default is null. 48 * 49 * @return mixed The result. Can be anything. 50 */ 27 51 function create( $args = array(), $generation_definitions = null ) { 28 52 if ( is_null( $generation_definitions ) ) { 29 53 $generation_definitions = $this->default_generation_definitions; … … 45 69 return $created; 46 70 } 47 71 72 /** 73 * Creates an object and returns its object. 74 * 75 * @param array $args Optional. The arguments for the object to create. Default is empty array. 76 * @param null $generation_definitions Optional. The default values for the object. Default is null. 77 * 78 * @return mixed The created object. Can be anything. 79 */ 48 80 function create_and_get( $args = array(), $generation_definitions = null ) { 49 81 $object_id = $this->create( $args, $generation_definitions ); 50 82 return $this->get_object_by_id( $object_id ); 51 83 } 52 84 85 /** 86 * Retrieves an object by ID. 87 * 88 * @param int $object_id The object id. 89 * 90 * @return mixed The object. Can be anything. 91 */ 53 92 abstract function get_object_by_id( $object_id ); 54 93 94 /** 95 * Creates multiple objects. 96 * 97 * @param int $count Amount of objects to create. 98 * @param array $args Optional. The arguments for the object to create. Default is empty array. 99 * @param null $generation_definitions Optional. The default values for the object. Default is null. 100 * 101 * @return array 102 */ 55 103 function create_many( $count, $args = array(), $generation_definitions = null ) { 56 104 $results = array(); 57 105 for ( $i = 0; $i < $count; $i++ ) { … … 60 108 return $results; 61 109 } 62 110 111 /** 112 * Combines the given argument with the generation_definitions (defaults) and applies 113 * possibly set callbacks on it. 114 * 115 * @param array $args Optional. The arguments to combine with defaults. Default is empty array. 116 * @param array|null $generation_definitions Optional. The defaults. Default is null. 117 * @param array|null $callbacks Optional. Array with callbacks to apply on the fields. Default is null. 118 * 119 * @return array|WP_Error Combined array on success. WP_Error when default value is incorrent. 120 */ 63 121 function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { 64 122 $callbacks = array(); 65 123 if ( is_null( $generation_definitions ) ) { … … 88 146 return $args; 89 147 } 90 148 149 150 /** 151 * Applies the callbacks on the created object. 152 * 153 * @param WP_UnitTest_Factory_Callback_After_Create[] $callbacks Array with callback functions. 154 * @param mixed $created The object to apply callbacks for. 155 * 156 * @return array The altered fields. 157 */ 91 158 function apply_callbacks( $callbacks, $created ) { 92 159 $updated_fields = array(); 160 93 161 foreach ( $callbacks as $field_name => $generator ) { 94 162 $updated_fields[ $field_name ] = $generator->call( $created ); 95 163 } … … 96 164 return $updated_fields; 97 165 } 98 166 167 /** 168 * Instantiates a callback objects for the given function name. 169 * 170 * @param string $function The callback function. 171 * 172 * @return WP_UnitTest_Factory_Callback_After_Create 173 */ 99 174 function callback( $function ) { 100 175 return new WP_UnitTest_Factory_Callback_After_Create( $function ); 101 176 } 102 177 178 /** 179 * Adds slashes to the given value. 180 * 181 * @param array|object|string|mixed $value The value to add slashes to. 182 * 183 * @return array|string The value with the possibly applied slashes. 184 */ 103 185 function addslashes_deep( $value ) { 104 186 if ( is_array( $value ) ) { 105 187 $value = array_map( array( $this, 'addslashes_deep' ), $value ); -
tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php
21 21 ); 22 22 } 23 23 24 /** 25 * Inserts an user. 26 * 27 * @param array $args The user data to insert. 28 * 29 * @return int|WP_Error 30 */ 24 31 function create_object( $args ) { 25 32 return wp_insert_user( $args ); 26 33 } 27 34 35 /** 36 * Updates the user data. 37 * 38 * @param int $user_id The user id to update. 39 * @param array $fields The user data to update. 40 * 41 * @return int|WP_Error User id on success. WP_Error on failure. 42 */ 28 43 function update_object( $user_id, $fields ) { 29 44 $fields['ID'] = $user_id; 30 45 return wp_update_user( $fields ); 31 46 } 32 47 48 /** 49 * Retrieves the user for given user id. 50 * 51 * @param int $user_id The user id to get. 52 * 53 * @return WP_User The user. 54 */ 33 55 function get_object_by_id( $user_id ) { 34 56 return new WP_User( $user_id ); 35 57 }