From fc659aa3b907f37bf63ff4f984b4e8ae51ab156f Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 22 Apr 2024 17:02:51 +0100 Subject: [PATCH 1/2] Added tests --- tests/wpunit/APITest.php | 287 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 287 insertions(+) diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index 516c71c..b0ac29c 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -1251,6 +1251,293 @@ public function testTagUnsubscribeWithInvalidEmail() $this->assertEquals('tag_unsubscribe(): the email parameter is not a valid email address.', $result->get_error_message()); } + /** + * Test that get_tag_subscriptions() returns the expected data + * when a valid Tag ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetTagSubscriptions() + { + $result = $this->api->get_tag_subscriptions((int) $_ENV['CONVERTKIT_API_TAG_ID']); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + } + + /** + * Test that get_tag_subscriptions() returns the expected data + * when the total count is included. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsWithTotalCount() + { + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + null, // Created after. + null, // Created before. + null, // Tagged after. + null, // Tagged before. + true // Include total count. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Assert total count is included. + $this->assertArrayHasKey('total_count', $result['pagination']); + $this->assertGreaterThan(0, $result['pagination']['total_count']); + } + + /** + * Test that get_tag_subscriptions() returns the expected data + * when a valid Tag ID is specified and the subscription status + * is bounced. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsWithBouncedSubscriberState() + { + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'bounced' // Subscriber state. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Check the correct subscribers were returned. + $this->assertEquals($result['subscribers'][0]['state'], 'bounced'); + } + + + /** + * Test that get_tag_subscriptions() returns the expected data + * when a valid Tag ID is specified and the added_after parameter + * is used. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsWithTaggedAfterParam() + { + $date = new \DateTime('2024-01-01'); + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + null, // Created after. + null, // Created before. + $date // Tagged after. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Check the correct subscribers were returned. + $this->assertGreaterThanOrEqual( + $date->format('Y-m-d'), + date('Y-m-d', strtotime($result['subscribers'][0]['tagged_at'])) + ); + } + + /** + * Test that get_tag_subscriptions() returns the expected data + * when a valid Tag ID is specified and the tagged_before parameter + * is used. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsWithTaggedBeforeParam() + { + $date = new \DateTime('2024-01-01'); + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + null, // Created after. + null, // Created before. + null, // Tagged after. + $date // Tagged before. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Check the correct subscribers were returned. + $this->assertLessThanOrEqual( + $date->format('Y-m-d'), + date('Y-m-d', strtotime($result['subscribers'][0]['tagged_at'])) + ); + } + + /** + * Test that get_tag_subscriptions() returns the expected data + * when a valid Tag ID is specified and the created_after parameter + * is used. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsWithCreatedAfterParam() + { + $date = new \DateTime('2024-01-01'); + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + $date // Created after. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Check the correct subscribers were returned. + $this->assertGreaterThanOrEqual( + $date->format('Y-m-d'), + date('Y-m-d', strtotime($result['subscribers'][0]['created_at'])) + ); + } + + /** + * Test that get_tag_subscriptions() returns the expected data + * when a valid Tag ID is specified and the created_before parameter + * is used. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsWithCreatedBeforeParam() + { + $date = new \DateTime('2024-01-01'); + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + null, // Created after. + $date // Created before. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Check the correct subscribers were returned. + $this->assertLessThanOrEqual( + $date->format('Y-m-d'), + date('Y-m-d', strtotime($result['subscribers'][0]['created_at'])) + ); + } + + /** + * Test that get_tag_subscriptions() returns the expected data + * when a valid Tag ID is specified and pagination parameters + * and per_page limits are specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsPagination() + { + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + null, // Created after. + null, // Created before. + null, // Tagged after. + null, // Tagged before. + false, // Include total count. + '', // After cursor. + '', // Before cursor. + 1 // Per page. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Assert a single subscriber was returned. + $this->assertCount(1, $result['subscribers']); + + // Assert has_previous_page and has_next_page are correct. + $this->assertFalse($result['pagination']['has_previous_page']); + $this->assertTrue($result['pagination']['has_next_page']); + + // Use pagination to fetch next page. + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + null, // Created after. + null, // Created before. + null, // Tagged after. + null, // Tagged before. + false, // Include total count. + $result['pagination']['end_cursor'], // After cursor. + '', // Before cursor. + 1 // Per page. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + + // Assert a single subscriber was returned. + $this->assertCount(1, $result['subscribers']); + + // Assert has_previous_page and has_next_page are correct. + $this->assertTrue($result['pagination']['has_previous_page']); + $this->assertTrue($result['pagination']['has_next_page']); + + // Use pagination to fetch previous page. + $result = $this->api->get_tag_subscriptions( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], // Tag ID. + 'active', // Subscriber state. + null, // Created after. + null, // Created before. + null, // Tagged after. + null, // Tagged before. + false, // Include total count. + '', // After cursor. + $result['pagination']['start_cursor'], // Before cursor. + 1 // Per page. + ); + + // Assert subscribers and pagination exist. + $this->assertDataExists($result, 'subscribers'); + $this->assertPaginationExists($result); + } + + /** + * Test that get_tag_subscriptions() returns a WP_Error when + * an invalid Tag ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetTagSubscriptionsWithInvalidTagID() + { + $result = $this->api->get_tag_subscriptions(12345); + $this->assertInstanceOf(WP_Error::class, $result); + $this->assertEquals($result->get_error_code(), $this->errorCode); + } + /** * Test that get_subscribers() returns the expected data. * From 4a62119f9990007982d4f2c6c8433b86cddbd64c Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 23 Apr 2024 10:25:12 +0100 Subject: [PATCH 2/2] Coding standards --- tests/wpunit/APITest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index e5be5b5..9165c38 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -1980,7 +1980,7 @@ public function testRemoveTagFromSubscriberByEmailWithInvalidEmailAddress() */ public function testGetTagSubscriptions() { - $result = $this->api->get_tag_subscriptions((int) $_ENV['CONVERTKIT_API_TAG_ID']); + $result = $this->api->get_tag_subscriptions( (int) $_ENV['CONVERTKIT_API_TAG_ID']); // Assert subscribers and pagination exist. $this->assertDataExists($result, 'subscribers');