From 6c9cfaa7935290ff17c39db38cba7a716cb03744 Mon Sep 17 00:00:00 2001 From: Michael Pretty Date: Tue, 21 Mar 2023 10:46:49 -0400 Subject: [PATCH] Add unit tests for update-functions. --- .../php/includes/wc-update-functions-test.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 plugins/woocommerce/tests/php/includes/wc-update-functions-test.php diff --git a/plugins/woocommerce/tests/php/includes/wc-update-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-update-functions-test.php new file mode 100644 index 00000000000..923f59478bc --- /dev/null +++ b/plugins/woocommerce/tests/php/includes/wc-update-functions-test.php @@ -0,0 +1,84 @@ +query( + "ALTER TABLE `{$wpdb->prefix}wc_download_log` + ADD CONSTRAINT `wc_download_log_ib` + FOREIGN KEY (`permission_id`) + REFERENCES `{$wpdb->prefix}woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE, + ADD CONSTRAINT `wc_download_log_ib_2` + FOREIGN KEY (`permission_id`) + REFERENCES `{$wpdb->prefix}woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE" + ); + $table_definition = $wpdb->get_var( "SHOW CREATE TABLE {$wpdb->prefix}wc_download_log", 1 ); + $this->assertNotFalse( strpos( $table_definition, "wc_download_log_ib" ) ); + $this->assertNotFalse( strpos( $table_definition, "wc_download_log_ib_2" ) ); + + include_once WC_ABSPATH . 'includes/wc-update-functions.php'; + + wc_update_343_cleanup_foreign_keys(); + + // Verify that the keys were properly removed + $table_definition = $wpdb->get_var( "SHOW CREATE TABLE {$wpdb->prefix}wc_download_log", 1 ); + $this->assertFalse( strpos( $table_definition, "wc_download_log_ib" ) ); + } + + public function test_verify_wc_update_352_drop_download_log_fk_removes_foreign_keys() { + global $wpdb; + + // Add the foreign key between wc_download_log and wc_download_log_permission_id as it previously existed. + $wpdb->query( + "ALTER TABLE `{$wpdb->prefix}wc_download_log` + ADD CONSTRAINT `fk_wc_download_log_permission_id` + FOREIGN KEY (`permission_id`) + REFERENCES `{$wpdb->prefix}woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE" + ); + $table_definition = $wpdb->get_var( "SHOW CREATE TABLE {$wpdb->prefix}wc_download_log", 1 ); + $this->assertNotFalse( strpos( $table_definition, "fk_wc_download_log_permission_id" ) ); + + include_once WC_ABSPATH . 'includes/wc-update-functions.php'; + + wc_update_352_drop_download_log_fk(); + + // Verify that the key was properly removed + $table_definition = $wpdb->get_var( "SHOW CREATE TABLE {$wpdb->prefix}wc_download_log", 1 ); + $this->assertFalse( strpos( $table_definition, "fk_wc_download_log_permission_id" ) ); + } + + public function test_verify_wc_update_700_remove_download_log_fk_removes_foreign_keys() { + global $wpdb; + + // Add the foreign key between wc_download_log and wc_download_log_permission_id as it previously existed. + $wpdb->query( + "ALTER TABLE `{$wpdb->prefix}wc_download_log` + ADD CONSTRAINT `fk_{$wpdb->prefix}wc_download_log_permission_id` + FOREIGN KEY (`permission_id`) + REFERENCES `{$wpdb->prefix}woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE" + ); + $table_definition = $wpdb->get_var( "SHOW CREATE TABLE {$wpdb->prefix}wc_download_log", 1 ); + $this->assertNotFalse( strpos( $table_definition, "fk_{$wpdb->prefix}wc_download_log_permission_id" ) ); + + include_once WC_ABSPATH . 'includes/wc-update-functions.php'; + + wc_update_700_remove_download_log_fk(); + + // Verify that the key was properly removed. + $table_definition = $wpdb->get_var( "SHOW CREATE TABLE {$wpdb->prefix}wc_download_log", 1 ); + $this->assertFalse( strpos( $table_definition, "fk_{$wpdb->prefix}wc_download_log_permission_id" ) ); + } + + +}