Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues related to Moodle coding standards #25

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

namespace webservice_restful\privacy;

defined('MOODLE_INTERNAL') || die();

/**
* Privacy provider implementation for webservice_restful.
*
Expand All @@ -43,7 +41,7 @@ class provider implements \core_privacy\local\metadata\null_provider {
*
* @return string
*/
public static function _get_reason() {
public static function get_reason(): string {
return 'privacy:metadata';
}
}
13 changes: 5 additions & 8 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

'webservice/restful:use' => array(
$capabilities = [
'webservice/restful:use' => [
'captype' => 'read', // In fact this may be considered read and write at the same time.
'contextlevel' => CONTEXT_COURSE, // The context level should be probably CONTEXT_MODULE.
'archetypes' => array(
),
),

);
'archetypes' => [],
],
];
9 changes: 4 additions & 5 deletions lang/en/webservice_restful.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['pluginname'] = 'RESTful protocol';
$string['restful:use'] = 'Use RESTful protocol';

$string['noauthheader'] = 'No Authorization header found in request sent to Moodle';
$string['nowsfunction'] = 'No webservice function found in URL sent to Moodle';
$string['noacceptheader'] = 'No Accept header found in request sent to Moodle';
$string['noauthheader'] = 'No Authorization header found in request sent to Moodle';
$string['notypeheader'] = 'No Content Type header found in request sent to Moodle';
$string['nowsfunction'] = 'No webservice function found in URL sent to Moodle';
$string['pluginname'] = 'RESTful protocol';
$string['privacy:metadata'] = 'The RESTful protocol plugin does not store any personal data.';
$string['restful:use'] = 'Use RESTful protocol';
28 changes: 14 additions & 14 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public function __construct($authmethod) {
* @return array $returnheaders The headers from Apache.
*/
private function get_apache_headers() {
$capitalizearray = array(
$capitalizearray = [
'Content-Type',
'Accept',
'Authorization',
'Content-Length',
'User-Agent',
'Host'
);
'Host',
];
$headers = apache_request_headers();
$returnheaders = array();
$returnheaders = [];

foreach ($headers as $key => $value) {
if (in_array($key, $capitalizearray)) {
Expand All @@ -92,7 +92,7 @@ private function get_apache_headers() {
* @return array $headers HTTP headers.
*/
private function get_headers($headers=null) {
$returnheaders = array();
$returnheaders = [];

if (!$headers) {
if (function_exists('apache_request_headers')) { // Apache websever.
Expand Down Expand Up @@ -265,7 +265,7 @@ protected function parse_request() {

// Get the webservice function parameters or return false.
if (empty($this->get_parameters())) {
$this->parameters = array();
$this->parameters = [];
} else if (!($this->parameters = $this->get_parameters())) {
return false;
}
Expand All @@ -292,7 +292,7 @@ public function run() {
// Set up exception handler first, we want to sent them back in correct format that
// the other system understands.
// We do not need to call the original default handler because this ws handler does everything.
set_exception_handler(array($this, 'exception_handler'));
set_exception_handler([$this, 'exception_handler']);

// Init all properties from the request data.
if (!$this->parse_request()) {
Expand All @@ -307,11 +307,11 @@ public function run() {
$this->load_function_info();

// Log the web service request.
$params = array(
'other' => array(
'function' => $this->functionname
)
);
$params = [
'other' => [
'function' => $this->functionname,
],
];
$event = \core\event\webservice_function_called::create($params);
$event->trigger();

Expand Down Expand Up @@ -415,7 +415,7 @@ protected function generate_error($ex) {
$errorobject->errorcode = $ex->errorcode;
}
$errorobject->message = $ex->getMessage();
if (debugging() and isset($ex->debuginfo)) {
if (debugging() && isset($ex->debuginfo)) {
$errorobject->debuginfo = $ex->debuginfo;
}
$error = json_encode($errorobject);
Expand All @@ -425,7 +425,7 @@ protected function generate_error($ex) {
$error .= '<ERRORCODE>' . htmlspecialchars($ex->errorcode, ENT_COMPAT, 'UTF-8')
. '</ERRORCODE>' . "\n";
$error .= '<MESSAGE>'.htmlspecialchars($ex->getMessage(), ENT_COMPAT, 'UTF-8').'</MESSAGE>'."\n";
if (debugging() and isset($ex->debuginfo)) {
if (debugging() && isset($ex->debuginfo)) {
$error .= '<DEBUGINFO>'.htmlspecialchars($ex->debuginfo, ENT_COMPAT, 'UTF-8').'</DEBUGINFO>'."\n";
}
$error .= '</EXCEPTION>'."\n";
Expand Down
106 changes: 57 additions & 49 deletions tests/server_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Restful server tests.
*
* @package webservice_restful
* @copyright Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace webservice_restful;

defined('MOODLE_INTERNAL') || die();
global $CFG;
Expand All @@ -34,13 +28,15 @@
* @copyright Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class webservice_restful_server_testcase extends advanced_testcase {
class server_test extends \advanced_testcase {

/**
* Test get header method extracts HTTP headers.
*
* @covers ::get_headers()
*/
public function test_get_headers() {
$headers = array(
public function test_get_headers(): void {
$headers = [
'USER' => 'www-data',
'HOME' => '/var/www',
'HTTP_CONTENT_LENGTH' => '17',
Expand All @@ -54,23 +50,23 @@ public function test_get_headers() {
'SERVER_PORT' => '80',
'SERVER_ADDR' => '192.168.56.103',
'REMOTE_PORT' => '39402',
'REMOTE_ADDR' => '192.168.56.1'
);
$expected = array(
'REMOTE_ADDR' => '192.168.56.1',
];
$expected = [
'HTTP_CONTENT_LENGTH' => '17',
'HTTP_AUTHORIZATION' => 'e71561c88ca7f0f0c94fee66ca07247b',
'HTTP_ACCEPT' => 'application/json',
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'HTTP_USER_AGENT' => 'curl/7.47.0',
'HTTP_HOST' => 'moodle.local',
);
];

$builder = $this->getMockBuilder('webservice_restful_server');
$builder->disableOriginalConstructor();
$stub = $builder->getMock();

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_headers');
$method = new \ReflectionMethod('webservice_restful_server', 'get_headers');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke($stub, $headers); // Get result of invoked method.

Expand All @@ -79,22 +75,23 @@ public function test_get_headers() {

/**
* Test get wstoken method extracts token.
*
* @covers ::get_wstoken()
*/
public function test_get_wstoken() {
$headers = array(
public function test_get_wstoken(): void {
$headers = [
'HTTP_AUTHORIZATION' => 'e71561c88ca7f0f0c94fee66ca07247b',
'HTTP_ACCEPT' => 'application/json',
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded',

);
];
$expected = 'e71561c88ca7f0f0c94fee66ca07247b';

$builder = $this->getMockBuilder('webservice_restful_server');
$builder->disableOriginalConstructor();
$stub = $builder->getMock();

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_wstoken');
$method = new \ReflectionMethod('webservice_restful_server', 'get_wstoken');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke($stub, $headers); // Get result of invoked method.

Expand All @@ -104,32 +101,35 @@ public function test_get_wstoken() {
/**
* Test get wstoken method correctly errors.
*
* @covers ::get_wstoken()
*/
public function test_get_wstoken_error() {
$headers = array();
public function test_get_wstoken_error(): void {
$headers = [];
$this->expectOutputString('{"exception":"moodle_exception",'
.'"errorcode":"noauthheader",'
.'"message":"No Authorization header found in request sent to Moodle"}');

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_wstoken');
$method = new \ReflectionMethod('webservice_restful_server', 'get_wstoken');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke(new webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $headers);
$proxy = $method->invoke(new \webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $headers);
}

/**
* Test get wsfunction method extracts function.
*
* @covers ::get_wsfunction()
*/
public function test_get_wsfunction() {
$getvars = array('file' => '/core_course_get_courses');
public function test_get_wsfunction(): void {
$getvars = ['file' => '/core_course_get_courses'];
$expected = 'core_course_get_courses';

$builder = $this->getMockBuilder('webservice_restful_server');
$builder->disableOriginalConstructor();
$stub = $builder->getMock();

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_wsfunction');
$method = new \ReflectionMethod('webservice_restful_server', 'get_wsfunction');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke($stub, $getvars); // Get result of invoked method.

Expand All @@ -138,37 +138,40 @@ public function test_get_wsfunction() {

/**
* Test get wsfunction method correctly errors.
*
* @covers ::get_wsfunction()
*/
public function test_get_wsfunction_error() {
$getvars = array();
public function test_get_wsfunction_error(): void {
$getvars = [];
$this->expectOutputString('{"exception":"moodle_exception",'
.'"errorcode":"nowsfunction",'
.'"message":"No webservice function found in URL sent to Moodle"}');

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_wsfunction');
$method = new \ReflectionMethod('webservice_restful_server', 'get_wsfunction');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke(new webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $getvars);
$proxy = $method->invoke(new \webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $getvars);
}

/**
* Test get response format method extracts response format.
*
* @covers ::get_responseformat()
*/
public function test_get_responseformat() {
$headers = array(
public function test_get_responseformat(): void {
$headers = [
'HTTP_AUTHORIZATION' => 'e71561c88ca7f0f0c94fee66ca07247b',
'HTTP_ACCEPT' => 'application/json',
'HTTP_CONTENT_TYPE' => 'application/xml',

);
];
$expected = 'json';

$builder = $this->getMockBuilder('webservice_restful_server');
$builder->disableOriginalConstructor();
$stub = $builder->getMock();

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_responseformat');
$method = new \ReflectionMethod('webservice_restful_server', 'get_responseformat');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke($stub, $headers); // Get result of invoked method.

Expand All @@ -177,37 +180,40 @@ public function test_get_responseformat() {

/**
* Test get response format method correctly errors.
*
* @covers ::get_responseformat()
*/
public function test_get_responseformat_error() {
$headers = array();
public function test_get_responseformat_error(): void {
$headers = [];
$this->expectOutputString('{"exception":"moodle_exception",'
.'"errorcode":"noacceptheader",'
.'"message":"No Accept header found in request sent to Moodle"}');

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_responseformat');
$method = new \ReflectionMethod('webservice_restful_server', 'get_responseformat');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke(new webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $headers);
$proxy = $method->invoke(new \webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $headers);
}

/**
* Test get request format method extracts request format.
*
* @covers ::get_requestformat()
*/
public function test_get_requestformat() {
$headers = array(
public function test_get_requestformat(): void {
$headers = [
'HTTP_AUTHORIZATION' => 'e71561c88ca7f0f0c94fee66ca07247b',
'HTTP_ACCEPT' => 'application/json',
'HTTP_CONTENT_TYPE' => 'application/xml',

);
];
$expected = 'xml';

$builder = $this->getMockBuilder('webservice_restful_server');
$builder->disableOriginalConstructor();
$stub = $builder->getMock();

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_requestformat');
$method = new \ReflectionMethod('webservice_restful_server', 'get_requestformat');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke($stub, $headers); // Get result of invoked method.

Expand All @@ -216,16 +222,18 @@ public function test_get_requestformat() {

/**
* Test get request format method correctly errors.
*
* @covers ::get_requestformat()
*/
public function test_get_requestformat_error() {
$headers = array();
public function test_get_requestformat_error(): void {
$headers = [];
$this->expectOutputString('{"exception":"moodle_exception",'
.'"errorcode":"notypeheader",'
.'"message":"No Content Type header found in request sent to Moodle"}');

// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('webservice_restful_server', 'get_requestformat');
$method = new \ReflectionMethod('webservice_restful_server', 'get_requestformat');
$method->setAccessible(true); // Allow accessing of private method.
$proxy = $method->invoke(new webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $headers);
$proxy = $method->invoke(new \webservice_restful_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN), $headers);
}
}
Loading
Loading