| 1 | <?php |
| 2 | if ( is_multisite() ) : |
| 3 | |
| 4 | /** |
| 5 | * @group taxonomy |
| 6 | * @group multisite |
| 7 | */ |
| 8 | class Tests_Multisite_GlobalTerms extends WP_UnitTestCase { |
| 9 | protected $suppress = false; |
| 10 | static $global_terms_table_exists = false; |
| 11 | |
| 12 | public static function setUpBeforeClass() { |
| 13 | global $wpdb; |
| 14 | |
| 15 | add_filter( 'global_terms_enabled', '__return_true' ); |
| 16 | |
| 17 | self::$global_terms_table_exists = (bool) $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->sitecategories}'" ); |
| 18 | if ( ! self::$global_terms_table_exists ) { |
| 19 | if ( ! function_exists( 'install_global_terms' ) ) { |
| 20 | require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); |
| 21 | } |
| 22 | |
| 23 | install_global_terms(); |
| 24 | } |
| 25 | |
| 26 | self::commit_transaction(); |
| 27 | } |
| 28 | |
| 29 | public static function tearDownAfterClass() { |
| 30 | global $wpdb; |
| 31 | |
| 32 | if ( ! self::$global_terms_table_exists ) { |
| 33 | $wpdb->query( "DROP TABLE {$wpdb->sitecategories}" ); |
| 34 | } |
| 35 | |
| 36 | remove_filter( 'global_terms_enabled', '__return_true' ); |
| 37 | |
| 38 | self::commit_transaction(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @ticket 31149 |
| 43 | */ |
| 44 | public function test_conflict_between_global_and_local_term_id_should_be_resolved_via_recursion() { |
| 45 | global $wpdb; |
| 46 | $global_terms_exists = (bool) |
| 47 | |
| 48 | $sites = $this->factory->blog->create_many( 2 ); |
| 49 | |
| 50 | /* |
| 51 | * Fake a race condition by forcing a conflict between a global and a local term_id. |
| 52 | * |
| 53 | * On site 0: |
| 54 | * * Create a 'foo' term. |
| 55 | * * Fetch the ID of the corresponding term in the global terms table. |
| 56 | * |
| 57 | * On site 1: |
| 58 | * * Create a dummy term. |
| 59 | * * Update that term so that its local term_id matches the global_term_id identified above. This |
| 60 | * represents the race condition. |
| 61 | * * Create a term 'foo' that will be matched in global terms but whose global_term_id will be a |
| 62 | * mismatch with the local term_id. |
| 63 | */ |
| 64 | |
| 65 | switch_to_blog( $sites[0] ); |
| 66 | |
| 67 | $this->factory->category->create( array( |
| 68 | 'name' => 'Foo', |
| 69 | 'slug' => 'foo', |
| 70 | ) ); |
| 71 | |
| 72 | // Get the randomly-generated global term ID. |
| 73 | $global_term_id = $wpdb->get_var( "SELECT cat_ID from $wpdb->sitecategories WHERE category_nicename = 'foo'" ); |
| 74 | |
| 75 | restore_current_blog(); |
| 76 | |
| 77 | // On site 1: |
| 78 | switch_to_blog( $sites[1] ); |
| 79 | |
| 80 | // Create a term, and then manually manipulate it to have a term_id conflict with the global term ID. |
| 81 | $s2_c1 = $this->factory->category->create(); |
| 82 | $wpdb->update( $wpdb->terms, array( 'term_id' => $global_term_id ), array( 'term_id' => $s2_c1 ) ); |
| 83 | |
| 84 | // Now create a term that will trigger the conflict. |
| 85 | $s2_c2 = wp_insert_term( 'Foo', 'category', array( |
| 86 | 'slug' => 'foo', |
| 87 | ) ); |
| 88 | |
| 89 | restore_current_blog(); |
| 90 | |
| 91 | $this->assertEquals( $global_term_id, $s2_c2['term_id'] ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | endif; |