-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues related to Moodle coding standards
- Loading branch information
Showing
5 changed files
with
81 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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', | ||
|
@@ -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. | ||
|
||
|
@@ -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. | ||
|
||
|
@@ -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. | ||
|
||
|
@@ -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. | ||
|
||
|
@@ -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. | ||
|
||
|
@@ -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); | ||
} | ||
} |