From f43df83c5f26104e7ea2a53aaf468f6838665739 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 22 Apr 2024 16:00:50 +0100 Subject: [PATCH 1/4] Added tag tests --- tests/wpunit/APITest.php | 743 +++++++++++++++++++++++++++------------ 1 file changed, 510 insertions(+), 233 deletions(-) diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index 516c71c..788f50e 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -1005,251 +1005,528 @@ public function testSequenceSubscribeWithInvalidEmail() } /** - * Test that the `get_tags()` function returns expected data. - * - * @since 1.0.0 - */ - public function testGetTags() - { - $this->markTestIncomplete(); - - $result = $this->api->get_tags(); - $this->assertNotInstanceOf(WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertArrayHasKey('id', reset($result)); - $this->assertArrayHasKey('name', reset($result)); - } - - /** - * Test that the `get_tags()` function returns a blank array when no data - * exists on the ConvertKit account. - * - * @since 1.0.0 - */ - public function testGetTagsNoData() - { - $this->markTestIncomplete(); - - $result = $this->api_no_data->get_tags(); - $this->assertNotInstanceOf(WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertCount(0, $result); - } - - /** - * Test that the `tag_subscribe()` function returns expected data - * when valid parameters are provided. - * - * @since 1.0.0 - */ - public function testTagSubscribe() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_subscribe( - $_ENV['CONVERTKIT_API_TAG_ID'], - $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], - 'First', - array( - 'last_name' => 'Last', - 'phone_number' => '123-456-7890', + * Test that get_tags() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetTags() + { + $result = $this->api->get_tags(); + + // Assert sequences and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Check first tag in resultset has expected data. + $tag = $result['tags'][0]; + $this->assertArrayHasKey('id', $tag); + $this->assertArrayHasKey('name', $tag); + $this->assertArrayHasKey('created_at', $tag); + } + + /** + * Test that get_tags() returns the expected data + * when the total count is included. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagsWithTotalCount() + { + $result = $this->api->get_tags(true); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert total count is included. + $this->assertArrayHasKey('total_count', $result['pagination']); + $this->assertGreaterThan(0, $result['pagination']['total_count']); + } + + /** + * Test that get_tags() returns the expected data + * when pagination parameters and per_page limits are specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagsPagination() + { + $result = $this->api->get_tags(false, '', '', 1); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert a single tag was returned. + $this->assertCount(1, $result['tags']); + + // 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_tags(false, $result['pagination']['end_cursor'], '', 1); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert a single subscriber was returned. + $this->assertCount(1, $result['tags']); + + // 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_tags(false, '', $result['pagination']['start_cursor'], 1); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + } + + /** + * Test that create_tag() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testCreateTag() + { + $tagName = 'Tag Test ' . mt_rand(); + + // Add mock handler for this API request, as the API doesn't provide + // a method to delete tags to cleanup the test. + $this->mockResponses( + 201, + 'Created', + wp_json_encode( + 'tag' => [ + 'id' => 12345, + 'name' => $tagName, + 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', + ] ) ); - $this->assertNotInstanceOf(WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertArrayHasKey('subscription', $result); - } - /** - * Test that the `tag_subscribe()` function returns a WP_Error - * when an invalid $tag_id parameter is provided. - * - * @since 1.0.0 - */ - public function testTagSubscribeWithInvalidTagID() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_subscribe(12345, $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], 'First'); - $this->assertInstanceOf(WP_Error::class, $result); + // Send request. + $result = $this->api->create_tag($tagName); + + // Assert response contains correct data. + $this->assertArrayHasKey('id', $result['tag']); + $this->assertArrayHasKey('name', $result['tag']); + $this->assertArrayHasKey('created_at', $result['tag']); + $this->assertEquals($tag['name'], $tagName); + } + + /** + * Test that create_tag() returns a WP_Error when creating + * a blank tag. + * + * @since 1.0.0 + * + * @return void + */ + public function testCreateTagBlank() + { + $result = $this->api->create_tag(''); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('Tag not found: ', $result->get_error_message()); - } - - /** - * Test that the `tag_subscribe()` function returns a WP_Error - * when an empty $tag_id parameter is provided. - * - * @since 1.0.0 - */ - public function testTagSubscribeWithEmptyTagID() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_subscribe('', $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], 'First'); + } + + /** + * Test that create_tag() returns a WP_Error when creating + * a tag that already exists. + * + * @since 1.0.0 + * + * @return void + */ + public function testCreateTagThatExists() + { + $result = $this->api->create_tag($_ENV['CONVERTKIT_API_TAG_NAME']); $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('tag_subscribe(): the tag_id parameter is empty.', $result->get_error_message()); - } - - /** - * Test that the `tag_subscribe()` function returns a WP_Error - * when an empty $email parameter is provided. - * - * @since 1.0.0 - */ - public function testTagSubscribeWithEmptyEmail() - { - $this->markTestIncomplete(); + } + + /** + * Test that create_tags() returns the expected data. + * + * @since 2.0.0 + * + * @return void + */ + public function testCreateTags() + { + $tagNames = [ + 'Tag Test ' . mt_rand(), + 'Tag Test ' . mt_rand(), + ]; + + // Add mock handler for this API request, as the API doesn't provide + // a method to delete tags to cleanup the test. + $this->mockResponses( + 201, + 'Created', + wp_json_encode( + 'tags' => [ + [ + 'id' => 12345, + 'name' => $tagNames[0], + 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', + ], + [ + 'id' => 23456, + 'name' => $tagNames[1], + 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', + ], + ], + 'failures' => [], + ) + ); - $result = $this->api->tag_subscribe($_ENV['CONVERTKIT_API_TAG_ID'], '', 'First'); - $this->assertInstanceOf(WP_Error::class, $result); + $result = $this->api->create_tags($tagNames); + + // Assert no failures. + $this->assertCount(0, $result['failures']); + } + + /** + * Test that create_tags() returns failures when attempting + * to create blank tags. + * + * @since 2.0.0 + * + * @return void + */ + public function testCreateTagsBlank() + { + $result = $this->api->create_tags([ + '', + '', + ]); + + // Assert failures. + $this->assertCount(2, $result['failures']); + } + + /** + * Test that create_tags() returns a WP_Error when creating + * tags that already exists. + * + * @since 2.0.0 + * + * @return void + */ + public function testCreateTagsThatExist() + { + $result = $this->api->create_tags([ + $_ENV['CONVERTKIT_API_TAG_NAME'], + $_ENV['CONVERTKIT_API_TAG_NAME_2'], + ]); + + // Assert failures. + $this->assertCount(2, $result['failures']); + } + + /** + * Test that tag_subscriber_by_email() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testTagSubscriberByEmail() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + $this->assertArrayHasKey('subscriber', $subscriber); + $this->assertArrayHasKey('id', $subscriber['subscriber']); + $this->assertArrayHasKey('tagged_at', $subscriber['subscriber']); + + // Confirm the subscriber is tagged. + $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert correct tag was assigned. + $this->assertEquals($result['tags'][0]['id'], $_ENV['CONVERTKIT_API_TAG_ID']); + } + + /** + * Test that tag_subscriber_by_email() returns a WP_Error when an invalid + * tag is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberByEmailWithInvalidTagID() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + $result = $this->api->tag_subscriber_by_email( + 12345, + $emailAddress + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('tag_subscribe(): the email parameter is empty.', $result->get_error_message()); - } - - /** - * Test that the `tag_subscribe()` function returns a WP_Error - * when the $email parameter only consists of spaces. - * - * @since 1.0.0 - */ - public function testTagSubscribeWithSpacesInEmail() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_subscribe($_ENV['CONVERTKIT_API_TAG_ID'], ' ', 'First'); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that tag_subscriber_by_email() returns a WP_Error when an invalid + * email address is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberByEmailWithInvalidEmailAddress() + { + $result = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + 'not-an-email-address' + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('tag_subscribe(): the email parameter is empty.', $result->get_error_message()); - } - - /** - * Test that the `tag_subscribe()` function returns a WP_Error - * when an invalid $email parameter is provided. - * - * @since 1.0.0 - */ - public function testTagSubscribeWithInvalidEmail() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_subscribe($_ENV['CONVERTKIT_API_TAG_ID'], 'invalid-email-address', 'First'); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that tag_subscriber() returns the expected data. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriber() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $subscriber = $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $result = $this->api->tag_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $subscriber->subscriber->id, + ); + $this->assertArrayHasKey('subscriber', $result); + $this->assertArrayHasKey('id', $result['subscriber']); + $this->assertArrayHasKey('tagged_at', $result['subscriber']); + + // Confirm the subscriber is tagged. + $result = $this->api->get_subscriber_tags($result->subscriber->id); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert correct tag was assigned. + $this->assertEquals($result['tags'][0]['id'], $_ENV['CONVERTKIT_API_TAG_ID']); + } + + /** + * Test that tag_subscriber() returns a WP_Error when an invalid + * sequence ID is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberWithInvalidTagID() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $subscriber = $this->api->create_subscriber($emailAddress); + + $result = $this->api->tag_subscriber( + 12345, + $subscriber['subscriber']['id'] + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('Error updating subscriber: Email address is invalid', $result->get_error_message()); - } - - - /** - * Test that the `tag_unsubscribe()` function returns expected data - * when valid parameters are provided. - * - * @since 1.4.0 - */ - public function testTagUnsubscribe() - { - $this->markTestIncomplete(); - - // Subscribe the email address to the tag. - $result = $this->api->tag_subscribe( - $_ENV['CONVERTKIT_API_TAG_ID'], - $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'] - ); - - // Unsubscribe the email address from the tag. - $result = $this->api->tag_unsubscribe( - $_ENV['CONVERTKIT_API_TAG_ID'], - $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'] - ); - - $this->assertNotInstanceOf(WP_Error::class, $result); - $this->assertIsArray($result); - $this->assertArrayHasKey('id', $result); - $this->assertArrayHasKey('name', $result); - $this->assertArrayHasKey('created_at', $result); - $this->assertEquals($result['name'], $_ENV['CONVERTKIT_API_TAG_NAME']); - } - - /** - * Test that the `tag_unsubscribe()` function returns a WP_Error - * when an invalid $tag_id parameter is provided. - * - * @since 1.4.0 - */ - public function testTagUnsubscribeWithInvalidTagID() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_unsubscribe(12345, $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL']); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that tag_subscriber() returns a WP_Error when an invalid + * email address is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberWithInvalidSubscriberID() + { + $result = $this->api->tag_subscriber( + $_ENV['CONVERTKIT_API_TAG_ID'], + 12345 + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('Not Found: The entity you were trying to find doesn\'t exist', $result->get_error_message()); - } - - /** - * Test that the `tag_unsubscribe()` function returns a WP_Error - * when an empty $tag_id parameter is provided. - * - * @since 1.4.0 - */ - public function testTagUnsubscribeWithEmptyTagID() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_unsubscribe('', $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL']); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() works. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriber() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + + // Remove tag from subscriber. + $result = $this->api->remove_tag_from_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $subscriber['subscriber']['id'] + ); + + // Confirm that the subscriber no longer has the tag. + $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); + $this->assertIsArray($result['tags']); + $this->assertCount(0, $result['tags']); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * tag ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberWithInvalidTagID() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + + // Remove tag from subscriber. + $result = $this->api->remove_tag_from_subscriber( + 12345, + $subscriber->subscriber->id + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('tag_unsubscribe(): the tag_id parameter is empty.', $result->get_error_message()); - } - - /** - * Test that the `tag_unsubscribe()` function returns a WP_Error - * when an empty $email parameter is provided. - * - * @since 1.4.0 - */ - public function testTagUnsubscribeWithEmptyEmail() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_unsubscribe($_ENV['CONVERTKIT_API_TAG_ID'], ''); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * subscriber ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberWithInvalidSubscriberID() + { + $result = $this->api->remove_tag_from_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + 12345 + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('tag_unsubscribe(): the email parameter is empty.', $result->get_error_message()); - } - - /** - * Test that the `tag_unsubscribe()` function returns a WP_Error - * when the $email parameter only consists of spaces. - * - * @since 1.4.0 - */ - public function testTagUnsubscribeWithSpacesInEmail() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_unsubscribe($_ENV['CONVERTKIT_API_TAG_ID'], ' '); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() works. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberByEmail() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + + // Remove tag from subscriber. + $result = $this->api->remove_tag_from_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $subscriber['subscriber']['id'] + ); + + // Confirm that the subscriber no longer has the tag. + $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); + $this->assertIsArray($result['tags']); + $this->assertCount(0, $result['tags']); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * tag ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberByEmailWithInvalidTagID() + { + $result = $this->api->remove_tag_from_subscriber_by_email( + 12345, + $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'] + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('tag_unsubscribe(): the email parameter is empty.', $result->get_error_message()); - } - - /** - * Test that the `tag_unsubscribe()` function returns a WP_Error - * when an invalid $email parameter is provided. - * - * @since 1.4.0 - */ - public function testTagUnsubscribeWithInvalidEmail() - { - $this->markTestIncomplete(); - - $result = $this->api->tag_unsubscribe($_ENV['CONVERTKIT_API_TAG_ID'], 'invalid-email-address'); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * email address is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberByEmailWithInvalidEmailAddress() + { + $result = $this->api->remove_tag_from_subscriber_by_email( + $_ENV['CONVERTKIT_API_TAG_ID'], + 'not-an-email-address' + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - $this->assertEquals('tag_unsubscribe(): the email parameter is not a valid email address.', $result->get_error_message()); - } + } /** * Test that get_subscribers() returns the expected data. @@ -2770,7 +3047,7 @@ public function testCreateCustomField() } /** - * Test that create_custom_field() throws a ClientException when a blank + * Test that create_custom_field() returns a WP_Error when a blank * label is specified. * * @since 2.0.0 @@ -2852,7 +3129,7 @@ public function testUpdateCustomField() } /** - * Test that update_custom_field() throws a ClientException when an + * Test that update_custom_field() returns a WP_Error when an * invalid custom field ID is specified. * * @since 2.0.0 @@ -2897,7 +3174,7 @@ public function testDeleteCustomField() } /** - * Test that delete_custom_field() throws a ClientException when an + * Test that delete_custom_field() returns a WP_Error when an * invalid custom field ID is specified. * * @since 2.0.0 From c23bbabc9ea7c85b7d737efea4a8c9414f40fff0 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 22 Apr 2024 16:08:18 +0100 Subject: [PATCH 2/4] Coding standards --- tests/wpunit/APITest.php | 1006 +++++++++++++++++++------------------- 1 file changed, 507 insertions(+), 499 deletions(-) diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index 788f50e..65f0c20 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -1005,528 +1005,536 @@ public function testSequenceSubscribeWithInvalidEmail() } /** - * Test that get_tags() returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testGetTags() - { - $result = $this->api->get_tags(); - - // Assert sequences and pagination exist. - $this->assertDataExists($result, 'tags'); - $this->assertPaginationExists($result); - - // Check first tag in resultset has expected data. - $tag = $result['tags'][0]; - $this->assertArrayHasKey('id', $tag); - $this->assertArrayHasKey('name', $tag); - $this->assertArrayHasKey('created_at', $tag); - } - - /** - * Test that get_tags() returns the expected data - * when the total count is included. - * - * @since 2.0.0 - * - * @return void - */ - public function testGetTagsWithTotalCount() - { - $result = $this->api->get_tags(true); - - // Assert tags and pagination exist. - $this->assertDataExists($result, 'tags'); - $this->assertPaginationExists($result); - - // Assert total count is included. - $this->assertArrayHasKey('total_count', $result['pagination']); - $this->assertGreaterThan(0, $result['pagination']['total_count']); - } - - /** - * Test that get_tags() returns the expected data - * when pagination parameters and per_page limits are specified. - * - * @since 2.0.0 - * - * @return void - */ - public function testGetTagsPagination() - { - $result = $this->api->get_tags(false, '', '', 1); - - // Assert tags and pagination exist. - $this->assertDataExists($result, 'tags'); - $this->assertPaginationExists($result); - - // Assert a single tag was returned. - $this->assertCount(1, $result['tags']); - - // 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_tags(false, $result['pagination']['end_cursor'], '', 1); - - // Assert tags and pagination exist. - $this->assertDataExists($result, 'tags'); - $this->assertPaginationExists($result); - - // Assert a single subscriber was returned. - $this->assertCount(1, $result['tags']); - - // 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_tags(false, '', $result['pagination']['start_cursor'], 1); - - // Assert tags and pagination exist. - $this->assertDataExists($result, 'tags'); - $this->assertPaginationExists($result); - } - - /** - * Test that create_tag() returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testCreateTag() - { - $tagName = 'Tag Test ' . mt_rand(); - - // Add mock handler for this API request, as the API doesn't provide - // a method to delete tags to cleanup the test. - $this->mockResponses( + * Test that get_tags() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetTags() + { + $result = $this->api->get_tags(); + + // Assert sequences and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Check first tag in resultset has expected data. + $tag = $result['tags'][0]; + $this->assertArrayHasKey('id', $tag); + $this->assertArrayHasKey('name', $tag); + $this->assertArrayHasKey('created_at', $tag); + } + + /** + * Test that get_tags() returns the expected data + * when the total count is included. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagsWithTotalCount() + { + $result = $this->api->get_tags(true); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert total count is included. + $this->assertArrayHasKey('total_count', $result['pagination']); + $this->assertGreaterThan(0, $result['pagination']['total_count']); + } + + /** + * Test that get_tags() returns the expected data + * when pagination parameters and per_page limits are specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetTagsPagination() + { + $result = $this->api->get_tags(false, '', '', 1); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert a single tag was returned. + $this->assertCount(1, $result['tags']); + + // 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_tags(false, $result['pagination']['end_cursor'], '', 1); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert a single subscriber was returned. + $this->assertCount(1, $result['tags']); + + // 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_tags(false, '', $result['pagination']['start_cursor'], 1); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + } + + /** + * Test that create_tag() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testCreateTag() + { + $tagName = 'Tag Test ' . rand_mt_rand(); + + // Add mock handler for this API request, as the API doesn't provide + // a method to delete tags to cleanup the test. + $this->mockResponses( 201, 'Created', wp_json_encode( - 'tag' => [ - 'id' => 12345, - 'name' => $tagName, - 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', - ] + array( + 'tag' => array( + 'id' => 12345, + 'name' => $tagName, + 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', + ), + ) ) ); - // Send request. - $result = $this->api->create_tag($tagName); - - // Assert response contains correct data. - $this->assertArrayHasKey('id', $result['tag']); - $this->assertArrayHasKey('name', $result['tag']); - $this->assertArrayHasKey('created_at', $result['tag']); - $this->assertEquals($tag['name'], $tagName); - } - - /** - * Test that create_tag() returns a WP_Error when creating - * a blank tag. - * - * @since 1.0.0 - * - * @return void - */ - public function testCreateTagBlank() - { - $result = $this->api->create_tag(''); - $this->assertInstanceOf(WP_Error::class, $result); + // Send request. + $result = $this->api->create_tag($tagName); + + // Assert response contains correct data. + $this->assertArrayHasKey('id', $result['tag']); + $this->assertArrayHasKey('name', $result['tag']); + $this->assertArrayHasKey('created_at', $result['tag']); + $this->assertEquals($tag['name'], $tagName); + } + + /** + * Test that create_tag() returns a WP_Error when creating + * a blank tag. + * + * @since 1.0.0 + * + * @return void + */ + public function testCreateTagBlank() + { + $result = $this->api->create_tag(''); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that create_tag() returns a WP_Error when creating - * a tag that already exists. - * - * @since 1.0.0 - * - * @return void - */ - public function testCreateTagThatExists() - { - $result = $this->api->create_tag($_ENV['CONVERTKIT_API_TAG_NAME']); + } + + /** + * Test that create_tag() returns a WP_Error when creating + * a tag that already exists. + * + * @since 1.0.0 + * + * @return void + */ + public function testCreateTagThatExists() + { + $result = $this->api->create_tag($_ENV['CONVERTKIT_API_TAG_NAME']); $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that create_tags() returns the expected data. - * - * @since 2.0.0 - * - * @return void - */ - public function testCreateTags() - { - $tagNames = [ - 'Tag Test ' . mt_rand(), - 'Tag Test ' . mt_rand(), - ]; - - // Add mock handler for this API request, as the API doesn't provide - // a method to delete tags to cleanup the test. - $this->mockResponses( + } + + /** + * Test that create_tags() returns the expected data. + * + * @since 2.0.0 + * + * @return void + */ + public function testCreateTags() + { + $tagNames = [ + 'Tag Test ' . rand_mt_rand(), + 'Tag Test ' . rand_mt_rand(), + ]; + + // Add mock handler for this API request, as the API doesn't provide + // a method to delete tags to cleanup the test. + $this->mockResponses( 201, 'Created', wp_json_encode( - 'tags' => [ - [ - 'id' => 12345, - 'name' => $tagNames[0], - 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', - ], - [ - 'id' => 23456, - 'name' => $tagNames[1], - 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', - ], - ], - 'failures' => [], + array( + 'tags' => array( + array( + 'id' => 12345, + 'name' => $tagNames[0], + 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', + ), + array( + 'id' => 23456, + 'name' => $tagNames[1], + 'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z', + ), + ), + 'failures' => array(), + ) ) ); - $result = $this->api->create_tags($tagNames); - - // Assert no failures. - $this->assertCount(0, $result['failures']); - } - - /** - * Test that create_tags() returns failures when attempting - * to create blank tags. - * - * @since 2.0.0 - * - * @return void - */ - public function testCreateTagsBlank() - { - $result = $this->api->create_tags([ - '', - '', - ]); - - // Assert failures. - $this->assertCount(2, $result['failures']); - } - - /** - * Test that create_tags() returns a WP_Error when creating - * tags that already exists. - * - * @since 2.0.0 - * - * @return void - */ - public function testCreateTagsThatExist() - { - $result = $this->api->create_tags([ - $_ENV['CONVERTKIT_API_TAG_NAME'], - $_ENV['CONVERTKIT_API_TAG_NAME_2'], - ]); - - // Assert failures. - $this->assertCount(2, $result['failures']); - } - - /** - * Test that tag_subscriber_by_email() returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testTagSubscriberByEmail() - { - // Create subscriber. - $emailAddress = $this->generateEmailAddress(); - $this->api->create_subscriber($emailAddress); - - // Tag subscriber by email. - $subscriber = $this->api->tag_subscriber_by_email( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $emailAddress, - ); - $this->assertArrayHasKey('subscriber', $subscriber); - $this->assertArrayHasKey('id', $subscriber['subscriber']); - $this->assertArrayHasKey('tagged_at', $subscriber['subscriber']); - - // Confirm the subscriber is tagged. - $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); - - // Assert tags and pagination exist. - $this->assertDataExists($result, 'tags'); - $this->assertPaginationExists($result); - - // Assert correct tag was assigned. - $this->assertEquals($result['tags'][0]['id'], $_ENV['CONVERTKIT_API_TAG_ID']); - } - - /** - * Test that tag_subscriber_by_email() returns a WP_Error when an invalid - * tag is specified. - * - * @since 2.0.0 - * - * @return void - */ - public function testTagSubscriberByEmailWithInvalidTagID() - { - // Create subscriber. - $emailAddress = $this->generateEmailAddress(); - $this->api->create_subscriber($emailAddress); - - $result = $this->api->tag_subscriber_by_email( - 12345, - $emailAddress - ); - $this->assertInstanceOf(WP_Error::class, $result); + $result = $this->api->create_tags($tagNames); + + // Assert no failures. + $this->assertCount(0, $result['failures']); + } + + /** + * Test that create_tags() returns failures when attempting + * to create blank tags. + * + * @since 2.0.0 + * + * @return void + */ + public function testCreateTagsBlank() + { + $result = $this->api->create_tags( + [ + '', + '', + ] + ); + + // Assert failures. + $this->assertCount(2, $result['failures']); + } + + /** + * Test that create_tags() returns a WP_Error when creating + * tags that already exists. + * + * @since 2.0.0 + * + * @return void + */ + public function testCreateTagsThatExist() + { + $result = $this->api->create_tags( + [ + $_ENV['CONVERTKIT_API_TAG_NAME'], + $_ENV['CONVERTKIT_API_TAG_NAME_2'], + ] + ); + + // Assert failures. + $this->assertCount(2, $result['failures']); + } + + /** + * Test that tag_subscriber_by_email() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testTagSubscriberByEmail() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + $this->assertArrayHasKey('subscriber', $subscriber); + $this->assertArrayHasKey('id', $subscriber['subscriber']); + $this->assertArrayHasKey('tagged_at', $subscriber['subscriber']); + + // Confirm the subscriber is tagged. + $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert correct tag was assigned. + $this->assertEquals($result['tags'][0]['id'], $_ENV['CONVERTKIT_API_TAG_ID']); + } + + /** + * Test that tag_subscriber_by_email() returns a WP_Error when an invalid + * tag is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberByEmailWithInvalidTagID() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + $result = $this->api->tag_subscriber_by_email( + 12345, + $emailAddress + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that tag_subscriber_by_email() returns a WP_Error when an invalid - * email address is specified. - * - * @since 2.0.0 - * - * @return void - */ - public function testTagSubscriberByEmailWithInvalidEmailAddress() - { - $result = $this->api->tag_subscriber_by_email( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - 'not-an-email-address' - ); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that tag_subscriber_by_email() returns a WP_Error when an invalid + * email address is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberByEmailWithInvalidEmailAddress() + { + $result = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + 'not-an-email-address' + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that tag_subscriber() returns the expected data. - * - * @since 2.0.0 - * - * @return void - */ - public function testTagSubscriber() - { - // Create subscriber. - $emailAddress = $this->generateEmailAddress(); - $subscriber = $this->api->create_subscriber($emailAddress); - - // Tag subscriber by email. - $result = $this->api->tag_subscriber( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $subscriber->subscriber->id, - ); - $this->assertArrayHasKey('subscriber', $result); - $this->assertArrayHasKey('id', $result['subscriber']); - $this->assertArrayHasKey('tagged_at', $result['subscriber']); - - // Confirm the subscriber is tagged. - $result = $this->api->get_subscriber_tags($result->subscriber->id); - - // Assert tags and pagination exist. - $this->assertDataExists($result, 'tags'); - $this->assertPaginationExists($result); - - // Assert correct tag was assigned. - $this->assertEquals($result['tags'][0]['id'], $_ENV['CONVERTKIT_API_TAG_ID']); - } - - /** - * Test that tag_subscriber() returns a WP_Error when an invalid - * sequence ID is specified. - * - * @since 2.0.0 - * - * @return void - */ - public function testTagSubscriberWithInvalidTagID() - { - // Create subscriber. - $emailAddress = $this->generateEmailAddress(); - $subscriber = $this->api->create_subscriber($emailAddress); - - $result = $this->api->tag_subscriber( - 12345, - $subscriber['subscriber']['id'] - ); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that tag_subscriber() returns the expected data. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriber() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $subscriber = $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $result = $this->api->tag_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $subscriber->subscriber->id, + ); + $this->assertArrayHasKey('subscriber', $result); + $this->assertArrayHasKey('id', $result['subscriber']); + $this->assertArrayHasKey('tagged_at', $result['subscriber']); + + // Confirm the subscriber is tagged. + $result = $this->api->get_subscriber_tags($result->subscriber->id); + + // Assert tags and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert correct tag was assigned. + $this->assertEquals($result['tags'][0]['id'], $_ENV['CONVERTKIT_API_TAG_ID']); + } + + /** + * Test that tag_subscriber() returns a WP_Error when an invalid + * sequence ID is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberWithInvalidTagID() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $subscriber = $this->api->create_subscriber($emailAddress); + + $result = $this->api->tag_subscriber( + 12345, + $subscriber['subscriber']['id'] + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that tag_subscriber() returns a WP_Error when an invalid - * email address is specified. - * - * @since 2.0.0 - * - * @return void - */ - public function testTagSubscriberWithInvalidSubscriberID() - { - $result = $this->api->tag_subscriber( - $_ENV['CONVERTKIT_API_TAG_ID'], - 12345 - ); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that tag_subscriber() returns a WP_Error when an invalid + * email address is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testTagSubscriberWithInvalidSubscriberID() + { + $result = $this->api->tag_subscriber( + $_ENV['CONVERTKIT_API_TAG_ID'], + 12345 + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that remove_tag_from_subscriber() works. - * - * @since 1.0.0 - * - * @return void - */ - public function testRemoveTagFromSubscriber() - { - // Create subscriber. - $emailAddress = $this->generateEmailAddress(); - $this->api->create_subscriber($emailAddress); - - // Tag subscriber by email. - $subscriber = $this->api->tag_subscriber_by_email( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $emailAddress, - ); - - // Remove tag from subscriber. - $result = $this->api->remove_tag_from_subscriber( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $subscriber['subscriber']['id'] - ); - - // Confirm that the subscriber no longer has the tag. - $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); - $this->assertIsArray($result['tags']); - $this->assertCount(0, $result['tags']); - } - - /** - * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid - * tag ID is specified. - * - * @since 1.0.0 - * - * @return void - */ - public function testRemoveTagFromSubscriberWithInvalidTagID() - { - // Create subscriber. - $emailAddress = $this->generateEmailAddress(); - $this->api->create_subscriber($emailAddress); - - // Tag subscriber by email. - $subscriber = $this->api->tag_subscriber_by_email( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $emailAddress, - ); - - // Remove tag from subscriber. - $result = $this->api->remove_tag_from_subscriber( - 12345, - $subscriber->subscriber->id - ); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() works. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriber() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + + // Remove tag from subscriber. + $result = $this->api->remove_tag_from_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $subscriber['subscriber']['id'] + ); + + // Confirm that the subscriber no longer has the tag. + $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); + $this->assertIsArray($result['tags']); + $this->assertCount(0, $result['tags']); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * tag ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberWithInvalidTagID() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + + // Remove tag from subscriber. + $result = $this->api->remove_tag_from_subscriber( + 12345, + $subscriber->subscriber->id + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid - * subscriber ID is specified. - * - * @since 1.0.0 - * - * @return void - */ - public function testRemoveTagFromSubscriberWithInvalidSubscriberID() - { - $result = $this->api->remove_tag_from_subscriber( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - 12345 - ); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * subscriber ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberWithInvalidSubscriberID() + { + $result = $this->api->remove_tag_from_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + 12345 + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that remove_tag_from_subscriber() works. - * - * @since 1.0.0 - * - * @return void - */ - public function testRemoveTagFromSubscriberByEmail() - { - // Create subscriber. - $emailAddress = $this->generateEmailAddress(); - $this->api->create_subscriber($emailAddress); - - // Tag subscriber by email. - $subscriber = $this->api->tag_subscriber_by_email( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $emailAddress, - ); - - // Remove tag from subscriber. - $result = $this->api->remove_tag_from_subscriber( - (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $subscriber['subscriber']['id'] - ); - - // Confirm that the subscriber no longer has the tag. - $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); - $this->assertIsArray($result['tags']); - $this->assertCount(0, $result['tags']); - } - - /** - * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid - * tag ID is specified. - * - * @since 1.0.0 - * - * @return void - */ - public function testRemoveTagFromSubscriberByEmailWithInvalidTagID() - { - $result = $this->api->remove_tag_from_subscriber_by_email( - 12345, - $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'] - ); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() works. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberByEmail() + { + // Create subscriber. + $emailAddress = $this->generateEmailAddress(); + $this->api->create_subscriber($emailAddress); + + // Tag subscriber by email. + $subscriber = $this->api->tag_subscriber_by_email( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $emailAddress, + ); + + // Remove tag from subscriber. + $result = $this->api->remove_tag_from_subscriber( + (int) $_ENV['CONVERTKIT_API_TAG_ID'], + $subscriber['subscriber']['id'] + ); + + // Confirm that the subscriber no longer has the tag. + $result = $this->api->get_subscriber_tags($subscriber['subscriber']['id']); + $this->assertIsArray($result['tags']); + $this->assertCount(0, $result['tags']); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * tag ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberByEmailWithInvalidTagID() + { + $result = $this->api->remove_tag_from_subscriber_by_email( + 12345, + $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'] + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } - - /** - * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid - * email address is specified. - * - * @since 2.0.0 - * - * @return void - */ - public function testRemoveTagFromSubscriberByEmailWithInvalidEmailAddress() - { - $result = $this->api->remove_tag_from_subscriber_by_email( - $_ENV['CONVERTKIT_API_TAG_ID'], - 'not-an-email-address' - ); - $this->assertInstanceOf(WP_Error::class, $result); + } + + /** + * Test that remove_tag_from_subscriber() returns a WP_Error when an invalid + * email address is specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testRemoveTagFromSubscriberByEmailWithInvalidEmailAddress() + { + $result = $this->api->remove_tag_from_subscriber_by_email( + $_ENV['CONVERTKIT_API_TAG_ID'], + 'not-an-email-address' + ); + $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode); - } + } /** * Test that get_subscribers() returns the expected data. From 90f93e7b56f3c57d0d0f09d32d856a412d2c40d4 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 23 Apr 2024 10:06:01 +0100 Subject: [PATCH 3/4] Fix tests --- .env.dist.testing | 2 ++ .env.example | 2 ++ tests/wpunit/APITest.php | 8 ++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.env.dist.testing b/.env.dist.testing index e1cc30c..d2a1573 100644 --- a/.env.dist.testing +++ b/.env.dist.testing @@ -30,6 +30,8 @@ CONVERTKIT_API_PRODUCT_ID="36377" CONVERTKIT_API_SEQUENCE_ID="1030824" CONVERTKIT_API_TAG_NAME="wordpress" CONVERTKIT_API_TAG_ID="2744672" +CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1" +CONVERTKIT_API_TAG_ID_2="2907192" CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com" CONVERTKIT_API_SUBSCRIBER_ID="1579118532" CONVERTKIT_API_RECOMMENDATIONS_JS="https://cheerful-architect-3237.ck.page/WnaDZ370gtgOq750dwOl-recommendations.js" diff --git a/.env.example b/.env.example index ed515ed..9b7ae6f 100644 --- a/.env.example +++ b/.env.example @@ -38,6 +38,8 @@ CONVERTKIT_API_PRODUCT_ID="36377" CONVERTKIT_API_SEQUENCE_ID="1030824" CONVERTKIT_API_TAG_NAME="wordpress" CONVERTKIT_API_TAG_ID="2744672" +CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1" +CONVERTKIT_API_TAG_ID_2="2907192" CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com" CONVERTKIT_API_SUBSCRIBER_ID="1579118532" CONVERTKIT_API_SIGNED_SUBSCRIBER_ID= diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index 52f1a4c..1b66370 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -1535,7 +1535,7 @@ public function testGetTagsPagination() */ public function testCreateTag() { - $tagName = 'Tag Test ' . rand_mt_rand(); + $tagName = 'Tag Test ' . wp_rand(); // Add mock handler for this API request, as the API doesn't provide // a method to delete tags to cleanup the test. @@ -1560,7 +1560,7 @@ public function testCreateTag() $this->assertArrayHasKey('id', $result['tag']); $this->assertArrayHasKey('name', $result['tag']); $this->assertArrayHasKey('created_at', $result['tag']); - $this->assertEquals($tag['name'], $tagName); + $this->assertEquals($result['tag']['name'], $tagName); } /** @@ -1603,8 +1603,8 @@ public function testCreateTagThatExists() public function testCreateTags() { $tagNames = [ - 'Tag Test ' . rand_mt_rand(), - 'Tag Test ' . rand_mt_rand(), + 'Tag Test ' . wp_rand(), + 'Tag Test ' . wp_rand(), ]; // Add mock handler for this API request, as the API doesn't provide From 823e0a1cd1d5daba729151801183be47e1623af3 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 23 Apr 2024 10:10:00 +0100 Subject: [PATCH 4/4] Fix tag subscriber tests --- tests/wpunit/APITest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index 1b66370..2dfa0a2 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -1768,14 +1768,14 @@ public function testTagSubscriber() // Tag subscriber by email. $result = $this->api->tag_subscriber( (int) $_ENV['CONVERTKIT_API_TAG_ID'], - $subscriber->subscriber->id, + $subscriber['subscriber']['id'], ); $this->assertArrayHasKey('subscriber', $result); $this->assertArrayHasKey('id', $result['subscriber']); $this->assertArrayHasKey('tagged_at', $result['subscriber']); // Confirm the subscriber is tagged. - $result = $this->api->get_subscriber_tags($result->subscriber->id); + $result = $this->api->get_subscriber_tags($result['subscriber']['id']); // Assert tags and pagination exist. $this->assertDataExists($result, 'tags'); @@ -1879,7 +1879,7 @@ public function testRemoveTagFromSubscriberWithInvalidTagID() // Remove tag from subscriber. $result = $this->api->remove_tag_from_subscriber( 12345, - $subscriber->subscriber->id + $subscriber['subscriber']['id'] ); $this->assertInstanceOf(WP_Error::class, $result); $this->assertEquals($result->get_error_code(), $this->errorCode);