Skip to content

Commit

Permalink
Install/uninstall tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Sep 4, 2024
1 parent 3897dd0 commit 07866d9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function get_order() {
return $this->order;
}

public function get_id() {
return $this->get_order()->get_id();
}

/**
* @return WC_DateTime|null
*/
Expand Down Expand Up @@ -154,7 +158,7 @@ public function get_shipping_status() {
}

public function supports_third_party_email_transmission() {
$supports_email_transmission = function_exists( 'wc_gzd_order_supports_parcel_delivery_reminder' ) ? wc_gzd_order_supports_parcel_delivery_reminder( $this->get_order() ) : 'yes' === $this->get_order()->get_meta( '_parcel_delivery_opted_in' );
$supports_email_transmission = 'yes' === $this->get_order()->get_meta( '_parcel_delivery_opted_in' );

/**
* Filter to adjust whether the email address may be transmitted to third-parties, e.g.
Expand Down
41 changes: 41 additions & 0 deletions tests/unit-tests/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Vendidero\Germanized\Shipments\Tests\Helpers\ShipmentHelper;
use Vendidero\Germanized\Shipments\Tests\Helpers\PackagingHelper;

/**
* Class WC_Tests_Install.
* @package WooCommerce\Tests\Util
*/
class Install extends \Vendidero\Germanized\Shipments\Tests\Framework\UnitTestCase {

public function update() {
update_option( 'woocommerce_gzd_shipments_version', ( (float) \Vendidero\Germanized\Shipments\Package::get_version() - 1 ) );
update_option( 'woocommerce_gzd_shipments_db_version', \Vendidero\Germanized\Shipments\Package::get_version() );
\Vendidero\Germanized\Shipments\Package::check_version();

$this->assertTrue( did_action( 'woocommerce_gzd_shipments_updated' ) === 1 );
}

public function test_install() {
// clean existing install first
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
define( 'WP_UNINSTALL_PLUGIN', true );
define( 'WC_GZD_SHIPMENTS_REMOVE_ALL_DATA', true );
}

include( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/uninstall.php' );

\Vendidero\Germanized\Shipments\Install::install();

$this->assertTrue( get_option( 'woocommerce_gzd_shipments_version' ) === \Vendidero\Germanized\Shipments\Package::get_version() );
$this->assertEquals( 'yes', get_option( 'woocommerce_gzd_shipments_enable_auto_packing' ) );

// Check if Tables are installed
global $wpdb;

// Shipments
$table_name = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_gzd_shipments'" );
$this->assertEquals( "{$wpdb->prefix}woocommerce_gzd_shipments", $table_name );
}
}
49 changes: 49 additions & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}

global $wpdb, $wp_version;

if ( defined( 'WC_GZD_SHIPMENTS_REMOVE_ALL_DATA' ) && true === WC_GZD_SHIPMENTS_REMOVE_ALL_DATA ) {
// Delete options.
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'woocommerce_gzd_shipments\_%';" );

$meta_keys = array(
'_shipping_length',
'_shipping_width',
'_shipping_height',
'_hs_code',
'_manufacture_country',
'_customs_description',
'_is_non_returnable',
'_date_shipped',
'_return_request_key',
'_pickup_location_customer_number',
'_pickup_location_code'
);

// Delete gzd meta data
$wpdb->query( "DELETE meta FROM {$wpdb->postmeta} meta WHERE meta.meta_key IN ('" . join( "','", $meta_keys ) . "');" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

// Remove Tables
$custom_tables = array(
"{$wpdb->prefix}woocommerce_gzd_shipping_providermeta",
"{$wpdb->prefix}woocommerce_gzd_shipping_provider",
"{$wpdb->prefix}woocommerce_gzd_packagingmeta",
"{$wpdb->prefix}woocommerce_gzd_packaging",
"{$wpdb->prefix}woocommerce_gzd_shipment_labelmeta",
"{$wpdb->prefix}woocommerce_gzd_shipment_labels",
"{$wpdb->prefix}woocommerce_gzd_shipmentmeta",
"{$wpdb->prefix}woocommerce_gzd_shipments",
"{$wpdb->prefix}woocommerce_gzd_shipment_itemmeta",
"{$wpdb->prefix}woocommerce_gzd_shipment_items",
);

foreach ( $custom_tables as $table ) {
$result = $wpdb->query( 'DROP TABLE IF EXISTS ' . $table ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}

// Clear any cached data that has been removed
wp_cache_flush();
}

0 comments on commit 07866d9

Please sign in to comment.