Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n7studios committed Jan 30, 2025
1 parent de02bd2 commit 353bfde
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions includes/class-convertkit-broadcasts-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public function add_row_action( $actions, $post ) {
// Build URL.
$url = add_query_arg(
array(
'post_type' => $post->post_type,
'convertkit-action' => 'broadcast-export',
'id' => $post->ID,
'nonce' => wp_create_nonce( 'action-convertkit-broadcast-export' ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php
/**
* Tests Post export to Broadcast functionality.
*
* @since 2.7.2
*/
class BroadcastsExportCPTRowActionCest
{
/**
* Run common actions before running the test functions in this class.
*
* @since 2.7.2
*
* @param AcceptanceTester $I Tester.
*/
public function _before(AcceptanceTester $I)
{
// Activate ConvertKit Plugin.
$I->activateConvertKitPlugin($I);
$I->setupConvertKitPlugin($I);
$I->setupConvertKitPluginResources($I);

// Create a public Custom Post Type called Articles, using the Custom Post Type UI Plugin.
$I->registerCustomPostType($I, 'article', 'Articles', 'Article');
}

/**
* Tests that no action is displayed in the Articles table when the 'Enable Export Actions' is disabled
* in the Plugin's settings.
*
* @since 2.7.2
*
* @param AcceptanceTester $I Tester.
*/
public function testBroadcastsExportRowActionWhenDisabled(AcceptanceTester $I)
{
// Programmatically create an Article.
$postID = $I->havePostInDatabase(
[
'post_type' => 'article',
'post_title' => 'Kit: Export Article to Broadcast',
'post_content' => 'Kit: Export Article to Broadcast: Content',
'post_excerpt' => 'Kit: Export Article to Broadcast: Excerpt',
]
);

// Navigate to the Articles WP_List_Table.
$I->amOnAdminPage('edit.php?post_type=article');

// Confirm that no action to export the Post is displayed.
$I->dontSeeInSource('span.convertkit_broadcast_export');
}

/**
* Tests that an action is displayed in the Articles table when the 'Enable Export Actions' is enabled
* in the Plugin's settings, and a Broadcast is created in ConvertKit when clicked.
*
* @since 2.7.2
*
* @param AcceptanceTester $I Tester.
*/
public function testBroadcastsExportRowActionWhenEnabled(AcceptanceTester $I)
{
// Enable Export Actions for Articles.
$I->setupConvertKitPluginBroadcasts(
$I,
[
'enabled_export' => true,
]
);

// Programmatically create a Post.
$postID = $I->havePostInDatabase(
[
'post_type' => 'article',
'post_title' => 'Kit: Export Article to Broadcast',
'post_content' => '<p class="style-test">Kit: Export Article to Broadcast: Content</p>',
'post_excerpt' => 'Kit: Export Article to Broadcast: Excerpt',
]
);

// Navigate to the Articles WP_List_Table.
$I->amOnAdminPage('edit.php?post_type=article');

// Hover mouse over Post's table row.
$I->moveMouseOver('tr.iedit:first-child');

// Wait for export link to be visible.
$I->waitForElementVisible('tr.iedit:first-child span.convertkit_broadcast_export a');

// Click the export action.
$I->click('tr.iedit:first-child span.convertkit_broadcast_export a');

// Confirm that a success message displays.
$I->waitForElementVisible('.notice-success');
$I->see('Successfully created Kit Broadcast from Post');

// Get Broadcast ID from 'Click here' link.
$broadcastID = (int) filter_var($I->grabAttributeFrom('.notice-success p a', 'href'), FILTER_SANITIZE_NUMBER_INT);

// Fetch Broadcast from the API.
$broadcast = $I->apiGetBroadcast($broadcastID);

// Delete Broadcast.
$I->apiDeleteBroadcast($broadcastID);

// Confirm styles were included in the Broadcast.
$I->assertStringContainsString('class="style-test"', $broadcast['broadcast']['content']);
}

/**
* Tests that the 'Disable Styles' setting is honored when enabled in the Plugin's settings, and a
* Broadcast is created in ConvertKit.
*
* @since 2.7.2
*
* @param AcceptanceTester $I Tester.
*/
public function testBroadcastsExportActionWithDisableStylesEnabled(AcceptanceTester $I)
{
// Enable Export Actions for Articles.
$I->setupConvertKitPluginBroadcasts(
$I,
[
'enabled_export' => true,
'no_styles' => true,
]
);

// Programmatically create a Post.
$postID = $I->havePostInDatabase(
[
'post_type' => 'article',
'post_title' => 'Kit: Export Article to Broadcast: Disable Styles',
'post_content' => '<p class="style-test">Kit: Export Post to Broadcast: Disable Styles: Content</p>',
'post_excerpt' => 'Kit: Export Article to Broadcast: Disable Styles: Excerpt',
]
);

// Navigate to the Articles WP_List_Table.
$I->amOnAdminPage('edit.php?post_type=article');

// Hover mouse over Post's table row.
$I->moveMouseOver('tr.iedit:first-child');

// Wait for export link to be visible.
$I->waitForElementVisible('tr.iedit:first-child span.convertkit_broadcast_export a');

// Click the export action.
$I->click('tr.iedit:first-child span.convertkit_broadcast_export a');

// Confirm that a success message displays.
$I->waitForElementVisible('.notice-success');
$I->see('Successfully created Kit Broadcast from Post');

// Get Broadcast ID from 'Click here' link.
$broadcastID = (int) filter_var($I->grabAttributeFrom('.notice-success p a', 'href'), FILTER_SANITIZE_NUMBER_INT);

// Fetch Broadcast from the API.
$broadcast = $I->apiGetBroadcast($broadcastID);

// Delete Broadcast.
$I->apiDeleteBroadcast($broadcastID);

// Confirm styles were not included in the Broadcast.
$I->assertStringNotContainsString('class="style-test"', $broadcast['broadcast']['content']);
}

/**
* 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 2.7.2
*
* @param AcceptanceTester $I Tester.
*/
public function _passed(AcceptanceTester $I)
{
$I->unregisterCustomPostType($I, 'article');
$I->deactivateConvertKitPlugin($I);
$I->resetConvertKitPlugin($I);
}
}

0 comments on commit 353bfde

Please sign in to comment.