From 2514b6b26964feecb080742d8cde683f9b9945e1 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Thu, 9 Jul 2020 13:55:39 -0400 Subject: [PATCH] edd-connector test added. --- composer.json | 3 +- composer.lock | 85 ++++-- connectors/class-connector-edd.php | 43 ++- phpunit.xml | 2 +- .../connectors/test-class-connector-edd.php | 287 ++++++++++++++++++ 5 files changed, 367 insertions(+), 53 deletions(-) create mode 100644 tests/tests/connectors/test-class-connector-edd.php diff --git a/composer.json b/composer.json index 2288f29b..ee50c235 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "wp-coding-standards/wpcs": "^2.2", "wp-phpunit/wp-phpunit": "^5.4", "wpsh/local": "^0.2.3", - "wpackagist-plugin/advanced-custom-fields": "5.8.12" + "wpackagist-plugin/advanced-custom-fields": "5.8.12", + "wpackagist-plugin/easy-digital-downloads": "^2.9.23" }, "config": { "process-timeout": 600, diff --git a/composer.lock b/composer.lock index 23dea658..d45b1aa6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "028b548fadf428b765cbcfc049cc00b2", + "content-hash": "1d58819e7fbef294b0721f50a6564b19", "packages": [ { "name": "composer/installers", @@ -922,20 +922,20 @@ }, { "name": "johnpbloch/wordpress", - "version": "5.4.2", + "version": "5.5.0", "source": { "type": "git", "url": "https://github.com/johnpbloch/wordpress.git", - "reference": "18c41a328193d6f7425a3cea4e01faa220e90218" + "reference": "d8a952dd4aa7f7d4d8be6fa145840fde4e248450" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/johnpbloch/wordpress/zipball/18c41a328193d6f7425a3cea4e01faa220e90218", - "reference": "18c41a328193d6f7425a3cea4e01faa220e90218", + "url": "https://api.github.com/repos/johnpbloch/wordpress/zipball/d8a952dd4aa7f7d4d8be6fa145840fde4e248450", + "reference": "d8a952dd4aa7f7d4d8be6fa145840fde4e248450", "shasum": "" }, "require": { - "johnpbloch/wordpress-core": "5.4.2", + "johnpbloch/wordpress-core": "5.5.0", "johnpbloch/wordpress-core-installer": "^1.0 || ^2.0", "php": ">=5.6.20" }, @@ -957,20 +957,20 @@ "cms", "wordpress" ], - "time": "2020-06-10T22:05:43+00:00" + "time": "2020-08-11T18:47:19+00:00" }, { "name": "johnpbloch/wordpress-core", - "version": "5.4.2", + "version": "5.5.0", "source": { "type": "git", "url": "https://github.com/johnpbloch/wordpress-core.git", - "reference": "3e67d8b6ee6c11e8ce404da4627117fb7fbf9266" + "reference": "c49d46de4e4c8972b8ed2aebe3e850f4f218186f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/johnpbloch/wordpress-core/zipball/3e67d8b6ee6c11e8ce404da4627117fb7fbf9266", - "reference": "3e67d8b6ee6c11e8ce404da4627117fb7fbf9266", + "url": "https://api.github.com/repos/johnpbloch/wordpress-core/zipball/c49d46de4e4c8972b8ed2aebe3e850f4f218186f", + "reference": "c49d46de4e4c8972b8ed2aebe3e850f4f218186f", "shasum": "" }, "require": { @@ -978,7 +978,7 @@ "php": ">=5.6.20" }, "provide": { - "wordpress/core-implementation": "5.4.2" + "wordpress/core-implementation": "5.5.0" }, "type": "wordpress-core", "notification-url": "https://packagist.org/downloads/", @@ -998,7 +998,7 @@ "cms", "wordpress" ], - "time": "2020-06-10T22:05:38+00:00" + "time": "2020-08-11T18:47:13+00:00" }, { "name": "johnpbloch/wordpress-core-installer", @@ -1881,6 +1881,7 @@ "keywords": [ "tokenizer" ], + "abandoned": true, "time": "2017-12-04T08:55:13+00:00" }, { @@ -2726,16 +2727,16 @@ }, { "name": "seld/jsonlint", - "version": "1.8.0", + "version": "1.8.1", "source": { "type": "git", "url": "https://github.com/Seldaek/jsonlint.git", - "reference": "ff2aa5420bfbc296cf6a0bc785fa5b35736de7c1" + "reference": "3d5eb71705adfa34bd34b993400622932b2f62fd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/ff2aa5420bfbc296cf6a0bc785fa5b35736de7c1", - "reference": "ff2aa5420bfbc296cf6a0bc785fa5b35736de7c1", + "url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/3d5eb71705adfa34bd34b993400622932b2f62fd", + "reference": "3d5eb71705adfa34bd34b993400622932b2f62fd", "shasum": "" }, "require": { @@ -2771,7 +2772,17 @@ "parser", "validator" ], - "time": "2020-04-30T19:05:18+00:00" + "funding": [ + { + "url": "https://github.com/Seldaek", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/seld/jsonlint", + "type": "tidelift" + } + ], + "time": "2020-08-13T09:07:59+00:00" }, { "name": "seld/phar-utils", @@ -2819,16 +2830,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.5.5", + "version": "3.5.6", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6" + "reference": "e97627871a7eab2f70e59166072a6b767d5834e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/73e2e7f57d958e7228fce50dc0c61f58f017f9f6", - "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/e97627871a7eab2f70e59166072a6b767d5834e0", + "reference": "e97627871a7eab2f70e59166072a6b767d5834e0", "shasum": "" }, "require": { @@ -2866,7 +2877,7 @@ "phpcs", "standards" ], - "time": "2020-04-17T01:09:41+00:00" + "time": "2020-08-10T04:50:15+00:00" }, { "name": "symfony/config", @@ -5960,16 +5971,16 @@ }, { "name": "wp-phpunit/wp-phpunit", - "version": "5.4.2", + "version": "5.5.0", "source": { "type": "git", "url": "https://github.com/wp-phpunit/wp-phpunit.git", - "reference": "15925e03c8594c08cf2822e24e511af8dc6f779e" + "reference": "5cf5a40954b8c47941afdc08b0000ce42efe5993" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/wp-phpunit/wp-phpunit/zipball/15925e03c8594c08cf2822e24e511af8dc6f779e", - "reference": "15925e03c8594c08cf2822e24e511af8dc6f779e", + "url": "https://api.github.com/repos/wp-phpunit/wp-phpunit/zipball/5cf5a40954b8c47941afdc08b0000ce42efe5993", + "reference": "5cf5a40954b8c47941afdc08b0000ce42efe5993", "shasum": "" }, "type": "library", @@ -5999,7 +6010,7 @@ "test", "wordpress" ], - "time": "2020-04-01T14:35:27+00:00" + "time": "2020-08-12T15:43:21+00:00" }, { "name": "wpackagist-plugin/advanced-custom-fields", @@ -6019,6 +6030,24 @@ "type": "wordpress-plugin", "homepage": "https://wordpress.org/plugins/advanced-custom-fields/" }, + { + "name": "wpackagist-plugin/easy-digital-downloads", + "version": "2.9.23", + "source": { + "type": "svn", + "url": "https://plugins.svn.wordpress.org/easy-digital-downloads/", + "reference": "tags/2.9.23" + }, + "dist": { + "type": "zip", + "url": "https://downloads.wordpress.org/plugin/easy-digital-downloads.2.9.23.zip" + }, + "require": { + "composer/installers": "~1.0" + }, + "type": "wordpress-plugin", + "homepage": "https://wordpress.org/plugins/easy-digital-downloads/" + }, { "name": "wpsh/local", "version": "0.2.3", diff --git a/connectors/class-connector-edd.php b/connectors/class-connector-edd.php index 7c2e5148..6074e9fb 100644 --- a/connectors/class-connector-edd.php +++ b/connectors/class-connector-edd.php @@ -330,14 +330,8 @@ public function check( $option, $old_value, $new_value ) { $replacement = str_replace( '-', '_', $option ); if ( method_exists( $this, 'check_' . $replacement ) ) { - call_user_func( - array( - $this, - 'check_' . $replacement, - ), - $old_value, - $new_value - ); + $method = "check_{$replacement}"; + $this->{$method}( $old_value, $new_value ); } else { $data = $this->options[ $option ]; $option_title = $data['label']; @@ -376,6 +370,7 @@ public function check_edd_settings( $old_value, $new_value ) { foreach ( $options as $option => $option_value ) { $field = null; + $tab = null; if ( 'banned_email' === $option ) { $field = array( @@ -383,10 +378,13 @@ public function check_edd_settings( $old_value, $new_value ) { ); $tab = 'general'; } else { - foreach ( $settings as $tab => $fields ) { - if ( isset( $fields[ $option ] ) ) { - $field = $fields[ $option ]; - break; + foreach ( $settings as $current_tab => $tab_sections ) { + foreach ( $tab_sections as $section => $section_fields ) { + if ( in_array( $option, array_keys( $section_fields ), true ) ) { + $field = $section_fields[ $option ]; + $tab = $current_tab; + break; + } } } } @@ -401,8 +399,8 @@ public function check_edd_settings( $old_value, $new_value ) { array( 'option_title' => $field['name'], 'option' => $option, - 'old_value' => $old_value, - 'value' => $new_value, + 'old_value' => isset( $old_value[ $option ] ) ? $old_value[ $option ] : null, + 'value' => isset( $new_value[ $option ] ) ? $new_value[ $option ] : null, 'tab' => $tab, ), null, @@ -652,6 +650,12 @@ public function callback_delete_user_meta( $meta_id, $object_id, $meta_key, $_me * @param bool $is_add Is this a new meta?. */ public function meta( $object_id, $key, $value, $is_add = false ) { + // For catching "edd_user_public_key" in newer versions of EDD. + if ( in_array( $value, $this->user_meta, true ) ) { + $key = $value; + $value = 1; // Probably, should avoid storing the api key. + } + if ( ! in_array( $key, $this->user_meta, true ) ) { return false; } @@ -662,15 +666,8 @@ public function meta( $object_id, $key, $value, $is_add = false ) { return false; } - return call_user_func( - array( - $this, - 'meta_' . $key, - ), - $object_id, - $value, - $is_add - ); + $method = "meta_{$key}"; + return $this->{$method}( $object_id, $value, $is_add ); } /** diff --git a/phpunit.xml b/phpunit.xml index 47e6e721..a982b0cc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,7 +10,7 @@ diff --git a/tests/tests/connectors/test-class-connector-edd.php b/tests/tests/connectors/test-class-connector-edd.php new file mode 100644 index 00000000..bb457995 --- /dev/null +++ b/tests/tests/connectors/test-class-connector-edd.php @@ -0,0 +1,287 @@ +plugin->connectors->unload_connectors(); + + $post_connector = new Connector_Posts(); + $post_connector->register(); + + // Make partial of Connector_EDD class, with mocked "log" function. + $this->mock = $this->getMockBuilder( Connector_EDD::class ) + ->setMethods( [ 'log' ] ) + ->getMock(); + + $this->mock->register(); + } + + /** + * Runs after each test + */ + public function tearDown() { + parent::tearDown(); + } + + /** + * Create a download + * + * @return int + */ + private function create_simple_download() { + $post_id = wp_insert_post( array( + 'post_title' => 'Test Download Product', + 'post_name' => 'test-download-product', + 'post_type' => 'download', + 'post_status' => 'publish' + ) ); + + $_download_files = array( + array( + 'name' => 'Simple File 1', + 'file' => 'http://localhost/simple-file1.jpg', + 'condition' => 0 + ), + ); + + $meta = array( + 'edd_price' => '20.00', + '_variable_pricing' => 0, + 'edd_variable_prices' => false, + 'edd_download_files' => array_values( $_download_files ), + '_edd_download_limit' => 20, + '_edd_hide_purchase_link' => 1, + 'edd_product_notes' => 'Purchase Notes', + '_edd_product_type' => 'default', + '_edd_download_earnings' => 40, + '_edd_download_sales' => 2, + '_edd_download_limit_override_1' => 1, + 'edd_sku' => 'sku_0012' + ); + + foreach( $meta as $key => $value ) { + update_post_meta( $post_id, $key, $value ); + } + + return get_post( $post_id ); + } + + /** + * Create a percentage discount + * + * @return int + */ + private function create_simple_percent_discount() { + $post = array( + 'code' => '20OFF', + 'uses' => 54, + 'max' => 10, + 'name' => '20 Percent Off', + 'type' => 'percent', + 'amount' => '20', + 'start' => '12/12/2010 00:00:00', + 'expiration' => '12/31/2050 23:59:59', + 'min_price' => 128, + 'status' => 'active', + 'product_condition' => 'all' + ); + $discount_id = edd_store_discount( $post ); + + return $discount_id; + } + + public function test_edd_installed_and_activated() { + $this->assertTrue( class_exists( 'Easy_Digital_Downloads' ) ); + } + + public function test_check() { + // Expected log calls. + $this->mock->expects( $this->exactly( 3 ) ) + ->method( 'log' ) + ->withConsecutive( + [ + $this->equalTo( __( '"%s" setting updated', 'stream' ) ), + $this->equalTo( + array( + 'option_title' => 'Thousands Separator', + 'option' => 'thousands_separator', + 'old_value' => null, + 'value' => '.', + 'tab' => 'general', + ) + ), + $this->equalTo( null ), + $this->equalTo( 'settings' ), + $this->equalTo( 'updated' ) + ], + [ + $this->equalTo( __( '"%s" setting updated', 'stream' ) ), + $this->equalTo( + array( + 'option_title' => 'Thousands Separator', + 'option' => 'thousands_separator', + 'old_value' => '.', + 'value' => ',', + 'tab' => 'general', + ) + ), + $this->equalTo( null ), + $this->equalTo( 'settings' ), + $this->equalTo( 'updated' ) + ], + [ + $this->equalTo( __( '"%s" setting updated', 'stream' ) ), + $this->equalTo( + array( + 'option_title' => 'Thousands Separator', + 'option' => 'thousands_separator', + 'old_value' => ',', + 'value' => null, + 'tab' => 'general', + ) + ), + $this->equalTo( null ), + $this->equalTo( 'settings' ), + $this->equalTo( 'updated' ) + ] + ); + + // Update option to trigger callback. + edd_update_option( 'thousands_separator', '.' ); + edd_update_option( 'thousands_separator', ',' ); + edd_update_option( 'thousands_separator' ); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_add_option' ) ); + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_update_option' ) ); + } + + public function test_log_override() { + // Callback for validating expected log data. + $asserted = 0; + add_action( + 'wp_stream_log_data', + function( $data ) use( &$asserted ) { + if ( 'edd' === $data['connector'] && in_array( $data['context'], array( 'downloads', 'discounts' ), true ) ) { + $asserted++; + } + + return $data; + }, + 99 + ); + + // Create download and discount to trigger logs. + $this->create_simple_download(); + $this->create_simple_percent_discount(); + + // Check assertion flage + $this->assertSame( $asserted, 2 ); + } + + public function test_callback_edd_pre_update_discount_status() { + // Create discount for later use. + $post_id = $this->create_simple_percent_discount(); + $discount = new \EDD_Discount( $post_id ); + + // Expected log calls. + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + sprintf( + __( '"%1$s" discount %2$s', 'stream' ), + $discount->name, + esc_html__( 'deactivated', 'stream' ) + ) + ), + $this->equalTo( + [ + 'post_id' => $post_id, + 'status' => 'inactive', + ] + ), + $this->equalTo( $post_id ), + $this->equalTo( 'discounts' ), + $this->equalTo( 'updated' ) + ); + + // Update discount status to trigger callback. + $discount->update_status( 'inactive' ); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_edd_pre_update_discount_status' ) ); + } + + public function test_settings_transport_callbacks() { + // Expected log calls. + $this->mock->expects( $this->exactly( 2 ) ) + ->method( 'log' ) + ->withConsecutive( + [ + $this->equalTo( __( 'Imported Settings', 'stream' ) ), + $this->equalTo( array() ), + $this->equalTo( null ), + $this->equalTo( 'settings' ), + $this->equalTo( 'imported' ), + ], + [ + $this->equalTo( __( 'Exported Settings', 'stream' ) ), + $this->equalTo( array() ), + $this->equalTo( null ), + $this->equalTo( 'settings' ), + $this->equalTo( 'exported' ), + ] + ); + + // Manually trigger callbacks. + do_action( 'edd_import_settings' ); + do_action( 'edd_export_settings' ); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_edd_import_settings' ) ); + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_edd_export_settings' ) ); + } + + public function test_meta() { + // Create and authenticate user. + $user_id = self::factory()->user->create( [ 'role' => 'administrator' ] ); + \wp_set_current_user( $user_id ); + + // Expected log calls. + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( __( 'User API Key created', 'stream' ) ), + $this->equalTo( [ 'meta_value' => 1 ] ), + $this->equalTo( $user_id ), + $this->equalTo( 'api_keys' ), + 'created' + ); + + // Update API key and trigger callback.. + $_POST['edd_set_api_key'] = 1; + \edd_update_user_api_key( $user_id ); + + // Check callback test action. + $this->assertGreaterThan( 0, did_action( 'wp_stream_test_callback_add_user_meta' ) ); + } +}