Skip to content

Commit

Permalink
test: remove network dependent tests and fix a flaky one (#3541)
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek authored Nov 14, 2024
1 parent 489ce0f commit 340fcb5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 445 deletions.
10 changes: 10 additions & 0 deletions tests/mocks/newsletters-mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
if ( ! class_exists( 'Newspack_Newsletters_Contacts' ) ) {
class Newspack_Newsletters_Contacts {}
}

if ( ! class_exists( 'Newspack_Newsletters' ) ) {
class Newspack_Newsletters {
const EMAIL_HTML_META = 'newspack_email_html';

public static function service_provider() {
return get_option( 'newspack_newsletters_service_provider', false );
}
}
}
156 changes: 0 additions & 156 deletions tests/unit-tests/api-plugins-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public function set_up() {
do_action( 'rest_api_init' );

$this->administrator = $this->factory->user->create( [ 'role' => 'administrator' ] );

// Delete any lingering managed plugins.
Plugin_Manager::uninstall( array_keys( Plugin_Manager::get_managed_plugins() ) );
}

/**
Expand Down Expand Up @@ -85,159 +82,6 @@ public function test_get_plugins_authorized() {
$this->assertEquals( $expected_jetpack_info, $data['jetpack'] );
}

/**
* Test unauthorized users can't retrieve plugin info.
*/
public function test_get_plugin_unauthorized() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'GET', $this->api_namespace . '/plugins/jetpack' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}

/**
* Test retrieving one plugin's info.
*/
public function test_get_plugin_authorized() {
wp_set_current_user( $this->administrator );

$request = new WP_REST_Request( 'GET', $this->api_namespace . '/plugins/jetpack' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );

$expected_data = [
'Name' => 'Jetpack',
'Status' => 'uninstalled',
];
$response_data = $response->get_data();
$this->assertEquals( $expected_data['Name'], $response_data['Name'] );
$this->assertEquals( $expected_data['Status'], $response_data['Status'] );

$request = new WP_REST_Request( 'GET', $this->api_namespace . '/plugins/this-dont-exist' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}

/**
* Test unauthorized users can't activate/deactivate plugins.
*/
public function test_activate_deactivate_plugin_unauthorized() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/pwa/activate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );

$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/pwa/deactivate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}

/**
* Test activating/deactivating plugins.
*/
public function test_activate_deactivate_plugin_authorized() {
wp_set_current_user( $this->administrator );

$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/pwa/activate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$expected_data = [
'Name' => 'PWA',
'Status' => 'active',
];
$response_data = $response->get_data();
$this->assertEquals( $expected_data['Name'], $response_data['Name'] );
$this->assertEquals( $expected_data['Status'], $response_data['Status'] );

// Activating the plugin again should fail.
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/pwa/activate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 500, $response->get_status() );

$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/pwa/deactivate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$expected_data = [
'Name' => 'PWA',
'Status' => 'inactive',
];
$response_data = $response->get_data();
$this->assertEquals( $expected_data['Name'], $response_data['Name'] );
$this->assertEquals( $expected_data['Status'], $response_data['Status'] );

// Dectivating the plugin again should fail.
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/pwa/deactivate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 500, $response->get_status() );

$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/this-dont-exist/activate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );

// Should not be able to activate plugins that aren't managed.
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/hello-dolly/activate' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}

/**
* Test unauthorized users can't install/uninstall plugins.
*/
public function test_install_uninstall_unauthorized() {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/pwa/install' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}

/**
* Test installing/uninstalling plugins.
*/
public function test_install_uninstall_authorized() {
wp_set_current_user( $this->administrator );

$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/jetpack/install' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$expected_data = [
'Name' => 'Jetpack',
'Status' => 'inactive',
];
$response_data = $response->get_data();
$this->assertEquals( $expected_data['Name'], $response_data['Name'] );
$this->assertEquals( $expected_data['Status'], $response_data['Status'] );

// Installing the plugin again should fail.
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/jetpack/install' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 500, $response->get_status() );

$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/jetpack/uninstall' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$expected_data = [
'Name' => 'Jetpack',
'Status' => 'uninstalled',
];
$response_data = $response->get_data();
$this->assertEquals( $expected_data['Name'], $response_data['Name'] );
$this->assertEquals( $expected_data['Status'], $response_data['Status'] );

// Uninstalling the plugin again should fail.
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/jetpack/uninstall' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 500, $response->get_status() );

$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/this-dont-exist/install' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );

// Should not be able to install plugins that aren't managed.
$request = new WP_REST_Request( 'POST', $this->api_namespace . '/plugins/hello-dolly/install' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}

/**
* Test the schema.
*/
Expand Down
24 changes: 0 additions & 24 deletions tests/unit-tests/emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,6 @@ private static function get_test_email( $type ) {
* Email setup & defaults generation.
*/
public function test_emails_setup() {
self::assertEquals(
Emails::get_emails( [ 'test-email-config' ] ),
[],
'Emails are empty until configured.'
);
self::assertFalse(
Emails::can_send_email( 'test-email-config' ),
'Test email cannot be sent.'
);
self::assertFalse(
Emails::supports_emails(),
'Emails are not configured until the Newspack Newsletters plugin is active.'
);
$send_result = Emails::send_email(
'test-email-config',
'[email protected]'
);
self::assertFalse( $send_result, 'Email cannot be sent until the instance is configured.' );

Plugin_Manager::activate( 'newspack-newsletters' );
self::assertTrue(
Emails::supports_emails(),
'Emails are configured after Newspack Newsletters plugin is active.'
Expand Down Expand Up @@ -104,8 +84,6 @@ public function test_emails_setup() {
* Email sending, with a template.
*/
public function test_emails_send_with_template() {
Plugin_Manager::activate( 'newspack-newsletters' );

$test_email = self::get_test_email( 'test-email-config' );

$recipient = '[email protected]';
Expand Down Expand Up @@ -152,7 +130,6 @@ public function test_emails_send_with_template() {
* Sending by email id.
*/
public function test_emails_send_by_id() {
Plugin_Manager::activate( 'newspack-newsletters' );
$test_email = self::get_test_email( 'test-email-config' );

$send_result = Emails::send_email(
Expand All @@ -172,7 +149,6 @@ public function test_emails_send_by_id() {
* Email post status handling.
*/
public function test_emails_status() {
Plugin_Manager::activate( 'newspack-newsletters' );
$test_email = self::get_test_email( 'test-email-config' );
wp_update_post(
[
Expand Down
Loading

0 comments on commit 340fcb5

Please sign in to comment.