2014-09-01 06:00:12 +00:00
|
|
|
<?php
|
2014-09-01 06:59:23 +00:00
|
|
|
/**
|
2017-12-18 13:58:40 +00:00
|
|
|
* Unit tests for the core functions.
|
|
|
|
*
|
2015-03-06 15:32:40 +00:00
|
|
|
* @package WooCommerce\Tests\Util
|
2014-09-01 06:59:23 +00:00
|
|
|
* @since 2.2
|
|
|
|
*/
|
2017-12-18 13:58:40 +00:00
|
|
|
|
2020-01-29 05:21:29 +00:00
|
|
|
use Automattic\Jetpack\Constants;
|
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
/**
|
|
|
|
* Core function unit tests.
|
|
|
|
*/
|
2016-03-23 12:14:13 +00:00
|
|
|
class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
|
2019-05-22 10:24:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up test
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->wc = WC();
|
|
|
|
}
|
|
|
|
|
2014-09-01 06:59:23 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test get_woocommerce_currency().
|
2014-09-01 06:59:23 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
2014-09-01 06:00:12 +00:00
|
|
|
public function test_get_woocommerce_currency() {
|
|
|
|
|
|
|
|
$this->assertEquals( 'GBP', get_woocommerce_currency() );
|
|
|
|
}
|
|
|
|
|
2014-09-01 06:59:23 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test get_woocommerce_currencies().
|
2014-09-01 06:59:23 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
2014-09-01 06:00:12 +00:00
|
|
|
public function test_get_woocommerce_currencies() {
|
2018-07-11 12:25:42 +00:00
|
|
|
static $currencies;
|
2014-09-01 06:00:12 +00:00
|
|
|
|
|
|
|
$expected_currencies = array(
|
2016-10-12 10:16:30 +00:00
|
|
|
'AED' => 'United Arab Emirates dirham',
|
|
|
|
'AFN' => 'Afghan afghani',
|
|
|
|
'ALL' => 'Albanian lek',
|
|
|
|
'AMD' => 'Armenian dram',
|
|
|
|
'ANG' => 'Netherlands Antillean guilder',
|
|
|
|
'AOA' => 'Angolan kwanza',
|
|
|
|
'ARS' => 'Argentine peso',
|
|
|
|
'AUD' => 'Australian dollar',
|
|
|
|
'AWG' => 'Aruban florin',
|
|
|
|
'AZN' => 'Azerbaijani manat',
|
|
|
|
'BAM' => 'Bosnia and Herzegovina convertible mark',
|
|
|
|
'BBD' => 'Barbadian dollar',
|
|
|
|
'BDT' => 'Bangladeshi taka',
|
|
|
|
'BGN' => 'Bulgarian lev',
|
|
|
|
'BHD' => 'Bahraini dinar',
|
|
|
|
'BIF' => 'Burundian franc',
|
|
|
|
'BMD' => 'Bermudian dollar',
|
|
|
|
'BND' => 'Brunei dollar',
|
|
|
|
'BOB' => 'Bolivian boliviano',
|
|
|
|
'BRL' => 'Brazilian real',
|
|
|
|
'BSD' => 'Bahamian dollar',
|
|
|
|
'BTC' => 'Bitcoin',
|
|
|
|
'BTN' => 'Bhutanese ngultrum',
|
|
|
|
'BWP' => 'Botswana pula',
|
2017-11-07 17:50:01 +00:00
|
|
|
'BYR' => 'Belarusian ruble (old)',
|
2017-11-07 19:04:46 +00:00
|
|
|
'BYN' => 'Belarusian ruble',
|
2016-10-12 10:16:30 +00:00
|
|
|
'BZD' => 'Belize dollar',
|
|
|
|
'CAD' => 'Canadian dollar',
|
|
|
|
'CDF' => 'Congolese franc',
|
|
|
|
'CHF' => 'Swiss franc',
|
|
|
|
'CLP' => 'Chilean peso',
|
|
|
|
'CNY' => 'Chinese yuan',
|
|
|
|
'COP' => 'Colombian peso',
|
|
|
|
'CRC' => 'Costa Rican colón',
|
|
|
|
'CUC' => 'Cuban convertible peso',
|
|
|
|
'CUP' => 'Cuban peso',
|
|
|
|
'CVE' => 'Cape Verdean escudo',
|
|
|
|
'CZK' => 'Czech koruna',
|
|
|
|
'DJF' => 'Djiboutian franc',
|
|
|
|
'DKK' => 'Danish krone',
|
|
|
|
'DOP' => 'Dominican peso',
|
|
|
|
'DZD' => 'Algerian dinar',
|
|
|
|
'EGP' => 'Egyptian pound',
|
|
|
|
'ERN' => 'Eritrean nakfa',
|
|
|
|
'ETB' => 'Ethiopian birr',
|
|
|
|
'EUR' => 'Euro',
|
|
|
|
'FJD' => 'Fijian dollar',
|
|
|
|
'FKP' => 'Falkland Islands pound',
|
|
|
|
'GBP' => 'Pound sterling',
|
|
|
|
'GEL' => 'Georgian lari',
|
|
|
|
'GGP' => 'Guernsey pound',
|
|
|
|
'GHS' => 'Ghana cedi',
|
|
|
|
'GIP' => 'Gibraltar pound',
|
|
|
|
'GMD' => 'Gambian dalasi',
|
|
|
|
'GNF' => 'Guinean franc',
|
|
|
|
'GTQ' => 'Guatemalan quetzal',
|
|
|
|
'GYD' => 'Guyanese dollar',
|
|
|
|
'HKD' => 'Hong Kong dollar',
|
|
|
|
'HNL' => 'Honduran lempira',
|
|
|
|
'HRK' => 'Croatian kuna',
|
|
|
|
'HTG' => 'Haitian gourde',
|
|
|
|
'HUF' => 'Hungarian forint',
|
|
|
|
'IDR' => 'Indonesian rupiah',
|
|
|
|
'ILS' => 'Israeli new shekel',
|
|
|
|
'IMP' => 'Manx pound',
|
|
|
|
'INR' => 'Indian rupee',
|
|
|
|
'IQD' => 'Iraqi dinar',
|
|
|
|
'IRR' => 'Iranian rial',
|
2017-01-03 11:57:21 +00:00
|
|
|
'IRT' => 'Iranian toman',
|
2016-10-12 10:16:30 +00:00
|
|
|
'ISK' => 'Icelandic króna',
|
|
|
|
'JEP' => 'Jersey pound',
|
|
|
|
'JMD' => 'Jamaican dollar',
|
|
|
|
'JOD' => 'Jordanian dinar',
|
|
|
|
'JPY' => 'Japanese yen',
|
|
|
|
'KES' => 'Kenyan shilling',
|
|
|
|
'KGS' => 'Kyrgyzstani som',
|
|
|
|
'KHR' => 'Cambodian riel',
|
|
|
|
'KMF' => 'Comorian franc',
|
|
|
|
'KPW' => 'North Korean won',
|
|
|
|
'KRW' => 'South Korean won',
|
|
|
|
'KWD' => 'Kuwaiti dinar',
|
|
|
|
'KYD' => 'Cayman Islands dollar',
|
|
|
|
'KZT' => 'Kazakhstani tenge',
|
|
|
|
'LAK' => 'Lao kip',
|
|
|
|
'LBP' => 'Lebanese pound',
|
|
|
|
'LKR' => 'Sri Lankan rupee',
|
|
|
|
'LRD' => 'Liberian dollar',
|
|
|
|
'LSL' => 'Lesotho loti',
|
|
|
|
'LYD' => 'Libyan dinar',
|
|
|
|
'MAD' => 'Moroccan dirham',
|
|
|
|
'MDL' => 'Moldovan leu',
|
|
|
|
'MGA' => 'Malagasy ariary',
|
|
|
|
'MKD' => 'Macedonian denar',
|
|
|
|
'MMK' => 'Burmese kyat',
|
|
|
|
'MNT' => 'Mongolian tögrög',
|
|
|
|
'MOP' => 'Macanese pataca',
|
2019-06-20 06:07:38 +00:00
|
|
|
'MRU' => 'Mauritanian ouguiya',
|
2016-10-12 10:16:30 +00:00
|
|
|
'MUR' => 'Mauritian rupee',
|
|
|
|
'MVR' => 'Maldivian rufiyaa',
|
|
|
|
'MWK' => 'Malawian kwacha',
|
|
|
|
'MXN' => 'Mexican peso',
|
|
|
|
'MYR' => 'Malaysian ringgit',
|
|
|
|
'MZN' => 'Mozambican metical',
|
|
|
|
'NAD' => 'Namibian dollar',
|
|
|
|
'NGN' => 'Nigerian naira',
|
|
|
|
'NIO' => 'Nicaraguan córdoba',
|
|
|
|
'NOK' => 'Norwegian krone',
|
|
|
|
'NPR' => 'Nepalese rupee',
|
|
|
|
'NZD' => 'New Zealand dollar',
|
|
|
|
'OMR' => 'Omani rial',
|
|
|
|
'PAB' => 'Panamanian balboa',
|
2019-01-30 15:43:52 +00:00
|
|
|
'PEN' => 'Sol',
|
2016-10-12 10:16:30 +00:00
|
|
|
'PGK' => 'Papua New Guinean kina',
|
|
|
|
'PHP' => 'Philippine peso',
|
|
|
|
'PKR' => 'Pakistani rupee',
|
|
|
|
'PLN' => 'Polish złoty',
|
|
|
|
'PRB' => 'Transnistrian ruble',
|
|
|
|
'PYG' => 'Paraguayan guaraní',
|
|
|
|
'QAR' => 'Qatari riyal',
|
|
|
|
'RON' => 'Romanian leu',
|
|
|
|
'RSD' => 'Serbian dinar',
|
|
|
|
'RUB' => 'Russian ruble',
|
|
|
|
'RWF' => 'Rwandan franc',
|
|
|
|
'SAR' => 'Saudi riyal',
|
|
|
|
'SBD' => 'Solomon Islands dollar',
|
|
|
|
'SCR' => 'Seychellois rupee',
|
|
|
|
'SDG' => 'Sudanese pound',
|
|
|
|
'SEK' => 'Swedish krona',
|
|
|
|
'SGD' => 'Singapore dollar',
|
|
|
|
'SHP' => 'Saint Helena pound',
|
|
|
|
'SLL' => 'Sierra Leonean leone',
|
|
|
|
'SOS' => 'Somali shilling',
|
|
|
|
'SRD' => 'Surinamese dollar',
|
|
|
|
'SSP' => 'South Sudanese pound',
|
2019-06-20 06:07:38 +00:00
|
|
|
'STN' => 'São Tomé and Príncipe dobra',
|
2016-10-12 10:16:30 +00:00
|
|
|
'SYP' => 'Syrian pound',
|
|
|
|
'SZL' => 'Swazi lilangeni',
|
|
|
|
'THB' => 'Thai baht',
|
|
|
|
'TJS' => 'Tajikistani somoni',
|
|
|
|
'TMT' => 'Turkmenistan manat',
|
|
|
|
'TND' => 'Tunisian dinar',
|
|
|
|
'TOP' => 'Tongan paʻanga',
|
|
|
|
'TRY' => 'Turkish lira',
|
|
|
|
'TTD' => 'Trinidad and Tobago dollar',
|
|
|
|
'TWD' => 'New Taiwan dollar',
|
|
|
|
'TZS' => 'Tanzanian shilling',
|
|
|
|
'UAH' => 'Ukrainian hryvnia',
|
|
|
|
'UGX' => 'Ugandan shilling',
|
2018-04-03 13:37:31 +00:00
|
|
|
'USD' => 'United States (US) dollar',
|
2016-10-12 10:16:30 +00:00
|
|
|
'UYU' => 'Uruguayan peso',
|
|
|
|
'UZS' => 'Uzbekistani som',
|
|
|
|
'VEF' => 'Venezuelan bolívar',
|
2019-01-21 16:02:28 +00:00
|
|
|
'VES' => 'Bolívar soberano',
|
2016-10-12 10:16:30 +00:00
|
|
|
'VND' => 'Vietnamese đồng',
|
|
|
|
'VUV' => 'Vanuatu vatu',
|
|
|
|
'WST' => 'Samoan tālā',
|
|
|
|
'XAF' => 'Central African CFA franc',
|
|
|
|
'XCD' => 'East Caribbean dollar',
|
|
|
|
'XOF' => 'West African CFA franc',
|
|
|
|
'XPF' => 'CFP franc',
|
|
|
|
'YER' => 'Yemeni rial',
|
|
|
|
'ZAR' => 'South African rand',
|
|
|
|
'ZMW' => 'Zambian kwacha',
|
2014-09-01 06:00:12 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $expected_currencies, get_woocommerce_currencies() );
|
2018-07-11 12:25:42 +00:00
|
|
|
|
|
|
|
// Unset cached currencies and test again.
|
|
|
|
unset( $currencies );
|
|
|
|
$this->assertEquals( $expected_currencies, get_woocommerce_currencies() );
|
2014-09-01 06:00:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 06:59:23 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test get_woocommerce_currency_symbol().
|
2014-09-01 06:59:23 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
2014-09-01 06:00:12 +00:00
|
|
|
public function test_get_woocommerce_currency_symbol() {
|
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
// Default currency.
|
2014-09-01 06:00:12 +00:00
|
|
|
$this->assertEquals( '£', get_woocommerce_currency_symbol() );
|
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
// Given specific currency.
|
2014-09-01 06:59:23 +00:00
|
|
|
$this->assertEquals( '$', get_woocommerce_currency_symbol( 'USD' ) );
|
2014-09-01 06:00:12 +00:00
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
// Each case.
|
2014-09-01 06:59:23 +00:00
|
|
|
foreach ( array_keys( get_woocommerce_currencies() ) as $currency_code ) {
|
|
|
|
$this->assertInternalType( 'string', get_woocommerce_currency_symbol( $currency_code ) );
|
|
|
|
}
|
2014-09-01 06:00:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 12:38:33 +00:00
|
|
|
/**
|
|
|
|
* Test wc_get_theme_support()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_theme_support() {
|
|
|
|
$this->assertEquals( 'default', wc_get_theme_support( '', 'default' ) );
|
|
|
|
|
|
|
|
$theme_support_options = array(
|
|
|
|
'thumbnail_image_width' => 150,
|
|
|
|
'single_image_width' => 300,
|
|
|
|
'product_grid' => array(
|
|
|
|
'default_rows' => 3,
|
|
|
|
'min_rows' => 2,
|
|
|
|
'max_rows' => 8,
|
|
|
|
'default_columns' => 4,
|
|
|
|
'min_columns' => 2,
|
|
|
|
'max_columns' => 5,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
add_theme_support( 'woocommerce', $theme_support_options );
|
|
|
|
|
|
|
|
$this->assertEquals( $theme_support_options, wc_get_theme_support() );
|
|
|
|
$this->assertEquals( $theme_support_options['product_grid'], wc_get_theme_support( 'product_grid' ) );
|
|
|
|
}
|
|
|
|
|
2014-09-01 06:59:23 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test get_woocommerce_api_url().
|
2014-09-01 06:59:23 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_get_woocommerce_api_url() {
|
2014-09-01 06:00:12 +00:00
|
|
|
|
2014-09-05 15:17:53 +00:00
|
|
|
$base_uri = get_home_url();
|
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
// Base URI.
|
2015-06-18 22:24:57 +00:00
|
|
|
$this->assertEquals( "$base_uri/wc-api/v3/", get_woocommerce_api_url( null ) );
|
2014-09-01 06:00:12 +00:00
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
// Path.
|
2015-06-18 22:24:57 +00:00
|
|
|
$this->assertEquals( "$base_uri/wc-api/v3/orders", get_woocommerce_api_url( 'orders' ) );
|
2014-09-01 06:00:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:55:54 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_log_file_path().
|
2015-04-08 15:55:54 +00:00
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
public function test_wc_get_log_file_path() {
|
2018-09-26 10:41:28 +00:00
|
|
|
$log_dir = trailingslashit( WC_LOG_DIR );
|
|
|
|
$hash_name = sanitize_file_name( wp_hash( 'unit-tests' ) );
|
2020-04-05 19:41:39 +00:00
|
|
|
$date_suffix = date( 'Y-m-d', time() ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
|
2015-04-08 15:55:54 +00:00
|
|
|
|
2018-05-01 16:32:29 +00:00
|
|
|
$this->assertEquals( $log_dir . 'unit-tests-' . $date_suffix . '-' . $hash_name . '.log', wc_get_log_file_path( 'unit-tests' ) );
|
2015-04-08 15:55:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 19:52:24 +00:00
|
|
|
/**
|
|
|
|
* Test wc_get_logger().
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-15 19:52:24 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_get_logger() {
|
2017-12-18 13:58:40 +00:00
|
|
|
// This filter should have no effect because the class does not implement WC_Logger_Interface.
|
2017-01-14 17:45:51 +00:00
|
|
|
add_filter( 'woocommerce_logging_class', array( $this, 'return_bad_logger' ) );
|
|
|
|
|
|
|
|
$this->setExpectedIncorrectUsage( 'wc_get_logger' );
|
2016-12-15 19:52:24 +00:00
|
|
|
$log_a = wc_get_logger();
|
|
|
|
$log_b = wc_get_logger();
|
|
|
|
|
2016-12-16 20:37:58 +00:00
|
|
|
$this->assertInstanceOf( 'WC_Logger', $log_a );
|
|
|
|
$this->assertInstanceOf( 'WC_Logger', $log_b );
|
2016-12-15 19:52:24 +00:00
|
|
|
$this->assertSame( $log_a, $log_b, '`wc_get_logger()` should return the same instance' );
|
|
|
|
}
|
|
|
|
|
2018-09-26 10:41:28 +00:00
|
|
|
/**
|
|
|
|
* Test wc_get_logger() to check if can return instance when given in filter.
|
|
|
|
*/
|
|
|
|
public function test_wc_get_logger_for_instance() {
|
|
|
|
add_filter( 'woocommerce_logging_class', array( $this, 'return_valid_logger_instance' ) );
|
|
|
|
|
|
|
|
$logger = wc_get_logger();
|
|
|
|
|
2018-09-26 13:37:08 +00:00
|
|
|
$this->assertInstanceOf( 'WC_Logger_Interface', $logger, '`wc_get_logger()` should return valid Dummy_WC_Logger instance' );
|
2018-09-26 10:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return valid logger instance that implements WC_Logger_Interface.
|
|
|
|
*
|
|
|
|
* @return WC_Logger_Interface
|
|
|
|
*/
|
|
|
|
public function return_valid_logger_instance() {
|
2019-04-02 14:18:06 +00:00
|
|
|
if ( ! class_exists( 'Dummy_WC_Logger' ) ) {
|
|
|
|
include_once 'dummy-wc-logger.php';
|
|
|
|
}
|
2018-09-26 10:41:28 +00:00
|
|
|
return new Dummy_WC_Logger();
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:45:51 +00:00
|
|
|
/**
|
|
|
|
* Return class which does not implement WC_Logger_Interface
|
|
|
|
*
|
|
|
|
* This is a helper function to test woocommerce_logging_class filter and wc_get_logger.
|
|
|
|
*
|
|
|
|
* @return string Class name
|
|
|
|
*/
|
|
|
|
public function return_bad_logger() {
|
|
|
|
return __CLASS__;
|
|
|
|
}
|
|
|
|
|
2014-11-18 14:56:13 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_base_location().
|
2014-11-18 14:56:13 +00:00
|
|
|
*
|
|
|
|
* @since 2.3.0
|
|
|
|
*/
|
2015-01-05 10:26:35 +00:00
|
|
|
public function test_wc_get_base_location() {
|
|
|
|
$default = wc_get_base_location();
|
2014-11-18 14:56:13 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'GB', $default['country'] );
|
|
|
|
$this->assertEquals( '', $default['state'] );
|
|
|
|
}
|
|
|
|
|
2016-02-16 07:31:29 +00:00
|
|
|
/**
|
|
|
|
* Test wc_format_country_state_string().
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
public function test_wc_format_country_state_string() {
|
2016-02-16 09:05:32 +00:00
|
|
|
// Test with correct values.
|
2017-12-18 13:58:40 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'country' => 'US',
|
2018-09-26 10:41:28 +00:00
|
|
|
'state' => 'CA',
|
2017-12-18 13:58:40 +00:00
|
|
|
),
|
|
|
|
wc_format_country_state_string( 'US:CA' )
|
|
|
|
);
|
|
|
|
|
2016-02-16 09:05:32 +00:00
|
|
|
// Test what happens when we pass an incorrect value.
|
2017-12-18 13:58:40 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'country' => 'US-CA',
|
2018-09-26 10:41:28 +00:00
|
|
|
'state' => '',
|
2017-12-18 13:58:40 +00:00
|
|
|
),
|
|
|
|
wc_format_country_state_string( 'US-CA' )
|
|
|
|
);
|
2016-02-16 07:31:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 21:29:38 +00:00
|
|
|
/**
|
|
|
|
* Test wc_get_shipping_method_count()
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
public function test_wc_get_shipping_method_count() {
|
|
|
|
// Without legacy methods.
|
|
|
|
$this->assertEquals( 0, wc_get_shipping_method_count( false ) );
|
|
|
|
|
|
|
|
// With legacy methods.
|
|
|
|
$this->assertEquals( 0, wc_get_shipping_method_count( true ) );
|
|
|
|
}
|
2016-12-17 20:16:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_print_r()
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-17 20:16:26 +00:00
|
|
|
*/
|
|
|
|
public function test_wc_print_r() {
|
2017-12-18 13:58:40 +00:00
|
|
|
$arr = array(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
'a',
|
|
|
|
'b',
|
|
|
|
'c' => 'd',
|
|
|
|
);
|
2016-12-17 20:16:26 +00:00
|
|
|
|
|
|
|
// This filter will sequentially remove handlers, allowing us to test as though our
|
2020-08-25 21:03:03 +00:00
|
|
|
// functions were accumulatively blocked, adding one on each call.
|
2016-12-17 20:16:26 +00:00
|
|
|
add_filter( 'woocommerce_print_r_alternatives', array( $this, 'filter_wc_print_r_alternatives' ) );
|
|
|
|
|
|
|
|
$this->expectOutputString(
|
|
|
|
print_r( $arr, true ),
|
|
|
|
$return_value = wc_print_r( $arr )
|
|
|
|
);
|
|
|
|
$this->assertTrue( $return_value );
|
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
$this->expectOutputString(
|
|
|
|
var_export( $arr, true ),
|
|
|
|
$return_value = wc_print_r( $arr )
|
|
|
|
);
|
|
|
|
$this->assertTrue( $return_value );
|
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
$this->expectOutputString(
|
|
|
|
json_encode( $arr ),
|
|
|
|
$return_value = wc_print_r( $arr )
|
|
|
|
);
|
|
|
|
$this->assertTrue( $return_value );
|
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
$this->expectOutputString(
|
|
|
|
serialize( $arr ),
|
|
|
|
$return_value = wc_print_r( $arr )
|
|
|
|
);
|
|
|
|
$this->assertTrue( $return_value );
|
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
$this->expectOutputString(
|
|
|
|
'',
|
|
|
|
$return_value = wc_print_r( $arr )
|
|
|
|
);
|
|
|
|
$this->assertFalse( $return_value );
|
|
|
|
ob_clean();
|
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
// Reset filter to include all handlers.
|
2016-12-17 20:16:26 +00:00
|
|
|
$this->filter_wc_print_r_alternatives( array(), true );
|
|
|
|
|
|
|
|
$this->assertEquals( print_r( $arr, true ), wc_print_r( $arr, true ) );
|
|
|
|
$this->assertEquals( var_export( $arr, true ), wc_print_r( $arr, true ) );
|
2019-02-20 12:00:47 +00:00
|
|
|
$this->assertEquals( wp_json_encode( $arr ), wc_print_r( $arr, true ) );
|
2016-12-17 20:16:26 +00:00
|
|
|
$this->assertEquals( serialize( $arr ), wc_print_r( $arr, true ) );
|
|
|
|
$this->assertFalse( wc_print_r( $arr, true ) );
|
|
|
|
|
|
|
|
remove_filter( 'woocommerce_print_r_alternatives', array( $this, 'filter_wc_print_r_alternatives' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter function to help test wc_print_r alternatives via filter.
|
|
|
|
*
|
|
|
|
* On the first run, all alternatives are returned.
|
|
|
|
* On each successive run, an alternative is excluded from the beginning.
|
|
|
|
* Eventually, no handlers are returned.
|
|
|
|
*
|
|
|
|
* @param array $alternatives Input array of alternatives.
|
2017-12-18 13:58:40 +00:00
|
|
|
* @param bool $reset Optional. Default false. True to reset excluded alternatives.
|
2016-12-17 20:16:26 +00:00
|
|
|
* @return array|bool Alternatives. True on reset.
|
|
|
|
*/
|
|
|
|
public function filter_wc_print_r_alternatives( $alternatives, $reset = false ) {
|
|
|
|
static $skip = 0;
|
|
|
|
if ( $reset ) {
|
|
|
|
$skip = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return array_slice( $alternatives, $skip++ );
|
|
|
|
}
|
|
|
|
|
2017-01-22 01:24:20 +00:00
|
|
|
/**
|
|
|
|
* Test wc_get_wildcard_postcodes
|
|
|
|
*/
|
|
|
|
public function test_wc_get_wildcard_postcodes() {
|
|
|
|
$postcode = 'cb23 6as';
|
|
|
|
$country = 'GB';
|
|
|
|
$wildcards = array( 'cb23 6as', 'CB23 6AS', 'CB23 6AS*', 'CB23 6A*', 'CB23 6*', 'CB23 *', 'CB23*', 'CB2*', 'CB*', 'C*', '*' );
|
|
|
|
$this->assertEquals( $wildcards, wc_get_wildcard_postcodes( $postcode, $country ) );
|
|
|
|
|
|
|
|
$postcode = 'GIJóN';
|
|
|
|
$country = '';
|
|
|
|
$wildcards = array( 'GIJóN', 'GIJÓN', 'GIJÓN*', 'GIJÓ*', 'GIJ*', 'GI*', 'G*', '*' );
|
|
|
|
$this->assertEquals( $wildcards, wc_get_wildcard_postcodes( $postcode, $country ) );
|
|
|
|
}
|
2017-12-18 13:58:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests wc_maybe_define_constant().
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function test_wc_maybe_define_constant() {
|
2020-01-29 05:21:29 +00:00
|
|
|
$this->assertFalse( Constants::is_defined( 'WC_TESTING_DEFINE_FUNCTION' ) );
|
2017-12-18 13:58:40 +00:00
|
|
|
|
|
|
|
// Check if defined.
|
|
|
|
wc_maybe_define_constant( 'WC_TESTING_DEFINE_FUNCTION', true );
|
2020-01-29 05:21:29 +00:00
|
|
|
$this->assertTrue( Constants::is_defined( 'WC_TESTING_DEFINE_FUNCTION' ) );
|
2017-12-18 13:58:40 +00:00
|
|
|
|
|
|
|
// Check value.
|
|
|
|
wc_maybe_define_constant( 'WC_TESTING_DEFINE_FUNCTION', false );
|
|
|
|
$this->assertTrue( WC_TESTING_DEFINE_FUNCTION );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests wc_create_order() and wc_update_order() currency handling.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function test_wc_create_update_order_currency() {
|
|
|
|
$old_currency = get_woocommerce_currency();
|
|
|
|
$new_currency = 'BGN';
|
|
|
|
|
|
|
|
update_option( 'woocommerce_currency', $new_currency );
|
|
|
|
|
|
|
|
// New order should be created using shop currency.
|
2018-09-26 10:41:28 +00:00
|
|
|
$order = wc_create_order(
|
|
|
|
array(
|
|
|
|
'status' => 'pending',
|
|
|
|
'customer_id' => 1,
|
|
|
|
'created_via' => 'unit tests',
|
|
|
|
'cart_hash' => '',
|
|
|
|
)
|
|
|
|
);
|
2017-12-18 13:58:40 +00:00
|
|
|
$this->assertEquals( $new_currency, $order->get_currency() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_currency', $old_currency );
|
|
|
|
|
|
|
|
// Currency should not change when order is updated.
|
2018-09-26 10:41:28 +00:00
|
|
|
$order = wc_update_order(
|
|
|
|
array(
|
|
|
|
'customer_id' => 2,
|
|
|
|
'order_id' => $order->get_id(),
|
|
|
|
)
|
|
|
|
);
|
2017-12-18 13:58:40 +00:00
|
|
|
$this->assertEquals( $new_currency, $order->get_currency() );
|
|
|
|
|
2018-09-26 10:41:28 +00:00
|
|
|
$order = wc_update_order(
|
|
|
|
array(
|
|
|
|
'customer_id' => 2,
|
|
|
|
)
|
|
|
|
);
|
2017-12-18 13:58:40 +00:00
|
|
|
$this->assertInstanceOf( 'WP_Error', $order );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the wc_is_active_theme function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_is_active_theme() {
|
|
|
|
$current_theme = get_template();
|
|
|
|
$this->assertTrue( wc_is_active_theme( $current_theme ) );
|
|
|
|
$this->assertFalse( wc_is_active_theme( 'somegiberish' ) );
|
|
|
|
$this->assertTrue( wc_is_active_theme( array( $current_theme, 'somegiberish' ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-02 14:18:06 +00:00
|
|
|
* Test the wc_get_template_part function.
|
2017-12-18 13:58:40 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_template_part() {
|
|
|
|
$this->assertEmpty( wc_get_template_part( 'nothinghere' ) );
|
|
|
|
}
|
|
|
|
|
2020-04-05 19:15:39 +00:00
|
|
|
/**
|
|
|
|
* Tests the wc_tokenize_path function.
|
|
|
|
*/
|
|
|
|
public function test_wc_tokenize_path() {
|
2020-04-05 19:41:39 +00:00
|
|
|
$path = wc_tokenize_path( ABSPATH . 'test', array() );
|
|
|
|
$this->assertEquals( ABSPATH . 'test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
|
|
|
|
$path = wc_tokenize_path(
|
2020-04-05 19:41:39 +00:00
|
|
|
ABSPATH . 'test',
|
2020-04-05 19:15:39 +00:00
|
|
|
array(
|
|
|
|
'ABSPATH' => ABSPATH,
|
|
|
|
)
|
|
|
|
);
|
2020-04-05 19:41:39 +00:00
|
|
|
$this->assertEquals( '{{ABSPATH}}test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
|
|
|
|
$path = wc_tokenize_path(
|
2020-04-05 19:41:39 +00:00
|
|
|
ABSPATH . 'test',
|
2020-04-05 19:15:39 +00:00
|
|
|
array(
|
|
|
|
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
|
|
|
)
|
|
|
|
);
|
2020-04-05 19:41:39 +00:00
|
|
|
$this->assertEquals( ABSPATH . 'test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
|
|
|
|
$path = wc_tokenize_path(
|
2020-04-05 19:41:39 +00:00
|
|
|
WP_CONTENT_DIR . 'test',
|
2020-04-05 19:15:39 +00:00
|
|
|
array(
|
|
|
|
'ABSPATH' => ABSPATH,
|
2020-04-05 19:41:39 +00:00
|
|
|
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
2020-04-05 19:15:39 +00:00
|
|
|
)
|
|
|
|
);
|
2020-04-05 19:41:39 +00:00
|
|
|
$this->assertEquals( '{{WP_CONTENT_DIR}}test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the wc_untokenize_path function.
|
|
|
|
*/
|
|
|
|
public function test_wc_untokenize_path() {
|
2020-04-05 19:41:39 +00:00
|
|
|
$path = wc_untokenize_path( '{{ABSPATH}}test', array() );
|
|
|
|
$this->assertEquals( '{{ABSPATH}}test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
|
|
|
|
$path = wc_untokenize_path(
|
2020-04-05 19:41:39 +00:00
|
|
|
'{{ABSPATH}}test',
|
2020-04-05 19:15:39 +00:00
|
|
|
array(
|
|
|
|
'ABSPATH' => ABSPATH,
|
|
|
|
)
|
|
|
|
);
|
2020-04-05 19:41:39 +00:00
|
|
|
$this->assertEquals( ABSPATH . 'test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
|
|
|
|
$path = wc_untokenize_path(
|
2020-04-05 19:41:39 +00:00
|
|
|
'{{ABSPATH}}test',
|
2020-04-05 19:15:39 +00:00
|
|
|
array(
|
|
|
|
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
|
|
|
)
|
|
|
|
);
|
2020-04-05 19:41:39 +00:00
|
|
|
$this->assertEquals( '{{ABSPATH}}test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
|
|
|
|
$path = wc_untokenize_path(
|
2020-04-05 19:41:39 +00:00
|
|
|
'{{WP_CONTENT_DIR}}test',
|
2020-04-05 19:15:39 +00:00
|
|
|
array(
|
|
|
|
'WP_CONTENT_DIR' => WP_CONTENT_DIR,
|
|
|
|
'ABSPATH' => ABSPATH,
|
|
|
|
)
|
|
|
|
);
|
2020-04-05 19:41:39 +00:00
|
|
|
$this->assertEquals( WP_CONTENT_DIR . 'test', $path );
|
2020-04-05 19:15:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 20:07:49 +00:00
|
|
|
/**
|
|
|
|
* Tests the wc_get_path_define_tokens function.
|
|
|
|
*/
|
|
|
|
public function test_wc_get_path_define_tokens() {
|
|
|
|
$defines = wc_get_path_define_tokens();
|
|
|
|
$this->assertArrayHasKey( 'ABSPATH', $defines );
|
|
|
|
$this->assertEquals( ABSPATH, $defines['ABSPATH'] );
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:18:06 +00:00
|
|
|
/**
|
|
|
|
* Test wc_get_template.
|
|
|
|
*
|
|
|
|
* @expectedIncorrectUsage wc_get_template
|
|
|
|
*/
|
2019-04-03 15:08:12 +00:00
|
|
|
public function test_wc_get_template_invalid_action_args() {
|
2019-04-10 12:50:42 +00:00
|
|
|
ob_start();
|
2019-04-02 14:18:06 +00:00
|
|
|
wc_get_template(
|
2019-04-03 15:08:12 +00:00
|
|
|
'global/wrapper-start.php',
|
2019-04-02 14:18:06 +00:00
|
|
|
array(
|
|
|
|
'action_args' => 'this is bad',
|
|
|
|
)
|
|
|
|
);
|
2019-04-10 12:50:42 +00:00
|
|
|
$template = ob_get_clean();
|
2019-04-02 14:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_get_template.
|
|
|
|
*/
|
|
|
|
public function test_wc_get_template() {
|
|
|
|
ob_start();
|
|
|
|
wc_get_template( 'global/wrapper-start.php' );
|
|
|
|
$template = ob_get_clean();
|
|
|
|
$this->assertNotEmpty( $template );
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
wc_get_template(
|
|
|
|
'global/wrapper-start.php',
|
|
|
|
array(
|
|
|
|
'template' => 'x',
|
|
|
|
'located' => 'x',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$template = ob_get_clean();
|
|
|
|
$this->assertNotEmpty( $template );
|
|
|
|
}
|
|
|
|
|
2020-01-31 00:24:07 +00:00
|
|
|
/**
|
|
|
|
* This test ensures that the absolute path to template files is replaced with a token. We do this so
|
|
|
|
* that the path can be made relative to each installation, and the cache can be shared.
|
|
|
|
*/
|
|
|
|
public function test_wc_get_template_cleans_absolute_path() {
|
|
|
|
add_filter( 'woocommerce_locate_template', array( $this, 'force_template_path' ), 10, 2 );
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
try {
|
|
|
|
wc_get_template( 'global/wrapper-start.php' );
|
2020-04-05 19:41:39 +00:00
|
|
|
} catch ( \Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
|
2020-01-31 00:24:07 +00:00
|
|
|
// Since the file doesn't really exist this is going to throw an exception (which is fine for our test).
|
|
|
|
}
|
|
|
|
ob_end_clean();
|
|
|
|
|
2020-01-31 02:12:00 +00:00
|
|
|
remove_filter( 'woocommerce_locate_template', array( $this, 'force_template_path' ) );
|
2020-01-31 00:24:07 +00:00
|
|
|
|
|
|
|
$file_path = wp_cache_get( sanitize_key( 'template-global/wrapper-start.php---' . WC_VERSION ), 'woocommerce' );
|
|
|
|
|
|
|
|
$this->assertEquals( '{{ABSPATH}}global/wrapper-start.php', $file_path );
|
|
|
|
}
|
|
|
|
|
2017-12-18 13:58:40 +00:00
|
|
|
/**
|
|
|
|
* Test wc_get_image_size function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_image_size() {
|
|
|
|
$this->assertArrayHasKey( 'width', wc_get_image_size( array( 100, 100, 1 ) ) );
|
|
|
|
$this->assertArrayHasKey( 'height', wc_get_image_size( 'shop_single' ) );
|
|
|
|
update_option( 'woocommerce_thumbnail_cropping', 'uncropped' );
|
|
|
|
$this->assertArrayHasKey( 'crop', wc_get_image_size( 'shop_thumbnail' ) );
|
|
|
|
update_option( 'woocommerce_thumbnail_cropping', 'custom' );
|
|
|
|
$this->assertArrayHasKey( 'crop', wc_get_image_size( 'shop_thumbnail' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_enqueue_js and wc_print_js functions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_enqueue_js_wc_print_js() {
|
|
|
|
$js = 'alert( "test" );';
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
wc_print_js();
|
|
|
|
$printed_js = ob_get_clean();
|
|
|
|
$this->assertNotContains( $js, $printed_js );
|
|
|
|
|
|
|
|
wc_enqueue_js( $js );
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
wc_print_js();
|
|
|
|
$printed_js = ob_get_clean();
|
|
|
|
$this->assertContains( $js, $printed_js );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_get_log_file_name function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_log_file_name() {
|
|
|
|
$this->assertNotEmpty( wc_get_log_file_name( 'test' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_get_page_children function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_page_children() {
|
2018-09-26 10:41:28 +00:00
|
|
|
$page_id = wp_insert_post(
|
|
|
|
array(
|
|
|
|
'post_title' => 'Parent Page',
|
|
|
|
'post_type' => 'page',
|
|
|
|
'post_name' => 'parent-page',
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'post_author' => 1,
|
|
|
|
'menu_order' => 0,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$child_page_id = wp_insert_post(
|
|
|
|
array(
|
|
|
|
'post_parent' => $page_id,
|
|
|
|
'post_title' => 'Parent Page',
|
|
|
|
'post_type' => 'page',
|
|
|
|
'post_name' => 'parent-page',
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'post_author' => 1,
|
|
|
|
'menu_order' => 0,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$children = wc_get_page_children( $page_id );
|
2017-12-18 13:58:40 +00:00
|
|
|
$this->assertEquals( $child_page_id, $children[0] );
|
|
|
|
|
|
|
|
wp_delete_post( $page_id, true );
|
|
|
|
wp_delete_post( $child_page_id, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test hash_equals function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_hash_equals() {
|
|
|
|
$this->assertTrue( hash_equals( 'abc', 'abc' ) ); // @codingStandardsIgnoreLine.
|
|
|
|
$this->assertFalse( hash_equals( 'abcd', 'abc' ) ); // @codingStandardsIgnoreLine.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_rand_hash function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_rand_hash() {
|
|
|
|
$this->assertNotEquals( wc_rand_hash(), wc_rand_hash() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_transaction_query function.
|
|
|
|
*/
|
|
|
|
public function test_wc_transaction_query() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$wpdb->insert(
|
|
|
|
$wpdb->prefix . 'options',
|
|
|
|
array(
|
|
|
|
'option_name' => 'transaction_test',
|
|
|
|
'option_value' => '1',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
)
|
|
|
|
);
|
2018-01-03 16:13:08 +00:00
|
|
|
wc_transaction_query( 'start', true );
|
2017-12-18 13:58:40 +00:00
|
|
|
$wpdb->update(
|
|
|
|
$wpdb->prefix . 'options',
|
|
|
|
array(
|
|
|
|
'option_value' => '0',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'option_name' => 'transaction_test',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$col = $wpdb->get_col( "SElECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'transaction_test'" );
|
|
|
|
$this->assertEquals( '0', $col[0] );
|
|
|
|
|
2018-01-03 16:13:08 +00:00
|
|
|
wc_transaction_query( 'rollback', true );
|
2017-12-18 13:58:40 +00:00
|
|
|
$col = $wpdb->get_col( "SElECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'transaction_test'" );
|
|
|
|
$this->assertEquals( '1', $col[0] );
|
|
|
|
|
2018-01-03 16:13:08 +00:00
|
|
|
wc_transaction_query( 'start', true );
|
2017-12-18 13:58:40 +00:00
|
|
|
$wpdb->update(
|
|
|
|
$wpdb->prefix . 'options',
|
|
|
|
array(
|
|
|
|
'option_value' => '0',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'option_name' => 'transaction_test',
|
|
|
|
)
|
|
|
|
);
|
2018-01-03 16:13:08 +00:00
|
|
|
wc_transaction_query( 'commit', true );
|
2017-12-18 13:58:40 +00:00
|
|
|
$col = $wpdb->get_col( "SElECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'transaction_test'" );
|
|
|
|
$this->assertEquals( '0', $col[0] );
|
|
|
|
|
|
|
|
$wpdb->delete(
|
|
|
|
$wpdb->prefix . 'options',
|
|
|
|
array(
|
|
|
|
'option_name' => 'transaction_test',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-04-18 13:54:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: wc_selected
|
|
|
|
*/
|
|
|
|
public function test_wc_selected() {
|
|
|
|
$test_cases = array(
|
|
|
|
// both value and options int.
|
|
|
|
array( 0, 0, true ),
|
|
|
|
array( 0, 1, false ),
|
|
|
|
array( 1, 0, false ),
|
|
|
|
|
|
|
|
// value string, options int.
|
|
|
|
array( '0', 0, true ),
|
|
|
|
array( '0', 1, false ),
|
|
|
|
array( '1', 0, false ),
|
|
|
|
|
|
|
|
// value int, options string.
|
|
|
|
array( 0, '0', true ),
|
|
|
|
array( 0, '1', false ),
|
|
|
|
array( 1, '0', false ),
|
|
|
|
|
|
|
|
// both value and options str.
|
|
|
|
array( '0', '0', true ),
|
|
|
|
array( '0', '1', false ),
|
|
|
|
array( '1', '0', false ),
|
|
|
|
|
|
|
|
// both value and options int.
|
|
|
|
array( 0, array( 0, 1, 2 ), true ),
|
|
|
|
array( 0, array( 1, 1, 1 ), false ),
|
|
|
|
|
|
|
|
// value string, options int.
|
|
|
|
array( '0', array( 0, 1, 2 ), true ),
|
|
|
|
array( '0', array( 1, 1, 1 ), false ),
|
|
|
|
|
|
|
|
// value int, options string.
|
|
|
|
array( 0, array( '0', '1', '2' ), true ),
|
|
|
|
array( 0, array( '1', '1', '1' ), false ),
|
|
|
|
|
|
|
|
// both value and options str.
|
|
|
|
array( '0', array( '0', '1', '2' ), true ),
|
|
|
|
array( '0', array( '1', '1', '1' ), false ),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $test_cases as $test_case ) {
|
|
|
|
list( $value, $options, $result ) = $test_case;
|
2018-09-26 10:41:28 +00:00
|
|
|
$actual_result = $result ? " selected='selected'" : '';
|
2018-04-18 13:54:57 +00:00
|
|
|
$this->assertEquals( wc_selected( $value, $options ), $actual_result );
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 12:59:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_array_cartesian.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_array_cartesian() {
|
2018-07-16 10:59:01 +00:00
|
|
|
$array = array(
|
|
|
|
'attr1' => array(
|
|
|
|
'Attr1 Value 1',
|
|
|
|
'Attr1 Value 2',
|
|
|
|
),
|
|
|
|
'attr2' => array(
|
|
|
|
'Attr2 Value 1',
|
|
|
|
'Attr2 Value 2',
|
|
|
|
),
|
|
|
|
);
|
2018-07-11 12:59:05 +00:00
|
|
|
|
|
|
|
$expected_combinations = array(
|
|
|
|
array(
|
|
|
|
'attr2' => 'Attr2 Value 1',
|
|
|
|
'attr1' => 'Attr1 Value 1',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'attr2' => 'Attr2 Value 2',
|
|
|
|
'attr1' => 'Attr1 Value 1',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'attr2' => 'Attr2 Value 1',
|
|
|
|
'attr1' => 'Attr1 Value 2',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'attr2' => 'Attr2 Value 2',
|
|
|
|
'attr1' => 'Attr1 Value 2',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2018-07-16 10:59:01 +00:00
|
|
|
$this->assertEquals( $expected_combinations, array_reverse( wc_array_cartesian( $array ) ) );
|
2018-07-11 12:59:05 +00:00
|
|
|
}
|
2018-07-11 13:05:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_get_credit_card_type_label.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_credit_card_type_label() {
|
|
|
|
$this->assertEquals( 'Visa', wc_get_credit_card_type_label( 'visa' ) );
|
|
|
|
$this->assertEquals( 'JCB', wc_get_credit_card_type_label( 'jCb' ) );
|
|
|
|
$this->assertEquals( 'MasterCard', wc_get_credit_card_type_label( 'Mastercard' ) );
|
|
|
|
$this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american_express' ) );
|
|
|
|
$this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american-express' ) );
|
2018-07-16 11:02:24 +00:00
|
|
|
$this->assertEquals( '', wc_get_credit_card_type_label( '' ) );
|
|
|
|
$this->assertEquals( 'Random name', wc_get_credit_card_type_label( 'random-name' ) );
|
2018-07-11 13:05:18 +00:00
|
|
|
}
|
2018-07-11 13:10:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_get_permalink_structure.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_permalink_structure() {
|
|
|
|
$expected_structure = array(
|
|
|
|
'product_base' => 'product',
|
|
|
|
'category_base' => 'product-category',
|
|
|
|
'tag_base' => 'product-tag',
|
|
|
|
'attribute_base' => '',
|
|
|
|
'use_verbose_page_rules' => '',
|
|
|
|
'product_rewrite_slug' => 'product',
|
|
|
|
'category_rewrite_slug' => 'product-category',
|
|
|
|
'tag_rewrite_slug' => 'product-tag',
|
|
|
|
'attribute_rewrite_slug' => '',
|
|
|
|
);
|
|
|
|
$this->assertEquals( $expected_structure, wc_get_permalink_structure() );
|
|
|
|
}
|
2018-07-11 13:17:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_decimal_to_fraction.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_decimal_to_fraction() {
|
|
|
|
$this->assertEquals( array( 7, 2 ), wc_decimal_to_fraction( '3.5' ) );
|
2018-07-25 12:07:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 12:09:04 +00:00
|
|
|
/**
|
2018-07-11 12:11:31 +00:00
|
|
|
* Test wc_get_user_agent function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_get_user_agent() {
|
2018-09-26 10:41:28 +00:00
|
|
|
$example_user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36';
|
2018-07-11 12:11:31 +00:00
|
|
|
$_SERVER['HTTP_USER_AGENT'] = $example_user_agent;
|
|
|
|
$this->assertEquals( $example_user_agent, wc_get_user_agent() );
|
2018-07-11 13:17:00 +00:00
|
|
|
}
|
2018-10-31 06:46:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the wc_checkout_fields_uasort_comparison function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_checkout_fields_uasort_comparison() {
|
|
|
|
$fields = array(
|
|
|
|
'billing_first_name' => array(
|
|
|
|
'priority' => 10,
|
|
|
|
),
|
2019-05-01 22:05:00 +00:00
|
|
|
'billing_last_name' => array(
|
2018-10-31 06:46:49 +00:00
|
|
|
'priority' => 20,
|
|
|
|
),
|
2019-05-01 22:05:00 +00:00
|
|
|
'billing_email' => array(
|
2018-10-31 06:46:49 +00:00
|
|
|
'priority' => 1,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
uasort( $fields, 'wc_checkout_fields_uasort_comparison' );
|
|
|
|
$this->assertSame( 0, array_search( 'billing_email', array_keys( $fields ) ) );
|
|
|
|
}
|
2019-01-11 11:48:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_ascii_uasort_comparison function
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_ascii_uasort_comparison() {
|
|
|
|
$unsorted_values = array(
|
|
|
|
'Benin',
|
|
|
|
'Bélgica',
|
|
|
|
);
|
|
|
|
|
|
|
|
// First test a normal asort which does not work right for accented characters.
|
|
|
|
$sorted_values = $unsorted_values;
|
|
|
|
asort( $sorted_values );
|
2019-01-11 12:02:54 +00:00
|
|
|
$this->assertSame( array( 'Benin', 'Bélgica' ), array_values( $sorted_values ) );
|
2019-01-11 11:48:37 +00:00
|
|
|
|
|
|
|
$sorted_values = $unsorted_values;
|
|
|
|
// Now test the new wc_ascii_uasort_comparison function which sorts the strings correctly.
|
|
|
|
uasort( $sorted_values, 'wc_ascii_uasort_comparison' );
|
2019-01-11 12:02:54 +00:00
|
|
|
$this->assertSame( array( 'Bélgica', 'Benin' ), array_values( $sorted_values ) );
|
2019-01-11 11:48:37 +00:00
|
|
|
}
|
2019-05-22 10:24:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test wc_load_cart function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_wc_load_cart() {
|
|
|
|
$this->assertInstanceOf( 'WC_Cart', $this->wc->cart );
|
|
|
|
$this->assertInstanceOf( 'WC_Customer', $this->wc->customer );
|
|
|
|
$this->assertInstanceOf( 'WC_Session', $this->wc->session );
|
|
|
|
|
2020-04-05 19:41:39 +00:00
|
|
|
$this->wc->cart = null;
|
|
|
|
$this->wc->customer = null;
|
|
|
|
$this->wc->session = null;
|
2019-05-22 10:24:31 +00:00
|
|
|
$this->assertNull( $this->wc->cart );
|
|
|
|
$this->assertNull( $this->wc->customer );
|
|
|
|
$this->assertNull( $this->wc->session );
|
|
|
|
|
|
|
|
wc_load_cart();
|
|
|
|
$this->assertInstanceOf( 'WC_Cart', $this->wc->cart );
|
|
|
|
$this->assertInstanceOf( 'WC_Customer', $this->wc->customer );
|
|
|
|
$this->assertInstanceOf( 'WC_Session', $this->wc->session );
|
|
|
|
|
|
|
|
}
|
2020-01-31 00:24:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows us to force the template path. Since the ABSPATH is to /tmp/wordpress in tests, we need to do this
|
|
|
|
* in order to keep the paths consistent for testing purposes.
|
|
|
|
*
|
|
|
|
* @param string $template The path to the template file.
|
|
|
|
* @param string $template_name The name of the template file.
|
|
|
|
* @return string The path to be used instead.
|
|
|
|
*/
|
|
|
|
public function force_template_path( $template, $template_name ) {
|
|
|
|
return ABSPATH . $template_name;
|
|
|
|
}
|
2014-09-01 06:00:12 +00:00
|
|
|
}
|