WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/09/2016 02:57:22 PM (5 years ago)
Author:
swissspidy
Message:

Taxonomy: Introduce unregister_taxonomy().

This new function can be used to completely unregister non built-in taxonomies.

Fixes #35227.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/taxonomy.php

    r36108 r36243  
    531531        $this->assertFalse( $tax->query_var );
    532532    }
     533
     534    /**
     535     * @ticket 35227
     536     */
     537    public function test_unregister_taxonomy_unknown_taxonomy() {
     538        $this->assertWPError( unregister_taxonomy( 'foo' ) );
     539    }
     540
     541    /**
     542     * @ticket 35227
     543     */
     544    public function test_unregister_taxonomy_twice() {
     545        register_taxonomy( 'foo', 'post' );
     546        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     547        $this->assertWPError( unregister_taxonomy( 'foo' ) );
     548    }
     549
     550    /**
     551     * @ticket 35227
     552     */
     553    public function test_unregister_taxonomy_disallow_builtin_taxonomy() {
     554        $this->assertWPError( unregister_taxonomy( 'post_tag' ) );
     555        $this->assertWPError( unregister_taxonomy( 'category' ) );
     556    }
     557
     558    /**
     559     * @ticket 35227
     560     */
     561    public function test_unregister_taxonomy_removes_query_vars() {
     562        global $wp;
     563
     564        register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar' ) );
     565
     566        $this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) );
     567        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     568        $this->assertFalse( array_search( 'bar', $wp->public_query_vars ) );
     569    }
     570
     571    /**
     572     * @ticket 35227
     573     */
     574    public function test_unregister_taxonomy_removes_permastruct() {
     575        $this->set_permalink_structure( '/%postname%' );
     576
     577        global $wp_rewrite;
     578
     579        register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar', 'rewrite' => true ) );
     580
     581        $this->assertInternalType( 'array', $wp_rewrite->extra_permastructs['foo'] );
     582        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     583        $this->assertFalse( isset($wp_rewrite->extra_permastructs['foo'] ) );
     584    }
     585
     586    /**
     587     * @ticket 35227
     588     */
     589    public function test_unregister_taxonomy_removes_rewrite_rules() {
     590        $this->set_permalink_structure( '/%postname%' );
     591
     592        global $wp_rewrite;
     593
     594        register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar' ) );
     595
     596        $count_before = count( $wp_rewrite->rewritereplace );
     597
     598        $this->assertContains( '%foo%', $wp_rewrite->rewritecode );
     599        $this->assertContains( 'bar=', $wp_rewrite->queryreplace );
     600        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     601        $this->assertNotContains( '%foo%', $wp_rewrite->rewritecode );
     602        $this->assertNotContains( 'bar=', $wp_rewrite->queryreplace );
     603        $this->assertSame( --$count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value.
     604    }
     605
     606    /**
     607     * @ticket 35227
     608     */
     609    public function test_unregister_taxonomy_removes_taxonomy_from_global() {
     610        global $wp_taxonomies;
     611
     612        register_taxonomy( 'foo', 'post' );
     613
     614        $this->assertInternalType( 'object', $wp_taxonomies['foo'] );
     615        $this->assertInternalType( 'object', get_taxonomy( 'foo' ) );
     616
     617        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     618
     619        $this->assertFalse( isset( $wp_taxonomies['foo'] ) );
     620        $this->assertFalse( get_taxonomy( 'foo' ) );
     621    }
     622
     623    /**
     624     * @ticket 35227
     625     */
     626    public function test_unregister_taxonomy_removes_meta_box_callback() {
     627        global $wp_filter;
     628
     629        register_taxonomy( 'foo', 'post' );
     630
     631        $this->assertSame( 1, count( $wp_filter['wp_ajax_add-foo'] ) );
     632        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     633        $this->assertSame( array(), $wp_filter['wp_ajax_add-foo'] );
     634    }
    533635}
Note: See TracChangeset for help on using the changeset viewer.