2019-12-30 07:47:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-04-20 02:04:13 +00:00
|
|
|
* Plugins REST API Test
|
2019-12-30 07:47:15 +00:00
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2019-12-30 07:47:15 +00:00
|
|
|
*/
|
|
|
|
|
2020-04-20 02:04:13 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\API\Plugins;
|
2021-11-16 13:57:23 +00:00
|
|
|
use Automattic\WooCommerce\Admin\PaymentMethodSuggestionsDataSourcePoller;
|
2019-12-30 07:47:15 +00:00
|
|
|
|
|
|
|
/**
|
2020-04-20 02:04:13 +00:00
|
|
|
* WC Tests API Plugins
|
2019-12-30 07:47:15 +00:00
|
|
|
*/
|
2020-04-20 02:04:13 +00:00
|
|
|
class WC_Tests_API_Plugins extends WC_REST_Unit_Test_Case {
|
2019-12-30 07:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoints.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-20 02:04:13 +00:00
|
|
|
protected $endpoint = '/wc-admin/plugins';
|
2019-12-30 07:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup test data. Called before every test.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = $this->factory->user->create(
|
|
|
|
array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that installation without permission is unauthorized.
|
|
|
|
*/
|
|
|
|
public function test_install_without_permission() {
|
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'POST', $this->endpoint . '/install' ) );
|
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that installing a valid plugin works.
|
|
|
|
*/
|
|
|
|
public function test_install_plugin() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/install' );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2020-05-27 07:28:13 +00:00
|
|
|
'plugins' => 'facebook-for-woocommerce',
|
2019-12-30 07:47:15 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$plugins = get_plugins();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2020-05-27 07:28:13 +00:00
|
|
|
$this->assertEquals( array( 'facebook-for-woocommerce' ), $data['data']['installed'] );
|
|
|
|
$this->assertEquals( true, $data['success'] );
|
2019-12-30 07:47:15 +00:00
|
|
|
$this->assertArrayHasKey( 'facebook-for-woocommerce/facebook-for-woocommerce.php', $plugins );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-27 07:28:13 +00:00
|
|
|
* Test that installing with invalid params fails.
|
2019-12-30 07:47:15 +00:00
|
|
|
*/
|
2020-05-27 07:28:13 +00:00
|
|
|
public function test_install_invalid_plugins_param() {
|
2019-12-30 07:47:15 +00:00
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
2020-05-27 07:28:13 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/install' );
|
2019-12-30 07:47:15 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2020-05-27 07:28:13 +00:00
|
|
|
$this->assertEquals( 'woocommerce_rest_invalid_plugins', $data['code'] );
|
2019-12-30 07:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that activating a valid plugin works.
|
|
|
|
*/
|
|
|
|
public function test_activate_plugin() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/activate' );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'plugins' => 'facebook-for-woocommerce',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
2020-04-20 02:04:13 +00:00
|
|
|
$active_plugins = Plugins::get_active_plugins();
|
2019-12-30 07:47:15 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2020-05-27 07:28:13 +00:00
|
|
|
$this->assertContains( 'facebook-for-woocommerce', $data['data']['activated'] );
|
|
|
|
$this->assertEquals( true, $data['success'] );
|
2019-12-30 07:47:15 +00:00
|
|
|
$this->assertContains( 'facebook-for-woocommerce', $active_plugins );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-27 07:28:13 +00:00
|
|
|
* Test that activating with invalid params fails.
|
|
|
|
*/
|
|
|
|
public function test_activate_invalid_plugins_param() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/activate' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'woocommerce_rest_invalid_plugins', $data['code'] );
|
|
|
|
}
|
2021-04-20 17:17:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that recommended payment plugins are returned correctly.
|
|
|
|
*/
|
|
|
|
public function test_get_recommended_payment_plugins() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
set_transient(
|
2021-11-16 13:57:23 +00:00
|
|
|
'woocommerce_admin_' . PaymentMethodSuggestionsDataSourcePoller::ID . '_specs',
|
2021-04-20 17:17:19 +00:00
|
|
|
array(
|
2021-11-16 13:57:23 +00:00
|
|
|
(object) array(
|
|
|
|
'plugins' => array( 'plugin' ),
|
|
|
|
'title' => 'test',
|
2021-04-20 17:17:19 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/recommended-payment-plugins' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 1, count( $data ) );
|
2021-11-16 13:57:23 +00:00
|
|
|
$this->assertEquals( 'plugin', $data[0]->plugins[0] );
|
|
|
|
delete_transient( 'woocommerce_admin_' . PaymentMethodSuggestionsDataSourcePoller::ID . '_specs' );
|
2021-04-20 17:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-16 13:57:23 +00:00
|
|
|
* @return string locale
|
|
|
|
*/
|
|
|
|
public function set_france_locale() {
|
|
|
|
return 'fr_FR';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that recommended payment plugins are not returned when active.
|
2021-04-20 17:17:19 +00:00
|
|
|
*/
|
2021-11-16 13:57:23 +00:00
|
|
|
public function test_get_recommended_payment_plugins_that_are_active() {
|
2021-04-20 17:17:19 +00:00
|
|
|
wp_set_current_user( $this->user );
|
2021-11-16 13:57:23 +00:00
|
|
|
update_option( 'active_plugins', array( 'woocommerce-gateway-stripe/woocommerce-gateway-stripe.php' ) );
|
2021-04-20 17:17:19 +00:00
|
|
|
set_transient(
|
2021-11-16 13:57:23 +00:00
|
|
|
'woocommerce_admin_' . PaymentMethodSuggestionsDataSourcePoller::ID . '_specs',
|
2021-04-20 17:17:19 +00:00
|
|
|
array(
|
2021-11-16 13:57:23 +00:00
|
|
|
(object) array(
|
|
|
|
'plugins' => array( 'woocommerce-gateway-stripe' ),
|
|
|
|
'title' => 'test',
|
|
|
|
'is_visible' => array(
|
|
|
|
(object) array(
|
|
|
|
'type' => 'not',
|
|
|
|
'operand' => array(
|
|
|
|
(object) array(
|
|
|
|
'type' => 'plugins_activated',
|
|
|
|
'plugins' => array( 'woocommerce-gateway-stripe' ),
|
|
|
|
),
|
2021-04-20 17:17:19 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/recommended-payment-plugins' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2021-11-16 13:57:23 +00:00
|
|
|
$this->assertEquals( 0, count( $data ) );
|
|
|
|
delete_transient( 'woocommerce_admin_' . PaymentMethodSuggestionsDataSourcePoller::ID . '_specs' );
|
|
|
|
|
|
|
|
delete_option( 'active_plugins' );
|
2021-04-20 17:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-16 13:57:23 +00:00
|
|
|
* Test that recommended payment plugins are not returned when active.
|
2021-04-20 17:17:19 +00:00
|
|
|
*/
|
2021-11-16 13:57:23 +00:00
|
|
|
public function test_plugins_when_dismissed_is_set_to_yes() {
|
2021-04-20 17:17:19 +00:00
|
|
|
wp_set_current_user( $this->user );
|
2021-11-16 13:57:23 +00:00
|
|
|
update_option( 'woocommerce_setting_payments_recommendations_hidden', 'yes' );
|
2021-04-20 17:17:19 +00:00
|
|
|
set_transient(
|
2021-11-16 13:57:23 +00:00
|
|
|
'woocommerce_admin_' . PaymentMethodSuggestionsDataSourcePoller::ID . '_specs',
|
2021-04-20 17:17:19 +00:00
|
|
|
array(
|
2021-11-16 13:57:23 +00:00
|
|
|
(object) array(
|
|
|
|
'plugins' => array( 'plugin' ),
|
|
|
|
'title' => 'test',
|
2021-04-20 17:17:19 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/recommended-payment-plugins' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 0, count( $data ) );
|
2021-11-16 13:57:23 +00:00
|
|
|
delete_transient( 'woocommerce_admin_' . PaymentMethodSuggestionsDataSourcePoller::ID . '_specs' );
|
|
|
|
delete_option( 'woocommerce_setting_payments_recommendations_hidden' );
|
2021-04-20 17:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-16 13:57:23 +00:00
|
|
|
* Test dismissing recommended payment plugins endpoint.
|
2021-04-20 17:17:19 +00:00
|
|
|
*/
|
2021-11-16 13:57:23 +00:00
|
|
|
public function test_dismiss_recommended_payment_plugins() {
|
|
|
|
$this->assertEquals( 'no', get_option( 'woocommerce_setting_payments_recommendations_hidden', 'no' ) );
|
2021-04-20 17:17:19 +00:00
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
2021-11-16 13:57:23 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/recommended-payment-plugins/dismiss' );
|
2021-04-20 17:17:19 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2021-11-16 13:57:23 +00:00
|
|
|
$this->assertEquals( true, $data );
|
|
|
|
|
|
|
|
$this->assertEquals( 'yes', get_option( 'woocommerce_setting_payments_recommendations_hidden' ) );
|
|
|
|
delete_option( 'woocommerce_setting_payments_recommendations_hidden' );
|
2021-04-20 17:17:19 +00:00
|
|
|
}
|
2019-12-30 07:47:15 +00:00
|
|
|
}
|