2014-11-25 17:09:19 +00:00
< ? php
2020-03-12 15:57:32 +00:00
/**
* Tests for the WC_Install class .
*
* @ package WooCommerce\Tests\Util
*/
2014-11-25 17:09:19 +00:00
2015-03-06 15:32:40 +00:00
/**
2015-11-03 13:31:20 +00:00
* Class WC_Tests_Install .
2020-03-12 15:57:32 +00:00
*
* @ covers WC_Install
2015-03-06 15:32:40 +00:00
*/
2016-03-23 12:14:13 +00:00
class WC_Tests_Install extends WC_Unit_Test_Case {
2014-11-25 17:09:19 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test check version .
2014-11-25 17:09:19 +00:00
*/
public function test_check_version () {
2017-01-04 18:45:45 +00:00
update_option ( 'woocommerce_version' , ( ( float ) WC () -> version - 1 ) );
2016-03-23 12:14:13 +00:00
WC_Install :: check_version ();
2014-11-25 17:09:19 +00:00
$this -> assertTrue ( did_action ( 'woocommerce_updated' ) === 1 );
update_option ( 'woocommerce_version' , WC () -> version );
2016-03-23 12:14:13 +00:00
WC_Install :: check_version ();
2014-11-25 17:09:19 +00:00
$this -> assertTrue ( did_action ( 'woocommerce_updated' ) === 1 );
2018-02-05 16:40:51 +00:00
update_option ( 'woocommerce_version' , ( float ) WC () -> version + 1 );
WC_Install :: check_version ();
$this -> assertTrue (
did_action ( 'woocommerce_updated' ) === 1 ,
'WC_Install::check_version() should not call install routine when the WC version stored in the database is bigger than the version in the code as downgrades are not supported.'
);
2014-11-25 17:09:19 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Test - install .
2014-11-25 17:09:19 +00:00
*/
2020-05-27 18:23:20 +00:00
/**
2014-11-25 17:09:19 +00:00
public function test_install () {
2017-10-05 17:11:25 +00:00
// clean existing install first.
2014-11-25 17:09:19 +00:00
if ( ! defined ( 'WP_UNINSTALL_PLUGIN' ) ) {
define ( 'WP_UNINSTALL_PLUGIN' , true );
2016-08-24 16:34:13 +00:00
define ( 'WC_REMOVE_ALL_DATA' , true );
2014-11-25 17:09:19 +00:00
}
2016-01-08 14:53:24 +00:00
2020-04-24 20:53:40 +00:00
include dirname ( dirname ( dirname ( dirname ( dirname ( __FILE__ ) ) ) ) ) . '/uninstall.php' ;
2017-10-05 17:11:25 +00:00
delete_transient ( 'wc_installing' );
2014-11-25 17:09:19 +00:00
2016-03-23 12:14:13 +00:00
WC_Install :: install ();
2014-11-25 17:09:19 +00:00
2018-02-05 16:21:07 +00:00
$this -> assertEquals ( WC () -> version , get_option ( 'woocommerce_version' ) );
2014-11-25 17:09:19 +00:00
}
2020-05-27 18:23:20 +00:00
*
**/
2014-11-25 17:09:19 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test - create pages .
2014-11-25 17:09:19 +00:00
*/
public function test_create_pages () {
2020-03-12 15:57:32 +00:00
// Clear options.
2014-11-25 17:09:19 +00:00
delete_option ( 'woocommerce_shop_page_id' );
delete_option ( 'woocommerce_cart_page_id' );
delete_option ( 'woocommerce_checkout_page_id' );
delete_option ( 'woocommerce_myaccount_page_id' );
2016-03-23 12:14:13 +00:00
WC_Install :: create_pages ();
2014-11-25 17:09:19 +00:00
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_shop_page_id' ) );
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_cart_page_id' ) );
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_checkout_page_id' ) );
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_myaccount_page_id' ) );
2020-03-12 15:57:32 +00:00
// Delete pages.
2014-11-25 17:09:19 +00:00
wp_delete_post ( get_option ( 'woocommerce_shop_page_id' ), true );
wp_delete_post ( get_option ( 'woocommerce_cart_page_id' ), true );
wp_delete_post ( get_option ( 'woocommerce_checkout_page_id' ), true );
wp_delete_post ( get_option ( 'woocommerce_myaccount_page_id' ), true );
2020-03-12 15:57:32 +00:00
// Clear options.
2014-11-25 17:09:19 +00:00
delete_option ( 'woocommerce_shop_page_id' );
delete_option ( 'woocommerce_cart_page_id' );
delete_option ( 'woocommerce_checkout_page_id' );
delete_option ( 'woocommerce_myaccount_page_id' );
2016-03-23 12:14:13 +00:00
WC_Install :: create_pages ();
2014-11-25 17:09:19 +00:00
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_shop_page_id' ) );
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_cart_page_id' ) );
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_checkout_page_id' ) );
$this -> assertGreaterThan ( 0 , get_option ( 'woocommerce_myaccount_page_id' ) );
}
/**
2015-11-03 13:31:20 +00:00
* Test - create roles .
2014-11-25 17:09:19 +00:00
*/
public function test_create_roles () {
2020-03-12 15:57:32 +00:00
// Clean existing install first.
2014-11-25 17:09:19 +00:00
if ( ! defined ( 'WP_UNINSTALL_PLUGIN' ) ) {
define ( 'WP_UNINSTALL_PLUGIN' , true );
2016-08-24 16:34:13 +00:00
define ( 'WC_REMOVE_ALL_DATA' , true );
2014-11-25 17:09:19 +00:00
}
2020-04-24 20:53:40 +00:00
include dirname ( dirname ( dirname ( dirname ( dirname ( __FILE__ ) ) ) ) ) . '/uninstall.php' ;
2014-11-25 17:09:19 +00:00
2016-03-23 12:14:13 +00:00
WC_Install :: create_roles ();
2014-11-25 17:09:19 +00:00
$this -> assertNotNull ( get_role ( 'customer' ) );
$this -> assertNotNull ( get_role ( 'shop_manager' ) );
}
/**
2015-11-03 13:31:20 +00:00
* Test - remove roles .
2014-11-25 17:09:19 +00:00
*/
public function test_remove_roles () {
2016-03-23 12:14:13 +00:00
WC_Install :: remove_roles ();
2014-11-25 17:09:19 +00:00
$this -> assertNull ( get_role ( 'customer' ) );
$this -> assertNull ( get_role ( 'shop_manager' ) );
}
2018-02-06 13:19:40 +00:00
/**
* Make sure the list of tables returned by WC_Install :: get_tables () and used when uninstalling the plugin
* or deleting a site in a multi site install is not missing any of the WC tables . If a table is added to
* WC_Install : get_schema () but not to WC_Install :: get_tables (), this test will fail .
2018-04-02 12:50:11 +00:00
*
* @ group core - only
2018-02-06 13:19:40 +00:00
*/
public function test_get_tables () {
global $wpdb ;
$tables = $wpdb -> get_col (
2020-03-12 15:00:03 +00:00
" SHOW TABLES WHERE `Tables_in_ { $wpdb -> dbname } ` LIKE ' { $wpdb -> prefix } woocommerce \ _%'
OR `Tables_in_{$wpdb->dbname}` LIKE '{$wpdb->prefix}wc\_%' "
2018-02-06 13:19:40 +00:00
);
2018-05-11 22:46:39 +00:00
$result = WC_Install :: get_tables ();
2020-03-12 15:00:03 +00:00
$diff = array_diff ( $result , $tables );
$this -> assertEmpty (
$diff ,
sprintf (
'The following table(s) were returned from WC_Install::get_tables() but do not exist: %s' ,
implode ( ', ' , $diff )
)
);
2018-02-06 13:19:40 +00:00
}
2018-03-16 18:54:17 +00:00
/**
* Test - get tables should apply the woocommerce_install_get_tables filter .
*/
public function test_get_tables_enables_filter () {
2020-03-12 15:54:25 +00:00
$this -> assertNotContains ( 'some_table_name' , WC_Install :: get_tables () );
2018-03-16 18:54:17 +00:00
2020-03-12 15:57:32 +00:00
add_filter (
'woocommerce_install_get_tables' ,
function ( $tables ) {
$tables [] = 'some_table_name' ;
2018-03-16 18:54:17 +00:00
2020-03-12 15:57:32 +00:00
return $tables ;
}
);
2018-03-16 18:54:17 +00:00
2020-03-12 15:54:25 +00:00
$this -> assertContains ( 'some_table_name' , WC_Install :: get_tables () );
2018-03-16 18:54:17 +00:00
}
2014-11-25 17:09:19 +00:00
}