Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ wp-tests-config.php
/src/wp-content/object-cache.php
/src/wp-content/php-error.php
/src/wp-content/sunrise.php
/src/wp-content/test-suite-uploads

# Files and folders relating to wp-content build tools.
/src/wp-content/themes/twentynineteen/node_modules
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@

define( 'REST_TESTS_IMPOSSIBLY_HIGH_NUMBER', 99999999 );

/*
* Tests that cover the upload_path and upload_url_path options run in a separate process
* with WP_TESTS_SKIP_UPLOADS_CONSTANT set, as UPLOADS overrides those options.
*/
if ( ! defined( 'UPLOADS' ) && '1' !== getenv( 'WP_TESTS_SKIP_UPLOADS_CONSTANT' ) ) {
define( 'UPLOADS', 'wp-content/test-suite-uploads' );

/*
* Delete the test uploads dir exactly once per run, via an env var (we need it cos each
* @runInSeparateProcess test re-executes this in its own process).
*/
$delete_tests_uploads_dir = ( false === getenv( 'WP_TESTS_UPLOADS_RUN_ID' ) );

if ( $delete_tests_uploads_dir ) {
putenv( 'WP_TESTS_UPLOADS_RUN_ID=' . getmypid() . '-' . uniqid() );
}
}

$PHP_SELF = '/index.php';
$GLOBALS['PHP_SELF'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
Expand Down Expand Up @@ -315,6 +333,23 @@ function wp_tests_options( $value ) {
// Load WordPress.
require_once ABSPATH . 'wp-settings.php';

// Process delete wp-content/test-suite-uploads folder.
if ( ! empty( $delete_tests_uploads_dir ) ) {
$uploads_test_run_dir = ABSPATH . UPLOADS;

if ( is_dir( $uploads_test_run_dir ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';

// Use the direct transport, as WP_Filesystem() may pick FTP and fail without credentials.
$uploads_test_run_fs = new WP_Filesystem_Direct( null );
$uploads_test_run_fs->delete( $uploads_test_run_dir, true );
}

unset( $uploads_test_run_dir, $uploads_test_run_fs );
}
unset( $delete_tests_uploads_dir );

// Override the PHPMailer.
require_once __DIR__ . '/mock-mailer.php';

Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ function _upload_dir_https( $uploads ) {
return $uploads;
}

/**
* Builds the upload base URL used throughout the test suite.
*
* @param string $path Path/filename to append after the uploads URL. No leading slash needed.
* @param string $scheme URL scheme, 'http' or 'https'.
* @return string The upload URL.
*/
function _upload_get_url( $path = '', $scheme = 'http' ) {
return $scheme . '://' . WP_TESTS_DOMAIN . '/' . trailingslashit( UPLOADS ) . ltrim( $path, '/' );
}

/**
* Use the Spy_REST_Server class for the REST server.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/fonts/font-library/wpFontsDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function test_fonts_dir_for_multisite() {
$actual = wp_get_font_dir();
$expected = array(
'path' => untrailingslashit( $main_site_upload_dir['basedir'] ) . "/sites/{$blog_id}/fonts",
'url' => untrailingslashit( $main_site_upload_dir['baseurl'] ) . "/sites/{$blog_id}/fonts",
'url' => get_option( 'siteurl' ) . '/' . UPLOADS . "/sites/{$blog_id}/fonts",
'subdir' => '',
'basedir' => untrailingslashit( $main_site_upload_dir['basedir'] ) . "/sites/{$blog_id}/fonts",
'baseurl' => untrailingslashit( $main_site_upload_dir['baseurl'] ) . "/sites/{$blog_id}/fonts",
'baseurl' => get_option( 'siteurl' ) . '/' . UPLOADS . "/sites/{$blog_id}/fonts",
'error' => false,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ function ( $font_dir ) {
'The font_face_settings data should match the expected data.'
);

$expected_file_path = WP_CONTENT_DIR . '/uploads/fonts/subdir/' . reset( $files )['name'];
$expected_file_path = ABSPATH . '/' . UPLOADS . '/fonts/subdir/' . reset( $files )['name'];
$expected_post_meta = 'subdir/' . reset( $files )['name'];
$this->assertFileExists( $expected_file_path, 'The font file should exist in the expected subdirectory.' );
$this->assertSame( $expected_post_meta, get_post_meta( $data['id'], '_wp_font_face_file', true ), 'The post meta should match the expected subdirectory.' );
Expand Down
68 changes: 34 additions & 34 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@
'post_type' => 'attachment',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg',
'guid' => _upload_get_url( 'test-image.jpg' ),
)
);

Expand Down Expand Up @@ -1073,7 +1073,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids1[] = $attachment_id;
$ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$ids1_srcs[] = _upload_get_url( "image$i.jpg" );
}

$ids2 = array();
Expand All @@ -1090,7 +1090,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids2[] = $attachment_id;
$ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$ids2_srcs[] = _upload_get_url( "image$i.jpg" );
}

$ids1_joined = implode( ',', array_slice( $ids1, 0, 3 ) );
Expand Down Expand Up @@ -1124,7 +1124,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids1[] = $attachment_id;
$ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$ids1_srcs[] = _upload_get_url( "image$i.jpg" );
}

$ids2 = array();
Expand All @@ -1141,7 +1141,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids2[] = $attachment_id;
$ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$ids2_srcs[] = _upload_get_url( "image$i.jpg" );
}

$ids1_joined = implode( ',', $ids1 );
Expand Down Expand Up @@ -1174,7 +1174,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids[] = $attachment_id;
$url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$url = _upload_get_url( "image$i.jpg" );
$ids_srcs[] = $url;
$imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
}
Expand Down Expand Up @@ -1212,7 +1212,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids[] = $attachment_id;
$url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$url = _upload_get_url( "image$i.jpg" );
$ids_srcs[] = $url;
$imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';

Expand Down Expand Up @@ -1254,7 +1254,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids[] = $attachment_id;
$url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$url = _upload_get_url( "image$i.jpg" );
$ids_srcs[] = $url;
$imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
}
Expand Down Expand Up @@ -1297,7 +1297,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids[] = $attachment_id;
$url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$url = _upload_get_url( "image$i.jpg" );
$ids_srcs[] = $url;
$imgs[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';

Expand Down Expand Up @@ -1340,7 +1340,7 @@
$metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids[] = $attachment_id;
$url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
$url = _upload_get_url( "image$i.jpg" );
$ids_srcs[] = $url;
$imgs[] = '<!-- wp:image {"id":' . $attachment_id . ',"sizeSlug":"large","linkDestination":"none"} --><figure class="wp-block-image size-large"><img src="' . $url . '" /></figure><!-- /wp:image -->';

Expand Down Expand Up @@ -1823,7 +1823,7 @@
)
);

$image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
$image_url = _upload_get_url( $image_path );
$this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
}

Expand All @@ -1841,7 +1841,7 @@
)
);

$image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
$image_url = _upload_get_url( $image_path, 'https' );
$this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
}

Expand Down Expand Up @@ -1869,7 +1869,7 @@
)
);

$image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path_upper_case;
$image_url = _upload_get_url( $image_path_upper_case );
$this->assertSame( $attachment_id_upper_case, attachment_url_to_postid( $image_url ) );
}

Expand Down Expand Up @@ -2399,7 +2399,7 @@

$year_month = gmdate( 'Y/m' );
$image_meta = wp_get_attachment_metadata( self::$large_id );
$uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
$uploads_dir_url = _upload_get_url();

// Set up test cases for all expected size names.
$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
Expand Down Expand Up @@ -2442,7 +2442,7 @@
* @ticket 33641
* @requires function imagejpeg
*/
public function test_wp_calculate_image_srcset_no_date_uploads() {

Check warning on line 2445 in tests/phpunit/tests/media.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 / MySQL 8.4 (test reporting enabled)

Slow PHPUnit test

Tests_Media::test_wp_calculate_image_srcset_no_date_uploads took 1.225888s
$_wp_additional_image_sizes = wp_get_additional_image_sizes();

// Disable date organized uploads.
Expand All @@ -2453,7 +2453,7 @@
$id = self::factory()->attachment->create_upload_object( $filename );

$image_meta = wp_get_attachment_metadata( $id );
$uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
$uploads_dir_url = _upload_get_url();

// Set up test cases for all expected size names.
$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
Expand Down Expand Up @@ -2544,7 +2544,7 @@

$year_month = gmdate( 'Y/m' );
$image_meta = wp_get_attachment_metadata( self::$large_id );
$uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
$uploads_dir_url = _upload_get_url();

// Set up test cases for all expected size names.
$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
Expand Down Expand Up @@ -2645,7 +2645,7 @@
public function test_wp_calculate_image_srcset_ratio_variance() {
// Mock data for this test.
$size_array = array( 218, 300 );
$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png';
$image_src = _upload_get_url( '2015/12/test-768x1055-218x300.png' );
$image_meta = array(
'width' => 768,
'height' => 1055,
Expand Down Expand Up @@ -2678,7 +2678,7 @@
),
);

$uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
$uploads_url = _upload_get_url( '2015/12/' );

$expected_srcset = $uploads_url . 'test-768x1055-218x300.png 218w, ' .
$uploads_url . 'test-768x1055-600x824.png 600w, ' .
Expand All @@ -2694,7 +2694,7 @@
public function test_wp_calculate_image_srcset_include_src() {
// Mock data for this test.
$size_array = array( 2000, 1000 );
$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png';
$image_src = _upload_get_url( '2015/12/test.png' );
$image_meta = array(
'width' => 2000,
'height' => 1000,
Expand Down Expand Up @@ -2727,7 +2727,7 @@
),
);

$uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
$uploads_url = _upload_get_url( '2015/12/' );

$expected_srcset = $uploads_url . 'test.png 2000w, ' .
$uploads_url . 'test-300x150.png 300w, ' .
Expand All @@ -2742,7 +2742,7 @@
*/
public function test_wp_calculate_image_srcset_corrupted_image_meta() {
$size_array = array( 300, 150 );
$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png';
$image_src = _upload_get_url( '2015/12/test-300x150.png' );
$image_meta = array(
'width' => 1600,
'height' => 800,
Expand Down Expand Up @@ -2776,10 +2776,10 @@
);

$srcset = array(
300 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w',
768 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w',
1024 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w',
1600 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 1600w',
300 => _upload_get_url( '2015/12/test-300x150.png' ) . ' 300w',
768 => _upload_get_url( '2015/12/test-768x384.png' ) . ' 768w',
1024 => _upload_get_url( '2015/12/test-1024x512.png' ) . ' 1024w',
1600 => _upload_get_url( '2015/12/test.png' ) . ' 1600w',
);

// No sizes array.
Expand Down Expand Up @@ -2816,7 +2816,7 @@
*/
public function test_wp_calculate_image_srcset_with_spaces_in_filenames() {
// Mock data for this test.
$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test image-300x150.png';
$image_src = _upload_get_url( '2015/12/test image-300x150.png' );
$image_meta = array(
'width' => 3000,
'height' => 1500,
Expand Down Expand Up @@ -2849,7 +2849,7 @@
),
);

$uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
$uploads_url = _upload_get_url( '2015/12/' );

$expected_srcset = $uploads_url . 'test%20image-300x150.png 300w, ' .
$uploads_url . 'test%20image-768x384.png 768w, ' .
Expand All @@ -2871,7 +2871,7 @@
$srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );

$year_month = gmdate( 'Y/m' );
$uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
$uploads_dir = _upload_get_url();

// Set up test cases for all expected size names.
$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
Expand Down Expand Up @@ -3074,7 +3074,7 @@
$img = wp_img_tag_add_loading_optimization_attrs( $img, 'test' );

// Replace the src URL.
$image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img );
$image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="' . _upload_get_url( 'foo.jpg' ) . '"', $img );

$this->assertSame( $image_wrong_src, wp_filter_content_tags( $image_wrong_src ) );
}
Expand Down Expand Up @@ -3203,8 +3203,8 @@
),
);

$full_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'];
$large_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
$full_src = _upload_get_url( $image_meta['file'] );
$large_src = _upload_get_url( $image_meta['sizes']['large']['file'] );

// Test with soft resized size array.
$size_array = array( 900, 450 );
Expand Down Expand Up @@ -3301,11 +3301,11 @@

// Test using the large file size.
$size_array = array( 1024, 512 );
$image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
$image_url = _upload_get_url( $image_meta['sizes']['large']['file'] );

$_SERVER['HTTPS'] = 'on';

$uploads_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
$uploads_url = _upload_get_url( '', 'https' );

$expected = $uploads_url . 'test-1024x512.jpg 1024w, ' .
$uploads_url . 'test-300x150.jpg 300w, ' .
Expand Down Expand Up @@ -3434,7 +3434,7 @@

$basename = wp_basename( self::$large_filename, '.jpg' );
$year_month = gmdate( 'Y/m' );
$uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/';
$uploads_url = _upload_get_url( $year_month . '/' );

$expected = '<img width="999" height="999" ' .
'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' .
Expand Down Expand Up @@ -6230,7 +6230,7 @@
*
* @ticket 56442
*/
public function test_quality_with_image_conversion_file_sizes() {

Check warning on line 6233 in tests/phpunit/tests/media.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 / MySQL 8.4 (test reporting enabled)

Slow PHPUnit test

Tests_Media::test_quality_with_image_conversion_file_sizes took 3.004894s
add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
$temp_dir = get_temp_dir();
$file = $temp_dir . '/33772.jpg';
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/media/getAdjacentImageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function data_get_adjacent_image_link() {
'when has previous link' => array(
'current_attachment_index' => 3,
'expected_attachment_index' => 2,
'expected' => '<a href=\'http://' . WP_TESTS_DOMAIN . '/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>',
'expected' => '<a href=\'http://' . WP_TESTS_DOMAIN . '/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . _upload_get_url( 'image2.jpg' ) . '" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>',
),
'with text when has previous link' => array(
'current_attachment_index' => 3,
Expand All @@ -43,7 +43,7 @@ public static function data_get_adjacent_image_link() {
'when has next link' => array(
'current_attachment_index' => 4,
'expected_attachment_index' => 5,
'expected' => '<a href=\'http://' . WP_TESTS_DOMAIN . '/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>',
'expected' => '<a href=\'http://' . WP_TESTS_DOMAIN . '/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . _upload_get_url( 'image5.jpg' ) . '" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>',
'args' => array( 'prev' => false ),
),
'with text when has next link' => array(
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/media/getNextImageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function data_get_next_image_link() {
'when has next link' => array(
'current_attachment_index' => 4,
'expected_attachment_index' => 5,
'expected' => '<a href=\'http://' . WP_TESTS_DOMAIN . '/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>',
'expected' => '<a href=\'http://' . WP_TESTS_DOMAIN . '/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . _upload_get_url( 'image5.jpg' ) . '" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a>',
),
'with text when has next link' => array(
'current_attachment_index' => 4,
Expand Down
Loading
Loading