From bfac5234cf9f3e639b38b38058fb269979002ba3 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 29 Mar 2024 16:16:58 +0000 Subject: [PATCH 01/10] Update `get_forms` and `get_landing_pages` methods --- src/ConvertKit_API.php | 210 ++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 127 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 07ca60d..914ee24 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -386,31 +386,102 @@ public function get_growth_stats(\DateTime $starting = null, \DateTime $ending = } /** - * Gets all forms. - * + * Get forms. + * * @since 1.0.0 * + * @param boolean $include_total_count To include the total count of records in the response, use true. + * @param string $after_cursor Return results after the given pagination cursor. + * @param string $before_cursor Return results before the given pagination cursor. + * @param integer $per_page Number of results to return. + * * @see https://developers.convertkit.com/v4.html#convertkit-api-forms * - * @return false|mixed + * @return false|array */ - public function get_forms() - { - return $this->get_resources('forms'); + public function get_forms( + bool $include_archived = false, + bool $include_total_count = false, + string $after_cursor = '', + string $before_cursor = '', + int $per_page = 100 + ) { + // Get forms and landing pages. + $result = $this->get( + endpoint: 'forms', + args: $this->build_total_count_and_pagination_params( + include_total_count: $include_total_count, + after_cursor: $after_cursor, + before_cursor: $before_cursor, + per_page: $per_page + ) + ); + + // Exclude archived and landing pages. + foreach ($result->forms as $index => $form) { + // Exclude hosted forms i.e. landing pages. + if ($form->type === 'hosted') { + unset($result->forms[ $index ] ); + continue; + } + + // Exclude archived forms, if required. + if (!$include_archived && isset($form->archived) && $form->archived) { + unset($result->forms[ $index ] ); + continue; + } + } + + return $result; } /** - * Gets all landing pages. - * + * Get landing pages. + * * @since 1.0.0 * + * @param boolean $include_total_count To include the total count of records in the response, use true. + * @param string $after_cursor Return results after the given pagination cursor. + * @param string $before_cursor Return results before the given pagination cursor. + * @param integer $per_page Number of results to return. + * * @see https://developers.convertkit.com/v4.html#convertkit-api-forms * - * @return false|mixed + * @return false|array */ - public function get_landing_pages() - { - return $this->get_resources('landing_pages'); + public function get_landing_pages( + bool $include_archived = false, + bool $include_total_count = false, + string $after_cursor = '', + string $before_cursor = '', + int $per_page = 100 + ) { + $result = $this->get( + endpoint: 'forms', + args: $this->build_total_count_and_pagination_params( + include_total_count: $include_total_count, + after_cursor: $after_cursor, + before_cursor: $before_cursor, + per_page: $per_page + ) + ); + + // Exclude archived and non-hosted forms. + foreach ($result->forms as $index => $form) { + // Exclude non-hosted forms i.e. forms. + if ($form->type !== 'hosted') { + unset($result->forms[ $index ] ); + continue; + } + + // Exclude archived forms, if required. + if (!$include_archived && isset($form->archived) && $form->archived) { + unset($result->forms[ $index ] ); + continue; + } + } + + return $result; } /** @@ -883,121 +954,6 @@ public function get_email_templates( ); } - /** - * Gets a resource index - * Possible resources: forms, landing_pages, subscription_forms, tags - * - * GET /{$resource}/ - * - * @param string $resource Resource type. - * - * @throws \InvalidArgumentException If the resource argument is not a supported resource type. - * - * @return array API response - */ - public function get_resources(string $resource) - { - // Assign the resource to the request variable. - $request = $resource; - - // Landing pages are included in the /forms endpoint. - if ($resource === 'landing_pages') { - $request = 'forms'; - } - - // Fetch resources. - $resources = $this->get($request); - - $this->create_log(sprintf('%s response %s', $resource, json_encode($resources))); - - // Return a blank array if no resources exist. - if (!$resources) { - $this->create_log('No resources'); - return []; - } - - // Build array of resources. - $_resource = []; - switch ($resource) { - // Forms. - case 'forms': - // Bail if no forms are set. - if (!isset($resources->forms)) { - $this->create_log('No form resources'); - return []; - } - - // Build array of forms. - foreach ($resources->forms as $form) { - // Exclude archived forms. - if (isset($form->archived) && $form->archived) { - continue; - } - - // Exclude hosted forms. - if ($form->type === 'hosted') { - continue; - } - - $_resource[] = $form; - } - break; - - // Landing Pages. - case 'landing_pages': - // Bail if no landing pages are set. - if (!isset($resources->forms)) { - $this->create_log('No landing page resources'); - return []; - } - - foreach ($resources->forms as $form) { - // Exclude archived landing pages. - if (isset($form->archived) && $form->archived) { - continue; - } - - // Exclude non-hosted (i.e. forms). - if ($form->type !== 'hosted') { - continue; - } - - $_resource[] = $form; - } - break; - - // Subscription Forms. - case 'subscription_forms': - // Exclude archived subscription forms. - foreach ($resources as $mapping) { - if (isset($mapping->archived) && $mapping->archived) { - continue; - } - - $_resource[$mapping->id] = $mapping->form_id; - } - break; - - // Tags. - case 'tags': - // Bail if no tags are set. - if (!isset($resources->tags)) { - $this->create_log('No tag resources'); - return []; - } - - foreach ($resources->tags as $tag) { - $_resource[] = $tag; - } - break; - - default: - throw new \InvalidArgumentException('An unsupported resource was specified.'); - }//end switch - - return $_resource; - } - /** * List subscribers. * From 6e5a0b01c4d015fc5e1ace047aa372921cc45c79 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 29 Mar 2024 16:17:01 +0000 Subject: [PATCH 02/10] Started tests --- tests/ConvertKitAPITest.php | 69 ++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index dc146c0..c30fccb 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -697,20 +697,33 @@ public function testGetGrowthStatsWithEndDate() public function testGetForms() { $result = $this->api->get_forms(); - $this->assertIsArray($result); - // Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10. - $form = get_object_vars($result[0]); - $this->assertArrayHasKey('id', $form); - $this->assertArrayHasKey('name', $form); - $this->assertArrayHasKey('created_at', $form); - $this->assertArrayHasKey('type', $form); - $this->assertArrayHasKey('format', $form); - $this->assertArrayHasKey('embed_js', $form); - $this->assertArrayHasKey('embed_url', $form); - $this->assertArrayHasKey('archived', $form); + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Iterate through each form, confirming no landing pages were included. + foreach ($result->forms as $form) { + $form = get_object_vars($form); + + // Assert shape of object is valid. + $this->assertArrayHasKey('id', $form); + $this->assertArrayHasKey('name', $form); + $this->assertArrayHasKey('created_at', $form); + $this->assertArrayHasKey('type', $form); + $this->assertArrayHasKey('format', $form); + $this->assertArrayHasKey('embed_js', $form); + $this->assertArrayHasKey('embed_url', $form); + $this->assertArrayHasKey('archived', $form); + + // Assert form isn't archived or a landing page. + $this->assertFalse($form['archived']); + $this->assertEquals($form['type'], 'embed'); + } } + + /** * Test that get_landing_pages() returns the expected data. * @@ -721,19 +734,29 @@ public function testGetForms() public function testGetLandingPages() { $result = $this->api->get_landing_pages(); - $this->assertIsArray($result); - // Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10. - $landingPage = get_object_vars($result[0]); - $this->assertArrayHasKey('id', $landingPage); - $this->assertArrayHasKey('name', $landingPage); - $this->assertArrayHasKey('created_at', $landingPage); - $this->assertArrayHasKey('type', $landingPage); - $this->assertEquals('hosted', $landingPage['type']); - $this->assertArrayHasKey('format', $landingPage); - $this->assertArrayHasKey('embed_js', $landingPage); - $this->assertArrayHasKey('embed_url', $landingPage); - $this->assertArrayHasKey('archived', $landingPage); + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Iterate through each landing page, confirming no forms were included. + foreach ($result->forms as $form) { + $form = get_object_vars($form); + + // Assert shape of object is valid. + $this->assertArrayHasKey('id', $form); + $this->assertArrayHasKey('name', $form); + $this->assertArrayHasKey('created_at', $form); + $this->assertArrayHasKey('type', $form); + $this->assertArrayHasKey('format', $form); + $this->assertArrayHasKey('embed_js', $form); + $this->assertArrayHasKey('embed_url', $form); + $this->assertArrayHasKey('archived', $form); + + // Assert form isn't archived and is hosted i.e. landing page. + $this->assertFalse($form['archived']); + $this->assertEquals($form['type'], 'hosted'); + } } /** From a6753fa0b6944d2ae1f997518e882ecb3c5abcab Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 1 Apr 2024 16:51:37 +0100 Subject: [PATCH 03/10] Coding standards --- src/ConvertKit_API.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 85802d8..f91831f 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -387,14 +387,15 @@ public function get_growth_stats(\DateTime $starting = null, \DateTime $ending = /** * Get forms. - * - * @since 1.0.0 * + * @param boolean $include_archived To include archived forms in the response, use true. * @param boolean $include_total_count To include the total count of records in the response, use true. * @param string $after_cursor Return results after the given pagination cursor. * @param string $before_cursor Return results before the given pagination cursor. * @param integer $per_page Number of results to return. * + * @since 1.0.0 + * * @see https://developers.convertkit.com/v4.html#convertkit-api-forms * * @return false|array @@ -421,13 +422,13 @@ public function get_forms( foreach ($result->forms as $index => $form) { // Exclude hosted forms i.e. landing pages. if ($form->type === 'hosted') { - unset($result->forms[ $index ] ); + unset($result->forms[$index]); continue; } // Exclude archived forms, if required. if (!$include_archived && isset($form->archived) && $form->archived) { - unset($result->forms[ $index ] ); + unset($result->forms[$index]); continue; } } @@ -437,14 +438,15 @@ public function get_forms( /** * Get landing pages. - * - * @since 1.0.0 * + * @param boolean $include_archived To include archived landing pages in the response, use true. * @param boolean $include_total_count To include the total count of records in the response, use true. * @param string $after_cursor Return results after the given pagination cursor. * @param string $before_cursor Return results before the given pagination cursor. * @param integer $per_page Number of results to return. * + * @since 1.0.0 + * * @see https://developers.convertkit.com/v4.html#convertkit-api-forms * * @return false|array @@ -470,13 +472,13 @@ public function get_landing_pages( foreach ($result->forms as $index => $form) { // Exclude non-hosted forms i.e. forms. if ($form->type !== 'hosted') { - unset($result->forms[ $index ] ); + unset($result->forms[$index]); continue; } // Exclude archived forms, if required. if (!$include_archived && isset($form->archived) && $form->archived) { - unset($result->forms[ $index ] ); + unset($result->forms[$index]); continue; } } From 95a2256aec287030948d01b1d1e15fb30877ffe0 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 1 Apr 2024 16:51:45 +0100 Subject: [PATCH 04/10] Add form pagination and total count tests --- tests/ConvertKitAPITest.php | 83 ++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index adc89be..6b51eba 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -722,7 +722,88 @@ public function testGetForms() } } - + /** + * Test that get_forms() returns the expected data + * when the total count is included. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetFormsWithTotalCount() + { + $result = $this->api->get_forms( + include_total_count: true + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert total count is included. + $this->assertArrayHasKey('total_count', get_object_vars($result->pagination)); + $this->assertGreaterThan(0, $result->pagination->total_count); + } + + /** + * Test that get_forms() returns the expected data when pagination parameters + * and per_page limits are specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetFormsPagination() + { + $result = $this->api->get_forms( + per_page: 2 + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert a single form was returned. + $this->assertCount(1, $result->forms); + + // 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_forms( + per_page: 2, + after_cursor: $result->pagination->end_cursor + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert a single form was returned. + $this->assertCount(1, $result->forms); + + // 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_forms( + per_page: 2, + before_cursor: $result->pagination->start_cursor + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert a single form was returned. + $this->assertCount(1, $result->forms); + + // Assert has_previous_page and has_next_page are correct. + $this->assertFalse($result->pagination->has_previous_page); + $this->assertTrue($result->pagination->has_next_page); + } /** * Test that get_landing_pages() returns the expected data. From 512ea0ae9c340ac0791bb4d01e6b5b850cb7d9d8 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 1 Apr 2024 17:05:30 +0100 Subject: [PATCH 05/10] Added landing page tests --- tests/ConvertKitAPITest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 6b51eba..440cff9 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -840,6 +840,29 @@ public function testGetLandingPages() } } + /** + * Test that get_landing_pages() returns the expected data + * when the total count is included. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetLandingPagesWithTotalCount() + { + $result = $this->api->get_landing_pages( + include_total_count: true + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert total count is included. + $this->assertArrayHasKey('total_count', get_object_vars($result->pagination)); + $this->assertGreaterThan(0, $result->pagination->total_count); + } + /** * Test that get_form_subscriptions() returns the expected data * when a valid Form ID is specified. From 619765298531bc341f1708e3881849bf938cc1cc Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 1 Apr 2024 17:09:12 +0100 Subject: [PATCH 06/10] Remove deprecated `get_resources` tests --- tests/ConvertKitAPITest.php | 68 ------------------------------------- 1 file changed, 68 deletions(-) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 440cff9..7367bda 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -2438,74 +2438,6 @@ public function testGetTagSubscriptionsWithInvalidTagID() $result = $this->api->get_tag_subscriptions(12345); } - /** - * Test that get_resources() for Forms returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testGetResourcesForms() - { - $result = $this->api->get_resources('forms'); - $this->assertIsArray($result); - } - - /** - * Test that get_resources() for Landing Pages returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testGetResourcesLandingPages() - { - $result = $this->api->get_resources('landing_pages'); - $this->assertIsArray($result); - } - - /** - * Test that get_resources() for Subscription Forms returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testGetResourcesSubscriptionForms() - { - $this->markTestIncomplete(); - $result = $this->api->get_resources('subscription_forms'); - $this->assertIsArray($result); - } - - /** - * Test that get_resources() for Tags returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testGetResourcesTags() - { - $result = $this->api->get_resources('tags'); - $this->assertIsArray($result); - } - - /** - * Test that get_resources() throws a ClientException when an invalid - * resource type is specified. - * - * @since 1.0.0 - * - * @return void - */ - public function testGetResourcesInvalidResourceType() - { - $this->expectException(ClientException::class); - $result = $this->api->get_resources('invalid-resource-type'); - $this->assertIsArray($result); - } - /** * Test that add_subscriber_to_form_by_email() returns the expected data. * From b6e94c05a847c763d39a26cf5a61359e8a52bbce Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 2 Apr 2024 11:02:41 +0100 Subject: [PATCH 07/10] Use `type` parameter to fetch Forms or Landing Pages --- src/ConvertKit_API.php | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index f91831f..03ee994 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -411,6 +411,7 @@ public function get_forms( $result = $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( + params: [ 'type' => 'embed' ], include_total_count: $include_total_count, after_cursor: $after_cursor, before_cursor: $before_cursor, @@ -418,16 +419,14 @@ public function get_forms( ) ); - // Exclude archived and landing pages. - foreach ($result->forms as $index => $form) { - // Exclude hosted forms i.e. landing pages. - if ($form->type === 'hosted') { - unset($result->forms[$index]); - continue; - } + // If archived results are included, return now. + if ($include_archived) { + return $result; + } - // Exclude archived forms, if required. - if (!$include_archived && isset($form->archived) && $form->archived) { + // Remove archived landing pages. + foreach ($result->forms as $index => $form) { + if (isset($form->archived) && $form->archived) { unset($result->forms[$index]); continue; } @@ -461,6 +460,7 @@ public function get_landing_pages( $result = $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( + params: [ 'type' => 'hosted' ], include_total_count: $include_total_count, after_cursor: $after_cursor, before_cursor: $before_cursor, @@ -468,16 +468,14 @@ public function get_landing_pages( ) ); - // Exclude archived and non-hosted forms. - foreach ($result->forms as $index => $form) { - // Exclude non-hosted forms i.e. forms. - if ($form->type !== 'hosted') { - unset($result->forms[$index]); - continue; - } + // If archived results are included, return now. + if ($include_archived) { + return $result; + } - // Exclude archived forms, if required. - if (!$include_archived && isset($form->archived) && $form->archived) { + // Remove archived landing pages. + foreach ($result->forms as $index => $form) { + if (isset($form->archived) && $form->archived) { unset($result->forms[$index]); continue; } From 87e871ac1c66d52a644326ec3f552e098549a5b9 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 2 Apr 2024 19:06:56 +0100 Subject: [PATCH 08/10] Completed tests for `type` filtering --- src/ConvertKit_API.php | 39 ++------------------ tests/ConvertKitAPITest.php | 72 +++++++++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 03ee994..94ff38f 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -388,7 +388,6 @@ public function get_growth_stats(\DateTime $starting = null, \DateTime $ending = /** * Get forms. * - * @param boolean $include_archived To include archived forms in the response, use true. * @param boolean $include_total_count To include the total count of records in the response, use true. * @param string $after_cursor Return results after the given pagination cursor. * @param string $before_cursor Return results before the given pagination cursor. @@ -401,14 +400,12 @@ public function get_growth_stats(\DateTime $starting = null, \DateTime $ending = * @return false|array */ public function get_forms( - bool $include_archived = false, bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', int $per_page = 100 ) { - // Get forms and landing pages. - $result = $this->get( + return $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( params: [ 'type' => 'embed' ], @@ -418,27 +415,11 @@ public function get_forms( per_page: $per_page ) ); - - // If archived results are included, return now. - if ($include_archived) { - return $result; - } - - // Remove archived landing pages. - foreach ($result->forms as $index => $form) { - if (isset($form->archived) && $form->archived) { - unset($result->forms[$index]); - continue; - } - } - - return $result; } /** * Get landing pages. * - * @param boolean $include_archived To include archived landing pages in the response, use true. * @param boolean $include_total_count To include the total count of records in the response, use true. * @param string $after_cursor Return results after the given pagination cursor. * @param string $before_cursor Return results before the given pagination cursor. @@ -451,13 +432,12 @@ public function get_forms( * @return false|array */ public function get_landing_pages( - bool $include_archived = false, bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', int $per_page = 100 ) { - $result = $this->get( + return $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( params: [ 'type' => 'hosted' ], @@ -467,21 +447,6 @@ public function get_landing_pages( per_page: $per_page ) ); - - // If archived results are included, return now. - if ($include_archived) { - return $result; - } - - // Remove archived landing pages. - foreach ($result->forms as $index => $form) { - if (isset($form->archived) && $form->archived) { - unset($result->forms[$index]); - continue; - } - } - - return $result; } /** diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 7367bda..72b4420 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -716,8 +716,7 @@ public function testGetForms() $this->assertArrayHasKey('embed_url', $form); $this->assertArrayHasKey('archived', $form); - // Assert form isn't archived or a landing page. - $this->assertFalse($form['archived']); + // Assert form is not a landing page i.e embed. $this->assertEquals($form['type'], 'embed'); } } @@ -756,7 +755,7 @@ public function testGetFormsWithTotalCount() public function testGetFormsPagination() { $result = $this->api->get_forms( - per_page: 2 + per_page: 1 ); // Assert forms and pagination exist. @@ -772,7 +771,7 @@ public function testGetFormsPagination() // Use pagination to fetch next page. $result = $this->api->get_forms( - per_page: 2, + per_page: 1, after_cursor: $result->pagination->end_cursor ); @@ -789,7 +788,7 @@ public function testGetFormsPagination() // Use pagination to fetch previous page. $result = $this->api->get_forms( - per_page: 2, + per_page: 1, before_cursor: $result->pagination->start_cursor ); @@ -834,8 +833,7 @@ public function testGetLandingPages() $this->assertArrayHasKey('embed_url', $form); $this->assertArrayHasKey('archived', $form); - // Assert form isn't archived and is hosted i.e. landing page. - $this->assertFalse($form['archived']); + // Assert form is a landing page i.e. hosted. $this->assertEquals($form['type'], 'hosted'); } } @@ -863,6 +861,66 @@ public function testGetLandingPagesWithTotalCount() $this->assertGreaterThan(0, $result->pagination->total_count); } + /** + * Test that get_landing_pages() returns the expected data when pagination parameters + * and per_page limits are specified. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetLandingPagesPagination() + { + $result = $this->api->get_landing_pages( + per_page: 1 + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert a single form was returned. + $this->assertCount(1, $result->forms); + + // 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_landing_pages( + per_page: 1, + after_cursor: $result->pagination->end_cursor + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert a single form was returned. + $this->assertCount(1, $result->forms); + + // Assert has_previous_page and has_next_page are correct. + $this->assertTrue($result->pagination->has_previous_page); + $this->assertFalse($result->pagination->has_next_page); + + // Use pagination to fetch previous page. + $result = $this->api->get_landing_pages( + per_page: 1, + before_cursor: $result->pagination->start_cursor + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert a single form was returned. + $this->assertCount(1, $result->forms); + + // Assert has_previous_page and has_next_page are correct. + $this->assertFalse($result->pagination->has_previous_page); + $this->assertTrue($result->pagination->has_next_page); + } + /** * Test that get_form_subscriptions() returns the expected data * when a valid Form ID is specified. From 57bbb00ea10b01c0615c9f3ec8966b61399b7a81 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 4 Apr 2024 13:09:13 +0100 Subject: [PATCH 09/10] Updated `get_forms` and `get_landing_pages` to support new `status` filter --- src/ConvertKit_API.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 874a239..ed2652e 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -388,6 +388,7 @@ public function get_growth_stats(\DateTime $starting = null, \DateTime $ending = /** * Get forms. * + * @param string $status Form status (active|archived|trashed|all). * @param boolean $include_total_count To include the total count of records in the response, use true. * @param string $after_cursor Return results after the given pagination cursor. * @param string $before_cursor Return results before the given pagination cursor. @@ -400,6 +401,7 @@ public function get_growth_stats(\DateTime $starting = null, \DateTime $ending = * @return false|array */ public function get_forms( + string $status = 'active', bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', @@ -408,7 +410,10 @@ public function get_forms( return $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( - params: [ 'type' => 'embed' ], + params: [ + 'type' => 'embed', + 'status' => $status, + ], include_total_count: $include_total_count, after_cursor: $after_cursor, before_cursor: $before_cursor, @@ -420,6 +425,7 @@ public function get_forms( /** * Get landing pages. * + * @param string $status Form status (active|archived|trashed|all). * @param boolean $include_total_count To include the total count of records in the response, use true. * @param string $after_cursor Return results after the given pagination cursor. * @param string $before_cursor Return results before the given pagination cursor. @@ -432,6 +438,7 @@ public function get_forms( * @return false|array */ public function get_landing_pages( + string $status = 'active', bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', @@ -440,7 +447,10 @@ public function get_landing_pages( return $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( - params: [ 'type' => 'hosted' ], + params: [ + 'type' => 'hosted', + 'status' => $status, + ], include_total_count: $include_total_count, after_cursor: $after_cursor, before_cursor: $before_cursor, From 66cb9ae7b171217eedebe408fece1d0ed0c28ed0 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 4 Apr 2024 14:14:06 +0100 Subject: [PATCH 10/10] Completed tests --- src/ConvertKit_API.php | 8 ++--- tests/ConvertKitAPITest.php | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index ed2652e..5b43cbe 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -410,8 +410,8 @@ public function get_forms( return $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( - params: [ - 'type' => 'embed', + params: [ + 'type' => 'embed', 'status' => $status, ], include_total_count: $include_total_count, @@ -447,8 +447,8 @@ public function get_landing_pages( return $this->get( endpoint: 'forms', args: $this->build_total_count_and_pagination_params( - params: [ - 'type' => 'hosted', + params: [ + 'type' => 'hosted', 'status' => $status, ], include_total_count: $include_total_count, diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index ddb0f86..c123fd9 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -746,6 +746,49 @@ public function testGetForms() // Assert form is not a landing page i.e embed. $this->assertEquals($form['type'], 'embed'); + + // Assert form is not archived. + $this->assertFalse($form['archived']); + } + } + + /** + * Test that get_forms() returns the expected data when + * the status is set to archived. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetFormsWithArchivedStatus() + { + $result = $this->api->get_forms( + status: 'archived' + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Iterate through each form, confirming no landing pages were included. + foreach ($result->forms as $form) { + $form = get_object_vars($form); + + // Assert shape of object is valid. + $this->assertArrayHasKey('id', $form); + $this->assertArrayHasKey('name', $form); + $this->assertArrayHasKey('created_at', $form); + $this->assertArrayHasKey('type', $form); + $this->assertArrayHasKey('format', $form); + $this->assertArrayHasKey('embed_js', $form); + $this->assertArrayHasKey('embed_url', $form); + $this->assertArrayHasKey('archived', $form); + + // Assert form is not a landing page i.e embed. + $this->assertEquals($form['type'], 'embed'); + + // Assert form is not archived. + $this->assertTrue($form['archived']); } } @@ -863,9 +906,34 @@ public function testGetLandingPages() // Assert form is a landing page i.e. hosted. $this->assertEquals($form['type'], 'hosted'); + + // Assert form is not archived. + $this->assertFalse($form['archived']); } } + /** + * Test that get_landing_pages() returns the expected data when + * the status is set to archived. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetLandingPagesWithArchivedStatus() + { + $result = $this->api->get_forms( + status: 'archived' + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert no landing pages are returned, as the account doesn't have any archived landing pages. + $this->assertCount(0, $result->forms); + } + /** * Test that get_landing_pages() returns the expected data * when the total count is included.