wpDeleteObjectTermRelationships.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * @group taxonomy
  4. * @covers ::wp_delete_object_term_relationships
  5. */
  6. class Tests_Term_WpDeleteObjectTermRelationships extends WP_UnitTestCase {
  7. public function test_single_taxonomy() {
  8. register_taxonomy( 'wptests_tax1', 'post' );
  9. register_taxonomy( 'wptests_tax2', 'post' );
  10. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  11. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  12. $object_id = 567;
  13. wp_set_object_terms( $object_id, array( $t1 ), 'wptests_tax1' );
  14. wp_set_object_terms( $object_id, array( $t2 ), 'wptests_tax2' );
  15. // Confirm the setup.
  16. $terms = wp_get_object_terms( $object_id, array( 'wptests_tax1', 'wptests_tax2' ), array( 'fields' => 'ids' ) );
  17. $this->assertSameSets( array( $t1, $t2 ), $terms );
  18. // wp_delete_object_term_relationships() doesn't have a return value.
  19. wp_delete_object_term_relationships( $object_id, 'wptests_tax2' );
  20. $terms = wp_get_object_terms( $object_id, array( 'wptests_tax1', 'wptests_tax2' ), array( 'fields' => 'ids' ) );
  21. $this->assertSameSets( array( $t1 ), $terms );
  22. }
  23. public function test_array_of_taxonomies() {
  24. register_taxonomy( 'wptests_tax1', 'post' );
  25. register_taxonomy( 'wptests_tax2', 'post' );
  26. register_taxonomy( 'wptests_tax3', 'post' );
  27. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  28. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  29. $t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax3' ) );
  30. $object_id = 567;
  31. wp_set_object_terms( $object_id, array( $t1 ), 'wptests_tax1' );
  32. wp_set_object_terms( $object_id, array( $t2 ), 'wptests_tax2' );
  33. wp_set_object_terms( $object_id, array( $t3 ), 'wptests_tax3' );
  34. // Confirm the setup.
  35. $terms = wp_get_object_terms( $object_id, array( 'wptests_tax1', 'wptests_tax2', 'wptests_tax3' ), array( 'fields' => 'ids' ) );
  36. $this->assertSameSets( array( $t1, $t2, $t3 ), $terms );
  37. // wp_delete_object_term_relationships() doesn't have a return value.
  38. wp_delete_object_term_relationships( $object_id, array( 'wptests_tax1', 'wptests_tax3' ) );
  39. $terms = wp_get_object_terms( $object_id, array( 'wptests_tax1', 'wptests_tax2', 'wptests_tax3' ), array( 'fields' => 'ids' ) );
  40. $this->assertSameSets( array( $t2 ), $terms );
  41. }
  42. }