mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Some PHPUnit tests were concerned with the state of files in the `build` directory. In order to allow the tests to run without requiring a build to be run first, these have been moved into assertions that run after the build step (and therefore cause it to fail if they do not pass), or into QUnit tests as necessary. Various other PHPUnit tests implictly depend on built JavaScript files being present. These files are now touched during the test setup to avoid PHP warnings if the `build` files are not present. The `wp-tests-config-sample.php` file and the GitHub Actions configuration have also been changed so `ABSPATH` uses `src` instead of `build`, therefore allowing the PHPUnit tests to be run without a build having to be run first. This means all new local installations of WordPress will use `src` for PHPUnit testing. If you would like to switch your existing installation over then change the location of `ABSPATH` in `wp-tests-config.php` to point to `src` instead of `build`. Props peterwilsoncc, iandunn, gziolo, desroj, johnbillion Fixes #51734 See #45863 git-svn-id: https://develop.svn.wordpress.org/trunk@50441 602fd350-edb4-49c9-b593-d223f7449a82
241 lines
7.2 KiB
PHP
241 lines
7.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group oembed
|
|
*/
|
|
class Tests_WP_oEmbed extends WP_UnitTestCase {
|
|
/**
|
|
* @var WP_oEmbed
|
|
*/
|
|
protected $oembed;
|
|
|
|
public $pre_oembed_result_filtered = false;
|
|
|
|
public function setUp() {
|
|
parent::setUp();
|
|
|
|
require_once ABSPATH . WPINC . '/class-wp-oembed.php';
|
|
$this->oembed = _wp_oembed_get_object();
|
|
|
|
$this->pre_oembed_result_filtered = false;
|
|
|
|
// `get_post_embed_html()` assumes `wp-includes/js/wp-embed.js` is present:
|
|
self::touch( ABSPATH . WPINC . '/js/wp-embed.js' );
|
|
}
|
|
|
|
public function _filter_pre_oembed_result( $result ) {
|
|
// If this is not null, the oEmbed result has been filtered before any HTTP requests were made.
|
|
$this->pre_oembed_result_filtered = $result;
|
|
|
|
// Return false to prevent HTTP requests during tests.
|
|
return $result ? $result : false;
|
|
}
|
|
|
|
public function test_wp_filter_pre_oembed_result_prevents_http_request_for_internal_permalinks() {
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
$this->assertNotFalse( $this->pre_oembed_result_filtered );
|
|
$this->assertSame( $this->pre_oembed_result_filtered, $actual );
|
|
}
|
|
|
|
public function test_wp_filter_pre_oembed_result_prevents_http_request_when_viewing_the_post() {
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
|
|
$this->go_to( $permalink );
|
|
$this->assertQueryTrue( 'is_single', 'is_singular' );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
$this->assertNotFalse( $this->pre_oembed_result_filtered );
|
|
$this->assertSame( $this->pre_oembed_result_filtered, $actual );
|
|
}
|
|
|
|
public function test_wp_filter_pre_oembed_result_non_existent_post() {
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
|
|
$this->go_to( $permalink );
|
|
$this->assertQueryTrue( 'is_single', 'is_singular' );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( 'https://example.com/' );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
$this->assertNotFalse( $this->pre_oembed_result_filtered );
|
|
$this->assertFalse( $actual );
|
|
}
|
|
|
|
/**
|
|
* @ticket 40673
|
|
* @group multisite
|
|
* @group ms-required
|
|
*/
|
|
public function test_wp_filter_pre_oembed_result_multisite_root_root() {
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
$this->assertNotNull( $this->pre_oembed_result_filtered );
|
|
$this->assertSame( $this->pre_oembed_result_filtered, $actual );
|
|
}
|
|
|
|
/**
|
|
* @ticket 40673
|
|
* @group multisite
|
|
* @group ms-required
|
|
*/
|
|
public function test_wp_filter_pre_oembed_result_multisite_sub_samesub() {
|
|
$user_id = self::factory()->user->create();
|
|
|
|
$blog_id = self::factory()->blog->create(
|
|
array(
|
|
'user_id' => $user_id,
|
|
)
|
|
);
|
|
|
|
switch_to_blog( $blog_id );
|
|
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
restore_current_blog();
|
|
|
|
$this->assertNotNull( $this->pre_oembed_result_filtered );
|
|
$this->assertSame( $this->pre_oembed_result_filtered, $actual );
|
|
}
|
|
|
|
/**
|
|
* @ticket 40673
|
|
* @group multisite
|
|
* @group ms-required
|
|
*/
|
|
public function test_wp_filter_pre_oembed_result_multisite_sub_othersub() {
|
|
$user_id = self::factory()->user->create();
|
|
|
|
$blog_id = self::factory()->blog->create(
|
|
array(
|
|
'user_id' => $user_id,
|
|
)
|
|
);
|
|
|
|
switch_to_blog( $blog_id );
|
|
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
|
|
$blog_id = self::factory()->blog->create(
|
|
array(
|
|
'user_id' => $user_id,
|
|
)
|
|
);
|
|
|
|
switch_to_blog( $blog_id );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
restore_current_blog();
|
|
|
|
$this->assertNotNull( $this->pre_oembed_result_filtered );
|
|
$this->assertSame( $this->pre_oembed_result_filtered, $actual );
|
|
}
|
|
|
|
/**
|
|
* @ticket 40673
|
|
* @group multisite
|
|
* @group ms-required
|
|
*/
|
|
public function test_wp_filter_pre_oembed_result_multisite_sub_main() {
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
$user_id = self::factory()->user->create();
|
|
$blog_id = self::factory()->blog->create(
|
|
array(
|
|
'user_id' => $user_id,
|
|
)
|
|
);
|
|
|
|
switch_to_blog( $blog_id );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
restore_current_blog();
|
|
|
|
$this->assertNotNull( $this->pre_oembed_result_filtered );
|
|
$this->assertSame( $this->pre_oembed_result_filtered, $actual );
|
|
}
|
|
|
|
/**
|
|
* @ticket 40673
|
|
* @group multisite
|
|
* @group ms-required
|
|
*/
|
|
public function test_wp_filter_pre_oembed_result_multisite_preserves_switched_state() {
|
|
$user_id = self::factory()->user->create();
|
|
|
|
$blog_id = self::factory()->blog->create( array( 'user_id' => $user_id ) );
|
|
switch_to_blog( $blog_id );
|
|
|
|
$expected_stack = $GLOBALS['_wp_switched_stack'];
|
|
|
|
$post_id = self::factory()->post->create();
|
|
$permalink = get_permalink( $post_id );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
$actual_stack = $GLOBALS['_wp_switched_stack'];
|
|
|
|
restore_current_blog();
|
|
|
|
$this->assertNotNull( $this->pre_oembed_result_filtered );
|
|
$this->assertSame( $this->pre_oembed_result_filtered, $actual );
|
|
$this->assertSame( $expected_stack, $actual_stack );
|
|
}
|
|
|
|
/**
|
|
* @ticket 40673
|
|
* @group multisite
|
|
* @group ms-required
|
|
*/
|
|
public function test_wp_filter_pre_oembed_result_multisite_restores_state_if_no_post_is_found() {
|
|
$current_blog_id = get_current_blog_id();
|
|
|
|
$user_id = self::factory()->user->create();
|
|
$blog_id = self::factory()->blog->create(
|
|
array(
|
|
'user_id' => $user_id,
|
|
)
|
|
);
|
|
|
|
$permalink = get_home_url( $blog_id, '/foo/' );
|
|
|
|
add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
$actual = $this->oembed->get_html( $permalink );
|
|
remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) );
|
|
|
|
$this->assertNull( $this->pre_oembed_result_filtered );
|
|
$this->assertFalse( $actual );
|
|
$this->assertSame( $current_blog_id, get_current_blog_id() );
|
|
}
|
|
}
|