diff --git a/tests/phpunit/includes/factory.php b/tests/phpunit/includes/factory.php index e9c4743834..7cb8219158 100644 --- a/tests/phpunit/includes/factory.php +++ b/tests/phpunit/includes/factory.php @@ -1,5 +1,16 @@ default_generation_definitions = array( - 'post_status' => 'publish', - 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ), - 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ), - 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ), - 'post_type' => 'post' - ); - } - - function create_object( $args ) { - return wp_insert_post( $args ); - } - - function update_object( $post_id, $fields ) { - $fields['ID'] = $post_id; - return wp_update_post( $fields ); - } - - function get_object_by_id( $post_id ) { - return get_post( $post_id ); - } -} - -class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post { - - function create_object( $file, $parent = 0, $args = array() ) { - return wp_insert_attachment( $args, $file, $parent ); - } - - function create_upload_object( $file, $parent = 0 ) { - $contents = file_get_contents($file); - $upload = wp_upload_bits(basename($file), null, $contents); - - $type = ''; - if ( ! empty($upload['type']) ) { - $type = $upload['type']; - } else { - $mime = wp_check_filetype( $upload['file'] ); - if ($mime) - $type = $mime['type']; - } - - $attachment = array( - 'post_title' => basename( $upload['file'] ), - 'post_content' => '', - 'post_type' => 'attachment', - 'post_parent' => $parent, - 'post_mime_type' => $type, - 'guid' => $upload[ 'url' ], - ); - - // Save the data - $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent ); - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); - - return $id; - } -} - -class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing { - - function __construct( $factory = null ) { - parent::__construct( $factory ); - $this->default_generation_definitions = array( - 'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ), - 'user_pass' => 'password', - 'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ), - ); - } - - function create_object( $args ) { - return wp_insert_user( $args ); - } - - function update_object( $user_id, $fields ) { - $fields['ID'] = $user_id; - return wp_update_user( $fields ); - } - - function get_object_by_id( $user_id ) { - return new WP_User( $user_id ); - } -} - -class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing { - - function __construct( $factory = null ) { - parent::__construct( $factory ); - $this->default_generation_definitions = array( - 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ), - 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ), - 'comment_approved' => 1, - 'comment_content' => 'This is a comment' - ); - } - - function create_object( $args ) { - return wp_insert_comment( $this->addslashes_deep( $args ) ); - } - - function update_object( $comment_id, $fields ) { - $fields['comment_ID'] = $comment_id; - return wp_update_comment( $this->addslashes_deep( $fields ) ); - } - - function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) { - $args['comment_post_ID'] = $post_id; - return $this->create_many( $count, $args, $generation_definitions ); - } - - function get_object_by_id( $comment_id ) { - return get_comment( $comment_id ); - } -} - -class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing { - - function __construct( $factory = null ) { - global $current_site, $base; - parent::__construct( $factory ); - $this->default_generation_definitions = array( - 'domain' => $current_site->domain, - 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ), - 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ), - 'site_id' => $current_site->id, - ); - } - - function create_object( $args ) { - global $wpdb; - $meta = isset( $args['meta'] ) ? $args['meta'] : array(); - $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id(); - // temp tables will trigger db errors when we attempt to reference them as new temp tables - $suppress = $wpdb->suppress_errors(); - $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] ); - $wpdb->suppress_errors( $suppress ); - - // Tell WP we're done installing. - wp_installing( false ); - - return $blog; - } - - function update_object( $blog_id, $fields ) {} - - function get_object_by_id( $blog_id ) { - return get_blog_details( $blog_id, false ); - } -} - - -class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing { - - function __construct( $factory = null ) { - parent::__construct( $factory ); - $this->default_generation_definitions = array( - 'domain' => WP_TESTS_DOMAIN, - 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ), - 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ), - 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ), - 'subdomain_install' => false, - ); - } - - function create_object( $args ) { - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; - - if ( ! isset( $args['user'] ) ) { - $email = WP_TESTS_EMAIL; - } else { - $email = get_userdata( $args['user'] )->user_email; - } - - populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] ); - return $args['network_id']; - } - - function update_object( $network_id, $fields ) {} - - function get_object_by_id( $network_id ) { - return wp_get_network( $network_id ); - } -} - -class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing { - - private $taxonomy; - const DEFAULT_TAXONOMY = 'post_tag'; - - function __construct( $factory = null, $taxonomy = null ) { - parent::__construct( $factory ); - $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY; - $this->default_generation_definitions = array( - 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ), - 'taxonomy' => $this->taxonomy, - 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ), - ); - } - - function create_object( $args ) { - $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args ); - $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args ); - if ( is_wp_error( $term_id_pair ) ) - return $term_id_pair; - return $term_id_pair['term_id']; - } - - function update_object( $term, $fields ) { - $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields ); - if ( is_object( $term ) ) - $taxonomy = $term->taxonomy; - $term_id_pair = wp_update_term( $term, $taxonomy, $fields ); - return $term_id_pair['term_id']; - } - - function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { - return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); - } - - function create_and_get( $args = array(), $generation_definitions = null ) { - $term_id = $this->create( $args, $generation_definitions ); - $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy; - return get_term( $term_id, $taxonomy ); - } - - function get_object_by_id( $term_id ) { - return get_term( $term_id, $this->taxonomy ); - } -} - -abstract class WP_UnitTest_Factory_For_Thing { - - var $default_generation_definitions; - var $factory; - - /** - * Creates a new factory, which will create objects of a specific Thing - * - * @param object $factory Global factory that can be used to create other objects on the system - * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values - * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence}, - * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}. - */ - function __construct( $factory, $default_generation_definitions = array() ) { - $this->factory = $factory; - $this->default_generation_definitions = $default_generation_definitions; - } - - abstract function create_object( $args ); - abstract function update_object( $object, $fields ); - - function create( $args = array(), $generation_definitions = null ) { - if ( is_null( $generation_definitions ) ) - $generation_definitions = $this->default_generation_definitions; - - $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); - $created = $this->create_object( $generated_args ); - if ( !$created || is_wp_error( $created ) ) - return $created; - - if ( $callbacks ) { - $updated_fields = $this->apply_callbacks( $callbacks, $created ); - $save_result = $this->update_object( $created, $updated_fields ); - if ( !$save_result || is_wp_error( $save_result ) ) - return $save_result; - } - return $created; - } - - function create_and_get( $args = array(), $generation_definitions = null ) { - $object_id = $this->create( $args, $generation_definitions ); - return $this->get_object_by_id( $object_id ); - } - - abstract function get_object_by_id( $object_id ); - - function create_many( $count, $args = array(), $generation_definitions = null ) { - $results = array(); - for ( $i = 0; $i < $count; $i++ ) { - $results[] = $this->create( $args, $generation_definitions ); - } - return $results; - } - - function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { - $callbacks = array(); - if ( is_null( $generation_definitions ) ) - $generation_definitions = $this->default_generation_definitions; - - foreach( array_keys( $generation_definitions ) as $field_name ) { - if ( !isset( $args[$field_name] ) ) { - $generator = $generation_definitions[$field_name]; - if ( is_scalar( $generator ) ) - $args[$field_name] = $generator; - elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { - $callbacks[$field_name] = $generator; - } elseif ( is_object( $generator ) ) - $args[$field_name] = $generator->next(); - else - return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' ); - } - } - return $args; - } - - function apply_callbacks( $callbacks, $created ) { - $updated_fields = array(); - foreach( $callbacks as $field_name => $generator ) { - $updated_fields[$field_name] = $generator->call( $created ); - } - return $updated_fields; - } - - function callback( $function ) { - return new WP_UnitTest_Factory_Callback_After_Create( $function ); - } - - function addslashes_deep($value) { - if ( is_array( $value ) ) { - $value = array_map( array( $this, 'addslashes_deep' ), $value ); - } elseif ( is_object( $value ) ) { - $vars = get_object_vars( $value ); - foreach ($vars as $key=>$data) { - $value->{$key} = $this->addslashes_deep( $data ); - } - } elseif ( is_string( $value ) ) { - $value = addslashes( $value ); - } - - return $value; - } - -} - -class WP_UnitTest_Generator_Sequence { - static $incr = -1; - public $next; - public $template_string; - - function __construct( $template_string = '%s', $start = null ) { - if ( $start ) { - $this->next = $start; - } else { - self::$incr++; - $this->next = self::$incr; - } - $this->template_string = $template_string; - } - - function next() { - $generated = sprintf( $this->template_string , $this->next ); - $this->next++; - return $generated; - } -} - -class WP_UnitTest_Factory_Callback_After_Create { - var $callback; - - function __construct( $callback ) { - $this->callback = $callback; - } - - function call( $object ) { - return call_user_func( $this->callback, $object ); - } -} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php new file mode 100644 index 0000000000..d02d9a3070 --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php @@ -0,0 +1,13 @@ +callback = $callback; + } + + function call( $object ) { + return call_user_func( $this->callback, $object ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php new file mode 100644 index 0000000000..592591ed54 --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php @@ -0,0 +1,37 @@ + basename( $upload['file'] ), + 'post_content' => '', + 'post_type' => 'attachment', + 'post_parent' => $parent, + 'post_mime_type' => $type, + 'guid' => $upload[ 'url' ], + ); + + // Save the data + $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent ); + wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); + + return $id; + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php new file mode 100644 index 0000000000..84f339a5bd --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php @@ -0,0 +1,36 @@ +default_generation_definitions = array( + 'domain' => $current_site->domain, + 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ), + 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ), + 'site_id' => $current_site->id, + ); + } + + function create_object( $args ) { + global $wpdb; + $meta = isset( $args['meta'] ) ? $args['meta'] : array(); + $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id(); + // temp tables will trigger db errors when we attempt to reference them as new temp tables + $suppress = $wpdb->suppress_errors(); + $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] ); + $wpdb->suppress_errors( $suppress ); + + // Tell WP we're done installing. + wp_installing( false ); + + return $blog; + } + + function update_object( $blog_id, $fields ) {} + + function get_object_by_id( $blog_id ) { + return get_blog_details( $blog_id, false ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php new file mode 100644 index 0000000000..cd853cc863 --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php @@ -0,0 +1,32 @@ +default_generation_definitions = array( + 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ), + 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ), + 'comment_approved' => 1, + 'comment_content' => 'This is a comment' + ); + } + + function create_object( $args ) { + return wp_insert_comment( $this->addslashes_deep( $args ) ); + } + + function update_object( $comment_id, $fields ) { + $fields['comment_ID'] = $comment_id; + return wp_update_comment( $this->addslashes_deep( $fields ) ); + } + + function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) { + $args['comment_post_ID'] = $post_id; + return $this->create_many( $count, $args, $generation_definitions ); + } + + function get_object_by_id( $comment_id ) { + return get_comment( $comment_id ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php new file mode 100644 index 0000000000..6025769e92 --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php @@ -0,0 +1,34 @@ +default_generation_definitions = array( + 'domain' => WP_TESTS_DOMAIN, + 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ), + 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ), + 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ), + 'subdomain_install' => false, + ); + } + + function create_object( $args ) { + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + + if ( ! isset( $args['user'] ) ) { + $email = WP_TESTS_EMAIL; + } else { + $email = get_userdata( $args['user'] )->user_email; + } + + populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] ); + return $args['network_id']; + } + + function update_object( $network_id, $fields ) {} + + function get_object_by_id( $network_id ) { + return wp_get_network( $network_id ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php new file mode 100644 index 0000000000..1ab7d4bc44 --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php @@ -0,0 +1,28 @@ +default_generation_definitions = array( + 'post_status' => 'publish', + 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ), + 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ), + 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ), + 'post_type' => 'post' + ); + } + + function create_object( $args ) { + return wp_insert_post( $args ); + } + + function update_object( $post_id, $fields ) { + $fields['ID'] = $post_id; + return wp_update_post( $fields ); + } + + function get_object_by_id( $post_id ) { + return get_post( $post_id ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php new file mode 100644 index 0000000000..1b6f342e2c --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php @@ -0,0 +1,47 @@ +taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY; + $this->default_generation_definitions = array( + 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ), + 'taxonomy' => $this->taxonomy, + 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ), + ); + } + + function create_object( $args ) { + $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args ); + $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args ); + if ( is_wp_error( $term_id_pair ) ) + return $term_id_pair; + return $term_id_pair['term_id']; + } + + function update_object( $term, $fields ) { + $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields ); + if ( is_object( $term ) ) + $taxonomy = $term->taxonomy; + $term_id_pair = wp_update_term( $term, $taxonomy, $fields ); + return $term_id_pair['term_id']; + } + + function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { + return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); + } + + function create_and_get( $args = array(), $generation_definitions = null ) { + $term_id = $this->create( $args, $generation_definitions ); + $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy; + return get_term( $term_id, $taxonomy ); + } + + function get_object_by_id( $term_id ) { + return get_term( $term_id, $this->taxonomy ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php new file mode 100644 index 0000000000..cf412c85e2 --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php @@ -0,0 +1,105 @@ +factory = $factory; + $this->default_generation_definitions = $default_generation_definitions; + } + + abstract function create_object( $args ); + abstract function update_object( $object, $fields ); + + function create( $args = array(), $generation_definitions = null ) { + if ( is_null( $generation_definitions ) ) + $generation_definitions = $this->default_generation_definitions; + + $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); + $created = $this->create_object( $generated_args ); + if ( !$created || is_wp_error( $created ) ) + return $created; + + if ( $callbacks ) { + $updated_fields = $this->apply_callbacks( $callbacks, $created ); + $save_result = $this->update_object( $created, $updated_fields ); + if ( !$save_result || is_wp_error( $save_result ) ) + return $save_result; + } + return $created; + } + + function create_and_get( $args = array(), $generation_definitions = null ) { + $object_id = $this->create( $args, $generation_definitions ); + return $this->get_object_by_id( $object_id ); + } + + abstract function get_object_by_id( $object_id ); + + function create_many( $count, $args = array(), $generation_definitions = null ) { + $results = array(); + for ( $i = 0; $i < $count; $i++ ) { + $results[] = $this->create( $args, $generation_definitions ); + } + return $results; + } + + function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { + $callbacks = array(); + if ( is_null( $generation_definitions ) ) + $generation_definitions = $this->default_generation_definitions; + + foreach( array_keys( $generation_definitions ) as $field_name ) { + if ( !isset( $args[$field_name] ) ) { + $generator = $generation_definitions[$field_name]; + if ( is_scalar( $generator ) ) + $args[$field_name] = $generator; + elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { + $callbacks[$field_name] = $generator; + } elseif ( is_object( $generator ) ) + $args[$field_name] = $generator->next(); + else + return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' ); + } + } + return $args; + } + + function apply_callbacks( $callbacks, $created ) { + $updated_fields = array(); + foreach( $callbacks as $field_name => $generator ) { + $updated_fields[$field_name] = $generator->call( $created ); + } + return $updated_fields; + } + + function callback( $function ) { + return new WP_UnitTest_Factory_Callback_After_Create( $function ); + } + + function addslashes_deep($value) { + if ( is_array( $value ) ) { + $value = array_map( array( $this, 'addslashes_deep' ), $value ); + } elseif ( is_object( $value ) ) { + $vars = get_object_vars( $value ); + foreach ($vars as $key=>$data) { + $value->{$key} = $this->addslashes_deep( $data ); + } + } elseif ( is_string( $value ) ) { + $value = addslashes( $value ); + } + + return $value; + } + +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php new file mode 100644 index 0000000000..4c1151a8a5 --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php @@ -0,0 +1,26 @@ +default_generation_definitions = array( + 'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ), + 'user_pass' => 'password', + 'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ), + ); + } + + function create_object( $args ) { + return wp_insert_user( $args ); + } + + function update_object( $user_id, $fields ) { + $fields['ID'] = $user_id; + return wp_update_user( $fields ); + } + + function get_object_by_id( $user_id ) { + return new WP_User( $user_id ); + } +} diff --git a/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php b/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php new file mode 100644 index 0000000000..940edc4dca --- /dev/null +++ b/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php @@ -0,0 +1,23 @@ +next = $start; + } else { + self::$incr++; + $this->next = self::$incr; + } + $this->template_string = $template_string; + } + + function next() { + $generated = sprintf( $this->template_string , $this->next ); + $this->next++; + return $generated; + } +}