wordpress-develop/tests/phpunit/tests/term/wpTerm.php
Scott Taylor 02319efa71 Unit Tests:
* Automatically delete objects that we were created during `wpSetUpBeforeClass` - posts, comments, terms (except 1), and user (except 1)
* The amount of leftover data between tests was breathtaking - use the new function: `_delete_all_data()`
* Commit database transactions for all `TestCase`s, not just those that implement `wpSetUpBeforeClass` and `wpTearDownAfterClass`
* The tests run 10-20 seconds faster now

See #37699.


git-svn-id: https://develop.svn.wordpress.org/trunk@38398 602fd350-edb4-49c9-b593-d223f7449a82
2016-08-27 08:35:16 +00:00

109 lines
2.2 KiB
PHP

<?php
/**
* @group taxonomy
*/
class Tests_Term_WpTerm extends WP_UnitTestCase {
protected static $term_id;
public function setUp() {
parent::setUp();
register_taxonomy( 'wptests_tax', 'post' );
}
public static function wpSetUpBeforeClass( $factory ) {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
// Ensure that there is a term with ID 1.
if ( ! get_term( 1 ) ) {
$wpdb->insert( $wpdb->terms, array(
'term_id' => 1,
) );
$wpdb->insert( $wpdb->term_taxonomy, array(
'term_id' => 1,
'taxonomy' => 'wptests_tax',
) );
clean_term_cache( 1, 'wptests_tax' );
}
self::$term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_work_for_numeric_string() {
$found = WP_Term::get_instance( (string) self::$term_id );
$this->assertSame( self::$term_id, $found->term_id );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_fail_for_negative_number() {
$found = WP_Term::get_instance( -self::$term_id );
$this->assertFalse( $found );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_fail_for_non_numeric_string() {
$found = WP_Term::get_instance( 'abc' );
$this->assertFalse( $found );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_fail_for_bool() {
$found = WP_Term::get_instance( true );
$this->assertFalse( $found );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_succeed_for_float_that_is_equal_to_post_id() {
$found = WP_Term::get_instance( 1.0 );
$this->assertSame( 1, $found->term_id );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_fail_for_float() {
$found = WP_Term::get_instance( 1.6 );
$this->assertFalse( $found );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_fail_for_array() {
$found = WP_Term::get_instance( array( 1 ) );
$this->assertFalse( $found );
}
/**
* @ticket 37738
*/
public function test_get_instance_should_fail_for_class() {
$class = new stdClass();
$found = WP_Term::get_instance( $class );
$this->assertFalse( $found );
}
}