Skip to content

Commit daa0bdd

Browse files
refactor Api Pdf Settings arrange folder structure
1 parent 0971367 commit daa0bdd

7 files changed

Lines changed: 177 additions & 48 deletions

File tree

src/Api/V1/Pdf/Settings/Api_Pdf_Settings.php

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GFPDF\Api\V1\Base_Api;
66
use GFPDF\Helper\Helper_Misc;
77
use Psr\Log\LoggerInterface;
8+
use WP_Error;
89

910
/**
1011
* @package Gravity PDF
@@ -116,52 +117,33 @@ public function register() {
116117
}
117118

118119
/**
119-
* Create a file in our tmp directory and check if it is publically accessible (i.e no .htaccess protection)
120+
* Create a file in our tmp directory and check if it is publicly accessible (i.e no .htaccess protection)
120121
*
121122
* @param $_POST ['nonce']
122123
*
123124
* @return WP_REST_Response
124125
*
125126
* @since 5.2
126127
*/
127-
public function check_tmp_pdf_security( \WP_REST_Request $request ) {
128+
public function check_tmp_pdf_security() {
129+
/* first create the test file in the tmp directory */
130+
$this->create_public_tmp_directory_test_file();
128131

129-
/* Create our tmp file and do our actual check */
130-
$result = json_encode( $this->test_public_tmp_directory_access() );
131-
132-
if (!$result) {
133-
return new \WP_Error( 'test_public_tmp_directory_access', 'Unable to create tmp Directory', [ 'status' => 401 ] );
134-
}
135-
136-
return [ 'message' => 'Tmp file successfully created' ];
137-
}
138-
139-
/**
140-
* Create a file in our tmp directory and verify if it's protected from the public
141-
*
142-
* @return boolean
143-
*
144-
* @since 5.2
145-
*/
146-
public function test_public_tmp_directory_access() {
147-
148-
/* create our file */
149-
file_put_contents( $this->template_font_location . $this->tmp_test_file, 'failed-if-read' );
150-
151-
/* verify it exists */
152-
if ( is_file( $this->template_font_location . $this->tmp_test_file ) ) {
153-
154-
/* Run our test */
132+
/* check if tmp directotyr file is publicly accessible */
133+
if ( file_exists( $this->template_font_location . $this->tmp_test_file ) ) {
155134
$site_url = $this->misc->convert_path_to_url( $this->template_font_location );
156-
157-
if ( $site_url !== false ) {
158-
135+
136+
/* file found */
137+
if ( $site_url !== false ) {
159138
$response = wp_remote_get( $site_url . $this->tmp_test_file );
160139

161-
if ( ! is_wp_error( $response ) ) {
140+
/* Cleanup our test file */
141+
@unlink( $this->template_font_location . $this->tmp_test_file );
162142

143+
if ( ! is_wp_error( $response ) ) {
163144
/* Check if the web server responded with a OK status code and we can read the contents of our file, then fail our test */
164-
if ( isset( $response['response']['code'] ) && $response['response']['code'] === 200 &&
145+
if ( isset( $response['response']['code'] ) &&
146+
$response['response']['code'] === 200 &&
165147
isset( $response['body'] ) && $response['body'] === 'failed-if-read'
166148
) {
167149
$response_object = $response['http_response'];
@@ -174,17 +156,47 @@ public function test_public_tmp_directory_access() {
174156
'response' => $raw_response->raw,
175157
]
176158
);
177-
178-
$this->has_access = false;
159+
//@todo which one to return
160+
// success but file is publicly accessible
161+
// return [ 'message' => 'Tmp file successfully created but publicly accessible', 'has_access' => true ];
162+
return true;
179163
}
164+
//@todo which one to return
165+
// success and file is secured
166+
// return [ 'message' => 'Tmp file successfully created and not publicly accessible', 'has_access' => false ];
167+
return false;
180168
}
169+
/* Unable to get url */
170+
return new WP_Error( 'wp_remote_get_response', 'Response Error', [ 'status' => 400 ] );
181171
}
172+
/* Unable to convert path to url */
173+
return new WP_Error( 'convert_path_to_url', 'Unable to find path to convert to url', [ 'status' => 404 ] );
182174
}
175+
/* file or directory not created */
176+
return new WP_Error( 'create_public_tmp_directory_test_file', 'Tmp directory and test file not found', [ 'status' => 404 ] );
177+
}
183178

184-
/* Cleanup our test file */
185-
@unlink( $this->template_font_location . $this->tmp_test_file );
179+
/**
180+
* Create a file in our tmp directory
181+
*
182+
* @param $_POST ['nonce']
183+
*
184+
* @return Bool
185+
* @return WP_Error
186+
*
187+
* @since 5.2
188+
*/
189+
public function create_public_tmp_directory_test_file() {
190+
/* create our file */
191+
file_put_contents( $this->template_font_location . $this->tmp_test_file, 'failed-if-read' );
192+
193+
/* verify it exists */
194+
if ( is_file( $this->template_font_location . $this->tmp_test_file ) ) {
195+
return true;
196+
}
186197

187-
return $this->has_access;
198+
return new \WP_Error( 'test_public_tmp_directory_access', 'Unable to create tmp Directory', [ 'status' => 401 ] );
199+
188200
}
189201

190202
}

tests/phpunit/unit-tests/Api/FontCoreApiEndpointRoutes.php renamed to tests/phpunit/unit-tests/Api/V1/Fonts/Core/Test_Fonts_Core.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
*/
4242

4343
/**
44-
* Class TestFontCoreApiEndpoint
44+
* Class TestFontCore
4545
*
4646
* @package GFPDF\Tests\GravityPDF
4747
*
4848
* @group REST-API
4949
*/
50-
class TestFontCoreApiEndpointRoutes extends WP_UnitTestCase {
50+
class TestFontCore extends WP_UnitTestCase {
5151

5252
/**
5353
* @var $class

tests/phpunit/unit-tests/Api/ApiFontsEndpointRoutes.php renamed to tests/phpunit/unit-tests/Api/V1/Fonts/Test_Api_Fonts.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
*/
4545

4646
/**
47-
* Class TestApiFontsEndpoint
47+
* Class TestApiFonts
4848
*
4949
* @package GFPDF\Tests\GravityPDF
5050
*
5151
* @group REST-API
5252
*/
53-
class TestApiFontsEndpointRoutes extends WP_UnitTestCase {
53+
class TestApiFonts extends WP_UnitTestCase {
5454

5555
/**
5656
* @var $class

tests/phpunit/unit-tests/Api/V1/License/Test_Api_License.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@
4343
*/
4444

4545
/**
46-
* Class TestApiLicenseEndpoint
46+
* Class TestApiLicense
4747
*
4848
* @package GFPDF\Tests\GravityPDF
4949
*
5050
* @group REST-API
5151
*/
52-
class TestApiLicenseEndpointRoutes extends WP_UnitTestCase {
52+
class TestApiLicense extends WP_UnitTestCase {
5353

5454
/**
5555
* @var Api_License
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace GFPDF\Api\V1\Pdf\Settings;
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 TestApiPdfSettings
47+
*
48+
* @package GFPDF\Tests\GravityPDF
49+
*
50+
* @group REST-API
51+
*/
52+
class TestApiPdfSettings extends WP_UnitTestCase {
53+
54+
/**
55+
* @var Api_Pdf_Settings
56+
* @since 5.2
57+
*/
58+
protected $class;
59+
60+
/**
61+
* @var string
62+
*
63+
* @since 5.2
64+
*/
65+
protected $template_font_location;
66+
67+
/**
68+
* @var string
69+
*
70+
* @since 5.2
71+
*/
72+
protected $tmp_test_file = 'public_tmp_directory_test.txt';
73+
74+
/**
75+
* @since 5.2
76+
*/
77+
public function setUp() {
78+
79+
$this->template_font_location = plugin_dir_path(__FILE__) . 'tmp/gravityforms/fonts/';
80+
81+
// $this->data = GPDFAPI::get_data_class();
82+
$this->class = new Api_Pdf_Settings( GPDFAPI::get_log_class(), GPDFAPI::get_misc_class(), $this->template_font_location );
83+
$this->class->init();
84+
85+
parent::setUp();
86+
}
87+
88+
/**
89+
* @since 5.2
90+
*/
91+
public function test_rest_api_license_endpoints() {
92+
$wp_rest_server = rest_get_server();
93+
do_action( 'rest_api_init' );
94+
95+
$this->assertContains( 'gravity-pdf/v1', $wp_rest_server->get_namespaces() );
96+
$this->assertArrayHasKey( '/gravity-pdf/v1/pdf/settings', $wp_rest_server->get_routes() );
97+
}
98+
99+
/**
100+
* @since 5.2
101+
*/
102+
public function test_check_tmp_pdf_security() {
103+
104+
/* Test unable to access directory */
105+
$response = $this->class->check_tmp_pdf_security();
106+
107+
$this->assertSame( 401, $response->get_error_data( 'test_public_tmp_directory_access' )['status'] );
108+
109+
/* Test successful access on directory */
110+
// $request = $this->get_request( [ 'addon_name' => 'test', 'license' => '12345' ] );
111+
$response = $this->class->test_public_tmp_directory_access();
112+
$this->assertSame( 200, $response->get_error_data( 'test_public_tmp_directory_access' )['status'] );
113+
}
114+
115+
116+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
failed-if-read

tests/phpunit/unit-tests/Api/ApiTemplateEndpointRoutes.php renamed to tests/phpunit/unit-tests/Api/V1/Template/Test_Api_Template.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace GFPDF\Api\V1\Template\Core;
3+
namespace GFPDF\Api\V1\Template;
44

55
use GFPDF\Api\V1\Base_Api;
6-
use GFPDF\Api\V1\Template\Core;
6+
use GFPDF\Api\V1\Template;
77
use WP_UnitTestCase;
88
use WP_REST_Request;
99
use GPDFAPI;
@@ -41,13 +41,13 @@
4141
*/
4242

4343
/**
44-
* Class TestApiTemplateEndpoint
44+
* Class TestApiTemplate
4545
*
4646
* @package GFPDF\Tests\GravityPDF
4747
*
4848
* @group REST-API
4949
*/
50-
class TestApiTemplateEndpointRoutes extends WP_UnitTestCase {
50+
class TestApiTemplate extends WP_UnitTestCase {
5151

5252
/**
5353
* @var $class

0 commit comments

Comments
 (0)