From 1f0fd6b1b147d89b90773d3bfad1298d6762b27b Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Thu, 30 Nov 2023 19:44:09 +0100 Subject: [PATCH] add tests for pos_only products --- composer.json | 3 +- includes/Templates/Receipt.php | 47 ------------------------ includes/Templates/Received.php | 14 ++++---- tests/includes/Test_Products.php | 58 ++++++++++++++++++++++++++++-- woocommerce-pos.php | 61 +++++++++++++++++++++++--------- 5 files changed, 109 insertions(+), 74 deletions(-) diff --git a/composer.json b/composer.json index 3dc2f4a..e8b5d08 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,7 @@ "php": ">=7.4", "ext-json": "*", "ramsey/uuid": "^4.2", - "salesforce/handlebars-php": "3.0.1", - "vlucas/phpdotenv": "v5.5.0" + "salesforce/handlebars-php": "3.0.1" }, "config": { "platform": { diff --git a/includes/Templates/Receipt.php b/includes/Templates/Receipt.php index 0e2ca72..2ff8561 100644 --- a/includes/Templates/Receipt.php +++ b/includes/Templates/Receipt.php @@ -8,10 +8,6 @@ namespace WCPOS\WooCommercePOS\Templates; use Exception; -use Handlebars\Handlebars; -use Handlebars\Helpers; -use Handlebars\Loader\StringLoader; -use WCPOS\WooCommercePOS\Server; class Receipt { /** @@ -60,16 +56,6 @@ public function get_template(): void { wp_die( esc_html__( 'Sorry, this order is invalid.', 'woocommerce-pos' ) ); } - // if ( ! $order->is_paid() ) { - // wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) ); - // } - - if ( isset( $_GET['template'] ) ) { - if ( 'legacy' === $_GET['template'] ) { - $this->legacy_receipt_template(); - } - } - /** * Put WC_Order into the global scope so that the template can access it. */ @@ -100,37 +86,4 @@ private function get_template_path( string $file_name ) { */ return apply_filters( 'woocommerce_pos_print_receipt_path', woocommerce_pos_locate_template( $file_name ) ); } - - - private function legacy_receipt_template(): void { - $server = new Server(); - $order_json = $server->wp_rest_request( '/wc/v3/orders/' . $this->order_id ); - $path = $this->get_template_path( 'legacy-receipt.php' ); - - ob_start(); - include $path; - $template = ob_get_clean(); - - $engine = new Handlebars(array( - 'loader' => new StringLoader(), - 'helpers' => new Helpers(), - // 'enableDataVariables' => true, - )); - $engine->addHelper('formatAddress', function ( $template, $context, $args, $source ) { - return 'formatAddress'; - }); - $engine->addHelper('formatDate', function ( $template, $context, $args, $source ) { - return 'formatDate'; - }); - $engine->addHelper('number', function ( $template, $context, $args, $source ) { - return 'number'; - }); - $engine->addHelper('money', function ( $template, $context, $args, $source ) { - return 'money'; - }); - $receipt = $engine->render( $template, $order_json ); - - echo $receipt; - exit; - } } diff --git a/includes/Templates/Received.php b/includes/Templates/Received.php index 00df2a0..9ad2de7 100644 --- a/includes/Templates/Received.php +++ b/includes/Templates/Received.php @@ -3,6 +3,7 @@ * @author Paul Kilmurray * * @see http://wcpos.com + * @package WooCommercePOS\Templates */ namespace WCPOS\WooCommercePOS\Templates; @@ -19,26 +20,27 @@ class Received { public function __construct( int $order_id ) { $this->order_id = $order_id; - add_filter('show_admin_bar', '__return_false'); + add_filter( 'show_admin_bar', '__return_false' ); } public function get_template(): void { try { // get order - $order = wc_get_order( $this->order_id ); + $order = \wc_get_order( $this->order_id ); // Order or receipt url is invalid. if ( ! $order ) { wp_die( esc_html__( 'Sorry, this order is invalid.', 'woocommerce-pos' ) ); } -// if ( ! $order->is_paid() ) { -// wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) ); -// } + // if ( ! $order->is_paid() ) { + // wp_die( esc_html__( 'Sorry, this order has not been paid.', 'woocommerce-pos' ) ); + // } /** * @TODO - this is a hack and needs to be fixed + * @NOTE - the received template will be removed once we move to session based checkout * * - hardcoding the rest endpoint is a receipe for disaster */ @@ -53,7 +55,7 @@ public function get_template(): void { include woocommerce_pos_locate_template( 'received.php' ); exit; } catch ( Exception $e ) { - wc_print_notice( $e->getMessage(), 'error' ); + \wc_print_notice( $e->getMessage(), 'error' ); } } } diff --git a/tests/includes/Test_Products.php b/tests/includes/Test_Products.php index dcf1a66..71f29c6 100644 --- a/tests/includes/Test_Products.php +++ b/tests/includes/Test_Products.php @@ -7,6 +7,7 @@ use WP_Query; use WC_Query; use WCPOS\WooCommercePOS\Products; +use WC_Product_Variation; /** * @internal @@ -23,7 +24,7 @@ public function tearDown(): void { } /** - * @TODO - I have no idea why this test isn't working + * */ public function test_pos_only_products() { add_filter( @@ -56,7 +57,7 @@ function () { $query = new WP_Query( $query_args ); WC()->query->product_query( $query ); - $queried_ids = wp_list_pluck( $query->posts, 'ID' ); + $queried_ids = wp_list_pluck( $query->get_posts(), 'ID' ); // Assert that the visible product is in the query $this->assertContains( $visible_product->get_id(), $queried_ids ); @@ -64,4 +65,57 @@ function () { // Assert that the hidden product is not in the query $this->assertNotContains( $hidden_product->get_id(), $queried_ids ); } + + /** + * + */ + public function test_pos_only_variations() { + add_filter( + 'woocommerce_pos_general_settings', + function () { + return array( + 'pos_only_products' => true, + ); + } + ); + new Products(); // reinstantiate the class to apply the filter + + // create variations + $product = ProductHelper::create_variation_product(); + $variation_3 = new WC_Product_Variation(); + $variation_3->set_props( + array( + 'parent_id' => $product->get_id(), + 'sku' => 'DUMMY SKU VARIABLE MEDIUM', + 'regular_price' => 10, + ) + ); + $variation_3->set_attributes( array( 'pa_size' => 'medium' ) ); + $variation_3->save(); + + $variation_ids = $product->get_children(); + update_post_meta( $variation_ids[0], '_pos_visibility', 'pos_only' ); + update_post_meta( $variation_ids[1], '_pos_visibility', 'online_only' ); + + // Mimic the main WooCommerce query for product variations + $query_args = array( + 'post_type' => 'product_variation', + 'post_status' => 'publish', + 'posts_per_page' => -1, // Get all variations for testing + 'post_parent' => $product->get_id(), // Ensure variations of the specific product are fetched + ); + + $query = new WP_Query( $query_args ); + WC()->query->product_query( $query ); + $queried_variation_ids = wp_list_pluck( $query->get_posts(), 'ID' ); + + // Assert that the variation with '_pos_visibility' set to 'pos_only' is NOT in the query + $this->assertNotContains( $variation_ids[0], $queried_variation_ids ); + + // Assert that the variation with '_pos_visibility' set to 'online_only' IS in the query + $this->assertContains( $variation_ids[1], $queried_variation_ids ); + + // Assert that the variation without '_pos_visibility' set is in the query + $this->assertContains( $variation_ids[2], $queried_variation_ids ); + } } diff --git a/woocommerce-pos.php b/woocommerce-pos.php index 78d0364..4cef624 100644 --- a/woocommerce-pos.php +++ b/woocommerce-pos.php @@ -16,13 +16,11 @@ * @author Paul Kilmurray * * @see http://wcpos.com + * @package WooCommercePOS */ namespace WCPOS\WooCommercePOS; -use function define; -use Dotenv\Dotenv; - // Define plugin constants. const VERSION = '1.4.0-beta.3'; const PLUGIN_NAME = 'woocommerce-pos'; @@ -31,36 +29,65 @@ \define( __NAMESPACE__ . '\PLUGIN_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) ); \define( __NAMESPACE__ . '\PLUGIN_URL', trailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) ); -// minimum requirements +// minimum requirements. const WC_MIN_VERSION = '5.3'; const PHP_MIN_VERSION = '7.4'; const MIN_PRO_VERSION = '1.2.0'; -// Autoloader +// load .env flags (for development). +function load_env( $file ) { + if ( ! file_exists( $file ) ) { + return; + } + + $lines = file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); + foreach ( $lines as $line ) { + if ( strpos( trim( $line ), '#' ) === 0 ) { + continue; + } + + list($name, $value) = explode( '=', $line, 2 ); + $name = trim( $name ); + $value = trim( $value ); + + if ( ! array_key_exists( $name, $_SERVER ) && ! array_key_exists( $name, $_ENV ) ) { + putenv( sprintf( '%s=%s', $name, $value ) ); + $_ENV[ $name ] = $value; + } + } +} + +// Autoloader. if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { require_once __DIR__ . '/vendor/autoload.php'; // Environment variables. - Dotenv::createImmutable( __DIR__ )->safeLoad(); + load_env( __DIR__ . '/.env' ); - // Activate plugin + // Activate plugin. new Activator(); - // Deactivate plugin + // Deactivate plugin. new Deactivator(); } else { - add_action( 'admin_notices', function(): void { - ?> + add_action( + 'admin_notices', + function (): void { + ?>

-