Skip to content

Commit

Permalink
Merge pull request #594 from ConvertKit/test-refactor
Browse files Browse the repository at this point in the history
Tests: Refactor `setupConvertKitPlugin` method
  • Loading branch information
n7studios authored Dec 14, 2023
2 parents f1b0e1b + 4c74484 commit e10bba7
Show file tree
Hide file tree
Showing 39 changed files with 1,914 additions and 1,790 deletions.
218 changes: 218 additions & 0 deletions tests/_support/Helper/Acceptance/ConvertKitBroadcasts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php
namespace Helper\Acceptance;

/**
* Helper methods and actions related to the ConvertKit Plugin's Broadcasts
* functionality, which are then available using $I->{yourFunctionName}.
*
* @since 1.9.7.5
*/
class ConvertKitBroadcasts extends \Codeception\Module
{
/**
* Helper method to setup the Plugin's Broadcasts to Posts settings.
*
* @since 2.2.8
*
* @param AcceptanceTester $I AcceptanceTester.
* @param bool|array $settings Array of key/value settings. If not defined, uses expected defaults.
*/
public function setupConvertKitPluginBroadcasts($I, $settings = false)
{
// Go to the Plugin's Broadcasts screen.
$I->loadConvertKitSettingsBroadcastsScreen($I);

// Complete fields.
if ( $settings ) {
foreach ( $settings as $key => $value ) {
switch ( $key ) {
case 'enabled':
case 'enabled_export':
case 'no_styles':
if ( $value ) {
$I->checkOption('_wp_convertkit_settings_broadcasts[' . $key . ']');
} else {
$I->uncheckOption('_wp_convertkit_settings_broadcasts[' . $key . ']');
}
break;

case 'post_status':
case 'category_id':
case 'author_id':
$I->fillSelect2Field($I, '#select2-_wp_convertkit_settings_broadcasts_' . $key . '-container', $value);

break;
default:
$I->fillField('_wp_convertkit_settings_broadcasts[' . $key . ']', $value);
break;
}
}
}

// Click the Save Changes button.
$I->click('Save Changes');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);
}

/**
* Helper method to load the Plugin's Settings > Broadcasts screen.
*
* @since 2.2.8
*
* @param AcceptanceTester $I AcceptanceTester.
*/
public function loadConvertKitSettingsBroadcastsScreen($I)
{
$I->amOnAdminPage('options-general.php?page=_wp_convertkit_settings&tab=broadcasts');

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);
}

/**
* Check that expected HTML exists in the DOM of the page we're viewing for
* a Broadcasts block or shortcode, based on its configuration.
*
* @since 1.9.7.5
*
* @param AcceptanceTester $I Tester.
* @param bool|int $numberOfPosts Number of Broadcasts listed.
* @param bool|string $seePrevPaginationLabel Test if the "previous" pagination link is output and matches expected label.
* @param bool|string $seeNextPaginationLabel Test if the "next" pagination link is output and matches expected label.
* @param bool $seeGrid Test if the broadcasts are displayed in grid view (false = list view).
* @param bool $seeImage Test if the broadcasts display images.
* @param bool $seeDescription Test if the broadcasts display descriptions.
* @param bool|string $seeReadMore Test if the broadcasts display a read more link matching the given text.
*/
public function seeBroadcastsOutput($I, $numberOfPosts = false, $seePrevPaginationLabel = false, $seeNextPaginationLabel = false, $seeGrid = false, $seeImage = false, $seeDescription = false, $seeReadMore = false)
{
// Confirm that the block displays.
$I->seeElementInDOM('div.convertkit-broadcasts');
$I->seeElementInDOM('div.convertkit-broadcasts ul.convertkit-broadcasts-list');
$I->seeElementInDOM('div.convertkit-broadcasts ul.convertkit-broadcasts-list li.convertkit-broadcast');
$I->seeElementInDOM('div.convertkit-broadcasts ul.convertkit-broadcasts-list li.convertkit-broadcast a.convertkit-broadcast-title');

// Confirm that UTM parameters exist on a broadcast link.
$I->assertStringContainsString(
'utm_source=wordpress&utm_term=en_US&utm_content=convertkit',
$I->grabAttributeFrom('a.convertkit-broadcast-title', 'href')
);

// If Display as grid is enabled, confirm the applicable HTML exists so that CSS can style this layout.
if ($seeGrid) {
$I->seeElementInDOM('div.convertkit-broadcasts[data-display-grid="1"]');
} else {
$I->dontSeeElementInDOM('div.convertkit-broadcasts[data-display-grid="1"]');
}

// If Display image is enabled, confirm the image is displayed.
if ($seeImage) {
$I->seeElementInDOM('a.convertkit-broadcast-image img');
$I->assertStringContainsString(
'utm_source=wordpress&utm_term=en_US&utm_content=convertkit',
$I->grabAttributeFrom('a.convertkit-broadcast-image', 'href')
);
} else {
$I->dontSeeElementInDOM('a.convertkit-broadcast-image img');
}

// If Display description is enabled, confirm the description is displayed.
if ($seeDescription) {
$I->seeElementInDOM('.convertkit-broadcast-description');
} else {
$I->dontSeeElementInDOM('.convertkit-broadcast-description');
}

// If Display read more link is enabled, confirm the read more link is displayed and matches the given text.
if ($seeReadMore) {
$I->seeElementInDOM('a.convertkit-broadcast-read-more');
$I->assertStringContainsString(
'utm_source=wordpress&utm_term=en_US&utm_content=convertkit',
$I->grabAttributeFrom('a.convertkit-broadcast-read-more', 'href')
);
} else {
$I->dontSeeElementInDOM('a.convertkit-broadcast-read-more');
}

// Confirm that the number of expected broadcasts displays.
if ($numberOfPosts !== false) {
$I->seeNumberOfElements('li.convertkit-broadcast', $numberOfPosts);
}

// Confirm that previous pagination displays, if expected.
if ($seePrevPaginationLabel !== false) {
$I->seeElementInDOM('div.convertkit-broadcasts ul.convertkit-broadcasts-pagination li.convertkit-broadcasts-pagination-prev a');
$I->seeInSource($seePrevPaginationLabel);
}

// Confirm that next pagination displays, if expected.
if ($seeNextPaginationLabel !== false) {
$I->seeElementInDOM('div.convertkit-broadcasts ul.convertkit-broadcasts-pagination li.convertkit-broadcasts-pagination-next a');
}
}

/**
* Tests that the Broadcasts pagination works, and that the expected Broadcast
* is displayed after using previous and next links.
*
* @since 2.0.0
*
* @param AcceptanceTester $I Tester.
* @param string $previousLabel Previous / Newer Broadcasts Label.
* @param string $nextLabel Next / Older Broadcasts Label.
*/
public function testBroadcastsPagination($I, $previousLabel, $nextLabel)
{
// Confirm that the block displays one broadcast with a pagination link to older broadcasts.
$I->seeBroadcastsOutput($I, 1, false, $nextLabel);

// Click the Older Posts link.
$I->click('li.convertkit-broadcasts-pagination-next a');

// Wait for the AJAX request to complete, by checking if the convertkit-broadcasts-loading class has been
// removed from the block.
$I->waitForBroadcastsToLoad($I);

// Confirm that the block displays one broadcast with a pagination link to newer broadcasts.
$I->seeBroadcastsOutput($I, 1, $previousLabel, false);

// Confirm that the expected Broadcast name is displayed and links to the expected URL, with UTM parameters.
$I->seeInSource('<a href="' . $_ENV['CONVERTKIT_API_BROADCAST_SECOND_URL'] . '?utm_source=wordpress&amp;utm_term=en_US&amp;utm_content=convertkit" target="_blank" rel="nofollow noopener"');
$I->seeInSource($_ENV['CONVERTKIT_API_BROADCAST_SECOND_TITLE']);

// Click the Newer Posts link.
$I->click('li.convertkit-broadcasts-pagination-prev a');

// Wait for the AJAX request to complete, by checking if the convertkit-broadcasts-loading class has been
// removed from the block.
$I->waitForBroadcastsToLoad($I);

// Confirm that the block displays one broadcast with a pagination link to older broadcasts.
$I->seeBroadcastsOutput($I, 1, false, $nextLabel);

// Confirm that the expected Broadcast name is displayed and links to the expected URL, with UTM parameters.
$I->seeInSource('<a href="' . $_ENV['CONVERTKIT_API_BROADCAST_FIRST_URL'] . '?utm_source=wordpress&amp;utm_term=en_US&amp;utm_content=convertkit" target="_blank" rel="nofollow noopener"');
$I->seeInSource($_ENV['CONVERTKIT_API_BROADCAST_FIRST_TITLE']);
}

/**
* Wait for the AJAX request to complete, by checking if the convertkit-broadcasts-loading class has been
* removed from the block.
*
* @since 1.9.7.6
*
* @param AcceptanceTester $I Tester.
*/
public function waitForBroadcastsToLoad($I)
{
$I->waitForElementChange(
'div.convertkit-broadcasts',
function(\Facebook\WebDriver\WebDriverElement $el) {
return ( strpos($el->getAttribute('class'), 'convertkit-broadcasts-loading') === false ? true : false );
},
5
);
}
}
120 changes: 120 additions & 0 deletions tests/_support/Helper/Acceptance/ConvertKitForms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
namespace Helper\Acceptance;

/**
* Helper methods and actions related to the ConvertKit Plugin's Forms
* functionality, which are then available using $I->{yourFunctionName}.
*
* @since 2.2.0
*/
class ConvertKitForms extends \Codeception\Module
{
/**
* Check that expected HTML exists in the DOM of the page we're viewing for
* a Form Trigger block or shortcode, and that the button loads the expected
* ConvertKit Form.
*
* @since 2.2.0
*
* @param AcceptanceTester $I Tester.
* @param string $formURL Form URL.
* @param bool|string $text Test if the button text matches the given value.
* @param bool|string $textColor Test if the given text color is applied.
* @param bool|string $backgroundColor Test is the given background color is applied.
*/
public function seeFormTriggerOutput($I, $formURL, $text = false, $textColor = false, $backgroundColor = false)
{
// Confirm that the button stylesheet loaded.
$I->seeInSource('<link rel="stylesheet" id="convertkit-button-css" href="' . $_ENV['TEST_SITE_WP_URL'] . '/wp-content/plugins/convertkit/resources/frontend/css/button.css');

// Confirm that the block displays.
$I->seeElementInDOM('a.convertkit-formtrigger.wp-block-button__link');

// Confirm that the button links to the correct form.
$I->assertEquals($formURL, $I->grabAttributeFrom('a.convertkit-formtrigger', 'href'));

// Confirm that the text is as expected.
if ($text !== false) {
$I->see($text);
}

// Confirm that the text color is as expected.
if ($textColor !== false) {
$I->seeElementInDOM('a.convertkit-formtrigger.has-text-color');
$I->assertStringContainsString(
'color:' . $textColor,
$I->grabAttributeFrom('a.convertkit-formtrigger', 'style')
);
}

// Confirm that the background color is as expected.
if ($backgroundColor !== false) {
$I->seeElementInDOM('a.convertkit-formtrigger.has-background');
$I->assertStringContainsString(
'background-color:' . $backgroundColor,
$I->grabAttributeFrom('a.convertkit-formtrigger', 'style')
);
}

// Click the button to confirm that the ConvertKit modal displays.
$I->click('a.convertkit-formtrigger');
$I->waitForElementVisible('div.formkit-overlay');
}

/**
* Check that expected HTML does not exist in the DOM of the page we're viewing for
* a Form Trigger block or shortcode.
*
* @since 2.2.0
*
* @param AcceptanceTester $I Tester.
*/
public function dontSeeFormTriggerOutput($I)
{
// Confirm that the block does not display.
$I->dontSeeElementInDOM('div.wp-block-button a.convertkit-formtrigger');
}

/**
* Check that expected HTML exists in the DOM of the page we're viewing for
* a Form Trigger link, and that the link loads the expected
* ConvertKit Form.
*
* @since 2.2.0
*
* @param AcceptanceTester $I Tester.
* @param string $formURL Form URL.
* @param bool|string $text Test if the text matches the given value.
*/
public function seeFormTriggerLinkOutput($I, $formURL, $text = false)
{
// Confirm that the link displays.
$I->seeElementInDOM('a.convertkit-form-link');

// Confirm that the button links to the correct form.
$I->assertEquals($formURL, $I->grabAttributeFrom('a.convertkit-form-link', 'href'));

// Confirm that the text is as expected.
if ($text !== false) {
$I->see($text);
}

// Click the link to confirm that the ConvertKit form displays.
$I->click('a.convertkit-form-link');
$I->waitForElementVisible('div.formkit-overlay');
}

/**
* Check that expected HTML does not exist in the DOM of the page we're viewing for
* a Form Trigger link formatter.
*
* @since 2.2.0
*
* @param AcceptanceTester $I Tester.
*/
public function dontSeeFormTriggerLinkOutput($I)
{
// Confirm that the link does not display.
$I->dontSeeElementInDOM('a.convertkit-form-link');
}
}
Loading

0 comments on commit e10bba7

Please sign in to comment.