Make WordPress Core

Ticket #11058: 11058-5.diff

File 11058-5.diff, 4.5 KB (added by leewillis77, 13 years ago)

Updated patch to remove use of rand_str in tests based on advice from tierra in IRC.

  • src/wp-includes/taxonomy.php

     
    482482 * @param string $object_type Name of the object type
    483483 * @return bool True if successful, false if not
    484484 */
    485 function register_taxonomy_for_object_type( $taxonomy, $object_type) {
     485function register_taxonomy_for_object_type( $taxonomy, $object_type ) {
    486486        global $wp_taxonomies;
    487487
    488         if ( !isset($wp_taxonomies[$taxonomy]) )
     488        if ( ! isset( $wp_taxonomies[$taxonomy] ) )
    489489                return false;
    490490
    491         if ( ! get_post_type_object($object_type) )
     491        // If a single taxonomy handles both a post type and a non-post type, you
     492        // can end up with ID conflicts, so we only allow this for post-type
     493        // objects.
     494        if ( ! get_post_type_object( $object_type ) )
    492495                return false;
    493496
    494497        if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )
     
    497500        return true;
    498501}
    499502
     503/**
     504 * Remove an already registered taxonomy from an object type.
     505 *
     506 * @package WordPress
     507 * @subpackage Taxonomy
     508 * @since 3.7.0
     509 * @uses $wp_taxonomies Modifies taxonomy object
     510 *
     511 * @param string $taxonomy Name of taxonomy object
     512 * @param string $object_type Name of the object type
     513 * @return bool True if successful, false if not
     514 */
     515function unregister_taxonomy_from_object_type( $taxonomy, $object_type ) {
     516        global $wp_taxonomies;
     517
     518        if ( ! isset( $wp_taxonomies[$taxonomy] ) )
     519                return false;
     520
     521        // For consistency with register_taxonomy_for_object_type()
     522        if ( ! get_post_type_object( $object_type ) )
     523                return false;
     524
     525        foreach ( array_keys( $wp_taxonomies[$taxonomy]->object_type ) as $array_key ) {
     526                if ( $wp_taxonomies[$taxonomy]->object_type[$array_key] === $object_type ) {
     527                        unset( $wp_taxonomies[$taxonomy]->object_type[$array_key] );
     528                        return true;
     529                }
     530        }
     531
     532        return false;
     533}
     534
    500535//
    501536// Term API
    502537//
  • tests/phpunit/tests/taxonomy.php

     
    103103        function test_register_long_taxonomy() {
    104104                $this->assertInstanceOf( 'WP_Error', register_taxonomy( 'abcdefghijklmnopqrstuvwxyz0123456789', 'post', array() ) );
    105105        }
     106
     107        function test_registering_taxonomies_to_object_types() {
     108
     109                // Create a taonomy to test with
     110                $tax = 'test_tax';
     111                $this->assertFalse( taxonomy_exists($tax) );
     112                register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     113
     114                // Create a post type to test with
     115                $post_type = 'test_cpt';
     116                $this->assertFalse( get_post_type( $post_type ) );
     117                $this->assertObjectHasAttribute( 'name', register_post_type( $post_type ) );
     118
     119                // Core taxonomy, core post type
     120                $this->assertTrue( unregister_taxonomy_from_object_type( 'category', 'post' ) );
     121                $this->assertFalse( unregister_taxonomy_from_object_type( 'category', 'post' ) );
     122                $this->assertTrue( register_taxonomy_for_object_type( 'category', 'post' ) );
     123
     124                // Core taxonomy, non-core post type
     125                $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
     126                $this->assertTrue( unregister_taxonomy_from_object_type( 'category', $post_type ) );
     127                $this->assertFalse( unregister_taxonomy_from_object_type( 'category', $post_type ) );
     128                $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
     129
     130                // Core taxonomies, non-post object types
     131                $this->assertFalse( register_taxonomy_for_object_type( 'category', 'user' ) );
     132                $this->assertFalse( unregister_taxonomy_from_object_type( 'category', 'user' ) );
     133
     134                // Non-core taxonomy, core post type
     135                $this->assertTrue( unregister_taxonomy_from_object_type( $tax, 'post' ) );
     136                $this->assertFalse( unregister_taxonomy_from_object_type( $tax, 'post' ) );
     137                $this->assertTrue( register_taxonomy_for_object_type( $tax, 'post' ) );
     138
     139                // Non-core taxonomy, non-core post type
     140                $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
     141                $this->assertTrue( unregister_taxonomy_from_object_type( $tax, $post_type ) );
     142                $this->assertFalse( unregister_taxonomy_from_object_type( $tax, $post_type ) );
     143                $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
     144
     145                // Non-core taxonomies, non-post object types
     146                $this->assertFalse( register_taxonomy_for_object_type( $tax, 'user' ) );
     147                $this->assertFalse( unregister_taxonomy_from_object_type( $tax, 'user' ) );
     148
     149                unset($GLOBALS['wp_taxonomies'][$tax]);
     150                _unregister_post_type( $post_type );
     151
     152        }
    106153}