Make WordPress Core


Ignore:
Timestamp:
08/27/2016 08:35:16 AM (8 years ago)
Author:
wonderboymusic
Message:

Unit Tests:

  • Automatically delete objects that we were created during wpSetUpBeforeClass - posts, comments, terms (except 1), and user (except 1)
  • The amount of leftover data between tests was breathtaking - use the new function: _delete_all_data()
  • Commit database transactions for all TestCases, not just those that implement wpSetUpBeforeClass and wpTearDownAfterClass
  • The tests run 10-20 seconds faster now

See #37699.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/functions.php

    r38285 r38398  
    4949}
    5050
     51function _delete_all_data() {
     52    global $wpdb;
     53
     54    foreach ( array(
     55        $wpdb->posts,
     56        $wpdb->postmeta,
     57        $wpdb->comments,
     58        $wpdb->commentmeta,
     59        $wpdb->term_relationships,
     60        $wpdb->termmeta
     61    ) as $table ) {
     62        $wpdb->query( "DELETE FROM {$table}" );
     63    }
     64
     65    foreach ( array(
     66        $wpdb->terms,
     67        $wpdb->term_taxonomy
     68    ) as $table ) {
     69        $wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" );
     70    }
     71
     72    $wpdb->query( "UPDATE {$wpdb->term_taxonomy} SET count = 0" );
     73
     74    $wpdb->query( "DELETE FROM {$wpdb->users} WHERE ID != 1" );
     75    $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE user_id != 1" );
     76}
     77
    5178function _delete_all_posts() {
    5279    global $wpdb;
    5380
    54     $all_posts = $wpdb->get_col("SELECT ID from {$wpdb->posts}");
    55     if ($all_posts) {
    56         foreach ($all_posts as $id)
    57             wp_delete_post( $id, true );
     81    $all_posts = $wpdb->get_results( "SELECT ID, post_type from {$wpdb->posts}", ARRAY_A );
     82    if ( ! $all_posts ) {
     83        return;
     84    }
     85
     86    foreach ( $all_posts as $data ) {
     87        if ( 'attachment' === $data['post_type'] ) {
     88            wp_delete_attachment( $data['ID'], true );
     89        } else {
     90            wp_delete_post( $data['ID'], true );
     91        }
    5892    }
    5993}
Note: See TracChangeset for help on using the changeset viewer.