<?php

/**
 * Test WPDB methods
 */
class TestWPDB extends _WPEmptyBlog {

	/**
	 * Query log
	 * @var array
	 */
	protected $_queries = array();
	
	/**
	 * Set up the test fixture
	 */
	public function setUp() {
		parent::setUp();
		$this->_queries = array();
		add_filter( 'query', array( $this, 'query_filter' ) );
	}

	/**
	 * Tear down the test fixture
	 */
	public function tearDown() {
		parent::tearDown();
		remove_filter( 'query', array( $this, 'query_filter' ) );
	}

	/**
	 * Log each query
	 * @param string $sql
	 * @return string
	 */
	public function query_filter( $sql ) {
		$this->_queries[] = $sql;
		return $sql;
	}

	/**
	 * Test that floats formatted as "0,700" get sanitized properly by wpdb
	 * @global mixed $wpdb
	 */
	public function test_locale_floats() {
		global $wpdb;

		$this->knownWPBug( 19861 );

		// Save the current locale
		$current_locale = setlocale( LC_ALL, NULL );
		
		// Switch to Russian
		setlocale( LC_ALL, 'ru_RU.utf8', 'rus' );
		
		// Try an update query
		$wpdb->update( 
			'test_table', 
			array( 'float_column' => 0.7 ), 
			array( 'meta_id' => 5 ), 
			array( '%f' ), 
			array( '%d' ) 
		);
		
		// Ensure the float isn't 0,700
		$this->assertContains( '0.700', array_pop( $this->_queries ) );

		// Try a prepare
		$sql = $wpdb->prepare( "UPDATE test_table SET float_column = %f AND meta_id = %d", 0.7, 5 );
		$this->assertContains( '0.700', $sql );

		// Restore locale
		setlocale( LC_ALL, $current_locale );
	}
}
