query.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Test WP_Meta_Query, in wp-includes/meta.php
  4. *
  5. * @group meta
  6. */
  7. class Tests_Meta_Query extends WP_UnitTestCase {
  8. function test_default_relation() {
  9. $query = new WP_Meta_Query( array( array( 'key' => 'abc' ) ) );
  10. $this->assertEquals( 'AND', $query->relation );
  11. }
  12. function test_set_relation() {
  13. $query = new WP_Meta_Query( array( array( 'key' => 'abc' ), 'relation' => 'AND' ) );
  14. $this->assertEquals( 'AND', $query->relation );
  15. $query = new WP_Meta_Query( array( array( 'key' => 'abc' ), 'relation' => 'OR' ) );
  16. $this->assertEquals( 'OR', $query->relation );
  17. }
  18. /**
  19. * Test all key only meta queries use the same INNER JOIN when using relation=OR
  20. *
  21. * @ticket 19729
  22. */
  23. function test_single_inner_join_for_keys_only() {
  24. global $wpdb;
  25. $query = new WP_Meta_Query( array(
  26. array( 'key' => 'abc' ),
  27. array( 'key' => 'def' ),
  28. 'relation' => 'OR'
  29. ) );
  30. $sql = $query->get_sql( 'post', $wpdb->posts, 'ID' );
  31. $this->assertEquals( 1, substr_count( $sql['join'], 'INNER JOIN' ) );
  32. // also check mixing key and key => value
  33. $query = new WP_Meta_Query( array(
  34. array( 'key' => 'abc' ),
  35. array( 'key' => 'def' ),
  36. array( 'key' => 'ghi', 'value' => 'abc' ),
  37. 'relation' => 'OR'
  38. ) );
  39. $sql = $query->get_sql( 'post', $wpdb->posts, 'ID' );
  40. $this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
  41. }
  42. /**
  43. * Test the conversion between "WP_Query" style meta args (meta_value=x&meta_key=y)
  44. * to a meta query array
  45. */
  46. function test_parse_query_vars() {
  47. $query = new WP_Meta_Query();
  48. // just meta_value
  49. $query->parse_query_vars( array( 'meta_key' => 'abc' ) );
  50. $this->assertEquals( array( array( 'key' => 'abc' ) ), $query->queries );
  51. // meta_key & meta_value
  52. $query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_value' => 'def' ) );
  53. $this->assertEquals( array( array( 'key' => 'abc', 'value' => 'def' ) ), $query->queries );
  54. // meta_compare
  55. $query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_compare' => '=>' ) );
  56. $this->assertEquals( array( array( 'key' => 'abc', 'compare' => '=>' ) ), $query->queries );
  57. }
  58. /**
  59. * @ticket 22096
  60. */
  61. function test_empty_value_sql() {
  62. global $wpdb;
  63. $query = new WP_Meta_Query();
  64. $the_complex_query['meta_query'] = array(
  65. array( 'key' => 'my_first_key', 'value' => 'my_amazing_value' ),
  66. array( 'key' => 'my_second_key', 'compare' => 'NOT EXISTS' ),
  67. array( 'key' => 'my_third_key', 'value' => array( ), 'compare' => 'IN' ),
  68. );
  69. $query->parse_query_vars( $the_complex_query );
  70. $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  71. // We should have 2 joins - one for my_first_key and one for my_second_key
  72. $this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
  73. // The WHERE should check my_third_key against an unaliased table
  74. $this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
  75. }
  76. /**
  77. * @ticket 22967
  78. */
  79. function test_null_value_sql() {
  80. global $wpdb;
  81. $query = new WP_Meta_Query( array(
  82. array( 'key' => 'abc', 'value' => null, 'compare' => '=' )
  83. ) );
  84. $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  85. $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
  86. }
  87. /**
  88. * @ticket 23033
  89. */
  90. function test_get_cast_for_type() {
  91. $query = new WP_Meta_Query();
  92. $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
  93. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) );
  94. $this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) );
  95. $this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) );
  96. $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) );
  97. $this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) );
  98. $this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) );
  99. $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) );
  100. $this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) );
  101. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) );
  102. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) );
  103. $this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
  104. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
  105. $this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) );
  106. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) );
  107. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) );
  108. $this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) );
  109. $this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) );
  110. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) );
  111. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) );
  112. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) );
  113. $this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
  114. $this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) );
  115. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
  116. $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
  117. }
  118. }