From 2d48034f14ba668ba8660101df85003115cbd460 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 12 Dec 2025 23:25:13 +0800 Subject: [PATCH] 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); + } +}