|
| 1 | +<?php |
| 2 | + |
| 3 | +use Automattic\Jetpack\Connection\Tokens; |
| 4 | +use Automattic\Jetpack\Constants; |
| 5 | +use Automattic\Jetpack\My_Jetpack\Wpcom_Products; |
| 6 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | + |
| 9 | +require_once JETPACK__PLUGIN_DIR . '/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php'; |
| 10 | + |
| 11 | +/** |
| 12 | + * @covers \Jetpack_Core_API_Site_Endpoint |
| 13 | + */ |
| 14 | +#[CoversClass( Jetpack_Core_API_Site_Endpoint::class )] |
| 15 | +class Jetpack_Core_Api_Site_Purchases_Test extends WP_UnitTestCase { |
| 16 | + use \Automattic\Jetpack\PHPUnit\WP_UnitTestCase_Fix; |
| 17 | + |
| 18 | + /** |
| 19 | + * Requests sent to WordPress.com. |
| 20 | + * |
| 21 | + * @var int |
| 22 | + */ |
| 23 | + private $requests = 0; |
| 24 | + |
| 25 | + /** |
| 26 | + * Status code the mocked `/upgrades` request answers with. |
| 27 | + * |
| 28 | + * @var int |
| 29 | + */ |
| 30 | + private $status = 200; |
| 31 | + |
| 32 | + /** |
| 33 | + * Body the mocked `/upgrades` request answers with. |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + private $body = ''; |
| 38 | + |
| 39 | + public function set_up() { |
| 40 | + parent::set_up(); |
| 41 | + |
| 42 | + ( new Tokens() )->update_blog_token( 'test.test' ); |
| 43 | + Jetpack_Options::update_option( 'id', 123 ); |
| 44 | + Constants::set_constant( 'JETPACK__WPCOM_JSON_API_BASE', 'https://public-api.wordpress.com' ); |
| 45 | + Wpcom_Products::reset_purchases_cache(); |
| 46 | + Wpcom_Products::reset_request_failures(); |
| 47 | + add_filter( 'pre_http_request', array( $this, 'mock_upgrades' ), 10, 3 ); |
| 48 | + } |
| 49 | + |
| 50 | + public function tear_down() { |
| 51 | + remove_filter( 'pre_http_request', array( $this, 'mock_upgrades' ) ); |
| 52 | + Wpcom_Products::reset_purchases_cache(); |
| 53 | + Wpcom_Products::reset_request_failures(); |
| 54 | + Constants::clear_constants(); |
| 55 | + Jetpack_Options::delete_option( array( 'id', 'blog_token' ) ); |
| 56 | + |
| 57 | + parent::tear_down(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param false|array $preempt Whether to preempt the request. |
| 62 | + * @param array $args Request arguments. |
| 63 | + * @param string $url Request URL. |
| 64 | + * @return false|array |
| 65 | + */ |
| 66 | + public function mock_upgrades( $preempt, $args, $url ) { |
| 67 | + if ( ! str_contains( $url, '/upgrades' ) ) { |
| 68 | + return $preempt; |
| 69 | + } |
| 70 | + ++$this->requests; |
| 71 | + |
| 72 | + return array( |
| 73 | + 'body' => $this->body, |
| 74 | + 'response' => array( |
| 75 | + 'code' => $this->status, |
| 76 | + 'message' => '', |
| 77 | + ), |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return array[] |
| 83 | + */ |
| 84 | + public static function provide_purchases() { |
| 85 | + return array( |
| 86 | + 'one purchase' => array( '[{"product_slug":"jetpack_complete","expiry_date":"2027-11-18T00:00:00+00:00"}]' ), |
| 87 | + 'no purchases' => array( '[]' ), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @dataProvider provide_purchases |
| 93 | + * |
| 94 | + * @param string $body The `/upgrades` response body. |
| 95 | + */ |
| 96 | + #[DataProvider( 'provide_purchases' )] |
| 97 | + public function test_reuses_the_my_jetpack_purchases_lookup_and_keeps_the_payload( $body ) { |
| 98 | + $this->body = $body; |
| 99 | + Wpcom_Products::get_site_current_purchases(); |
| 100 | + |
| 101 | + $response = Jetpack_Core_API_Site_Endpoint::get_purchases(); |
| 102 | + |
| 103 | + $this->assertSame( 1, $this->requests ); |
| 104 | + $this->assertSame( json_decode( $body, true ), json_decode( $response->get_data()['data'], true ) ); |
| 105 | + } |
| 106 | + |
| 107 | + public function test_failed_lookup_returns_an_error() { |
| 108 | + $this->status = 500; |
| 109 | + |
| 110 | + $result = Jetpack_Core_API_Site_Endpoint::get_purchases(); |
| 111 | + |
| 112 | + $this->assertWPError( $result ); |
| 113 | + $this->assertSame( 'failed_to_fetch_data', $result->get_error_code() ); |
| 114 | + } |
| 115 | +} |
0 commit comments