Make WordPress Core


Ignore:
Timestamp:
11/21/2014 03:16:08 AM (12 years ago)
Author:
boonebgorges
Message:

Improve cleanup of cached term_ids after shared terms are split.

  • If the split term ID is stored as 'default_category', 'default_link_category', or 'default_email_category', update it to the new ID.
  • If the split term ID is associated with a nav menu item, update that piece of postmeta to the new ID.

Props mboynes.
See #30335.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/splitSharedTerm.php

    r30347 r30494  
    55 */
    66class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
    7         protected $tt_ids = array();
     7        protected $terms = array();
    88
    99        public function setUp() {
     
    4141                ) );
    4242
    43                 $this->tt_ids = array(
    44                         't1' => $t1['term_taxonomy_id'],
    45                         't2' => $t2['term_taxonomy_id'],
    46                         't3' => $t3['term_taxonomy_id'],
    47                         't2_child' => $t2_child['term_taxonomy_id'],
     43                $this->terms = array(
     44                        't1' => $t1,
     45                        't2' => $t2,
     46                        't3' => $t3,
     47                        't2_child' => $t2_child,
    4848                );
    4949
     
    5656         */
    5757        public function test_should_create_new_term_ids() {
    58                 $t1_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t1'], 'wptests_tax' );
    59                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
    60                 $t3_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t3'], 'wptests_tax_3' );
     58                $t1_term = get_term_by( 'term_taxonomy_id', $this->terms['t1']['term_taxonomy_id'], 'wptests_tax' );
     59                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     60                $t3_term = get_term_by( 'term_taxonomy_id', $this->terms['t3']['term_taxonomy_id'], 'wptests_tax_3' );
    6161
    6262                $this->assertNotEquals( $t1_term->term_id, $t2_term->term_id );
     
    6969         */
    7070        public function test_should_retain_child_terms_when_using_get_terms_parent() {
    71                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
     71                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
    7272                $children = get_terms( 'wptests_tax_2', array(
    7373                        'parent' => $t2_term->term_id,
     
    7575                ) );
    7676
    77                 $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     77                $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
    7878        }
    7979
     
    8282         */
    8383        public function test_should_retain_child_terms_when_using_get_terms_child_of() {
    84                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
     84                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
    8585                $children = get_terms( 'wptests_tax_2', array(
    8686                        'child_of' => $t2_term->term_id,
     
    8888                ) );
    8989
    90                 $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     90                $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
    9191        }
    9292
     
    122122                $this->assertEquals( array( $new_term_id ), $t2_children );
    123123        }
     124
     125        /**
     126         * @ticket 30335
     127         */
     128        public function test_should_update_default_category_on_term_split() {
     129                global $wpdb;
     130                $t1 = wp_insert_term( 'Foo Default', 'category' );
     131
     132                update_option( 'default_category', $t1['term_id'] );
     133
     134                register_taxonomy( 'wptests_tax_5', 'post' );
     135                $t2 = wp_insert_term( 'Foo Default', 'wptests_tax_5' );
     136
     137                // Manually modify because split terms shouldn't naturally occur.
     138                $wpdb->update( $wpdb->term_taxonomy,
     139                        array( 'term_id' => $t1['term_id'] ),
     140                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     141                        array( '%d' ),
     142                        array( '%d' )
     143                );
     144
     145                $this->assertEquals( $t1['term_id'], get_option( 'default_category', -1 ) );
     146
     147                $new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
     148
     149                $this->assertNotEquals( $new_term_id, $t1['term_id'] );
     150                $this->assertEquals( $new_term_id, get_option( 'default_category', -1 ) );
     151        }
     152
     153        /**
     154         * @ticket 30335
     155         */
     156        public function test_should_update_menus_on_term_split() {
     157                global $wpdb;
     158
     159                $t1 = wp_insert_term( 'Foo Menu', 'category' );
     160
     161                register_taxonomy( 'wptests_tax_6', 'post' );
     162                $t2 = wp_insert_term( 'Foo Menu', 'wptests_tax_6' );
     163
     164                // Manually modify because split terms shouldn't naturally occur.
     165                $wpdb->update( $wpdb->term_taxonomy,
     166                        array( 'term_id' => $t1['term_id'] ),
     167                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     168                        array( '%d' ),
     169                        array( '%d' )
     170                );
     171
     172                $menu_id = wp_create_nav_menu( rand_str() );
     173                $cat_menu_item = wp_update_nav_menu_item( $menu_id, 0, array(
     174                        'menu-item-type' => 'taxonomy',
     175                        'menu-item-object' => 'category',
     176                        'menu-item-object-id' => $t1['term_id'],
     177                        'menu-item-status' => 'publish'
     178                ) );
     179                $this->assertEquals( $t1['term_id'], get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
     180
     181                $new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
     182                $this->assertNotEquals( $new_term_id, $t1['term_id'] );
     183                $this->assertEquals( $new_term_id, get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
     184        }
    124185}
Note: See TracChangeset for help on using the changeset viewer.