Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Settings and Tagging #25

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ convertkit-membermouse.zip
DEPLOYMENT.md
DEVELOPMENT.md
log.txt
log-api.txt
phpcs.xml
phpcs.tests.xml
phpstan.neon
Expand Down
4 changes: 4 additions & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ TEST_SITE_WP_DOMAIN=127.0.0.1
[email protected]
TEST_SITE_HTTP_USER_AGENT=HeadlessChrome
TEST_SITE_HTTP_USER_AGENT_MOBILE=HeadlessChromeMobile
CONVERTKIT_API_TAG_NAME="wordpress"
CONVERTKIT_API_TAG_ID="2744672"
CONVERTKIT_API_TAG_CANCEL_NAME="wishlistmember-unsubscribed"
CONVERTKIT_API_TAG_CANCEL_ID="2782797"
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ TEST_SITE_HTTP_USER_AGENT_MOBILE=HeadlessChromeMobile
CONVERTKIT_API_KEY_NO_DATA=
CONVERTKIT_API_SECRET_NO_DATA=
CONVERTKIT_API_KEY=
CONVERTKIT_API_SECRET=
CONVERTKIT_API_SECRET=
CONVERTKIT_API_TAG_NAME="wordpress"
CONVERTKIT_API_TAG_ID="2744672"
CONVERTKIT_API_TAG_CANCEL_NAME="wishlistmember-unsubscribed"
CONVERTKIT_API_TAG_CANCEL_ID="2782797"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ codeception.yml
composer.lock
log
log.txt
log-api.txt
phpstan.neon
tests/_output
vendor
Expand Down
8 changes: 1 addition & 7 deletions admin/class-convertkit-mm-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,7 @@ public function get_mm_membership_levels() {
return $levels;
}

$result = $wpdb->get_results(
$wpdb->prepare(
'SELECT id, name, status FROM %s',
array( MM_TABLE_MEMBERSHIP_LEVELS )
),
OBJECT
);
$result = $wpdb->get_results( 'SELECT id, name, status FROM ' . MM_TABLE_MEMBERSHIP_LEVELS, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL

foreach ( $result as $_level ) {
$levels[ $_level->id ] = $_level->name;
Expand Down
225 changes: 61 additions & 164 deletions tests/_support/Helper/Acceptance/ConvertKitAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,190 +10,94 @@
class ConvertKitAPI extends \Codeception\Module
{
/**
* Check the given email address exists as a subscriber, and optionally
* checks that the first name and custom fields contain the expected data.
* Check the given email address exists as a subscriber.
*
* @since 1.2.0
*
* @param AcceptanceTester $I AcceptanceTester.
* @param string $emailAddress Email Address.
* @param bool|string $firstName First Name.
* @param bool|array $customFields Custom Fields.
* @param AcceptanceTester $I AcceptanceTester.
* @param string $emailAddress Email Address.
* @return int Subscriber ID.
*/
public function apiCheckSubscriberExists($I, $emailAddress, $firstName = false, $customFields = false)
public function apiCheckSubscriberExists($I, $emailAddress)
{
// Run request.
$results = $this->apiRequest(
'subscribers',
'GET',
[
'email_address' => $emailAddress,
'include_total_count' => true,

// Some test email addresses might bounce, so we want to check all subscriber states.
'status' => 'all',
'email_address' => $emailAddress,
]
);

// Check at least one subscriber was returned and it matches the email address.
$I->assertGreaterThan(0, $results['pagination']['total_count']);
$I->assertGreaterThan(0, $results['total_subscribers']);
$I->assertEquals($emailAddress, $results['subscribers'][0]['email_address']);

// If a first name was provided, check it matches.
if ($firstName) {
$I->assertEquals($firstName, $results['subscribers'][0]['first_name']);
}

// If custom fields are provided, check they exist.
if ($customFields) {
foreach ($customFields as $customField => $customFieldValue) {
$I->assertEquals($results['subscribers'][0]['fields'][ $customField ], $customFieldValue);
}
}
return $results['subscribers'][0]['id'];
}

/**
* Check the given email address does not exists as a subscriber.
* Check the given subscriber ID has been assigned to the given tag ID.
*
* @since 1.2.0
*
* @param AcceptanceTester $I AcceptanceTester.
* @param string $emailAddress Email Address.
* @param int $subscriberID Subscriber ID.
* @param int $tagID Tag ID.
*/
public function apiCheckSubscriberDoesNotExist($I, $emailAddress)
public function apiCheckSubscriberHasTag($I, $subscriberID, $tagID)
{
// Run request.
$results = $this->apiRequest(
'subscribers',
'GET',
[
'email_address' => $emailAddress,
'include_total_count' => true,

// Some test email addresses might bounce, so we want to check all subscriber states.
'status' => 'all',
]
'subscribers/' . $subscriberID . '/tags',
'GET'
);

// Check no subscribers are returned by this request.
$I->assertEquals(0, $results['pagination']['total_count']);
}

/**
* Checks if the given email address has the given tag.
*
* @since 1.2.0
*
* @param AcceptanceTester $I AcceptanceTester.
* @param string $emailAddress Email Address.
* @param string $tagID Tag ID.
*/
public function apiCheckSubscriberHasTag($I, $emailAddress, $tagID)
{
// Get subscriber ID by email.
$subscriberID = $this->apiGetSubscriberIDByEmail($emailAddress);

// Get subscriber tags.
$subscriberTags = $this->apiGetSubscriberTags($subscriberID);

$subscriberTagged = false;
foreach ($subscriberTags as $tag) {
if ( (int) $tag['id'] === (int) $tagID) {
$subscriberTagged = true;
break;
}
}

// Check that the Subscriber is tagged.
$I->assertTrue($subscriberTagged);
// Confirm the tag has been assigned to the subscriber.
$I->assertEquals($tagID, $results['tags'][0]['id']);
}

/**
* Checks if the given email address does not have the given tag.
* Check the given subscriber ID has no tags assigned.
*
* @since 1.2.0
*
* @param AcceptanceTester $I AcceptanceTester.
* @param string $emailAddress Email Address.
* @param string $tagID Tag ID.
* @param AcceptanceTester $I AcceptanceTester.
* @param int $subscriberID Subscriber ID.
*/
public function apiCheckSubscriberDoesNotHaveTag($I, $emailAddress, $tagID)
public function apiCheckSubscriberHasNoTags($I, $subscriberID)
{
// Get subscriber ID by email.
$subscriberID = $this->apiGetSubscriberIDByEmail($emailAddress);

// Get subscriber tags.
$subscriberTags = $this->apiGetSubscriberTags($subscriberID);

$subscriberTagged = false;
foreach ($subscriberTags as $tag) {
if ( (int) $tag['id'] === (int) $tagID) {
$subscriberTagged = true;
break;
}
}
// Run request.
$results = $this->apiRequest(
'subscribers/' . $subscriberID . '/tags',
'GET'
);

// Check that the Subscriber is not tagged.
$I->assertFalse($subscriberTagged);
// Confirm no tags have been assigned to the subscriber.
$I->assertCount(0, $results['tags']);
}

/**
* Checks if the given email address has no tags in ConvertKit.
* Check the given email address does not exists as a subscriber.
*
* @since 1.2.0
*
* @param AcceptanceTester $I AcceptanceTester.
* @param AcceptanceTester $I AcceptanceTester.
* @param string $emailAddress Email Address.
*/
public function apiCheckSubscriberHasNoTags($I, $emailAddress)
{
// Get subscriber ID by email.
$subscriberID = $this->apiGetSubscriberIDByEmail($emailAddress);

// Get subscriber tags.
$subscriberTags = $this->apiGetSubscriberTags($subscriberID);

// Confirm no tags exist.
$I->assertCount(0, $subscriberTags);
}

/**
* Returns the subscriber ID for the given email address from the API.
*
* @since 1.2.0
*
* @param string $emailAddress Subscriber Email Address.
* @return array
*/
public function apiGetSubscriberIDByEmail($emailAddress)
public function apiCheckSubscriberDoesNotExist($I, $emailAddress)
{
$subscriber = $this->apiRequest(
// Run request.
$results = $this->apiRequest(
'subscribers',
'GET',
[
'email_address' => $emailAddress,
'include_total_count' => true,

// Some test email addresses might bounce, so we want to check all subscriber states.
'status' => 'all',
'email_address' => $emailAddress,
]
);

return $subscriber['subscribers'][0]['id'];
}

/**
* Returns all tags for the given subscriber ID from the API.
*
* @since 1.2.0
*
* @param int $subscriberID Subscriber ID.
* @return array
*/
public function apiGetSubscriberTags($subscriberID)
{
$tags = $this->apiRequest('subscribers/' . $subscriberID . '/tags');
return $tags['tags'];
// Check no subscribers are returned by this request.
$I->assertEquals(0, $results['total_subscribers']);
}

/**
Expand All @@ -208,40 +112,33 @@ public function apiGetSubscriberTags($subscriberID)
*/
public function apiRequest($endpoint, $method = 'GET', $params = array())
{
// Send request.
$client = new \GuzzleHttp\Client();
switch ($method) {
case 'GET':
$result = $client->request(
$method,
'https://api.convertkit.com/v4/' . $endpoint . '?' . http_build_query($params),
[
'headers' => [
'Authorization' => 'Bearer ' . $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'],
'timeout' => 5,
],
]
);
break;
// Build query parameters.
$params = array_merge(
$params,
[
'api_key' => $_ENV['CONVERTKIT_API_KEY'],
'api_secret' => $_ENV['CONVERTKIT_API_SECRET'],
]
);

default:
$result = $client->request(
$method,
'https://api.convertkit.com/v4/' . $endpoint,
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'Authorization' => 'Bearer ' . $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'],
'timeout' => 5,
],
'body' => (string) json_encode($params), // phpcs:ignore WordPress.WP.AlternativeFunctions
]
);
break;
// Send request.
try {
$client = new \GuzzleHttp\Client();
$result = $client->request(
$method,
'https://api.convertkit.com/v3/' . $endpoint . '?' . http_build_query($params),
[
'headers' => [
'Accept-Encoding' => 'gzip',
'timeout' => 5,
],
]
);

// Return JSON decoded response.
return json_decode($result->getBody()->getContents(), true);
} catch (\GuzzleHttp\Exception\ClientException $e) {
return [];
}

// Return JSON decoded response.
return json_decode($result->getBody()->getContents(), true);
}
}
2 changes: 1 addition & 1 deletion tests/_support/Helper/Acceptance/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Email extends \Codeception\Module
*/
public function generateEmailAddress()
{
return 'wordpress-' . uniqid() . '-' . date( 'Y-m-d-H-i-s' ) . '-php-' . PHP_VERSION_ID . '@n7studios.com';
return 'wordpress-' . rand(0, 1000) . '-' . date( 'YmdHis' ) . '-php-' . PHP_VERSION_ID . '@n7studios.com'; // phpcs:ignore WordPress.WP.AlternativeFunctions
}
}
37 changes: 37 additions & 0 deletions tests/_support/Helper/Acceptance/MemberMouse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace Helper\Acceptance;

/**
* Helper methods and actions related to the MemberMouse Plugin,
* which are then available using $I->{yourFunctionName}.
*
* @since 1.2.0
*/
class MemberMouse extends \Codeception\Module
{
/**
* Helper method to create a membership level.
*
* @since 1.2.0
*
* @param AcceptanceTester $I AcceptanceTester.
* @param string $name Membership Level Name.
* @return int Membership Level ID.
*/
public function memberMouseCreateMembershipLevel($I, $name)
{
return $I->haveInDatabase(
'wp_mm_membership_levels',
[
'reference_key' => 'm9BvU2',
'is_free' => 1,
'is_default' => 0,
'name' => $name,
'description' => $name,
'wp_role' => 'mm-ignore-role',
'default_product_id' => 0,
'status' => 1,
]
);
}
}
Loading
Loading