db.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Test WPDB methods
  4. *
  5. * @group wpdb
  6. */
  7. class Tests_DB extends WP_UnitTestCase {
  8. /**
  9. * Query log
  10. * @var array
  11. */
  12. protected $_queries = array();
  13. /**
  14. * Set up the test fixture
  15. */
  16. public function setUp() {
  17. parent::setUp();
  18. $this->_queries = array();
  19. add_filter( 'query', array( $this, 'query_filter' ) );
  20. }
  21. /**
  22. * Tear down the test fixture
  23. */
  24. public function tearDown() {
  25. parent::tearDown();
  26. remove_filter( 'query', array( $this, 'query_filter' ) );
  27. }
  28. /**
  29. * Log each query
  30. * @param string $sql
  31. * @return string
  32. */
  33. public function query_filter( $sql ) {
  34. $this->_queries[] = $sql;
  35. return $sql;
  36. }
  37. /**
  38. * Test that floats formatted as "0,700" get sanitized properly by wpdb
  39. * @global mixed $wpdb
  40. *
  41. * @ticket 19861
  42. */
  43. public function test_locale_floats() {
  44. global $wpdb;
  45. // Save the current locale settings
  46. $current_locales = explode( ';', setlocale( LC_ALL, 0 ) );
  47. // Switch to Russian
  48. $flag = setlocale( LC_ALL, 'ru_RU.utf8', 'rus', 'fr_FR.utf8', 'fr_FR', 'de_DE.utf8', 'de_DE', 'es_ES.utf8', 'es_ES' );
  49. if ( false === $flag )
  50. $this->markTestSkipped( 'No European languages available for testing' );
  51. // Try an update query
  52. $wpdb->suppress_errors( true );
  53. $wpdb->update(
  54. 'test_table',
  55. array( 'float_column' => 0.7 ),
  56. array( 'meta_id' => 5 ),
  57. array( '%f' ),
  58. array( '%d' )
  59. );
  60. $wpdb->suppress_errors( false );
  61. // Ensure the float isn't 0,700
  62. $this->assertContains( '0.700', array_pop( $this->_queries ) );
  63. // Try a prepare
  64. $sql = $wpdb->prepare( "UPDATE test_table SET float_column = %f AND meta_id = %d", 0.7, 5 );
  65. $this->assertContains( '0.700', $sql );
  66. // Restore locale settings
  67. foreach ( $current_locales as $locale_setting ) {
  68. if ( false !== strpos( $locale_setting, '=' ) ) {
  69. list( $category, $locale ) = explode( '=', $locale_setting );
  70. if ( defined( $category ) )
  71. setlocale( constant( $category ), $locale );
  72. } else {
  73. setlocale( LC_ALL, $locale_setting );
  74. }
  75. }
  76. }
  77. /**
  78. * @ticket 18510
  79. */
  80. function test_wpdb_supposedly_protected_properties() {
  81. global $wpdb;
  82. $this->assertNotEmpty( $wpdb->dbh );
  83. $dbh = $wpdb->dbh;
  84. $this->assertNotEmpty( $dbh );
  85. $this->assertTrue( isset( $wpdb->dbh ) ); // Test __isset()
  86. unset( $wpdb->dbh );
  87. $this->assertTrue( empty( $wpdb->dbh ) );
  88. $wpdb->dbh = $dbh;
  89. $this->assertNotEmpty( $wpdb->dbh );
  90. }
  91. /**
  92. * @ticket 18510
  93. */
  94. function test_wpdb_nonexistent_properties() {
  95. global $wpdb;
  96. $this->assertTrue( empty( $wpdb->nonexistent_property ) );
  97. $wpdb->nonexistent_property = true;
  98. $this->assertTrue( $wpdb->nonexistent_property );
  99. $this->assertTrue( isset( $wpdb->nonexistent_property ) );
  100. unset( $wpdb->nonexistent_property );
  101. $this->assertTrue( empty( $wpdb->nonexistent_property ) );
  102. }
  103. /**
  104. * Test that an escaped %%f is not altered
  105. * @ticket 19861
  106. */
  107. public function test_double_escaped_placeholders() {
  108. global $wpdb;
  109. $sql = $wpdb->prepare( "UPDATE test_table SET string_column = '%%f is a float, %%d is an int %d, %%s is a string', field = %s", 3, '4' );
  110. $this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql );
  111. }
  112. }