From e7e52af6a166dbb8e28c439e561828d672b6aa8a Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 8 Dec 2025 12:13:39 +0800 Subject: [PATCH] Tests: Add Custom Fields and Sequence Resource Tests --- .../ResourceCustomFieldsNoDataTest.php | 162 ++++++++++++ .../Integration/ResourceCustomFieldsTest.php | 236 ++++++++++++++++++ .../ResourceSequencesNoDataTest.php | 162 ++++++++++++ tests/Integration/ResourceSequencesTest.php | 236 ++++++++++++++++++ 4 files changed, 796 insertions(+) create mode 100644 tests/Integration/ResourceCustomFieldsNoDataTest.php create mode 100644 tests/Integration/ResourceCustomFieldsTest.php create mode 100644 tests/Integration/ResourceSequencesNoDataTest.php create mode 100644 tests/Integration/ResourceSequencesTest.php diff --git a/tests/Integration/ResourceCustomFieldsNoDataTest.php b/tests/Integration/ResourceCustomFieldsNoDataTest.php new file mode 100644 index 000000000..932ff42fa --- /dev/null +++ b/tests/Integration/ResourceCustomFieldsNoDataTest.php @@ -0,0 +1,162 @@ +settings = new \ConvertKit_Settings(); + update_option( + $this->settings::SETTINGS_NAME, + [ + 'access_token' => $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + ] + ); + + // Initialize the resource class we want to test. + $this->resource = new \ConvertKit_Resource_Custom_Fields(); + + // Confirm initialization didn't result in an error. + $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 3.1.3 + */ + public function tearDown(): void + { + // Delete Credentials and Resources from Plugin's settings. + delete_option($this->settings::SETTINGS_NAME); + delete_option($this->resource->settings_name); + delete_option($this->resource->settings_name . '_last_queried'); + + // Destroy the resource class we tested. + unset($this->resource); + + // Deactivate Plugin. + deactivate_plugins('convertkit/wp-convertkit.php'); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 3.1.3 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $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 3.1.3 + */ + 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 3.1.3 + */ + public function testGet() + { + // Confirm that the data is fetched from the options table when using get(), and includes some expected keys. + $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 3.1.3 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 3.1.3 + */ + public function testExist() + { + // Confirm that the function returns true, because resources 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 000000000..b073fe558 --- /dev/null +++ b/tests/Integration/ResourceCustomFieldsTest.php @@ -0,0 +1,236 @@ +settings = new \ConvertKit_Settings(); + update_option( + $this->settings::SETTINGS_NAME, + [ + 'access_token' => $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + ] + ); + + // Initialize the resource class we want to test. + $this->resource = new \ConvertKit_Resource_Custom_Fields(); + + // Confirm initialization didn't result in an error. + $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 3.1.3 + */ + public function tearDown(): void + { + // Delete Credentials and Resources from Plugin's settings. + delete_option($this->settings::SETTINGS_NAME); + delete_option($this->resource->settings_name); + delete_option($this->resource->settings_name . '_last_queried'); + + // Destroy the resource class we tested. + unset($this->resource); + + // Deactivate Plugin. + deactivate_plugins('convertkit/wp-convertkit.php'); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 3.1.3 + */ + 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 3.1.3 + */ + 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 3.1.3 + */ + 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('ck_field_1075083_url', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('ck_field_276295_payment_method', 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 3.1.3 + */ + 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('ck_field_276295_payment_method', reset($result)[ $this->resource->order_by ]); + $this->assertEquals('ck_field_1075083_url', 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 3.1.3 + */ + 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('ck_field_1075083_url', reset($result)['name']); + $this->assertEquals('ck_field_258240_notes', end($result)['name']); + } + + /** + * Test that the count() function returns the number of resources. + * + * @since 3.1.3 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 3.1.3 + */ + 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/ResourceSequencesNoDataTest.php b/tests/Integration/ResourceSequencesNoDataTest.php new file mode 100644 index 000000000..ae94451b4 --- /dev/null +++ b/tests/Integration/ResourceSequencesNoDataTest.php @@ -0,0 +1,162 @@ +settings = new \ConvertKit_Settings(); + update_option( + $this->settings::SETTINGS_NAME, + [ + 'access_token' => $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], + ] + ); + + // Initialize the resource class we want to test. + $this->resource = new \ConvertKit_Resource_Sequences(); + + // Confirm initialization didn't result in an error. + $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 3.1.3 + */ + public function tearDown(): void + { + // Delete Credentials and Resources from Plugin's settings. + delete_option($this->settings::SETTINGS_NAME); + delete_option($this->resource->settings_name); + delete_option($this->resource->settings_name . '_last_queried'); + + // Destroy the resource class we tested. + unset($this->resource); + + // Deactivate Plugin. + deactivate_plugins('convertkit/wp-convertkit.php'); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 3.1.3 + */ + public function testRefresh() + { + // Confirm that the data is stored in the options table and includes some expected keys. + $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 3.1.3 + */ + 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 3.1.3 + */ + public function testGet() + { + // Confirm that the data is fetched from the options table when using get(), and includes some expected keys. + $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 3.1.3 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 3.1.3 + */ + public function testExist() + { + // Confirm that the function returns true, because 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 000000000..4a967a8b7 --- /dev/null +++ b/tests/Integration/ResourceSequencesTest.php @@ -0,0 +1,236 @@ +settings = new \ConvertKit_Settings(); + update_option( + $this->settings::SETTINGS_NAME, + [ + 'access_token' => $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + ] + ); + + // Initialize the resource class we want to test. + $this->resource = new \ConvertKit_Resource_Sequences(); + + // Confirm initialization didn't result in an error. + $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 3.1.3 + */ + public function tearDown(): void + { + // Delete Credentials and Resources from Plugin's settings. + delete_option($this->settings::SETTINGS_NAME); + delete_option($this->resource->settings_name); + delete_option($this->resource->settings_name . '_last_queried'); + + // Destroy the resource class we tested. + unset($this->resource); + + // Deactivate Plugin. + deactivate_plugins('convertkit/wp-convertkit.php'); + + parent::tearDown(); + } + + /** + * Test that the refresh() function performs as expected. + * + * @since 3.1.3 + */ + 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 3.1.3 + */ + 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 3.1.3 + */ + 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 3.1.3 + */ + 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('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 3.1.3 + */ + 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 3.1.3 + */ + public function testCount() + { + $result = $this->resource->get(); + $this->assertEquals($this->resource->count(), count($result)); + } + + /** + * Test that the exist() function performs as expected. + * + * @since 3.1.3 + */ + public function testExist() + { + // Confirm that the function returns true, because resources exist. + $result = $this->resource->exist(); + $this->assertSame($result, true); + } +}