Make WordPress Core

Ticket #35492: 35492.diff

File 35492.diff, 135.6 KB (added by ericlewis, 9 years ago)
  • tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    4253class WP_UnitTest_Factory_Callback_After_Create {
    4264        var $callback;
    4275
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    923class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    934
    945        function create_object( $file, $parent = 0, $args = array() ) {
     
    12435                return $id;
    12536        }
    12637}
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    1843class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    1854
    1865        function __construct( $factory = null ) {
     
    21534                return get_blog_details( $blog_id, false );
    21635        }
    21736}
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    1533class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    1544
    1555        function __construct( $factory = null ) {
     
    18030                return get_comment( $comment_id );
    18131        }
    18232}
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    2203class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    2214
    2225        function __construct( $factory = null ) {
     
    24932                return wp_get_network( $network_id );
    25033        }
    25134}
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    653class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    664
    675        function __construct( $factory = null ) {
     
    8826                return get_post( $post_id );
    8927        }
    9028}
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    2533class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    2544
    2555        private $taxonomy;
     
    29545                return get_term( $term_id, $this->taxonomy );
    29646        }
    29747}
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    2993abstract class WP_UnitTest_Factory_For_Thing {
    3004
    3015        var $default_generation_definitions;
     
    399103        }
    400104
    401105}
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    1283class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    1294
    1305        function __construct( $factory = null ) {
     
    14924                return new WP_User( $user_id );
    15025        }
    15126}
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php

     
    11<?php
    22
    3 class WP_UnitTest_Factory {
    4 
    5         /**
    6          * @var WP_UnitTest_Factory_For_Post
    7          */
    8         public $post;
    9 
    10         /**
    11          * @var WP_UnitTest_Factory_For_Attachment
    12          */
    13         public $attachment;
    14 
    15         /**
    16          * @var WP_UnitTest_Factory_For_Comment
    17          */
    18         public $comment;
    19 
    20         /**
    21          * @var WP_UnitTest_Factory_For_User
    22          */
    23         public $user;
    24 
    25         /**
    26          * @var WP_UnitTest_Factory_For_Term
    27          */
    28         public $term;
    29 
    30         /**
    31          * @var WP_UnitTest_Factory_For_Term
    32          */
    33         public $category;
    34 
    35         /**
    36          * @var WP_UnitTest_Factory_For_Term
    37          */
    38         public $tag;
    39 
    40         /**
    41          * @var WP_UnitTest_Factory_For_Blog
    42          */
    43         public $blog;
    44 
    45         /**
    46          * @var WP_UnitTest_Factory_For_Network
    47          */
    48         public $network;
    49 
    50         function __construct() {
    51                 $this->post = new WP_UnitTest_Factory_For_Post( $this );
    52                 $this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
    53                 $this->comment = new WP_UnitTest_Factory_For_Comment( $this );
    54                 $this->user = new WP_UnitTest_Factory_For_User( $this );
    55                 $this->term = new WP_UnitTest_Factory_For_Term( $this );
    56                 $this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
    57                 $this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
    58                 if ( is_multisite() ) {
    59                         $this->blog = new WP_UnitTest_Factory_For_Blog( $this );
    60                         $this->network = new WP_UnitTest_Factory_For_Network( $this );
    61                 }
    62         }
    63 }
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    4033class WP_UnitTest_Generator_Sequence {
    4044        static $incr = -1;
    4055        public $next;
     
    42121                return $generated;
    42222        }
    42323}
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }
  • tests/phpunit/includes/factory.php

     
    11<?php
    22
     3require_once( 'factory/class-wp-unittest-factory-for-thing.php' );
     4require_once( 'factory/class-wp-unittest-factory-for-post.php' );
     5require_once( 'factory/class-wp-unittest-factory-for-attachment.php' );
     6require_once( 'factory/class-wp-unittest-factory-for-user.php' );
     7require_once( 'factory/class-wp-unittest-factory-for-comment.php' );
     8require_once( 'factory/class-wp-unittest-factory-for-blog.php' );
     9require_once( 'factory/class-wp-unittest-factory-for-network.php' );
     10require_once( 'factory/class-wp-unittest-factory-for-term.php' );
     11require_once( 'factory/class-wp-unittest-generator-sequence.php' );
     12require_once( 'factory/class-wp-unittest-factory-callback-after-create.php' );
     13
    314class WP_UnitTest_Factory {
    415
    516        /**
     
    6172                }
    6273        }
    6374}
    64 
    65 class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
    66 
    67         function __construct( $factory = null ) {
    68                 parent::__construct( $factory );
    69                 $this->default_generation_definitions = array(
    70                         'post_status' => 'publish',
    71                         'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
    72                         'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
    73                         'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
    74                         'post_type' => 'post'
    75                 );
    76         }
    77 
    78         function create_object( $args ) {
    79                 return wp_insert_post( $args );
    80         }
    81 
    82         function update_object( $post_id, $fields ) {
    83                 $fields['ID'] = $post_id;
    84                 return wp_update_post( $fields );
    85         }
    86 
    87         function get_object_by_id( $post_id ) {
    88                 return get_post( $post_id );
    89         }
    90 }
    91 
    92 class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
    93 
    94         function create_object( $file, $parent = 0, $args = array() ) {
    95                 return wp_insert_attachment( $args, $file, $parent );
    96         }
    97 
    98         function create_upload_object( $file, $parent = 0 ) {
    99                 $contents = file_get_contents($file);
    100                 $upload = wp_upload_bits(basename($file), null, $contents);
    101 
    102                 $type = '';
    103                 if ( ! empty($upload['type']) ) {
    104                         $type = $upload['type'];
    105                 } else {
    106                         $mime = wp_check_filetype( $upload['file'] );
    107                         if ($mime)
    108                                 $type = $mime['type'];
    109                 }
    110 
    111                 $attachment = array(
    112                         'post_title' => basename( $upload['file'] ),
    113                         'post_content' => '',
    114                         'post_type' => 'attachment',
    115                         'post_parent' => $parent,
    116                         'post_mime_type' => $type,
    117                         'guid' => $upload[ 'url' ],
    118                 );
    119 
    120                 // Save the data
    121                 $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
    122                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    123 
    124                 return $id;
    125         }
    126 }
    127 
    128 class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
    129 
    130         function __construct( $factory = null ) {
    131                 parent::__construct( $factory );
    132                 $this->default_generation_definitions = array(
    133                         'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
    134                         'user_pass' => 'password',
    135                         'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
    136                 );
    137         }
    138 
    139         function create_object( $args ) {
    140                 return wp_insert_user( $args );
    141         }
    142 
    143         function update_object( $user_id, $fields ) {
    144                 $fields['ID'] = $user_id;
    145                 return wp_update_user( $fields );
    146         }
    147 
    148         function get_object_by_id( $user_id ) {
    149                 return new WP_User( $user_id );
    150         }
    151 }
    152 
    153 class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
    154 
    155         function __construct( $factory = null ) {
    156                 parent::__construct( $factory );
    157                 $this->default_generation_definitions = array(
    158                         'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
    159                         'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
    160                         'comment_approved' => 1,
    161                         'comment_content' => 'This is a comment'
    162                 );
    163         }
    164 
    165         function create_object( $args ) {
    166                 return wp_insert_comment( $this->addslashes_deep( $args ) );
    167         }
    168 
    169         function update_object( $comment_id, $fields ) {
    170                 $fields['comment_ID'] = $comment_id;
    171                 return wp_update_comment( $this->addslashes_deep( $fields ) );
    172         }
    173 
    174         function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
    175                 $args['comment_post_ID'] = $post_id;
    176                 return $this->create_many( $count, $args, $generation_definitions );
    177         }
    178 
    179         function get_object_by_id( $comment_id ) {
    180                 return get_comment( $comment_id );
    181         }
    182 }
    183 
    184 class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
    185 
    186         function __construct( $factory = null ) {
    187                 global $current_site, $base;
    188                 parent::__construct( $factory );
    189                 $this->default_generation_definitions = array(
    190                         'domain' => $current_site->domain,
    191                         'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
    192                         'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
    193                         'site_id' => $current_site->id,
    194                 );
    195         }
    196 
    197         function create_object( $args ) {
    198                 global $wpdb;
    199                 $meta = isset( $args['meta'] ) ? $args['meta'] : array();
    200                 $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
    201                 // temp tables will trigger db errors when we attempt to reference them as new temp tables
    202                 $suppress = $wpdb->suppress_errors();
    203                 $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    204                 $wpdb->suppress_errors( $suppress );
    205 
    206                 // Tell WP we're done installing.
    207                 wp_installing( false );
    208 
    209                 return $blog;
    210         }
    211 
    212         function update_object( $blog_id, $fields ) {}
    213 
    214         function get_object_by_id( $blog_id ) {
    215                 return get_blog_details( $blog_id, false );
    216         }
    217 }
    218 
    219 
    220 class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
    221 
    222         function __construct( $factory = null ) {
    223                 parent::__construct( $factory );
    224                 $this->default_generation_definitions = array(
    225                         'domain' => WP_TESTS_DOMAIN,
    226                         'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
    227                         'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
    228                         'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
    229                         'subdomain_install' => false,
    230                 );
    231         }
    232 
    233         function create_object( $args ) {
    234                 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    235 
    236                 if ( ! isset( $args['user'] ) ) {
    237                         $email = WP_TESTS_EMAIL;
    238                 } else {
    239                         $email = get_userdata( $args['user'] )->user_email;
    240                 }
    241 
    242                 populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
    243                 return $args['network_id'];
    244         }
    245 
    246         function update_object( $network_id, $fields ) {}
    247 
    248         function get_object_by_id( $network_id ) {
    249                 return wp_get_network( $network_id );
    250         }
    251 }
    252 
    253 class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
    254 
    255         private $taxonomy;
    256         const DEFAULT_TAXONOMY = 'post_tag';
    257 
    258         function __construct( $factory = null, $taxonomy = null ) {
    259                 parent::__construct( $factory );
    260                 $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
    261                 $this->default_generation_definitions = array(
    262                         'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
    263                         'taxonomy' => $this->taxonomy,
    264                         'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
    265                 );
    266         }
    267 
    268         function create_object( $args ) {
    269                 $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
    270                 $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
    271                 if ( is_wp_error( $term_id_pair ) )
    272                         return $term_id_pair;
    273                 return $term_id_pair['term_id'];
    274         }
    275 
    276         function update_object( $term, $fields ) {
    277                 $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
    278                 if ( is_object( $term ) )
    279                         $taxonomy = $term->taxonomy;
    280                 $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
    281                 return $term_id_pair['term_id'];
    282         }
    283 
    284         function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
    285                 return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
    286         }
    287 
    288         function create_and_get( $args = array(), $generation_definitions = null ) {
    289                 $term_id = $this->create( $args, $generation_definitions );
    290                 $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
    291                 return get_term( $term_id, $taxonomy );
    292         }
    293 
    294         function get_object_by_id( $term_id ) {
    295                 return get_term( $term_id, $this->taxonomy );
    296         }
    297 }
    298 
    299 abstract class WP_UnitTest_Factory_For_Thing {
    300 
    301         var $default_generation_definitions;
    302         var $factory;
    303 
    304         /**
    305          * Creates a new factory, which will create objects of a specific Thing
    306          *
    307          * @param object $factory Global factory that can be used to create other objects on the system
    308          * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
    309          * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
    310          * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
    311          */
    312         function __construct( $factory, $default_generation_definitions = array() ) {
    313                 $this->factory = $factory;
    314                 $this->default_generation_definitions = $default_generation_definitions;
    315         }
    316 
    317         abstract function create_object( $args );
    318         abstract function update_object( $object, $fields );
    319 
    320         function create( $args = array(), $generation_definitions = null ) {
    321                 if ( is_null( $generation_definitions ) )
    322                         $generation_definitions = $this->default_generation_definitions;
    323 
    324                 $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
    325                 $created = $this->create_object( $generated_args );
    326                 if ( !$created || is_wp_error( $created ) )
    327                         return $created;
    328 
    329                 if ( $callbacks ) {
    330                         $updated_fields = $this->apply_callbacks( $callbacks, $created );
    331                         $save_result = $this->update_object( $created, $updated_fields );
    332                         if ( !$save_result || is_wp_error( $save_result ) )
    333                                 return $save_result;
    334                 }
    335                 return $created;
    336         }
    337 
    338         function create_and_get( $args = array(), $generation_definitions = null ) {
    339                 $object_id = $this->create( $args, $generation_definitions );
    340                 return $this->get_object_by_id( $object_id );
    341         }
    342 
    343         abstract function get_object_by_id( $object_id );
    344 
    345         function create_many( $count, $args = array(), $generation_definitions = null ) {
    346                 $results = array();
    347                 for ( $i = 0; $i < $count; $i++ ) {
    348                         $results[] = $this->create( $args, $generation_definitions );
    349                 }
    350                 return $results;
    351         }
    352 
    353         function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
    354                 $callbacks = array();
    355                 if ( is_null( $generation_definitions ) )
    356                         $generation_definitions = $this->default_generation_definitions;
    357 
    358                 foreach( array_keys( $generation_definitions ) as $field_name ) {
    359                         if ( !isset( $args[$field_name] ) ) {
    360                                 $generator = $generation_definitions[$field_name];
    361                                 if ( is_scalar( $generator ) )
    362                                         $args[$field_name] = $generator;
    363                                 elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
    364                                         $callbacks[$field_name] = $generator;
    365                                 } elseif ( is_object( $generator ) )
    366                                         $args[$field_name] = $generator->next();
    367                                 else
    368                                         return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
    369                         }
    370                 }
    371                 return $args;
    372         }
    373 
    374         function apply_callbacks( $callbacks, $created ) {
    375                 $updated_fields = array();
    376                 foreach( $callbacks as $field_name => $generator ) {
    377                         $updated_fields[$field_name] = $generator->call( $created );
    378                 }
    379                 return $updated_fields;
    380         }
    381 
    382         function callback( $function ) {
    383                 return new WP_UnitTest_Factory_Callback_After_Create( $function );
    384         }
    385 
    386         function addslashes_deep($value) {
    387                 if ( is_array( $value ) ) {
    388                         $value = array_map( array( $this, 'addslashes_deep' ), $value );
    389                 } elseif ( is_object( $value ) ) {
    390                         $vars = get_object_vars( $value );
    391                         foreach ($vars as $key=>$data) {
    392                                 $value->{$key} = $this->addslashes_deep( $data );
    393                         }
    394                 } elseif ( is_string( $value ) ) {
    395                         $value = addslashes( $value );
    396                 }
    397 
    398                 return $value;
    399         }
    400 
    401 }
    402 
    403 class WP_UnitTest_Generator_Sequence {
    404         static $incr = -1;
    405         public $next;
    406         public $template_string;
    407 
    408         function __construct( $template_string = '%s', $start = null ) {
    409                 if ( $start ) {
    410                         $this->next = $start;
    411                 } else {
    412                         self::$incr++;
    413                         $this->next = self::$incr;
    414                 }
    415                 $this->template_string = $template_string;
    416         }
    417 
    418         function next() {
    419                 $generated = sprintf( $this->template_string , $this->next );
    420                 $this->next++;
    421                 return $generated;
    422         }
    423 }
    424 
    425 class WP_UnitTest_Factory_Callback_After_Create {
    426         var $callback;
    427 
    428         function __construct( $callback ) {
    429                 $this->callback = $callback;
    430         }
    431 
    432         function call( $object ) {
    433                 return call_user_func( $this->callback, $object );
    434         }
    435 }