|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GFPDF\Api\V1\License; |
| 4 | + |
| 5 | +use GFPDF\Helper\Helper_Data; |
| 6 | +use GFPDF\Helper\Helper_Abstract_Addon; |
| 7 | +use GFPDF\Helper\Helper_Logger; |
| 8 | +use GFPDF\Helper\Helper_Singleton; |
| 9 | +use WP_UnitTestCase; |
| 10 | +use WP_REST_Request; |
| 11 | +use GPDFAPI; |
| 12 | + |
| 13 | +/** |
| 14 | + * @package Gravity PDF GravityPDF |
| 15 | + * @copyright Copyright (c) 2018, Blue Liquid Designs |
| 16 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 17 | + * @since 1.0 |
| 18 | + */ |
| 19 | + |
| 20 | +/* Exit if accessed directly */ |
| 21 | +if ( ! defined( 'ABSPATH' ) ) { |
| 22 | + exit; |
| 23 | +} |
| 24 | + |
| 25 | +/* |
| 26 | + This file is part of Gravity PDF GravityPDF. |
| 27 | +
|
| 28 | + Copyright (C) 2018, Blue Liquid Designs |
| 29 | +
|
| 30 | + This program is free software; you can redistribute it and/or modify |
| 31 | + it under the terms of the GNU General Public License as published by |
| 32 | + the Free Software Foundation; either version 2 of the License, or |
| 33 | + (at your option) any later version. |
| 34 | +
|
| 35 | + This program is distributed in the hope that it will be useful, |
| 36 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | + GNU General Public License for more details. |
| 39 | +
|
| 40 | + You should have received a copy of the GNU General Public License |
| 41 | + along with this program; if not, write to the Free Software |
| 42 | + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 43 | +*/ |
| 44 | + |
| 45 | +/** |
| 46 | + * Class TestApiLicenseEndpoint |
| 47 | + * |
| 48 | + * @package GFPDF\Tests\GravityPDF |
| 49 | + * |
| 50 | + * @group REST-API |
| 51 | + */ |
| 52 | +class TestApiLicenseEndpointRoutes extends WP_UnitTestCase { |
| 53 | + |
| 54 | + /** |
| 55 | + * @var Api_License |
| 56 | + * @since 5.2 |
| 57 | + */ |
| 58 | + protected $class; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var Helper_Data |
| 62 | + */ |
| 63 | + protected $data; |
| 64 | + |
| 65 | + /** |
| 66 | + * @since 5.2 |
| 67 | + */ |
| 68 | + public function setUp() { |
| 69 | + $this->data = GPDFAPI::get_data_class(); |
| 70 | + $this->class = new Api_License( GPDFAPI::get_log_class(), $this->data ); |
| 71 | + $this->class->init(); |
| 72 | + |
| 73 | + parent::setUp(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @since 5.2 |
| 78 | + */ |
| 79 | + public function test_rest_api_license_endpoints() { |
| 80 | + $wp_rest_server = rest_get_server(); |
| 81 | + do_action( 'rest_api_init' ); |
| 82 | + |
| 83 | + $this->assertContains( 'gravity-pdf/v1', $wp_rest_server->get_namespaces() ); |
| 84 | + $this->assertArrayHasKey( '/gravity-pdf/v1/license/(?P<id>\d+)/deactivate', $wp_rest_server->get_routes() ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param array $data |
| 89 | + * |
| 90 | + * @return WP_REST_Request |
| 91 | + * |
| 92 | + * @since 5.2 |
| 93 | + */ |
| 94 | + protected function get_request( $data ) { |
| 95 | + $request = new WP_REST_Request(); |
| 96 | + $request->set_body( json_encode( $data ) ); |
| 97 | + $request->set_header( 'content-type', 'application/json' ); |
| 98 | + $request->get_json_params(); |
| 99 | + |
| 100 | + return $request; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @since 5.2 |
| 105 | + */ |
| 106 | + public function test_deactivate_license_key_validation() { |
| 107 | + $request = $this->get_request( [ 'addon_name' => '', 'license' => '' ] ); |
| 108 | + $response = $this->class->process_license_deactivation( $request ); |
| 109 | + $this->assertSame( 400, $response->get_error_data( 'license_deactivation_fields_missing' )['status'] ); |
| 110 | + |
| 111 | + /* Test unregistered addon */ |
| 112 | + $request = $this->get_request( [ 'addon_name' => 'test', 'license' => '12345' ] ); |
| 113 | + $response = $this->class->process_license_deactivation( $request ); |
| 114 | + $this->assertSame( 404, $response->get_error_data( 'license_deactivation_addon_not_found' )['status'] ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @since 5.2 |
| 119 | + */ |
| 120 | + public function test_deactivate_license_key_api() { |
| 121 | + $request = $this->get_request( [ 'addon_name' => 'test', 'license' => '12345' ] ); |
| 122 | + |
| 123 | + /* Mock remote request and simulate success */ |
| 124 | + $this->data->addon['test'] = new TestAddon( 'test', 'Test', 'Gravity PDF', '1.0', '', $this->data, GPDFAPI::get_options_class(), new Helper_Singleton(), new Helper_Logger( 'test', 'Test' ), GPDFAPI::get_notice_class() ); |
| 125 | + |
| 126 | + $api_response = function() { |
| 127 | + return new \WP_Error(); |
| 128 | + }; |
| 129 | + |
| 130 | + add_filter( 'pre_http_request', $api_response ); |
| 131 | + |
| 132 | + $response = $this->class->process_license_deactivation( $request ); |
| 133 | + $this->assertSame( 400, $response->get_error_data( 'license_deactivation_schedule_license_check' )['status'] ); |
| 134 | + |
| 135 | + remove_filter( 'pre_http_request', $api_response ); |
| 136 | + |
| 137 | + /* Get a success test */ |
| 138 | + $api_response = function() { |
| 139 | + return [ |
| 140 | + 'response' => [ 'code' => 200 ], |
| 141 | + 'body' => json_encode( [ 'license' => 'deactivated' ] ), |
| 142 | + ]; |
| 143 | + }; |
| 144 | + |
| 145 | + add_filter( 'pre_http_request', $api_response ); |
| 146 | + $response = $this->class->process_license_deactivation( $request ); |
| 147 | + $this->assertArrayHasKey( 'success', $response ); |
| 148 | + remove_filter( 'pre_http_request', $api_response ); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +class TestAddon extends Helper_Abstract_Addon { |
| 153 | + public function plugin_updater() { |
| 154 | + |
| 155 | + } |
| 156 | +} |
0 commit comments