diff --git a/.distignore b/.distignore index 5f37507..22632cf 100644 --- a/.distignore +++ b/.distignore @@ -24,6 +24,7 @@ convertkit-membermouse.zip DEPLOYMENT.md DEVELOPMENT.md log.txt +log-api.txt phpcs.xml phpcs.tests.xml phpstan.neon diff --git a/.env.dist.testing b/.env.dist.testing index ccdc27b..c27c800 100644 --- a/.env.dist.testing +++ b/.env.dist.testing @@ -18,3 +18,7 @@ TEST_SITE_WP_DOMAIN=127.0.0.1 TEST_SITE_ADMIN_EMAIL=wordpress@convertkit.local 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" diff --git a/.env.example b/.env.example index 11823e2..8fa8020 100644 --- a/.env.example +++ b/.env.example @@ -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= \ No newline at end of file +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" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0d3ba78..b32c871 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ codeception.yml composer.lock log log.txt +log-api.txt phpstan.neon tests/_output vendor diff --git a/admin/class-convertkit-mm-admin.php b/admin/class-convertkit-mm-admin.php index 05f6d33..99e584d 100644 --- a/admin/class-convertkit-mm-admin.php +++ b/admin/class-convertkit-mm-admin.php @@ -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; diff --git a/tests/_support/Helper/Acceptance/ConvertKitAPI.php b/tests/_support/Helper/Acceptance/ConvertKitAPI.php index a8cf115..833f04e 100644 --- a/tests/_support/Helper/Acceptance/ConvertKitAPI.php +++ b/tests/_support/Helper/Acceptance/ConvertKitAPI.php @@ -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']); } /** @@ -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); } } diff --git a/tests/_support/Helper/Acceptance/Email.php b/tests/_support/Helper/Acceptance/Email.php index 444896c..e8200cc 100644 --- a/tests/_support/Helper/Acceptance/Email.php +++ b/tests/_support/Helper/Acceptance/Email.php @@ -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 } } diff --git a/tests/_support/Helper/Acceptance/MemberMouse.php b/tests/_support/Helper/Acceptance/MemberMouse.php new file mode 100644 index 0000000..1550261 --- /dev/null +++ b/tests/_support/Helper/Acceptance/MemberMouse.php @@ -0,0 +1,37 @@ +{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, + ] + ); + } +} diff --git a/tests/_support/Helper/Acceptance/Plugin.php b/tests/_support/Helper/Acceptance/Plugin.php index e62fb89..e8bd943 100644 --- a/tests/_support/Helper/Acceptance/Plugin.php +++ b/tests/_support/Helper/Acceptance/Plugin.php @@ -34,4 +34,49 @@ public function deactivateConvertKitPlugin($I) { $I->deactivateThirdPartyPlugin($I, 'convertkit-membermouse'); } + + /** + * Helper method to programmatically setup the Plugin's settings. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I AcceptanceTester. + * @param bool|array $options { + * Optional. An array of settings. + * + * @type string $api_key API Key (if specified, used instead of CONVERTKIT_API_KEY). + * @type string $debug Enable debugging (default: on). + * } + */ + public function setupConvertKitPlugin($I, $options = false) + { + // Define default options. + $defaults = [ + 'api-key' => $_ENV['CONVERTKIT_API_KEY'], + 'debug' => 'on', + ]; + + // If supplied options are an array, merge them with the defaults. + if (is_array($options)) { + $options = array_merge($defaults, $options); + } else { + $options = $defaults; + } + + // Define settings in options table. + $I->haveOptionInDatabase('convertkit-mm-options', $options); + } + + /** + * Helper method to reset the ConvertKit Plugin settings, as if it's a clean installation. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I AcceptanceTester. + */ + public function resetConvertKitPlugin($I) + { + // Plugin Settings. + $I->dontHaveOptionInDatabase('convertkit-mm-options'); + } } diff --git a/tests/acceptance.suite.yml b/tests/acceptance.suite.yml index 188cee5..0423b6d 100644 --- a/tests/acceptance.suite.yml +++ b/tests/acceptance.suite.yml @@ -20,6 +20,7 @@ modules: # If you created a new file in tests/_support/Helper/Acceptance, add its namespace and class below, - \Helper\Acceptance\ConvertKitAPI - \Helper\Acceptance\Email + - \Helper\Acceptance\MemberMouse - \Helper\Acceptance\Plugin - \Helper\Acceptance\ThirdPartyPlugin - \Helper\Acceptance\Xdebug diff --git a/tests/acceptance/general/MemberTagCest.php b/tests/acceptance/general/MemberTagCest.php new file mode 100644 index 0000000..c6b599d --- /dev/null +++ b/tests/acceptance/general/MemberTagCest.php @@ -0,0 +1,295 @@ +activateConvertKitPlugin($I); + $I->activateThirdPartyPlugin($I, 'membermouse-platform'); + } + + /** + * Test that the member is tagged with the configured "apply tag on add" + * setting when added to a Membership Level. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testMemberTaggedWhenMembershipLevelAdded(AcceptanceTester $I) + { + // Setup Plugin to tag users added to the Free Membership level to the + // ConvertKit Tag ID. + $I->setupConvertKitPlugin( + $I, + [ + 'convertkit-mapping-1' => $_ENV['CONVERTKIT_API_TAG_ID'], + ] + ); + + // Generate email address for test. + $emailAddress = $I->generateEmailAddress(); + + // Navigate to MemberMouse > Manage Members. + $I->amOnAdminPage('admin.php?page=manage_members'); + + // Create Member. + $I->click('Create Member'); + $I->waitForElementVisible('#mm-new-member-form-container'); + $I->fillField('#mm-new-first-name', 'First'); + $I->fillField('#mm-new-last-name', 'Last'); + $I->fillField('#mm-new-email', $emailAddress); + $I->fillField('#mm-new-password', '12345678'); + $I->click('Create Member', '.mm-dialog-button-container'); + $I->waitForElementNotVisible('#mm-new-member-form-container'); + + // Accept popup once user created. + // We have to wait as there's no specific event MemberMouse fires to tell + // us it completed adding the member. + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + + // Check subscriber exists. + $subscriberID = $I->apiCheckSubscriberExists($I, $emailAddress); + + // Check that the subscriber has been assigned to the tag. + $I->apiCheckSubscriberHasTag($I, $subscriberID, $_ENV['CONVERTKIT_API_TAG_ID']); + } + + /** + * Test that the member is tagged with the configured "apply tag" + * setting when the Membership Level is changed. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testMemberTaggedWhenMembershipLevelChanged(AcceptanceTester $I) + { + // Create an additional membership level. + $levelID = $I->memberMouseCreateMembershipLevel($I, 'Premium'); + + // Setup Plugin to tag users added to the Free Membership level to the + // ConvertKit Tag ID. + $I->setupConvertKitPlugin( + $I, + [ + 'convertkit-mapping-' . $levelID => $_ENV['CONVERTKIT_API_TAG_ID'], + ] + ); + + // Generate email address for test. + $emailAddress = $I->generateEmailAddress(); + + // Navigate to MemberMouse > Manage Members. + $I->amOnAdminPage('admin.php?page=manage_members'); + + // Create Member. + $I->click('Create Member'); + $I->waitForElementVisible('#mm-new-member-form-container'); + $I->fillField('#mm-new-first-name', 'First'); + $I->fillField('#mm-new-last-name', 'Last'); + $I->fillField('#mm-new-email', $emailAddress); + $I->fillField('#mm-new-password', '12345678'); + $I->click('Create Member', '.mm-dialog-button-container'); + $I->waitForElementNotVisible('#mm-new-member-form-container'); + + // Accept popup once user created. + // We have to wait as there's no specific event MemberMouse fires to tell + // us it completed adding the member. + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + + // Change the user's membership level. + $I->amOnAdminPage('admin.php?page=manage_members'); + $I->click($emailAddress); + $I->click('Access Rights'); + $I->selectOption('#mm-new-membership-selection', 'Premium'); + $I->click('Change Membership'); + + // Accept popups + // We have to wait as there's no specific event MemberMouse fires to tell + // us it completed changing the membership level. + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + + // Check subscriber exists. + $subscriberID = $I->apiCheckSubscriberExists($I, $emailAddress); + + // Check that the subscriber has been assigned to the tag. + $I->apiCheckSubscriberHasTag($I, $subscriberID, $_ENV['CONVERTKIT_API_TAG_ID']); + } + + /** + * Test that the member is tagged with the configured "apply tag on cancelled" + * setting when cancelled. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testMemberTaggedWhenMembershipLevelCancelled(AcceptanceTester $I) + { + // Setup Plugin to tag users added to the Free Membership level to the + // ConvertKit Tag ID, and assign them a different tag when their membership + // is cancelled. + $I->setupConvertKitPlugin( + $I, + [ + 'convertkit-mapping-1' => $_ENV['CONVERTKIT_API_TAG_ID'], + 'convertkit-mapping-1-cancel' => $_ENV['CONVERTKIT_API_TAG_CANCEL_ID'], + + ] + ); + + // Generate email address for test. + $emailAddress = $I->generateEmailAddress(); + + // Navigate to MemberMouse > Manage Members. + $I->amOnAdminPage('admin.php?page=manage_members'); + + // Create Member. + $I->click('Create Member'); + $I->waitForElementVisible('#mm-new-member-form-container'); + $I->fillField('#mm-new-first-name', 'First'); + $I->fillField('#mm-new-last-name', 'Last'); + $I->fillField('#mm-new-email', $emailAddress); + $I->fillField('#mm-new-password', '12345678'); + $I->click('Create Member', '.mm-dialog-button-container'); + $I->waitForElementNotVisible('#mm-new-member-form-container'); + + // Accept popup once user created. + // We have to wait as there's no specific event MemberMouse fires to tell + // us it completed adding the member. + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + + // Check subscriber exists. + $subscriberID = $I->apiCheckSubscriberExists($I, $emailAddress); + + // Check that the subscriber has been assigned to the tag. + $I->apiCheckSubscriberHasTag($I, $subscriberID, $_ENV['CONVERTKIT_API_TAG_ID']); + + // Cancel the user's membership level. + $I->amOnAdminPage('admin.php?page=manage_members'); + $I->click($emailAddress); + $I->click('Access Rights'); + $I->click('Cancel Membership'); + + // Accept popups + // We have to wait as there's no specific event MemberMouse fires to tell + // us it completed changing the membership level. + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + + // Check that the subscriber has been assigned to the cancelled tag. + $I->apiCheckSubscriberHasTag($I, $subscriberID, $_ENV['CONVERTKIT_API_TAG_CANCEL_ID']); + } + + /** + * Test that the member is tagged with the configured "apply tag on cancelled" + * setting when deleted. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testMemberTaggedWhenDeleted(AcceptanceTester $I) + { + // Setup Plugin to tag users added to the Free Membership level to the + // ConvertKit Tag ID, and assign them a different tag when their membership + // is cancelled through account deletion. + $I->setupConvertKitPlugin( + $I, + [ + 'convertkit-mapping-1' => $_ENV['CONVERTKIT_API_TAG_ID'], + 'convertkit-mapping-1-cancel' => $_ENV['CONVERTKIT_API_TAG_CANCEL_ID'], + + ] + ); + + // Generate email address for test. + $emailAddress = $I->generateEmailAddress(); + + // Navigate to MemberMouse > Manage Members. + $I->amOnAdminPage('admin.php?page=manage_members'); + + // Create Member. + $I->click('Create Member'); + $I->waitForElementVisible('#mm-new-member-form-container'); + $I->fillField('#mm-new-first-name', 'First'); + $I->fillField('#mm-new-last-name', 'Last'); + $I->fillField('#mm-new-email', $emailAddress); + $I->fillField('#mm-new-password', '12345678'); + $I->click('Create Member', '.mm-dialog-button-container'); + $I->waitForElementNotVisible('#mm-new-member-form-container'); + + // Accept popup once user created. + // We have to wait as there's no specific event MemberMouse fires to tell + // us it completed adding the member. + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + + // Check subscriber exists. + $subscriberID = $I->apiCheckSubscriberExists($I, $emailAddress); + + // Check that the subscriber has been assigned to the tag. + $I->apiCheckSubscriberHasTag($I, $subscriberID, $_ENV['CONVERTKIT_API_TAG_ID']); + + // Cancel the user's membership level. + $I->amOnAdminPage('admin.php?page=manage_members'); + $I->click($emailAddress); + $I->click('Delete Member'); + + // Accept popups + // We have to wait as there's no specific event MemberMouse fires to tell + // us it completed changing the membership level. + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + $I->acceptPopup(); + $I->wait(3); + + // Check that the subscriber has been assigned to the cancelled tag. + $I->apiCheckSubscriberHasTag($I, $subscriberID, $_ENV['CONVERTKIT_API_TAG_CANCEL_ID']); + } + + /** + * Deactivate and reset Plugin(s) after each test, if the test passes. + * We don't use _after, as this would provide a screenshot of the Plugin + * deactivation and not the true test error. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function _passed(AcceptanceTester $I) + { + $I->deactivateThirdPartyPlugin($I, 'membermouse-platform'); + $I->deactivateConvertKitPlugin($I); + $I->resetConvertKitPlugin($I); + } +} diff --git a/tests/acceptance/general/SettingsCest.php b/tests/acceptance/general/SettingsCest.php new file mode 100644 index 0000000..7026078 --- /dev/null +++ b/tests/acceptance/general/SettingsCest.php @@ -0,0 +1,142 @@ +activateConvertKitPlugin($I); + $I->activateThirdPartyPlugin($I, 'membermouse-platform'); + } + + /** + * Test that saving settings on the settings screen with no changes + * works with no errors. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testSaveSettingsWithNoChanges(AcceptanceTester $I) + { + // Go to the Plugin's Settings > General Screen. + $I->amOnAdminPage('options-general.php?page=convertkit-mm'); + + // Click save settings. + $I->click('Save Settings'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + } + + /** + * Test that saving a valid API Key on the settings screen + * works with no errors. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testSaveValidAPIKey(AcceptanceTester $I) + { + // Go to the Plugin's Settings > General Screen. + $I->amOnAdminPage('options-general.php?page=convertkit-mm'); + + // Complete API Field. + $I->fillField('convertkit-mm-options[api-key]', $_ENV['CONVERTKIT_API_KEY']); + + // Click save settings. + $I->click('Save Settings'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm settings saved. + $I->see('Settings saved.'); + } + + /** + * Test that saving an invalid API Key on the settings screen + * works with no errors. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testSaveInvalidAPIKey(AcceptanceTester $I) + { + // Go to the Plugin's Settings > General Screen. + $I->amOnAdminPage('options-general.php?page=convertkit-mm'); + + // Complete API Field. + $I->fillField('convertkit-mm-options[api-key]', 'fakeApiKey'); + + // Click save settings. + $I->click('Save Settings'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm settings saved. + // The Plugin doesn't show an error if an invalid API Key is present; in the future + // we'll want to add a notice in the Plugin and then test for it here. + $I->see('Settings saved.'); + } + + /** + * Test that saving an invalid API Key on the settings screen + * works with no errors. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function testSaveTagAssignment(AcceptanceTester $I) + { + // Setup Plugin. + $I->setupConvertKitPlugin($I); + + // Go to the Plugin's Settings > General Screen. + $I->amOnAdminPage('options-general.php?page=convertkit-mm'); + + // Assign tags. + $I->selectOption('convertkit-mm-options[convertkit-mapping-1]', $_ENV['CONVERTKIT_API_TAG_NAME']); + + // Click save settings. + $I->click('Save Settings'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm settings saved. + $I->see('Settings saved.'); + $I->seeOptionIsSelected('convertkit-mm-options[convertkit-mapping-1]', $_ENV['CONVERTKIT_API_TAG_NAME']); + } + + /** + * Deactivate and reset Plugin(s) after each test, if the test passes. + * We don't use _after, as this would provide a screenshot of the Plugin + * deactivation and not the true test error. + * + * @since 1.2.0 + * + * @param AcceptanceTester $I Tester. + */ + public function _passed(AcceptanceTester $I) + { + $I->deactivateThirdPartyPlugin($I, 'membermouse-platform'); + $I->deactivateConvertKitPlugin($I); + $I->resetConvertKitPlugin($I); + } +}