Skip to content

Commit

Permalink
Finished the cmi5_connectors unit tests. Updated cmi5_connectors/mino…
Browse files Browse the repository at this point in the history
…r issues
  • Loading branch information
ADLMeganBohland committed Apr 9, 2024
1 parent 63efbea commit 7b9a6a1
Show file tree
Hide file tree
Showing 4 changed files with 1,072 additions and 283 deletions.
67 changes: 51 additions & 16 deletions classes/local/cmi5_connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public function cmi5launch_create_tenant($urltosend, $username, $password, $newt

// Return an array with tenant name and info.
return $returnedinfo;
} else {
return false;
};
}

Expand Down Expand Up @@ -151,6 +153,8 @@ public function cmi5launch_retrieve_registration_with_get($registration, $id) {
if ($resulttest == true) {

return $result;
} else {
return false;
}
}

Expand Down Expand Up @@ -209,6 +213,8 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) {
$registration = $registrationinfo["code"];

return $registration;
} else {
return false;
}
}

Expand Down Expand Up @@ -238,11 +244,13 @@ public function cmi5launch_retrieve_token($url, $username, $password, $audience,
$result = $this->cmi5launch_send_request_to_cmi5_player_post($data, $url, $filetype, $username, $password);

// Check result and display message if not 200.
$resulttest = $this->cmi5launch_connectors_error_message($result, "retrieving the registration");
$resulttest = $this->cmi5launch_connectors_error_message($result, "retrieving the token");

if ($resulttest == true) {

return $result;
} else {
return false;
}
}

Expand Down Expand Up @@ -271,6 +279,7 @@ public function cmi5launch_retrieve_url($id, $auindex) {
$playerurl = $settings['cmi5launchplayerurl'];
$courseid = $userscourse->courseid;


// Build URL for launch URL request.
$url = $playerurl . "/api/v1/course/" . $courseid ."/launch-url/" . $auindex;

Expand Down Expand Up @@ -303,6 +312,8 @@ public function cmi5launch_retrieve_url($id, $auindex) {
$urldecoded = json_decode($result, true);

return $urldecoded;
} else {
return false;
}
}

Expand All @@ -324,13 +335,14 @@ public function cmi5launch_send_request_to_cmi5_player_post($databody, $url, $fi
} else if ("json") {
$contenttype = "application/json\r\n";
}

// If number of args is greater than one it is for retrieving tenant info and args are username and password.
if (count($tokenorpassword) == 2 ) {

$username = $tokenorpassword[0];
$password = $tokenorpassword[1];


// Use key 'http' even if you send the request to https://...
// There can be multiple headers but as an array under the ONE header.
// Content(body) must be JSON encoded here, as that is what CMI5 player accepts.
Expand All @@ -344,13 +356,11 @@ public function cmi5launch_send_request_to_cmi5_player_post($databody, $url, $fi
),
);

// The options are here placed into a stream to be sent.
$context = stream_context_create($options);

// Sends the stream to the specified URL and stores results.
// The false is use_include_path, which we dont want in this case, we want to go to the url.
$result = file_get_contents( $url, false, $context );
$result = $this->cmi5launch_stream_and_send( $url, $options );


// Else the args are what we need for posting a course.
} else {

Expand All @@ -372,12 +382,11 @@ public function cmi5launch_send_request_to_cmi5_player_post($databody, $url, $fi
),
);

// The options are placed into a stream to be sent.
$context = stream_context_create(($options));


// Sends the stream to the specified URL and stores results.
// The false is use_include_path, which we dont want in this case, we want to go to the url.
$result = file_get_contents( $url, false, $context );
$result = $this->cmi5launch_stream_and_send( $url, $options );

}

Expand Down Expand Up @@ -406,14 +415,14 @@ public function cmi5launch_send_request_to_cmi5_player_get($token, $url) {
),
);

// The options are here placed into a stream to be sent.
$context = stream_context_create(($options));

// Sends the stream to the specified URL and stores results. False is to not use_include_path, we want to go to the url.
$launchresponse = file_get_contents( $url, false, $context );
// Sends the stream to the specified URL and stores results.
// The false is use_include_path, which we dont want in this case, we want to go to the url.
$launchresponse = $this->cmi5launch_stream_and_send( $url, $options );

$sessiondecoded = json_decode($launchresponse, true);

// Return response.
return $sessiondecoded;
}

Expand Down Expand Up @@ -444,6 +453,8 @@ public function cmi5launch_retrieve_session_info_from_player($sessionid, $id) {
if ($resulttest == true) {

return $result;
} else {
return false;
}
}

Expand All @@ -456,7 +467,7 @@ public function cmi5launch_retrieve_session_info_from_player($sessionid, $id) {
* @param string $type - The type missing to be added to the error message.
* @return bool
*/
public function cmi5launch_connectors_error_message($resulttotest, $type) {
public static function cmi5launch_connectors_error_message($resulttotest, $type) {

// Decode result because if it is not 200 then something went wrong
// If it's a string, decode it.
Expand All @@ -476,13 +487,14 @@ public function cmi5launch_connectors_error_message($resulttotest, $type) {

echo "<br>";

return false;
}
else if( array_key_exists("statusCode", $resulttest) && $resulttest["statusCode"] != 200) {

echo "<br>";

echo "Something went wrong " . $type . ". CMI5 Player returned " . $resulttotest["statusCode"] . " error. With message '"
. $resulttotest["message"] . "'." ;
echo "Something went wrong " . $type . ". CMI5 Player returned " . $resulttest["statusCode"] . " error. With message '"
. $resulttest["message"] . "'." ;
echo "<br>";

return false;
Expand All @@ -492,4 +504,27 @@ public function cmi5launch_connectors_error_message($resulttotest, $type) {
return true;
}
}

/**
* Wrapper function to allow for testing where file_get_contents cannot be overriden.
* Also has create_stream as this makes a resource which interfers with testing.
* @param mixed $url - the url to be sent to
* @param mixed $use_include_path
* @param mixed $context - the data to be sent
* @param mixed $offset
* @param mixed $maxlen
* @return mixed $result - either a string or false.
*/
public function cmi5launch_stream_and_send($options, $url) {

// The options are placed into a stream to be sent.
$context = stream_context_create(($options));

// Sends the stream to the specified URL and stores results.
// The false is use_include_path, which we dont want in this case, we want to go to the url.
$result = file_get_contents( $url, false, $context );

// Return result.
return $result;
}
}
115 changes: 95 additions & 20 deletions cmi5PHP/tests/cmi5TestHelpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace cmi5Test;

use mod_cmi5launch\local\course;
use PHPUnit\Framework\TestCase;


Expand All @@ -13,32 +14,106 @@
* @covers \auHelpers
* @covers \auHelpers::getAuProperties
*/
class cmi5TestHelpers

// maybe put fake 'records here and the other one can call it? easier than and neater looking then just ploppin it down

/**
* Make a fake record for testing.
* @return mixed
*/
function maketestcmi5launch()
{
private $auProperties, $emptyStatement, $mockStatementValues, $mockStatement2, $returnedAUids;
global $DB, $cmi5launch;
//Ok for starters, what is cmilaunch

// Make a cmi5launch record
$cmi5launch = new \stdClass();
$cmi5launch->id = 1;
$cmi5launch->course = 1;
$cmi5launch->name = 'Test cmi5launch';
$cmi5launch->intro = 'Test cmi5launch intro';
$cmi5launch->introformat = 1;
$cmi5launch->cmi5activityid = 'testcmi5activityid';
$cmi5launch->registrationid = 'testregistrationid';
$cmi5launch->returnurl = 'testreturnurl';
$cmi5launch->courseid = 1;
$cmi5launch->cmi5verbid = 'testcmi5verbid';
$cmi5launch->cmi5expiry = 1;
$cmi5launch->overridedefaults = 1;
$cmi5launch->cmi5multipleregs = 1;
$cmi5launch->timecreated = 00;
$cmi5launch->timemodified = 00;
$cmi5launch->courseinfo = 'testcourseinfo';
$cmi5launch->aus = '{testaus:1, testaus:2}';

public function get_file_get_contents() {
return [$this, 'file_get_contents'];
}

public $auidForTest;
$newid = $DB->insert_record('cmi5launch', $cmi5launch);

//Return something to test intead of file
public function file_get_contents($url, $false, $context)
{
return 'Test';
}
return $newid;

public function cmi5launch_settings($id)
{
if ($id == 0){
return array ('cmi5launchplayerurl'=>'https://cmi5launchplayerurl.com');
}
}

}
/**
* Delete a fake record created for testing.
* @param mixed $createdid - id that was created for testing by maketestcmi5launch.
* @return void
*/
function deletetestcmi5launch($createdid)
{
global $DB, $cmi5launch;
// Delete the cmi5launch record
$DB->delete_records('cmi5launch', array('id' => $createdid));



}

/**
* Create a fake course for testing.
* @param mixed $createdid - id that was created for testing by maketestcmi5launch.
* @return void
*/
function maketestcourse ($createdid){

global $DB, $cmi5launch, $USER;

// cmi5 launch is coming in null, looks like we need to make a fake one of that! Wonder if theres a way to make one just, first time testing?
// Instead of before every test, like a way to prepare the test environment?
// Reload cmi5 course instance.
$record = $DB->get_record('cmi5launch', array('id' => $createdid));

// Make a new course record.
$userscourse = new course($record);

// Populate with data for testing.
$userscourse->userid = $USER->id;
$userscourse->returnurl = 'https://testmoodle.com';
$userscourse->registrationid = 'testregistrationid';
$userscourse->moodlecourseid = $createdid;


// Save new record to DB.
$newid = $DB->insert_record('cmi5launch_usercourse', $userscourse);

return $newid;

}
/*
function get_file_get_contents() {
return ['file_get_contents'];
}
*/
global $file_get_contents;
// Ok lets make a file_et_contents for this namespace, we know it should return a json string?
// And what should it receive? just the reular arguments?
/**
* A local file_get_contents to overide the PHP function for testing.
* As we do not want to actually get the file, we just want to test the function calling that function.
*
*/
function file_get_contents($url, $use_include_path = false, $context = null, $offset = 0, $maxlen = null)
{
// Do we wamt to test whats passed in as well? pr ois that not necessary?
// MAybe we can return the args as json encoded string?
return json_encode(func_get_args());
}


?>
Loading

0 comments on commit 7b9a6a1

Please sign in to comment.