getObjectTaxonomies.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Taxonomy_GetObjectTaxonomies extends WP_UnitTestCase {
  6. public function setUp() {
  7. parent::setUp();
  8. register_post_type( 'wptests_pt' );
  9. register_taxonomy( 'wptests_tax', 'wptests_pt' );
  10. }
  11. public function test_object_should_accept_string() {
  12. $found = get_object_taxonomies( 'wptests_pt' );
  13. $expected = array( 'wptests_tax' );
  14. $this->assertSame( $expected, $found );
  15. }
  16. public function test_object_should_accept_array_of_post_type_names() {
  17. $found = get_object_taxonomies( array( 'wptests_pt' ) );
  18. $expected = array( 'wptests_tax' );
  19. $this->assertSame( $expected, $found );
  20. }
  21. public function test_object_should_accept_post_object() {
  22. $p = self::factory()->post->create_and_get( array( 'post_type' => 'wptests_pt' ) );
  23. $found = get_object_taxonomies( $p );
  24. $expected = array( 'wptests_tax' );
  25. $this->assertSame( $expected, $found );
  26. }
  27. public function test_should_respect_output_names() {
  28. $found = get_object_taxonomies( 'wptests_pt', 'objects' );
  29. $this->assertSame( array( 'wptests_tax' ), array_keys( $found ) );
  30. $this->assertInternalType( 'object', $found['wptests_tax'] );
  31. $this->assertSame( 'wptests_tax', $found['wptests_tax']->name );
  32. }
  33. public function test_any_value_of_output_other_than_names_should_return_objects() {
  34. $found = get_object_taxonomies( 'wptests_pt', 'foo' );
  35. $expected = get_object_taxonomies( 'wptests_pt', 'objects' );
  36. $this->assertSame( $expected, $found );
  37. }
  38. /**
  39. * @ticket 37368
  40. */
  41. public function test_should_return_all_attachment_taxonomies_for_attachment_object_type() {
  42. register_taxonomy( 'wptests_tax2', 'attachment:image' );
  43. $a = self::factory()->attachment->create_object(
  44. 'image.jpg',
  45. 0,
  46. array(
  47. 'post_mime_type' => 'image/jpeg',
  48. 'post_type' => 'attachment',
  49. )
  50. );
  51. $attachment = get_post( $a );
  52. $found = get_object_taxonomies( $attachment, 'names' );
  53. $this->assertSame( array( 'wptests_tax2' ), $found );
  54. }
  55. /**
  56. * @ticket 37368
  57. */
  58. public function test_should_respect_output_objects_when_object_is_attachment() {
  59. register_taxonomy( 'wptests_tax2', 'attachment:image' );
  60. $a = self::factory()->attachment->create_object(
  61. 'image.jpg',
  62. 0,
  63. array(
  64. 'post_mime_type' => 'image/jpeg',
  65. 'post_type' => 'attachment',
  66. )
  67. );
  68. $attachment = get_post( $a );
  69. $found = get_object_taxonomies( $attachment, 'objects' );
  70. $this->assertSame( array( 'wptests_tax2' ), array_keys( $found ) );
  71. $this->assertInternalType( 'object', $found['wptests_tax2'] );
  72. $this->assertSame( 'wptests_tax2', $found['wptests_tax2']->name );
  73. }
  74. }