Make WordPress Core

Changeset 30658


Ignore:
Timestamp:
11/30/2014 07:05:52 PM (12 years ago)
Author:
wonderboymusic
Message:

wp_upload_dir() has a (little-known?) side effect: if you call it, it will attempt to create an uploads directory for the current month. As such, tearDown() and cleanup routines have to be in sync with this behavior when deleting bogus directories used in unit tests.

Examples: if you clean up directories in a test, or a test fails before the directories are cleaned, or a test fails before the 'upload_path' option is reset, the next call to wp_upload_dir() will recreate the directories you just tried to delete.

These changes ensure that src/foo and /tmp/wp-unit-test directories are deleted immediately after wp_upload_dir() is fired in the tests.

Fixes #30513.

Location:
trunk/tests/phpunit
Files:
2 edited

Legend:

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

    r30526 r30658  
    484484        }
    485485
     486        function delete_folders( $path ) {
     487                $this->matched_dirs = array();
     488                if ( ! is_dir( $path ) ) {
     489                        return;
     490                }
     491
     492                $this->scandir( $path );
     493                foreach ( array_reverse( $this->matched_dirs ) as $dir ) {
     494                        rmdir( $dir );
     495                }
     496                rmdir( $path );
     497        }
     498
     499        function scandir( $dir ) {
     500                foreach ( scandir( $dir ) as $path ) {
     501                        if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) {
     502                                $this->matched_dirs[] = $dir . '/' . $path;
     503                                $this->scandir( $dir . '/' . $path );
     504                        }
     505                }
     506        }
     507
    486508        /**
    487509         * Helper to Convert a microtime string into a float
  • trunk/tests/phpunit/tests/upload.php

    r29120 r30658  
    11<?php
    2 
    3 
    42/**
    53 * @group upload
    … …  
    119
    1210        function setUp() {
    13                 if ( is_multisite() )
     11                if ( is_multisite() ) {
    1412                        $this->knownUTBug( 35 );
     13                }
    1514
     15                $this->_reset_options();
    1616                parent::setUp();
    17                 return;
     17        }
     18
     19        function _reset_options() {
    1820                // system defaults
    1921                update_option( 'upload_path', 'wp-content/uploads' );
    2022                update_option( 'upload_url_path', '' );
    2123                update_option( 'uploads_use_yearmonth_folders', 1 );
    22         }
    23 
    24         function tearDown() {
    25                 $this->remove_added_uploads();
    26 
    27                 parent::tearDown();
    2824        }
    2925
    … …  
    4137                update_option( 'upload_path', 'foo/bar' );
    4238                $info = wp_upload_dir();
     39                $this->delete_folders( ABSPATH . 'foo' );
     40
    4341                $this->assertEquals( get_option( 'siteurl' ) . '/foo/bar/' . gmstrftime('%Y/%m'), $info['url'] );
    4442                $this->assertEquals( ABSPATH . 'foo/bar/' . gmstrftime('%Y/%m'), $info['path'] );
    … …  
    5755                update_option( 'upload_url_path', '/baz' );
    5856                $info = wp_upload_dir();
     57                $this->delete_folders( $path );
     58
    5959                $this->assertEquals( '/baz/' . gmstrftime('%Y/%m'), $info['url'] );
    6060                $this->assertEquals( "$path/" . gmstrftime('%Y/%m'), $info['path'] );
Note: See TracChangeset for help on using the changeset viewer.