From 7f52e3454a20d2dec09c05efe35ca1dfbad8a529 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 12 Dec 2025 21:12:30 +0800 Subject: [PATCH 1/6] Directly Remove Credentials on 401 --- composer.json | 2 +- ...grate-convertkit-wpforms-admin-notices.php | 223 ++++++++++++++++++ ...class-integrate-convertkit-wpforms-api.php | 7 + ...-integrate-convertkit-wpforms-resource.php | 30 ++- .../class-integrate-convertkit-wpforms.php | 68 +++++- includes/functions.php | 81 ++++++- integrate-convertkit-wpforms.php | 1 + ...settings-form-marketing-forms-dropdown.php | 9 + 8 files changed, 406 insertions(+), 15 deletions(-) create mode 100644 includes/class-integrate-convertkit-wpforms-admin-notices.php diff --git a/composer.json b/composer.json index c7080d8..8e6bb51 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "project", "license": "GPLv3", "require": { - "convertkit/convertkit-wordpress-libraries": "2.1.2" + "convertkit/convertkit-wordpress-libraries": "dev-add-invalid-access-token-to-hook" }, "require-dev": { "php-webdriver/webdriver": "^1.0", diff --git a/includes/class-integrate-convertkit-wpforms-admin-notices.php b/includes/class-integrate-convertkit-wpforms-admin-notices.php new file mode 100644 index 0000000..fcbe274 --- /dev/null +++ b/includes/class-integrate-convertkit-wpforms-admin-notices.php @@ -0,0 +1,223 @@ +key_prefix ); + if ( ! $notices ) { + return; + } + + // Output notices. + foreach ( $notices as $notice ) { + switch ( $notice ) { + case 'authorization_failed': + $output = sprintf( + '%s %s', + esc_html__( 'Kit for WPForms: Authorization failed. Please', 'integrate-convertkit-wpforms' ), + sprintf( + '%s', + esc_url( admin_url( 'admin.php?page=wpforms-settings&view=integrations' ) ), + esc_html__( 'reconnect your Kit account.', 'integrate-convertkit-wpforms' ) + ) + ); + break; + + default: + $output = ''; + + /** + * Define the text to output in an admin error notice. + * + * @since 1.8.9 + * + * @param string $notice Admin notice name. + */ + $output = apply_filters( 'integrate_convertkit_wpforms_admin_notices_output_' . $notice, $output ); + break; + } + + // If no output defined, skip. + if ( empty( $output ) ) { + continue; + } + ?> +
+

+ +

+
+ exist() ) { + return update_option( $this->key_prefix, array( $notice ) ); + } + + // Fetch existing persistent notices. + $notices = $this->get(); + + // Add notice to existing notices. + $notices[] = $notice; + + // Remove any duplicate notices. + $notices = array_values( array_unique( $notices ) ); + + // Update and return. + return update_option( $this->key_prefix, $notices ); + + } + + /** + * Returns all notices stored in the options table. + * + * @since 1.8.9 + * + * @return array + */ + public function get() { + + // Fetch all notices from the options table. + return get_option( $this->key_prefix ); + + } + + /** + * Whether any persistent notices are stored in the option table. + * + * @since 1.8.9 + * + * @return bool + */ + public function exist() { + + if ( ! $this->get() ) { + return false; + } + + return true; + + } + + /** + * Delete all persistent notices. + * + * @since 1.8.9 + * + * @param string $notice Notice name. + * @return bool Success + */ + public function delete( $notice ) { + + // If no persistent notices exist, there's nothing to delete. + if ( ! $this->exist() ) { + return false; + } + + // Fetch existing persistent notices. + $notices = $this->get(); + + // Remove notice from existing notices. + $index = array_search( $notice, $notices, true ); + if ( $index !== false ) { + unset( $notices[ $index ] ); + } + + // Update and return. + return update_option( $this->key_prefix, $notices ); + + } + + /** + * Returns the singleton instance of the class. + * + * @since 1.8.9 + * + * @return object Class. + */ + public static function get_instance() { + + if ( null === self::$instance ) { + self::$instance = new self(); + } + + return self::$instance; + + } + +} + +new Integrate_ConvertKit_WPForms_Admin_Notices(); diff --git a/includes/class-integrate-convertkit-wpforms-api.php b/includes/class-integrate-convertkit-wpforms-api.php index a766a0e..b21464f 100644 --- a/includes/class-integrate-convertkit-wpforms-api.php +++ b/includes/class-integrate-convertkit-wpforms-api.php @@ -29,6 +29,13 @@ class Integrate_ConvertKit_WPForms_API extends ConvertKit_API_V4 { */ public $error_messages = false; + /** + * Access Token + * + * @var string + */ + public $access_token = ''; + /** * Sets up the API with the required credentials. * diff --git a/includes/class-integrate-convertkit-wpforms-resource.php b/includes/class-integrate-convertkit-wpforms-resource.php index 551b730..f389b24 100644 --- a/includes/class-integrate-convertkit-wpforms-resource.php +++ b/includes/class-integrate-convertkit-wpforms-resource.php @@ -33,8 +33,34 @@ public function __construct( $api_instance, $account_id = '' ) { $this->settings_name .= '_' . $account_id; } - // Call parent initialization function. - parent::init(); + // Get last query time and existing resources. + $this->last_queried = get_option( $this->settings_name . '_last_queried' ); + $this->resources = get_option( $this->settings_name ); + + } + + /** + * Fetches resources (custom fields, forms, sequences or tags) from the API, storing them in the options table + * with a last queried timestamp. + * + * If the refresh results in a 401, removes the access and refresh tokens from the connection. + * + * @since 1.8.9 + * + * @return WP_Error|array + */ + public function refresh() { + + // Call parent refresh method. + $result = parent::refresh(); + + // If an error occured, maybe delete credentials from the Plugin's settings + // if the error is a 401 unauthorized. + if ( is_wp_error( $result ) ) { + integrate_convertkit_wpforms_maybe_delete_credentials( $result, INTEGRATE_CONVERTKIT_WPFORMS_OAUTH_CLIENT_ID, $this->api->access_token ); + } + + return $result; } diff --git a/includes/class-integrate-convertkit-wpforms.php b/includes/class-integrate-convertkit-wpforms.php index cc0e54b..b87e7e1 100644 --- a/includes/class-integrate-convertkit-wpforms.php +++ b/includes/class-integrate-convertkit-wpforms.php @@ -667,8 +667,8 @@ public function output_accounts( $connection_id = '', $connection = array() ) { } /** - * Outputs a dropdown of Kit Forms, Sequences and Tags, allowing the user + * to choose which resource to send form submissions to. * * @since 1.5.0 * @@ -691,28 +691,67 @@ public function output_lists( $connection_id = '', $connection = array() ) { return ''; } + // Get the selected ConvertKit subscribe setting, if one was already defined. + $value = ! empty( $connection['list_id'] ) ? $connection['list_id'] : ''; + + // Initialize resource classes. + $forms = new Integrate_ConvertKit_WPForms_Resource_Forms( $api, $connection['account_id'] ); + $sequences = new Integrate_ConvertKit_WPForms_Resource_Sequences( $api, $connection['account_id'] ); + $tags = new Integrate_ConvertKit_WPForms_Resource_Tags( $api, $connection['account_id'] ); + // Fetch Forms. // We use refresh() to ensure we get the latest data, as we're in the admin interface // and need to populate the select dropdown. - $forms = new Integrate_ConvertKit_WPForms_Resource_Forms( $api, $connection['account_id'] ); - $forms->refresh(); + $result = $forms->refresh(); + + // Return the dropdown if an error occurred, so the cached resources are + // available for selection. + if ( is_wp_error( $result ) ) { + return $this->output_select_dropdown( $forms, $sequences, $tags, $value, $connection_id, $result->get_error_message() ); + } // Fetch Tags. // We use refresh() to ensure we get the latest data, as we're in the admin interface // and need to populate the select dropdown. - $tags = new Integrate_ConvertKit_WPForms_Resource_Tags( $api, $connection['account_id'] ); - $tags->refresh(); + $result = $tags->refresh(); - // Get the selected ConvertKit subscribe setting, if one was already defined. - $value = ! empty( $connection['list_id'] ) ? $connection['list_id'] : ''; + // Return the dropdown. + return $this->output_select_dropdown( $forms, $sequences, $tags, $value, $connection_id ); + + } + + /** + * Outputs the From 1307cea753f8a112e16f91ac65c75d3ef2e61021 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 12 Dec 2025 23:22:12 +0800 Subject: [PATCH 2/6] Added tests --- tests/Integration/APITest.php | 50 +++ .../ResourceCustomFieldsNoDataTest.php | 196 +++++++++++ .../Integration/ResourceCustomFieldsTest.php | 276 ++++++++++++++++ tests/Integration/ResourceFormsNoDataTest.php | 196 +++++++++++ tests/Integration/ResourceFormsTest.php | 304 ++++++++++++++++++ .../ResourceSequencesNoDataTest.php | 196 +++++++++++ tests/Integration/ResourceSequencesTest.php | 270 ++++++++++++++++ tests/Integration/ResourceTagsNoDataTest.php | 196 +++++++++++ tests/Integration/ResourceTagsTest.php | 270 ++++++++++++++++ 9 files changed, 1954 insertions(+) create mode 100644 tests/Integration/ResourceCustomFieldsNoDataTest.php create mode 100644 tests/Integration/ResourceCustomFieldsTest.php create mode 100644 tests/Integration/ResourceFormsNoDataTest.php create mode 100644 tests/Integration/ResourceFormsTest.php create mode 100644 tests/Integration/ResourceSequencesNoDataTest.php create mode 100644 tests/Integration/ResourceSequencesTest.php create mode 100644 tests/Integration/ResourceTagsNoDataTest.php create mode 100644 tests/Integration/ResourceTagsTest.php diff --git a/tests/Integration/APITest.php b/tests/Integration/APITest.php index a2c3e7a..81ab917 100644 --- a/tests/Integration/APITest.php +++ b/tests/Integration/APITest.php @@ -110,6 +110,56 @@ public function testAccessTokenRefreshedAndSavedWhenExpired() $this->assertEquals($_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], $account['refresh_token']); } + /** + * Test that the Access Token, Refresh Token and Token Expiry are deleted from the Plugin's settings + * when the Access Token used is invalid. + * + * @since 1.8.9 + */ + public function testAccessTokenDeletedWhenInvalid() + { + // Save an invalid access token and refresh token in the Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => 'invalidAccessToken', + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + 'wpunittest1234' + ); + + // Confirm the tokens saved. + $providers = wpforms_get_providers_options(); + $account = reset( $providers['convertkit'] ); + $this->assertEquals( $account['access_token'], 'invalidAccessToken' ); + $this->assertEquals( $account['refresh_token'], $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] ); + + // Initialize the API using the invalid access token. + $api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['KIT_OAUTH_REDIRECT_URI'], + $account['access_token'], + $account['refresh_token'] + ); + + // Run request. + $result = $api->get_account(); + + // Confirm a WP_Error is returned. + $this->assertInstanceOf( 'WP_Error', $result ); + $this->assertEquals( $result->get_error_code(), 'convertkit_api_error' ); + $this->assertEquals( $result->get_error_message(), 'The access token is invalid' ); + + // Confirm tokens removed from the Plugin's settings, which confirms the `convertkit_api_access_token_invalid` hook was called when the tokens were deleted. + $providers = wpforms_get_providers_options(); + $account = reset( $providers['convertkit'] ); + $this->assertEmpty( $account['access_token'] ); + $this->assertEmpty( $account['refresh_token'] ); + } + /** * Test that a WordPress Cron event is created when an access token is obtained. * diff --git a/tests/Integration/ResourceCustomFieldsNoDataTest.php b/tests/Integration/ResourceCustomFieldsNoDataTest.php new file mode 100644 index 0000000..0b7be03 --- /dev/null +++ b/tests/Integration/ResourceCustomFieldsNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Custom_Fields( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns false, because resources do not exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceCustomFieldsTest.php b/tests/Integration/ResourceCustomFieldsTest.php new file mode 100644 index 0000000..30e7dad --- /dev/null +++ b/tests/Integration/ResourceCustomFieldsTest.php @@ -0,0 +1,276 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Custom_Fields( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + $this->assertArrayHasKey('key', reset($result)); + $this->assertArrayHasKey('label', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('Billing Address', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('URL', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'key'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + $this->assertArrayHasKey('key', reset($result)); + $this->assertArrayHasKey('label', reset($result)); + + // Assert order of data is in descending alphabetical order. + $this->assertEquals('url', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('billing_address', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + $this->assertArrayHasKey('key', reset($result)); + $this->assertArrayHasKey('label', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('URL', reset($result)['label']); + $this->assertEquals('Notes', end($result)['label']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } +} diff --git a/tests/Integration/ResourceFormsNoDataTest.php b/tests/Integration/ResourceFormsNoDataTest.php new file mode 100644 index 0000000..43dd735 --- /dev/null +++ b/tests/Integration/ResourceFormsNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Forms( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns false, because no resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceFormsTest.php b/tests/Integration/ResourceFormsTest.php new file mode 100644 index 0000000..836daaa --- /dev/null +++ b/tests/Integration/ResourceFormsTest.php @@ -0,0 +1,304 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Forms( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('AAA Test', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('WooCommerce Product Form', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'name'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in descending alphabetical order. + $this->assertEquals('WooCommerce Product Form', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('AAA Test', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('WPForms Form', reset($result)['name']); + $this->assertEquals('Legacy Form', end($result)['name']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } + + + /** + * Test that the is_legacy() function returns true for a Legacy Form ID. + * + * @since 1.8.9 + */ + public function testIsLegacyFormWithLegacyFormID() + { + $this->resource->refresh(); + $this->assertTrue($this->resource->is_legacy($_ENV['CONVERTKIT_API_LEGACY_FORM_ID'])); + } + + /** + * Test that the is_legacy() function returns false for a non-Legacy Form ID. + * + * @since 1.8.9 + */ + public function testIsLegacyFormWithFormID() + { + $this->resource->refresh(); + $this->assertFalse($this->resource->is_legacy($_ENV['CONVERTKIT_API_FORM_ID'])); + } + + /** + * Test that the is_legacy() function returns false for an invalid Form ID. + * + * @since 1.8.9 + */ + public function testIsLegacyFormWithInvalidFormID() + { + $this->resource->refresh(); + $this->assertFalse($this->resource->is_legacy(12345)); + } +} diff --git a/tests/Integration/ResourceSequencesNoDataTest.php b/tests/Integration/ResourceSequencesNoDataTest.php new file mode 100644 index 0000000..5a65962 --- /dev/null +++ b/tests/Integration/ResourceSequencesNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Sequences( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because no resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceSequencesTest.php b/tests/Integration/ResourceSequencesTest.php new file mode 100644 index 0000000..44ff76b --- /dev/null +++ b/tests/Integration/ResourceSequencesTest.php @@ -0,0 +1,270 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Sequences( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('Another Sequence', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('WordPress Sequence', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'name'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in descending alphabetical order. + $this->assertEquals('WordPress Sequence', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('Another Sequence', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('Another Sequence', reset($result)['name']); + $this->assertEquals('WordPress Sequence', end($result)['name']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } +} diff --git a/tests/Integration/ResourceTagsNoDataTest.php b/tests/Integration/ResourceTagsNoDataTest.php new file mode 100644 index 0000000..e7e8352 --- /dev/null +++ b/tests/Integration/ResourceTagsNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Tags( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because no resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceTagsTest.php b/tests/Integration/ResourceTagsTest.php new file mode 100644 index 0000000..5015565 --- /dev/null +++ b/tests/Integration/ResourceTagsTest.php @@ -0,0 +1,270 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Tags( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('gravityforms-tag-1', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('wpforms', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'name'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('wpforms', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('gravityforms-tag-1', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('wpforms', reset($result)['name']); + $this->assertEquals('wordpress', end($result)['name']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } +} From 2212ee288efd868fe200fa6d60d2126090758a2e Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 12 Dec 2025 23:24:53 +0800 Subject: [PATCH 3/6] Remove resource tests --- .../ResourceCustomFieldsNoDataTest.php | 196 ----------- .../Integration/ResourceCustomFieldsTest.php | 276 ---------------- tests/Integration/ResourceFormsNoDataTest.php | 196 ----------- tests/Integration/ResourceFormsTest.php | 304 ------------------ .../ResourceSequencesNoDataTest.php | 196 ----------- tests/Integration/ResourceSequencesTest.php | 270 ---------------- tests/Integration/ResourceTagsNoDataTest.php | 196 ----------- tests/Integration/ResourceTagsTest.php | 270 ---------------- 8 files changed, 1904 deletions(-) delete mode 100644 tests/Integration/ResourceCustomFieldsNoDataTest.php delete mode 100644 tests/Integration/ResourceCustomFieldsTest.php delete mode 100644 tests/Integration/ResourceFormsNoDataTest.php delete mode 100644 tests/Integration/ResourceFormsTest.php delete mode 100644 tests/Integration/ResourceSequencesNoDataTest.php delete mode 100644 tests/Integration/ResourceSequencesTest.php delete mode 100644 tests/Integration/ResourceTagsNoDataTest.php delete mode 100644 tests/Integration/ResourceTagsTest.php diff --git a/tests/Integration/ResourceCustomFieldsNoDataTest.php b/tests/Integration/ResourceCustomFieldsNoDataTest.php deleted file mode 100644 index 0b7be03..0000000 --- a/tests/Integration/ResourceCustomFieldsNoDataTest.php +++ /dev/null @@ -1,196 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Custom_Fields( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->refresh(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Test that the get() function performs as expected. - * - * @since 1.8.9 - */ - public function testGet() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->get(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns false, because resources do not exist. - $result = $this->resource->exist(); - $this->assertSame($result, false); - } -} diff --git a/tests/Integration/ResourceCustomFieldsTest.php b/tests/Integration/ResourceCustomFieldsTest.php deleted file mode 100644 index 30e7dad..0000000 --- a/tests/Integration/ResourceCustomFieldsTest.php +++ /dev/null @@ -1,276 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Custom_Fields( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that the data is stored in the options table and includes some expected keys. - $result = $this->resource->refresh(); - $this->assertIsArray($result); - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Tests that the get() function returns resources in alphabetical ascending order - * by default. - * - * @since 1.8.9 - */ - public function testGet() - { - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - $this->assertArrayHasKey('key', reset($result)); - $this->assertArrayHasKey('label', reset($result)); - - // Assert order of data is in ascending alphabetical order. - $this->assertEquals('Billing Address', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('URL', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in alphabetical descending order - * when a valid order_by and order properties are defined. - * - * @since 1.8.9 - */ - public function testGetWithValidOrderByAndOrder() - { - // Define order_by and order. - $this->resource->order_by = 'key'; - $this->resource->order = 'desc'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - $this->assertArrayHasKey('key', reset($result)); - $this->assertArrayHasKey('label', reset($result)); - - // Assert order of data is in descending alphabetical order. - $this->assertEquals('url', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('billing_address', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in their original order - * when populated with Forms and an invalid order_by value is specified. - * - * @since 1.8.9 - */ - public function testGetWithInvalidOrderBy() - { - // Define order_by with an invalid value (i.e. an array key that does not exist). - $this->resource->order_by = 'invalid_key'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - $this->assertArrayHasKey('key', reset($result)); - $this->assertArrayHasKey('label', reset($result)); - - // Assert order of data has not changed. - $this->assertEquals('URL', reset($result)['label']); - $this->assertEquals('Notes', end($result)['label']); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns true, because resources exist. - $result = $this->resource->exist(); - $this->assertSame($result, true); - } -} diff --git a/tests/Integration/ResourceFormsNoDataTest.php b/tests/Integration/ResourceFormsNoDataTest.php deleted file mode 100644 index 43dd735..0000000 --- a/tests/Integration/ResourceFormsNoDataTest.php +++ /dev/null @@ -1,196 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Forms( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->refresh(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Test that the get() function performs as expected. - * - * @since 1.8.9 - */ - public function testGet() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->get(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns false, because no resources exist. - $result = $this->resource->exist(); - $this->assertSame($result, false); - } -} diff --git a/tests/Integration/ResourceFormsTest.php b/tests/Integration/ResourceFormsTest.php deleted file mode 100644 index 836daaa..0000000 --- a/tests/Integration/ResourceFormsTest.php +++ /dev/null @@ -1,304 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Forms( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that the data is stored in the options table and includes some expected keys. - $result = $this->resource->refresh(); - $this->assertIsArray($result); - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Tests that the get() function returns resources in alphabetical ascending order - * by default. - * - * @since 1.8.9 - */ - public function testGet() - { - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data is in ascending alphabetical order. - $this->assertEquals('AAA Test', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('WooCommerce Product Form', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in alphabetical descending order - * when a valid order_by and order properties are defined. - * - * @since 1.8.9 - */ - public function testGetWithValidOrderByAndOrder() - { - // Define order_by and order. - $this->resource->order_by = 'name'; - $this->resource->order = 'desc'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data is in descending alphabetical order. - $this->assertEquals('WooCommerce Product Form', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('AAA Test', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in their original order - * when populated with Forms and an invalid order_by value is specified. - * - * @since 1.8.9 - */ - public function testGetWithInvalidOrderBy() - { - // Define order_by with an invalid value (i.e. an array key that does not exist). - $this->resource->order_by = 'invalid_key'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data has not changed. - $this->assertEquals('WPForms Form', reset($result)['name']); - $this->assertEquals('Legacy Form', end($result)['name']); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns true, because resources exist. - $result = $this->resource->exist(); - $this->assertSame($result, true); - } - - - /** - * Test that the is_legacy() function returns true for a Legacy Form ID. - * - * @since 1.8.9 - */ - public function testIsLegacyFormWithLegacyFormID() - { - $this->resource->refresh(); - $this->assertTrue($this->resource->is_legacy($_ENV['CONVERTKIT_API_LEGACY_FORM_ID'])); - } - - /** - * Test that the is_legacy() function returns false for a non-Legacy Form ID. - * - * @since 1.8.9 - */ - public function testIsLegacyFormWithFormID() - { - $this->resource->refresh(); - $this->assertFalse($this->resource->is_legacy($_ENV['CONVERTKIT_API_FORM_ID'])); - } - - /** - * Test that the is_legacy() function returns false for an invalid Form ID. - * - * @since 1.8.9 - */ - public function testIsLegacyFormWithInvalidFormID() - { - $this->resource->refresh(); - $this->assertFalse($this->resource->is_legacy(12345)); - } -} diff --git a/tests/Integration/ResourceSequencesNoDataTest.php b/tests/Integration/ResourceSequencesNoDataTest.php deleted file mode 100644 index 5a65962..0000000 --- a/tests/Integration/ResourceSequencesNoDataTest.php +++ /dev/null @@ -1,196 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Sequences( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->refresh(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Test that the get() function performs as expected. - * - * @since 1.8.9 - */ - public function testGet() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->get(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns true, because no resources exist. - $result = $this->resource->exist(); - $this->assertSame($result, false); - } -} diff --git a/tests/Integration/ResourceSequencesTest.php b/tests/Integration/ResourceSequencesTest.php deleted file mode 100644 index 44ff76b..0000000 --- a/tests/Integration/ResourceSequencesTest.php +++ /dev/null @@ -1,270 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Sequences( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that the data is stored in the options table and includes some expected keys. - $result = $this->resource->refresh(); - $this->assertIsArray($result); - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Tests that the get() function returns resources in alphabetical ascending order - * by default. - * - * @since 1.8.9 - */ - public function testGet() - { - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data is in ascending alphabetical order. - $this->assertEquals('Another Sequence', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('WordPress Sequence', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in alphabetical descending order - * when a valid order_by and order properties are defined. - * - * @since 1.8.9 - */ - public function testGetWithValidOrderByAndOrder() - { - // Define order_by and order. - $this->resource->order_by = 'name'; - $this->resource->order = 'desc'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data is in descending alphabetical order. - $this->assertEquals('WordPress Sequence', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('Another Sequence', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in their original order - * when populated with Forms and an invalid order_by value is specified. - * - * @since 1.8.9 - */ - public function testGetWithInvalidOrderBy() - { - // Define order_by with an invalid value (i.e. an array key that does not exist). - $this->resource->order_by = 'invalid_key'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data has not changed. - $this->assertEquals('Another Sequence', reset($result)['name']); - $this->assertEquals('WordPress Sequence', end($result)['name']); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns true, because resources exist. - $result = $this->resource->exist(); - $this->assertSame($result, true); - } -} diff --git a/tests/Integration/ResourceTagsNoDataTest.php b/tests/Integration/ResourceTagsNoDataTest.php deleted file mode 100644 index e7e8352..0000000 --- a/tests/Integration/ResourceTagsNoDataTest.php +++ /dev/null @@ -1,196 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Tags( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->refresh(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Test that the get() function performs as expected. - * - * @since 1.8.9 - */ - public function testGet() - { - // Confirm that no resources exist in the stored options table data. - $result = $this->resource->get(); - $this->assertNotInstanceOf(\WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns true, because no resources exist. - $result = $this->resource->exist(); - $this->assertSame($result, false); - } -} diff --git a/tests/Integration/ResourceTagsTest.php b/tests/Integration/ResourceTagsTest.php deleted file mode 100644 index 5015565..0000000 --- a/tests/Integration/ResourceTagsTest.php +++ /dev/null @@ -1,270 +0,0 @@ - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], - 'token_expires' => time() + 10000, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Initialize the API instance. - $this->api = new \Integrate_ConvertKit_WPForms_API( - $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], - $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], - $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], - $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] - ); - - // Initialize the resource class we want to test with the API instance and WPForms Account ID. - $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Tags( $this->api, $this->wpforms_account_id ); - $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); - - // Initialize the resource class, fetching resources from the API and caching them in the options table. - $result = $this->resource->init(); - - // Confirm calling init() didn't result in an error. - $this->assertNotInstanceOf(\WP_Error::class, $result); - } - - /** - * Performs actions after each test. - * - * @since 1.8.9 - */ - public function tearDown(): void - { - // Disable integration, removing Access Token and Refresh Token from Plugin's settings. - wpforms_update_providers_options( - 'convertkit', - array( - 'access_token' => '', - 'refresh_token' => '', - 'token_expires' => 0, - 'label' => 'ConvertKit WordPress', - 'date' => time(), - ), - $this->wpforms_account_id - ); - - // Delete Resources from options table. - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); - delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); - - // Destroy the classes we tested. - unset($this->api); - unset($this->resource); - - parent::tearDown(); - } - - /** - * Test that the refresh() function performs as expected. - * - * @since 1.8.9 - */ - public function testRefresh() - { - // Confirm that the data is stored in the options table and includes some expected keys. - $result = $this->resource->refresh(); - $this->assertIsArray($result); - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - } - - /** - * Test that the expiry timestamp is set and returns the expected value. - * - * @since 1.8.9 - */ - public function testExpiry() - { - // Define the expected expiry date based on the resource class' $cache_duration setting. - $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); - - // Fetch the actual expiry date set when the resource class was initialized. - $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); - - // Confirm both dates match. - $this->assertEquals($expectedExpiryDate, $expiryDate); - } - - /** - * Tests that the get() function returns resources in alphabetical ascending order - * by default. - * - * @since 1.8.9 - */ - public function testGet() - { - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data is in ascending alphabetical order. - $this->assertEquals('gravityforms-tag-1', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('wpforms', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in alphabetical descending order - * when a valid order_by and order properties are defined. - * - * @since 1.8.9 - */ - public function testGetWithValidOrderByAndOrder() - { - // Define order_by and order. - $this->resource->order_by = 'name'; - $this->resource->order = 'desc'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data is in ascending alphabetical order. - $this->assertEquals('wpforms', reset($result)[ $this->resource->order_by ]); - $this->assertEquals('gravityforms-tag-1', end($result)[ $this->resource->order_by ]); - } - - /** - * Tests that the get() function returns resources in their original order - * when populated with Forms and an invalid order_by value is specified. - * - * @since 1.8.9 - */ - public function testGetWithInvalidOrderBy() - { - // Define order_by with an invalid value (i.e. an array key that does not exist). - $this->resource->order_by = 'invalid_key'; - - // Call resource class' get() function. - $result = $this->resource->get(); - - // Assert result is an array. - $this->assertIsArray($result); - - // Assert top level array keys are preserved. - $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); - $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); - - // Assert resource within results has expected array keys. - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - - // Assert order of data has not changed. - $this->assertEquals('wpforms', reset($result)['name']); - $this->assertEquals('wordpress', end($result)['name']); - } - - /** - * Test that the count() function returns the number of resources. - * - * @since 1.8.9 - */ - public function testCount() - { - $result = $this->resource->get(); - $this->assertEquals($this->resource->count(), count($result)); - } - - /** - * Test that the exist() function performs as expected. - * - * @since 1.8.9 - */ - public function testExist() - { - // Confirm that the function returns true, because resources exist. - $result = $this->resource->exist(); - $this->assertSame($result, true); - } -} From 2d48034f14ba668ba8660101df85003115cbd460 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 12 Dec 2025 23:25:13 +0800 Subject: [PATCH 4/6] Tests: Add Resource Tests --- .../ResourceCustomFieldsNoDataTest.php | 196 +++++++++++ .../Integration/ResourceCustomFieldsTest.php | 276 ++++++++++++++++ tests/Integration/ResourceFormsNoDataTest.php | 196 +++++++++++ tests/Integration/ResourceFormsTest.php | 304 ++++++++++++++++++ .../ResourceSequencesNoDataTest.php | 196 +++++++++++ tests/Integration/ResourceSequencesTest.php | 270 ++++++++++++++++ tests/Integration/ResourceTagsNoDataTest.php | 196 +++++++++++ tests/Integration/ResourceTagsTest.php | 270 ++++++++++++++++ 8 files changed, 1904 insertions(+) create mode 100644 tests/Integration/ResourceCustomFieldsNoDataTest.php create mode 100644 tests/Integration/ResourceCustomFieldsTest.php create mode 100644 tests/Integration/ResourceFormsNoDataTest.php create mode 100644 tests/Integration/ResourceFormsTest.php create mode 100644 tests/Integration/ResourceSequencesNoDataTest.php create mode 100644 tests/Integration/ResourceSequencesTest.php create mode 100644 tests/Integration/ResourceTagsNoDataTest.php create mode 100644 tests/Integration/ResourceTagsTest.php diff --git a/tests/Integration/ResourceCustomFieldsNoDataTest.php b/tests/Integration/ResourceCustomFieldsNoDataTest.php new file mode 100644 index 0000000..0b7be03 --- /dev/null +++ b/tests/Integration/ResourceCustomFieldsNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Custom_Fields( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns false, because resources do not exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceCustomFieldsTest.php b/tests/Integration/ResourceCustomFieldsTest.php new file mode 100644 index 0000000..30e7dad --- /dev/null +++ b/tests/Integration/ResourceCustomFieldsTest.php @@ -0,0 +1,276 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Custom_Fields( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + $this->assertArrayHasKey('key', reset($result)); + $this->assertArrayHasKey('label', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('Billing Address', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('URL', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'key'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + $this->assertArrayHasKey('key', reset($result)); + $this->assertArrayHasKey('label', reset($result)); + + // Assert order of data is in descending alphabetical order. + $this->assertEquals('url', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('billing_address', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + $this->assertArrayHasKey('key', reset($result)); + $this->assertArrayHasKey('label', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('URL', reset($result)['label']); + $this->assertEquals('Notes', end($result)['label']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } +} diff --git a/tests/Integration/ResourceFormsNoDataTest.php b/tests/Integration/ResourceFormsNoDataTest.php new file mode 100644 index 0000000..43dd735 --- /dev/null +++ b/tests/Integration/ResourceFormsNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Forms( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns false, because no resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceFormsTest.php b/tests/Integration/ResourceFormsTest.php new file mode 100644 index 0000000..836daaa --- /dev/null +++ b/tests/Integration/ResourceFormsTest.php @@ -0,0 +1,304 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Forms( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('AAA Test', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('WooCommerce Product Form', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'name'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in descending alphabetical order. + $this->assertEquals('WooCommerce Product Form', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('AAA Test', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('WPForms Form', reset($result)['name']); + $this->assertEquals('Legacy Form', end($result)['name']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } + + + /** + * Test that the is_legacy() function returns true for a Legacy Form ID. + * + * @since 1.8.9 + */ + public function testIsLegacyFormWithLegacyFormID() + { + $this->resource->refresh(); + $this->assertTrue($this->resource->is_legacy($_ENV['CONVERTKIT_API_LEGACY_FORM_ID'])); + } + + /** + * Test that the is_legacy() function returns false for a non-Legacy Form ID. + * + * @since 1.8.9 + */ + public function testIsLegacyFormWithFormID() + { + $this->resource->refresh(); + $this->assertFalse($this->resource->is_legacy($_ENV['CONVERTKIT_API_FORM_ID'])); + } + + /** + * Test that the is_legacy() function returns false for an invalid Form ID. + * + * @since 1.8.9 + */ + public function testIsLegacyFormWithInvalidFormID() + { + $this->resource->refresh(); + $this->assertFalse($this->resource->is_legacy(12345)); + } +} diff --git a/tests/Integration/ResourceSequencesNoDataTest.php b/tests/Integration/ResourceSequencesNoDataTest.php new file mode 100644 index 0000000..5a65962 --- /dev/null +++ b/tests/Integration/ResourceSequencesNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Sequences( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because no resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceSequencesTest.php b/tests/Integration/ResourceSequencesTest.php new file mode 100644 index 0000000..44ff76b --- /dev/null +++ b/tests/Integration/ResourceSequencesTest.php @@ -0,0 +1,270 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Sequences( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('Another Sequence', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('WordPress Sequence', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'name'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in descending alphabetical order. + $this->assertEquals('WordPress Sequence', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('Another Sequence', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('Another Sequence', reset($result)['name']); + $this->assertEquals('WordPress Sequence', end($result)['name']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } +} diff --git a/tests/Integration/ResourceTagsNoDataTest.php b/tests/Integration/ResourceTagsNoDataTest.php new file mode 100644 index 0000000..e7e8352 --- /dev/null +++ b/tests/Integration/ResourceTagsNoDataTest.php @@ -0,0 +1,196 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Tags( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->refresh(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Test that the get() function performs as expected. + * + * @since 1.8.9 + */ + public function testGet() + { + // Confirm that no resources exist in the stored options table data. + $result = $this->resource->get(); + $this->assertNotInstanceOf(\WP_Error::class, $result); + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because no resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, false); + } +} diff --git a/tests/Integration/ResourceTagsTest.php b/tests/Integration/ResourceTagsTest.php new file mode 100644 index 0000000..5015565 --- /dev/null +++ b/tests/Integration/ResourceTagsTest.php @@ -0,0 +1,270 @@ + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => time() + 10000, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Initialize the API instance. + $this->api = new \Integrate_ConvertKit_WPForms_API( + $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], + $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'], + $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'] + ); + + // Initialize the resource class we want to test with the API instance and WPForms Account ID. + $this->resource = new \Integrate_ConvertKit_WPForms_Resource_Tags( $this->api, $this->wpforms_account_id ); + $this->assertNotInstanceOf(\WP_Error::class, $this->resource->resources); + + // Initialize the resource class, fetching resources from the API and caching them in the options table. + $result = $this->resource->init(); + + // Confirm calling init() didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $result); + } + + /** + * Performs actions after each test. + * + * @since 1.8.9 + */ + public function tearDown(): void + { + // Disable integration, removing Access Token and Refresh Token from Plugin's settings. + wpforms_update_providers_options( + 'convertkit', + array( + 'access_token' => '', + 'refresh_token' => '', + 'token_expires' => 0, + 'label' => 'ConvertKit WordPress', + 'date' => time(), + ), + $this->wpforms_account_id + ); + + // Delete Resources from options table. + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id); + delete_option($this->resource->settings_name . '_' . $this->wpforms_account_id . '_last_queried'); + + // Destroy the classes we tested. + unset($this->api); + unset($this->resource); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 1.8.9 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $result = $this->resource->refresh(); + $this->assertIsArray($result); + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + } + + /** + * Test that the expiry timestamp is set and returns the expected value. + * + * @since 1.8.9 + */ + public function testExpiry() + { + // Define the expected expiry date based on the resource class' $cache_duration setting. + $expectedExpiryDate = date('Y-m-d', time() + $this->resource->cache_duration); + + // Fetch the actual expiry date set when the resource class was initialized. + $expiryDate = date('Y-m-d', $this->resource->last_queried + $this->resource->cache_duration); + + // Confirm both dates match. + $this->assertEquals($expectedExpiryDate, $expiryDate); + } + + /** + * Tests that the get() function returns resources in alphabetical ascending order + * by default. + * + * @since 1.8.9 + */ + public function testGet() + { + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('gravityforms-tag-1', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('wpforms', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in alphabetical descending order + * when a valid order_by and order properties are defined. + * + * @since 1.8.9 + */ + public function testGetWithValidOrderByAndOrder() + { + // Define order_by and order. + $this->resource->order_by = 'name'; + $this->resource->order = 'desc'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data is in ascending alphabetical order. + $this->assertEquals('wpforms', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('gravityforms-tag-1', end($result)[ $this->resource->order_by ]); + } + + /** + * Tests that the get() function returns resources in their original order + * when populated with Forms and an invalid order_by value is specified. + * + * @since 1.8.9 + */ + public function testGetWithInvalidOrderBy() + { + // Define order_by with an invalid value (i.e. an array key that does not exist). + $this->resource->order_by = 'invalid_key'; + + // Call resource class' get() function. + $result = $this->resource->get(); + + // Assert result is an array. + $this->assertIsArray($result); + + // Assert top level array keys are preserved. + $this->assertArrayHasKey(array_key_first($this->resource->resources), $result); + $this->assertArrayHasKey(array_key_last($this->resource->resources), $result); + + // Assert resource within results has expected array keys. + $this->assertArrayHasKey('id', reset($result)); + $this->assertArrayHasKey('name', reset($result)); + + // Assert order of data has not changed. + $this->assertEquals('wpforms', reset($result)['name']); + $this->assertEquals('wordpress', end($result)['name']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 1.8.9 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 1.8.9 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } +} From 144a3e336a505efc1fe83dca43af7ed42dd1c770 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 15 Dec 2025 15:55:42 +0800 Subject: [PATCH 5/6] Add `access_token` method --- ...class-integrate-convertkit-wpforms-api.php | 20 ++++++++++++------- ...-integrate-convertkit-wpforms-resource.php | 9 ++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/includes/class-integrate-convertkit-wpforms-api.php b/includes/class-integrate-convertkit-wpforms-api.php index b21464f..01c9d30 100644 --- a/includes/class-integrate-convertkit-wpforms-api.php +++ b/includes/class-integrate-convertkit-wpforms-api.php @@ -29,13 +29,6 @@ class Integrate_ConvertKit_WPForms_API extends ConvertKit_API_V4 { */ public $error_messages = false; - /** - * Access Token - * - * @var string - */ - public $access_token = ''; - /** * Sets up the API with the required credentials. * @@ -120,4 +113,17 @@ public function __construct( $client_id, $redirect_uri, $access_token = false, $ } + /** + * Returns the access token. + * + * @since 1.8.9 + * + * @return string + */ + public function access_token() { + + return $this->access_token; + + } + } diff --git a/includes/class-integrate-convertkit-wpforms-resource.php b/includes/class-integrate-convertkit-wpforms-resource.php index f389b24..91e8b77 100644 --- a/includes/class-integrate-convertkit-wpforms-resource.php +++ b/includes/class-integrate-convertkit-wpforms-resource.php @@ -14,6 +14,13 @@ */ class Integrate_ConvertKit_WPForms_Resource extends ConvertKit_Resource_V4 { + /** + * The API class + * + * @var bool|Integrate_ConvertKit_WPForms_API + */ + public $api = false; + /** * Constructor. * @@ -57,7 +64,7 @@ public function refresh() { // If an error occured, maybe delete credentials from the Plugin's settings // if the error is a 401 unauthorized. if ( is_wp_error( $result ) ) { - integrate_convertkit_wpforms_maybe_delete_credentials( $result, INTEGRATE_CONVERTKIT_WPFORMS_OAUTH_CLIENT_ID, $this->api->access_token ); + integrate_convertkit_wpforms_maybe_delete_credentials( $result, INTEGRATE_CONVERTKIT_WPFORMS_OAUTH_CLIENT_ID, $this->api->access_token() ); } return $result; From 3c0e334d5ca55193eafa6df700b5bb8627cec2f9 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 15 Dec 2025 17:34:09 +0800 Subject: [PATCH 6/6] Added test --- tests/EndToEnd.suite.yml | 2 +- tests/EndToEnd/general/IntegrationsCest.php | 52 +++++++++++++++++++++ tests/Support/Helper/WPNotices.php | 37 +++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/Support/Helper/WPNotices.php diff --git a/tests/EndToEnd.suite.yml b/tests/EndToEnd.suite.yml index 07bcc91..bce6fb9 100644 --- a/tests/EndToEnd.suite.yml +++ b/tests/EndToEnd.suite.yml @@ -25,6 +25,7 @@ modules: - \Tests\Support\Helper\Plugin - \Tests\Support\Helper\ThirdPartyPlugin - \Tests\Support\Helper\WPForms + - \Tests\Support\Helper\WPNotices - \Tests\Support\Helper\Xdebug config: lucatume\WPBrowser\Module\WPWebDriver: @@ -40,7 +41,6 @@ modules: capabilities: "goog:chromeOptions": args: - - "--headless" - "--disable-gpu" - "--disable-dev-shm-usage" - "--disable-software-rasterizer" diff --git a/tests/EndToEnd/general/IntegrationsCest.php b/tests/EndToEnd/general/IntegrationsCest.php index a6733c9..08858f6 100644 --- a/tests/EndToEnd/general/IntegrationsCest.php +++ b/tests/EndToEnd/general/IntegrationsCest.php @@ -126,6 +126,58 @@ public function testAddIntegrationWithInvalidAPICredentials(EndToEndTester $I) $I->see($errorDescription); } + /** + * Test that an error notification is displayed when the API credentials are invalid. + * + * @since 1.8.9 + * + * @param EndToEndTester $I Tester. + */ + public function testInvalidCredentials(EndToEndTester $I) + { + // Define connection with invalid API credentials. + $I->setupWPFormsIntegration( + $I, + 'fakeAccessToken', + 'fakeRefreshToken' + ); + + // Setup WPForms Form and configuration for this test. + // Create Form. + $wpFormsID = $I->createWPFormsForm($I); + + // Load WPForms Editor. + $I->amOnAdminPage('admin.php?page=wpforms-builder&view=fields&form_id=' . $wpFormsID); + + // Click Marketing icon. + $I->waitForElementVisible('.wpforms-panel-providers-button'); + $I->click('.wpforms-panel-providers-button'); + + // Click ConvertKit tab. + $I->click('#wpforms-panel-providers a.wpforms-panel-sidebar-section-convertkit'); + + // Click Add New Connection. + $I->click('Add New Connection'); + + // Define name for connection. + $I->waitForElementVisible('.jconfirm-content'); + $I->fillField('#provider-connection-name', 'Kit'); + $I->click('OK'); + + // Get the connection ID. + $I->waitForElementVisible('.wpforms-provider-connections .wpforms-provider-connection'); + $connectionID = $I->grabAttributeFrom('.wpforms-provider-connections .wpforms-provider-connection', 'data-connection_id'); + + // Specify field values. + $I->waitForElementVisible('div[data-connection_id="' . $connectionID . '"] .wpforms-provider-fields', 30); + + // Navigate to the WordPress Admin. + $I->amOnAdminPage('index.php'); + + // Check that a notice is displayed that the API credentials are invalid. + $I->seeErrorNotice($I, 'Kit for WPForms: Authorization failed. Please reconnect your Kit account.'); + } + /** * Deactivate and reset Plugin(s) after each test, if the test passes. * We don't use _after, as this would provide a screenshot of the Plugin diff --git a/tests/Support/Helper/WPNotices.php b/tests/Support/Helper/WPNotices.php new file mode 100644 index 0000000..36191ab --- /dev/null +++ b/tests/Support/Helper/WPNotices.php @@ -0,0 +1,37 @@ +{yourFunctionName}. + * + * @since 1.8.9 + */ +class WPNotices extends \Codeception\Module +{ + /** + * Confirms that an error notification is output with the given text. + * + * @since 1.8.9 + * + * @param EndToEndTester $I EndToEnd Tester. + * @param string $message Message. + */ + public function seeErrorNotice($I, $message) + { + $I->see($message, 'div.notice-error'); + } + + /** + * Confirms that an error notification is not output with the given text. + * + * @since 1.8.9 + * + * @param EndToEndTester $I EndToEnd Tester. + * @param string $message Message. + */ + public function dontSeeErrorNotice($I, $message) + { + $I->dontSee($message, 'div.notice-error'); + } +}