query.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Tax_Query extends WP_UnitTestCase {
  6. protected $q;
  7. function setUp() {
  8. parent::setUp();
  9. unset( $this->q );
  10. $this->q = new WP_Query();
  11. }
  12. function test_category__and_var() {
  13. $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
  14. $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
  15. $post_id = $this->factory->post->create();
  16. wp_set_post_categories( $post_id, $term_id );
  17. $posts = $this->q->query( array( 'category__and' => array( $term_id ) ) );
  18. $this->assertEmpty( $this->q->get( 'category__and' ) );
  19. $this->assertCount( 0, $this->q->get( 'category__and' ) );
  20. $this->assertNotEmpty( $this->q->get( 'category__in' ) );
  21. $this->assertCount( 1, $this->q->get( 'category__in' ) );
  22. $this->assertNotEmpty( $posts );
  23. $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
  24. $posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
  25. $this->assertNotEmpty( $this->q->get( 'category__and' ) );
  26. $this->assertCount( 2, $this->q->get( 'category__and' ) );
  27. $this->assertEmpty( $this->q->get( 'category__in' ) );
  28. $this->assertCount( 0, $this->q->get( 'category__in' ) );
  29. $this->assertEmpty( $posts2 );
  30. }
  31. function test_taxonomy_with_attachments() {
  32. register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );
  33. $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) );
  34. $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
  35. 'post_mime_type' => 'image/jpeg',
  36. 'post_type' => 'attachment'
  37. ) );
  38. wp_set_object_terms( $image_id, $tag_id, 'post_tag' );
  39. $posts = $this->q->query( array(
  40. 'fields' => 'ids',
  41. 'post_type' => 'attachment',
  42. 'post_status' => 'inherit',
  43. 'tax_query' => array(
  44. array(
  45. 'taxonomy' => 'post_tag',
  46. 'field' => 'term_id',
  47. 'terms' => array( $tag_id )
  48. )
  49. )
  50. ) );
  51. $this->assertEquals( array( $image_id ), $posts );
  52. }
  53. }