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/remove network dependent #3541

Merged
merged 3 commits into from
Nov 14, 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
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'] );
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik, all these tests are not making any requests. server->dispatch is emulating the server receiving a request

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the way to test Rest API endpoints without needing http requests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about testing the REST API endpoints. The endpoint handler is making requests to the plugin repository to fetch plugin code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha

* 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' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we short circuit wp_mail to make sure no requests are made?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not about wp_mail. The issue with this test is that it installed newspack-newsletters from the plugin repository. I've replaced it with a mock of Newspack_Newsletters class, and to simplify removed the test assertions related to the plugin being inactive. This is not ideal, I know, but I don't think making it ideal is worth the effort.

'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