wordpress-develop/tests/phpunit/tests/post/formats.php
Sergey Biryukov bca693b190 Build/Test Tools: Replace assertInternalType() usage in unit tests.
The `assertInternalType()` and `assertNotInternalType()` methods are deprecated in PHPUnit 8 and removed in PHPUnit 9.

While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+.

These methods introduced in PHPUnit 7.5 should be used as an alternative:

* `assertIsArray()`
* `assertIsBool()`
* `assertIsFloat()`
* `assertIsInt()`
* `assertIsNumeric()`
* `assertIsObject()`
* `assertIsResource()`
* `assertIsString()`
* `assertIsScalar()`
* `assertIsCallable()`
* `assertIsIterable()`
* `assertIsNotArray()`
* `assertIsNotBool()`
* `assertIsNotFloat()`
* `assertIsNotInt()`
* `assertIsNotNumeric()`
* `assertIsNotObject()`
* `assertIsNotResource()`
* `assertIsNotString()`
* `assertIsNotScalar()`
* `assertIsNotCallable()`
* `assertIsNotIterable()`

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods are now added to the `WP_UnitTestCase` class for PHPUnit < 7.5.

Props pbearne, jrf, dd32, SergeyBiryukov.
Fixes #53491. See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51331 602fd350-edb4-49c9-b593-d223f7449a82
2021-07-05 17:21:53 +00:00

156 lines
5.5 KiB
PHP

<?php
/**
* @group post
*/
class Tests_Post_Formats extends WP_UnitTestCase {
function test_set_get_post_format_for_post() {
$post_id = self::factory()->post->create();
$format = get_post_format( $post_id );
$this->assertFalse( $format );
$result = set_post_format( $post_id, 'aside' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 1, count( $result ) );
$format = get_post_format( $post_id );
$this->assertSame( 'aside', $format );
$result = set_post_format( $post_id, 'standard' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 0, count( $result ) );
$result = set_post_format( $post_id, '' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 0, count( $result ) );
}
/**
* @ticket 22473
*/
function test_set_get_post_format_for_page() {
$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$format = get_post_format( $post_id );
$this->assertFalse( $format );
$result = set_post_format( $post_id, 'aside' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 1, count( $result ) );
// The format can be set but not retrieved until it is registered.
$format = get_post_format( $post_id );
$this->assertFalse( $format );
// Register format support for the page post type.
add_post_type_support( 'page', 'post-formats' );
// The previous set can now be retrieved.
$format = get_post_format( $post_id );
$this->assertSame( 'aside', $format );
$result = set_post_format( $post_id, 'standard' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 0, count( $result ) );
$result = set_post_format( $post_id, '' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 0, count( $result ) );
remove_post_type_support( 'page', 'post-formats' );
}
function test_has_format() {
$post_id = self::factory()->post->create();
$this->assertFalse( has_post_format( 'standard', $post_id ) );
$this->assertFalse( has_post_format( '', $post_id ) );
$result = set_post_format( $post_id, 'aside' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 1, count( $result ) );
$this->assertTrue( has_post_format( 'aside', $post_id ) );
$result = set_post_format( $post_id, 'standard' );
$this->assertNotWPError( $result );
$this->assertIsArray( $result );
$this->assertSame( 0, count( $result ) );
// Standard is a special case. It shows as false when set.
$this->assertFalse( has_post_format( 'standard', $post_id ) );
// Dummy format type.
$this->assertFalse( has_post_format( 'dummy', $post_id ) );
// Dummy post ID.
$this->assertFalse( has_post_format( 'aside', 12345 ) );
}
/**
* @ticket 23570
*/
function test_get_url_in_content() {
$link = 'http://nytimes.com';
$commentary = 'This is my favorite link';
$link_with_commentary = <<<DATA
$link
$commentary
DATA;
$href = '<a href="http://nytimes.com">NYT</a>';
$href_with_commentary = <<<DATA
$href
$commentary
DATA;
$link_post_id = self::factory()->post->create( array( 'post_content' => $link ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $link_post_id ) );
$this->assertFalse( $content_link );
$link_with_post_id = self::factory()->post->create( array( 'post_content' => $link_with_commentary ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $link_with_post_id ) );
$this->assertFalse( $content_link );
$content_link = get_url_in_content( get_post_field( 'post_content', $link_post_id ) );
$this->assertFalse( $content_link );
$content_link = get_url_in_content( get_post_field( 'post_content', $link_with_post_id ) );
$this->assertFalse( $content_link );
$empty_post_id = self::factory()->post->create( array( 'post_content' => '' ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $empty_post_id ) );
$this->assertFalse( $content_link );
$comm_post_id = self::factory()->post->create( array( 'post_content' => $commentary ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $comm_post_id ) );
$this->assertFalse( $content_link );
// Now with an href.
$href_post_id = self::factory()->post->create( array( 'post_content' => $href ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $href_post_id ) );
$this->assertSame( $link, $content_link );
$href_with_post_id = self::factory()->post->create( array( 'post_content' => $href_with_commentary ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $href_with_post_id ) );
$this->assertSame( $link, $content_link );
$content_link = get_url_in_content( get_post_field( 'post_content', $href_post_id ) );
$this->assertSame( $link, $content_link );
$content_link = get_url_in_content( get_post_field( 'post_content', $href_with_post_id ) );
$this->assertSame( $link, $content_link );
$empty_post_id = self::factory()->post->create( array( 'post_content' => '' ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $empty_post_id ) );
$this->assertFalse( $content_link );
$comm_post_id = self::factory()->post->create( array( 'post_content' => $commentary ) );
$content_link = get_url_in_content( get_post_field( 'post_content', $comm_post_id ) );
$this->assertFalse( $content_link );
}
}