diff --git a/tests/mocks/newsletters-mocks.php b/tests/mocks/newsletters-mocks.php index f0e4d7a0fa..6c935dd3f8 100644 --- a/tests/mocks/newsletters-mocks.php +++ b/tests/mocks/newsletters-mocks.php @@ -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 ); + } + } +} diff --git a/tests/unit-tests/api-plugins-controller.php b/tests/unit-tests/api-plugins-controller.php index abc942177c..401459bc4a 100644 --- a/tests/unit-tests/api-plugins-controller.php +++ b/tests/unit-tests/api-plugins-controller.php @@ -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() ) ); } /** @@ -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. */ diff --git a/tests/unit-tests/emails.php b/tests/unit-tests/emails.php index 99e781715e..36df4b835a 100644 --- a/tests/unit-tests/emails.php +++ b/tests/unit-tests/emails.php @@ -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', - 'someone@example.com' - ); - 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.' @@ -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 = 'tester@tests.com'; @@ -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( @@ -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( [ diff --git a/tests/unit-tests/plugin-manager.php b/tests/unit-tests/plugin-manager.php index 992aa40319..ab1207349b 100644 --- a/tests/unit-tests/plugin-manager.php +++ b/tests/unit-tests/plugin-manager.php @@ -11,55 +11,6 @@ * Test plugin management functionality. */ class Newspack_Test_Plugin_Manager extends WP_UnitTestCase { - - /** - * Plugin slug/folder. - * - * @var string - */ - protected $plugin_slug = 'pwa'; - - /** - * Plugin file path. - * - * @var string - */ - protected $plugin_file = 'pwa/pwa.php'; - - /** - * URL to plugin download. - * - * @var string - */ - protected $plugin_url = 'https://downloads.wordpress.org/plugin/pwa.0.7.1.zip'; - - /** - * Reset the global state when running each test. This helps avoid issues stemming from redefined constants. - * - * @var boolean - */ - protected $preserveGlobalState = false; // phpcs:ignore - - /** - * Run each test in a separate process. This helps avoid issues stemming from redefined constants. - * - * @var boolean - */ - protected $runTestInSeparateProcess = true; // phpcs:ignore - - /** - * Compatibility checks and clean up. - */ - public function set_up() { - // These tests can't run on environments where we can't install plugins e.g. VIP Go. - if ( ! Plugin_Manager::can_install_plugins() ) { - $this->markTestSkipped( 'Plugin installation is not allowed in the environment' ); - } - - // Clean up any lingering plugin installs that may exist from a previous test. - Plugin_Manager::uninstall( $this->plugin_file ); - } - /** * Test Plugin_Manager::get_managed_plugins. */ @@ -101,131 +52,4 @@ public function test_get_plugin_slug() { $this->assertEquals( false, Plugin_Manager::get_plugin_slug( new WP_Error() ) ); $this->assertEquals( false, Plugin_Manager::get_plugin_slug( 20 ) ); } - - /** - * Test that activating an uninstalled plugin from a slug will install and activate the plugin. - * Deactivating the plugin will not uninstall the plugin. - */ - public function test_activate_deactivate_wporg_uninstalled() { - $this->assertFalse( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - - $this->assertTrue( Plugin_Manager::activate( $this->plugin_slug ) ); - $this->assertTrue( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - $this->assertTrue( is_plugin_active( $this->plugin_file ) ); - - $this->assertTrue( Plugin_manager::deactivate( $this->plugin_file ) ); - $this->assertTrue( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - $this->assertFalse( is_plugin_active( $this->plugin_file ) ); - } - - /** - * Test that activating an uninstalled plugin from an URL will install and activate the plugin. - * Deactivating the plugin will not uninstall the plugin. - */ - public function test_activate_deactivate_url_uninstalled() { - $this->assertFalse( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - - $this->assertTrue( Plugin_Manager::activate( $this->plugin_url ) ); - $this->assertTrue( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - $this->assertTrue( is_plugin_active( $this->plugin_file ) ); - - $this->assertTrue( Plugin_manager::deactivate( $this->plugin_file ) ); - $this->assertTrue( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - $this->assertFalse( is_plugin_active( $this->plugin_file ) ); - } - - /** - * Test error handling when activating/deactivating. - */ - public function test_activate_deactivate_errors() { - $this->assertTrue( is_wp_error( Plugin_manager::activate( 'non-existant-plugin' ) ) ); - $this->assertTrue( is_wp_error( Plugin_manager::deactivate( 'non-existant-plugin' ) ) ); - - $this->assertTrue( is_wp_error( Plugin_manager::activate( 'https://downloads.wordpress.org/plugin/non-existant-plugin.zip' ) ) ); - $this->assertTrue( is_wp_error( Plugin_manager::deactivate( 'https://downloads.wordpress.org/plugin/non-existant-plugin.zip' ) ) ); - - $this->assertTrue( is_wp_error( Plugin_manager::activate( 'https://example.org/not-a-zip' ) ) ); - $this->assertTrue( is_wp_error( Plugin_manager::deactivate( 'https://example.org/not-a-zip' ) ) ); - } - - /** - * Test that activating/deactivating an installed plugin activates/deactivates the plugin. - */ - public function test_activate_deactivate_installed() { - Plugin_Manager::install( $this->plugin_slug ); - - // Activate by slug. - $this->assertTrue( Plugin_Manager::activate( $this->plugin_slug ) ); - $this->assertTrue( is_plugin_active( $this->plugin_file ) ); - - // If the plugin is already activated, activating it by slug should fail. - $this->assertTrue( is_wp_error( Plugin_manager::activate( $this->plugin_slug ) ) ); - - // Deactivate by slug. - $this->assertTrue( Plugin_manager::deactivate( $this->plugin_slug ) ); - $this->assertFalse( is_plugin_active( $this->plugin_file ) ); - - // If the plugin is already deactivated, deactivating should fail. - $this->assertTrue( is_wp_error( Plugin_manager::deactivate( $this->plugin_file ) ) ); - - // Activate by URL. - $this->assertTrue( Plugin_Manager::activate( $this->plugin_url ) ); - $this->assertTrue( is_plugin_active( $this->plugin_file ) ); - - // If the plugin is already activated, activating it by url should fail. - $this->assertTrue( is_wp_error( Plugin_manager::activate( $this->plugin_url ) ) ); - - // Deactivate by file. - $this->assertTrue( Plugin_manager::deactivate( $this->plugin_file ) ); - $this->assertFalse( is_plugin_active( $this->plugin_file ) ); - } - - /** - * Test that plugins install/uninstall from WordPress.org by slug. - */ - public function test_plugin_install_uninstall_wporg() { - $this->assertTrue( Plugin_Manager::install( $this->plugin_slug ) ); - $this->assertTrue( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - - // If the plugin is already installed, installing it by slug should fail. - $this->assertTrue( is_wp_error( Plugin_manager::install( $this->plugin_slug ) ) ); - - // The following test can't be performend until the core bug referenced below is resolved. - if ( defined( 'WP_UNINSTALL_PLUGIN' ) ) { - return $this->markTestSkipped( 'Cannot call delete_plugin more than once due to a core error. See: https://core.trac.wordpress.org/ticket/44884' ); - } - - // Uninstall by slug. - $this->assertTrue( Plugin_Manager::uninstall( $this->plugin_slug ) ); - $this->assertFalse( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - } - - /** - * Test that plugins install/uninstall from a URL. - */ - public function test_plugin_install_uninstall_url() { - $this->assertTrue( Plugin_Manager::install( $this->plugin_url ) ); - $this->assertTrue( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - - // If the plugin is already installed, installing it by URL should fail. - $this->assertTrue( is_wp_error( Plugin_manager::install( $this->plugin_url ) ) ); - - // Uninstall by file. - $this->assertTrue( Plugin_Manager::uninstall( $this->plugin_file ) ); - $this->assertFalse( file_exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ) ); - } - - /** - * Test error handling when installing/uninstalling. - */ - public function test_plugin_install_uninstall_errors() { - $this->assertTrue( is_wp_error( Plugin_manager::install( 'non-existant-plugin' ) ) ); - $this->assertTrue( is_wp_error( Plugin_manager::uninstall( 'non-existant-plugin' ) ) ); - - $this->assertTrue( is_wp_error( Plugin_manager::install( 'https://downloads.wordpress.org/plugin/non-existant-plugin.zip' ) ) ); - $this->assertTrue( is_wp_error( Plugin_manager::uninstall( 'https://downloads.wordpress.org/plugin/non-existant-plugin.zip' ) ) ); - - $this->assertTrue( is_wp_error( Plugin_manager::install( 'https://example.org/not-a-zip' ) ) ); - $this->assertTrue( is_wp_error( Plugin_manager::uninstall( 'https://example.org/not-a-zip' ) ) ); - } } diff --git a/tests/unit-tests/reader-activation-sync-woocommerce.php b/tests/unit-tests/reader-activation-sync-woocommerce.php index 786ed8aaf2..186173351b 100644 --- a/tests/unit-tests/reader-activation-sync-woocommerce.php +++ b/tests/unit-tests/reader-activation-sync-woocommerce.php @@ -166,7 +166,12 @@ public function test_payment_metadata_with_failed_order() { self::$current_order = $order; $contact_data = Sync\WooCommerce::get_contact_from_order( $order ); - $this->assertEquals( $contact_data['metadata']['last_payment_date'], $previous_order->get_date_paid()->date( Metadata::DATE_FORMAT ) ); + + $this->assertEquals( + // Disregard the seconds in comparison to avoid flaky tests. + substr( $contact_data['metadata']['last_payment_date'], 0, -3 ), + substr( $previous_order->get_date_paid()->date( Metadata::DATE_FORMAT ), 0, -3 ) + ); $this->assertEquals( $contact_data['metadata']['last_payment_amount'], self::$current_order->get_total() ); } diff --git a/tests/unit-tests/theme-manager.php b/tests/unit-tests/theme-manager.php deleted file mode 100644 index 32f018c016..0000000000 --- a/tests/unit-tests/theme-manager.php +++ /dev/null @@ -1,88 +0,0 @@ -markTestSkipped( 'Plugin installation is not allowed in the environment' ); - } - } - - /** - * Test installing Newspack theme. - */ - public function test_install_activate_default() { - $result = Theme_Manager::install_activate_theme(); - $this->assertTrue( $result ); - $this->assertEquals( 'newspack-theme', get_stylesheet() ); - } - - /** - * Test installing non-existent child theme. - */ - public function test_install_nonexistent_child_theme() { - $previous_stylesheet = get_stylesheet(); - - $result = Theme_Manager::install_activate_theme( 'newspack-fake' ); - $this->assertTrue( is_wp_error( $result ) ); - $this->assertEquals( $previous_stylesheet, get_stylesheet() ); - } - - /** - * Test installing Newspack Sacha child theme. - */ - public function test_install_activate_sacha() { - $result = Theme_Manager::install_activate_theme( 'newspack-sacha' ); - $this->assertTrue( $result ); - $this->assertEquals( 'newspack-sacha', get_stylesheet() ); - } - - /** - * Test installing Newspack Scott child theme. - */ - public function test_install_activate_scott() { - $result = Theme_Manager::install_activate_theme( 'newspack-scott' ); - $this->assertTrue( $result ); - $this->assertEquals( 'newspack-scott', get_stylesheet() ); - } - - /** - * Test installing Newspack Nelson child theme. - */ - public function test_install_activate_nelson() { - $result = Theme_Manager::install_activate_theme( 'newspack-nelson' ); - $this->assertTrue( $result ); - $this->assertEquals( 'newspack-nelson', get_stylesheet() ); - } - - /** - * Test installing Newspack Katharine child theme. - */ - public function test_install_activate_katharine() { - $result = Theme_Manager::install_activate_theme( 'newspack-katharine' ); - $this->assertTrue( $result ); - $this->assertEquals( 'newspack-katharine', get_stylesheet() ); - } - - /** - * Test installing Newspack Joseph child theme. - */ - public function test_install_activate_joseph() { - $result = Theme_Manager::install_activate_theme( 'newspack-joseph' ); - $this->assertTrue( $result ); - $this->assertEquals( 'newspack-joseph', get_stylesheet() ); - } -}