mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Since PHP 8.1, the `strftime()` and `gmstrftime()` functions are deprecated:
> The `strftime()` and `gmstrftime()` functions exhibit similar issues as `strptime()`, in that the formats they support, as well as their behavior, is platform-dependent. Unlike `strptime()`, these functions are available on Windows, though with a different feature set than on Linux. Musl-based distributions like Alpine do not support timezone-related format specifiers correctly. These functions are also locale-based, and as such may exhibit thread-safety issues.
>
> `date()` or `DateTime::format()` provide portable alternatives, and `IntlDateFormatter::format()` provides a more sophisticated, localization-aware alternative.
Reference: [https://wiki.php.net/rfc/deprecations_php_8_1#strftime_and_gmstrftime PHP RFC: Deprecations for PHP 8.1: strftime() and gmstrftime()]
> The `strftime()` and `gmstrftime()` functions have been deprecated in favor of
> `date()/DateTime::format()` (for locale-independent formatting) or
> `IntlDateFormatter::format()` (for locale-dependent formatting).
Reference: [1cf4fb739f/UPGRADING (L379-L381) PHP 8.1 Upgrade Notes].
Aside from one instance in SimplePie, the `strftime()` and `gmstrftime()` functions are only used within the test suite of WordPress to create formatted timestamps.
As the function is used in test code, this leads to test warnings like this on PHP 8.1:
{{{
Deprecated: Function strftime() is deprecated in path/to/tests/phpunit/tests/canonical/postStatus.php on line 37
}}}
These calls can all be safely converted to use a pattern along the lines of:
{{{#!php
<?php
date_format( date_create( 'time phrase or timestamp' ), $format )
}}}
Other references:
* [https://www.php.net/manual/en/function.strftime.php PHP Manual: strftime()] (for the old format string characters)
* [https://www.php.net/manual/en/datetime.format.php PHP Manual: DateTime::format()] (for the new format string characters)
* [https://www.php.net/manual/en/datetime.construct.php PHP Manual: DateTime::__construct()] (see Example 2 for a Unix timestamp code sample)
Props jrf, SergeyBiryukov.
Fixes #53897.
git-svn-id: https://develop.svn.wordpress.org/trunk@51587 602fd350-edb4-49c9-b593-d223f7449a82
306 lines
8.6 KiB
PHP
306 lines
8.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group oembed
|
|
*/
|
|
class Tests_Embed_Template extends WP_UnitTestCase {
|
|
function test_oembed_output_post() {
|
|
$user = self::factory()->user->create_and_get(
|
|
array(
|
|
'display_name' => 'John Doe',
|
|
)
|
|
);
|
|
|
|
$post_id = self::factory()->post->create(
|
|
array(
|
|
'post_author' => $user->ID,
|
|
'post_title' => 'Hello World',
|
|
'post_content' => 'Foo Bar',
|
|
'post_excerpt' => 'Bar Baz',
|
|
)
|
|
);
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
|
|
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_embed' );
|
|
|
|
// `print_embed_scripts()` assumes `wp-includes/js/wp-embed-template.js` is present:
|
|
self::touch( ABSPATH . WPINC . '/js/wp-embed-template.js' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringNotContainsString( 'That embed can’t be found.', $actual );
|
|
$this->assertStringContainsString( 'Hello World', $actual );
|
|
}
|
|
|
|
function test_oembed_output_post_with_thumbnail() {
|
|
$post_id = self::factory()->post->create(
|
|
array(
|
|
'post_title' => 'Hello World',
|
|
'post_content' => 'Foo Bar',
|
|
'post_excerpt' => 'Bar Baz',
|
|
)
|
|
);
|
|
$file = DIR_TESTDATA . '/images/canola.jpg';
|
|
$attachment_id = self::factory()->attachment->create_object(
|
|
$file,
|
|
$post_id,
|
|
array(
|
|
'post_mime_type' => 'image/jpeg',
|
|
)
|
|
);
|
|
set_post_thumbnail( $post_id, $attachment_id );
|
|
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
|
|
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_embed' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringNotContainsString( 'That embed can’t be found.', $actual );
|
|
$this->assertStringContainsString( 'Hello World', $actual );
|
|
$this->assertStringContainsString( 'canola.jpg', $actual );
|
|
}
|
|
|
|
function test_oembed_output_404() {
|
|
$this->go_to( home_url( '/?p=123&embed=true' ) );
|
|
$GLOBALS['wp_query']->query_vars['embed'] = true;
|
|
|
|
$this->assertQueryTrue( 'is_404', 'is_embed' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringContainsString( 'That embed can’t be found.', $actual );
|
|
}
|
|
|
|
function test_oembed_output_attachment() {
|
|
$post = self::factory()->post->create_and_get();
|
|
$file = DIR_TESTDATA . '/images/canola.jpg';
|
|
$attachment_id = self::factory()->attachment->create_object(
|
|
$file,
|
|
$post->ID,
|
|
array(
|
|
'post_mime_type' => 'image/jpeg',
|
|
'post_title' => 'Hello World',
|
|
'post_content' => 'Foo Bar',
|
|
'post_excerpt' => 'Bar Baz',
|
|
)
|
|
);
|
|
|
|
$this->go_to( get_post_embed_url( $attachment_id ) );
|
|
|
|
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_attachment', 'is_embed' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringNotContainsString( 'That embed can’t be found.', $actual );
|
|
$this->assertStringContainsString( 'Hello World', $actual );
|
|
$this->assertStringContainsString( 'canola.jpg', $actual );
|
|
}
|
|
|
|
function test_oembed_output_draft_post() {
|
|
$post_id = self::factory()->post->create(
|
|
array(
|
|
'post_title' => 'Hello World',
|
|
'post_content' => 'Foo Bar',
|
|
'post_excerpt' => 'Bar Baz',
|
|
'post_status' => 'draft',
|
|
)
|
|
);
|
|
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
|
|
$this->assertQueryTrue( 'is_404', 'is_embed' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringContainsString( 'That embed can’t be found.', $actual );
|
|
}
|
|
|
|
function test_oembed_output_scheduled_post() {
|
|
$post_id = self::factory()->post->create(
|
|
array(
|
|
'post_title' => 'Hello World',
|
|
'post_content' => 'Foo Bar',
|
|
'post_excerpt' => 'Bar Baz',
|
|
'post_status' => 'future',
|
|
'post_date' => date_format( date_create( '+1 day' ), 'Y-m-d H:i:s' ),
|
|
)
|
|
);
|
|
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
|
|
$this->assertQueryTrue( 'is_404', 'is_embed' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringContainsString( 'That embed can’t be found.', $actual );
|
|
}
|
|
|
|
function test_oembed_output_private_post() {
|
|
$post_id = self::factory()->post->create(
|
|
array(
|
|
'post_title' => 'Hello World',
|
|
'post_content' => 'Foo Bar',
|
|
'post_excerpt' => 'Bar Baz',
|
|
'post_status' => 'private',
|
|
)
|
|
);
|
|
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
|
|
$this->assertQueryTrue( 'is_404', 'is_embed' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringContainsString( 'That embed can’t be found.', $actual );
|
|
}
|
|
|
|
function test_oembed_output_private_post_with_permissions() {
|
|
$user_id = self::factory()->user->create( array( 'role' => 'editor' ) );
|
|
wp_set_current_user( $user_id );
|
|
|
|
$post_id = self::factory()->post->create(
|
|
array(
|
|
'post_title' => 'Hello World',
|
|
'post_content' => 'Foo Bar',
|
|
'post_excerpt' => 'Bar Baz',
|
|
'post_status' => 'private',
|
|
'post_author' => $user_id,
|
|
)
|
|
);
|
|
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
|
|
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_embed' );
|
|
|
|
ob_start();
|
|
require ABSPATH . WPINC . '/theme-compat/embed.php';
|
|
$actual = ob_get_clean();
|
|
|
|
$doc = new DOMDocument();
|
|
$this->assertTrue( $doc->loadHTML( $actual ) );
|
|
$this->assertStringNotContainsString( 'That embed can’t be found.', $actual );
|
|
$this->assertStringContainsString( 'Hello World', $actual );
|
|
}
|
|
|
|
function test_wp_embed_excerpt_more_no_embed() {
|
|
$GLOBALS['wp_query'] = new WP_Query();
|
|
|
|
$this->assertSame( 'foo bar', wp_embed_excerpt_more( 'foo bar' ) );
|
|
}
|
|
|
|
function test_wp_embed_excerpt_more() {
|
|
$post_id = self::factory()->post->create(
|
|
array(
|
|
'post_title' => 'Foo Bar',
|
|
'post_content' => 'Bar Baz',
|
|
)
|
|
);
|
|
|
|
$this->assertSame( '', wp_embed_excerpt_more( '' ) );
|
|
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
|
|
$actual = wp_embed_excerpt_more( '' );
|
|
|
|
$expected = sprintf(
|
|
' … <a href="%s" class="wp-embed-more" target="_top">Continue reading <span class="screen-reader-text">Foo Bar</span></a>',
|
|
get_the_permalink()
|
|
);
|
|
|
|
$this->assertSame( $expected, $actual );
|
|
}
|
|
|
|
function test_is_embed_post() {
|
|
$this->assertFalse( is_embed() );
|
|
|
|
$post_id = self::factory()->post->create();
|
|
$this->go_to( get_post_embed_url( $post_id ) );
|
|
$this->assertTrue( is_embed() );
|
|
}
|
|
|
|
function test_is_embed_attachment() {
|
|
$post_id = self::factory()->post->create();
|
|
$file = DIR_TESTDATA . '/images/canola.jpg';
|
|
$attachment_id = self::factory()->attachment->create_object(
|
|
$file,
|
|
$post_id,
|
|
array(
|
|
'post_mime_type' => 'image/jpeg',
|
|
)
|
|
);
|
|
$this->go_to( get_post_embed_url( $attachment_id ) );
|
|
$this->assertTrue( is_embed() );
|
|
}
|
|
|
|
function test_is_embed_404() {
|
|
$this->go_to( home_url( '/?p=12345&embed=true' ) );
|
|
$this->assertTrue( is_embed() );
|
|
}
|
|
|
|
function test_get_post_embed_html_non_existent_post() {
|
|
$this->assertFalse( get_post_embed_html( 200, 200, 0 ) );
|
|
$this->assertFalse( get_post_embed_html( 200, 200 ) );
|
|
}
|
|
|
|
function test_get_post_embed_html() {
|
|
$post_id = self::factory()->post->create();
|
|
$title = esc_attr(
|
|
sprintf(
|
|
__( '“%1$s” — %2$s' ),
|
|
get_the_title( $post_id ),
|
|
get_bloginfo( 'name' )
|
|
)
|
|
);
|
|
|
|
$expected = '<iframe sandbox="allow-scripts" security="restricted" src="' . esc_url( get_post_embed_url( $post_id ) ) . '" width="200" height="200" title="' . $title . '" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>';
|
|
|
|
$this->assertStringEndsWith( $expected, get_post_embed_html( 200, 200, $post_id ) );
|
|
}
|
|
|
|
function test_add_host_js() {
|
|
wp_oembed_add_host_js();
|
|
|
|
$this->assertTrue( wp_script_is( 'wp-embed' ) );
|
|
}
|
|
|
|
/**
|
|
* Confirms that no ampersands exist in src/wp-includes/js/wp-embed.js.
|
|
*
|
|
* See also the `verify:wp-embed` Grunt task for verifying the built file.
|
|
*
|
|
* @ticket 34698
|
|
*/
|
|
function test_js_no_ampersands() {
|
|
$this->assertStringNotContainsString( '&', file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ) );
|
|
}
|
|
}
|