WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/19/2016 03:55:19 AM (4 years ago)
Author:
ericlewis
Message:

Build/Test Tools: Move PHP factory classes into their own files.

This makes the code easier to browse.

factory.php loads the new files, so this is backwards compatible in case factory.php is loaded directly for access to one of the classes.

See #35492.

Location:
trunk/tests/phpunit/includes/factory
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php

    r36346 r36347  
    11<?php
    2 
    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 }
    2982
    2993abstract class WP_UnitTest_Factory_For_Thing {
     
    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 }
Note: See TracChangeset for help on using the changeset viewer.