diff --git a/classes/local/cmi5_connectors.php b/classes/local/cmi5_connectors.php index d87d803..a3426a0 100755 --- a/classes/local/cmi5_connectors.php +++ b/classes/local/cmi5_connectors.php @@ -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; }; } @@ -151,6 +153,8 @@ public function cmi5launch_retrieve_registration_with_get($registration, $id) { if ($resulttest == true) { return $result; + } else { + return false; } } @@ -209,6 +213,8 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) { $registration = $registrationinfo["code"]; return $registration; + } else { + return false; } } @@ -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; } } @@ -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; @@ -303,6 +312,8 @@ public function cmi5launch_retrieve_url($id, $auindex) { $urldecoded = json_decode($result, true); return $urldecoded; + } else { + return false; } } @@ -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. @@ -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 { @@ -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 ); } @@ -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; } @@ -444,6 +453,8 @@ public function cmi5launch_retrieve_session_info_from_player($sessionid, $id) { if ($resulttest == true) { return $result; + } else { + return false; } } @@ -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. @@ -476,13 +487,14 @@ public function cmi5launch_connectors_error_message($resulttotest, $type) { echo "
"; + return false; } else if( array_key_exists("statusCode", $resulttest) && $resulttest["statusCode"] != 200) { echo "
"; - 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 "
"; return false; @@ -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; + } } diff --git a/cmi5PHP/tests/cmi5TestHelpers.php b/cmi5PHP/tests/cmi5TestHelpers.php index bae3629..2897fa0 100755 --- a/cmi5PHP/tests/cmi5TestHelpers.php +++ b/cmi5PHP/tests/cmi5TestHelpers.php @@ -1,6 +1,7 @@ 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()); + } + + ?> diff --git a/cmi5PHP/tests/cmi5_connectorsTest.php b/cmi5PHP/tests/cmi5_connectorsTest.php index bcd5e42..7061c95 100755 --- a/cmi5PHP/tests/cmi5_connectorsTest.php +++ b/cmi5PHP/tests/cmi5_connectorsTest.php @@ -1,160 +1,144 @@ example = null; - } + global $DB, $cmi5launch, $cmi5launchid, $USER, $testcourseid, $cmi5launchsettings; + $cmi5launchsettings = array("cmi5launchtenanttoken" => "Testtoken", "cmi5launchplayerurl" => "http://test/launch.php", "cmi5launchcustomacchp" => "http://testhomepage.com"); - // Create course sends data to the player. - // Now we dont actually want to talk to player or we will either make a bunch of unreal courses - // unless we then delete it? - // or have to make a fake player??? - // Does php have mocks or stubs? - // They do, and what is actually returned is a massive json encoded string. It is the return - // response when a course is created from player, but all we care about is a string return from stub? - //I understand what I worte here. IS it enouh to just retreive a strin? Or does it need to be in - // same shape it would be from the player? Like do I need to verify it has, these cpmonets? - // or is ti enouh to not.... Is my RPORTAM robusdt enouh to catch that? - // I don't think it i, it just throughs eneric errors, this may - // be a place to improve it. - //Ok, so be that as it may THIS func gets a response and sends array back. If response is false it faisl - // robust enouh? - // I think since this job is just to take and pass on courseinfo, it doesn't have to validate the properties of returned array, that will fall into the testing of the functhat retrieves them? + // Override global variable and function so that it returns test data. + $USER = new \stdClass(); + $USER->username = "testname"; + $USER->id = 10; - public function testcmi5launch_create_course_pass() + + $testcourseid = maketestcourse($cmi5launchid); + } + + protected function tearDown(): void { + // Restore overridden global variable. + unset($GLOBALS['USER']); + unset($GLOBALS['cmi5launchsettings']); + } - // global $auidForTest; - global $auidForTest; - global $DB, $CFG, $filename; + /** + * Test of the cmi5launch_create_course method with a successful response from the player. + * @return void + */ + public function testcmi5launch_create_course_pass() + { // To determine the headers. - $filetype = "zip"; $id = 0; $tenanttoken = "testtoken"; //If we make filename an object with it's own get_content method, we can stub it out. $filename = new class { - - public function get_content() { - return "testfilecontents"; - } + public function get_content() { + return "testfilecontents"; + } }; - // We have added an error message, so we can make an error code and test that it is returned - $result = array("statusCode" => "200"); - - // Wait this moiht work, we can just make this object a method, and stub it out ourselves, - // or would i be better to try and use their mocks - // Mock a cmi5 connector object but only stub ONE method, as we want to test the other + // Player will return a string. + $result = json_encode(array("statusCode" => "200", + "message" => "testmessage") + ); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the other methods. // Create a mock of the send_request class as we don't actually want // to create a new course in the player. $csc = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post' )) ->getMock(); - - - - //$setting = $this->getMockBuilder(\stdclass::class)->addMethods(array('cmi5launch_settings'))->getMock(); - // $setting = $this->getFunctionMod(_mod_cmi5launch\local__, 'cmi5launch_settings'); - // $setting = $this->getMockBuilder(\stdclass::class) - // ->addMethods(array('cmi5launch_settings')) - // ->getMock(); - // $setting = $this->createMock(cmi5launch__settings::class) - //->addMethods(array('cmi5launch_settings')) - //->getMock(); - - // mod_cmi5launch\local\cmi5launch_settings() - // Can I mock lib? Then stuff like settings could be mocked? - - // We will have the mock return a basic string, as it's not under test // the string just needs to be returned as is. We do expect create_course to only call this once. $csc->expects($this->once()) - ->method('cmi5launch_send_request_to_cmi5_player_post') - // IT will call '/api/v1/course' nd not a whole url because that is accessed through "Settings" not reachable under test conditions, so it - // will only use the second part of concantation - ->with('testfilecontents', '/api/v1/course','zip', 'testtoken') - ->willReturn($result); + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with('testfilecontents', 'http://test/launch.php/api/v1/course','zip', 'testtoken') + ->willReturn($result); - - // $setting->expects($this->any()) - // ->method('cmi5launch_settings') - // ->willReturn(array('cmi5launchplayerurl' => 'http://localhost:8000/launch.php')) - // ->with('Request sent to player') - ; - - // I think I need to say expect to be called with these, - // because for some reason it says paraaam 0 is not matching? - //Call the method under test. + // Call the method under test. $returnedresult =$csc->cmi5launch_create_course($id, $tenanttoken, $filename); - // This should be the same, since the 'error messages' function in cmi5 connectooooooooooor will test thhe + // This should be the same, since the 'error messages' function in cmi5 connector will test the // 'result' and if it has 200, which we know it does, return it as is. - $this->assertSame($returnedresult, $result); - - + $this->assertSame($returnedresult, $result); + // And the return should be a string (the original method returns what the player sends back). + $this->assertIsString($returnedresult); } + /** + * Test of the cmi5launch_create_course method with a failed response from the player. + * @return void + */ public function testcmi5launch_create_course_fail_with_message() { - // global $auidForTest; - global $auidForTest; - global $DB, $CFG; + // Message we expect to be output. $expectedstring= "
Something went wrong creating the course. CMI5 Player returned 404 error. With message 'testmessage'.
"; + // Arguments to be passed to the method under test. $id = 0; $tenanttoken = "testtoken"; - + // Error message for stubbed method to return. $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + //If we make filename an object with it's own get_content method, we can stub it out. $filename = new class { public function get_content() { return "testfilecontents"; } }; - // Mock a cmi5 connector object but only stub ONE method, as we want to test the other + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the other methods. // Create a mock of the send_request class as we don't actually want // to create a new course in the player. $csc = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') @@ -162,210 +146,906 @@ public function get_content() { ->getMock(); // We will have the mock return a fake message as if the player had a problem with request. - // value, this should enable us to test the method under failing conditions. We do expect create_course to only call this once. - $csc->expects($this->once()) - ->method('cmi5launch_send_request_to_cmi5_player_post') - ->with('testfilecontents', '/api/v1/course', 'zip', 'testtoken') - ->willReturn($errormessage); + // This should enable us to test the method under failing conditions. We do expect create_course to only call this once. + $csc->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with('testfilecontents', 'http://test/launch.php/api/v1/course', 'zip', 'testtoken') + ->willReturn($errormessage); - //Call the method under test. + // Call the method under test. $returnedresult =$csc->cmi5launch_create_course($id, $tenanttoken, $filename); - // Result should be debug echo string and false + // Result should be debug echo string and false. $this->assertNotTrue($returnedresult, "Expected retrieved object to be false"); - //And it should output an error message, this can change mased onn error but the beginning should be the same - // $this->assertStringStartsWith("
Something went wrong creating the course. CMI5 Player returned", $returnedresult); - //And it should output this error message - $this->expectOutputString($expectedstring); + //And it should output this error message + $this->expectOutputString($expectedstring); + } + + + /** + * Test of the cmi5launch_create_tenant method with a successful response from the player. + * @return void + */ + public function testcmi5launch_create_tenant_pass() + { + // Arguments to be passed to the method under test. + $urltosend = "playerwebaddress"; + $username = "testname"; + $password = "testpassword"; + $newtenantname = "testtenantname"; + $data = array ('code' => 'testtenantname'); + // Encode data as it will be encoded when sent to player + $data = json_encode($data); + // This is the expected return value. + $returnvalue = array( + "code" => "testtenantname", + "id" => 9 + ); + + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the other methods. + // Create a mock of the send_request class as we don't actually want + // to create a new tenant. + $csc = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) + ->getMock(); + + // We will have the mock return a basic string, as it's not under test. + // The string just needs to be returned as is. We do expect create_tenant to only call this once. + $csc->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with($data, 'playerwebaddress','json', 'testname', 'testpassword') // for tomorrow, is thi failing because with only evaluates strings? Like do we need to string the array out> + // IRL it returns something that needs to be json decoded, so lets pass somethin that is encoded> + ->willReturn('{ + "code": "testtenantname", + "id": 9 + }' + ); + + //Call the method under test. + $result =$csc->cmi5launch_create_tenant($urltosend, $username, $password, $newtenantname); + + // And the return should be a string (the original method returns what the player sends back json-decoded or FALSE) + $this->assertIsArray($result); + $this->assertEquals( $returnvalue, $result); } - public function testcmi5launch_create_course_fail_with_false () + + /** + * Test of the cmi5launch_create_tenant method with a error response from the player. + * @return void + */ + public function testcmi5launch_create_tenant_fail() { - // global $auidForTest; - global $auidForTest; - global $DB, $CFG; + // Arguments to be passed to the method under test. + $urltosend = "playerwebaddress"; + $username = "testname"; + $password = "testpassword"; + $newtenantname = "testtenantname"; + $data = array ('code' => 'testtenantname'); + // Encode data as it will be encoded when sent to player + $data = json_encode($data); - $expectedstring= "
Something went wrong creating the course. CMI5 Player is not communicating. Is it running?
"; - $id = 0; - $tenanttoken = "testtoken"; + // The expected error message to be output. + $expectedstring= "
Something went wrong creating the tenant. CMI5 Player returned 404 error. With message 'testmessage'.
"; - $errormessage = false; - - $filename = new class { - public function get_content() { - return "testfilecontents"; - } - }; - // Mock a cmi5 connector object but only stub ONE method, as we want to test the other + // Error message for stubbed method to return. + $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. // Create a mock of the send_request class as we don't actually want // to create a new course in the player. $csc = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) ->getMock(); + // We will have the mock return a fake message as if the player had a problem with request. - // value, this should enable us to test the method under failing conditions. We do expect create_course to only call this once. - $csc->expects($this->once()) - ->method('cmi5launch_send_request_to_cmi5_player_post') - ->with('testfilecontents', '/api/v1/course', 'zip', 'testtoken') - ->willReturn($errormessage); + // This will enable us to test the method under failing conditions. We do expect create_tenant to only call this once. + $csc->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with($data, 'playerwebaddress', 'json', 'testname', 'testpassword') + ->willReturn($errormessage); //Call the method under test. - $returnedresult =$csc->cmi5launch_create_course($id, $tenanttoken, $filename); + $result =$csc->cmi5launch_create_tenant($urltosend, $username, $password, $newtenantname); // Result should be debug echo string and false - $this->assertNotTrue($returnedresult, "Expected retrieved object to be false"); - //And it should output an error message, this can change mased onn error but the beginning should be the same - // $this->assertStringStartsWith("
Something went wrong creating the course. CMI5 Player returned", $returnedresult); - //And it should output this error message - $this->expectOutputString($expectedstring); - + $this->assertNotTrue($result, "Expected retrieved object to be false"); + // And it should output this error message + $this->expectOutputString($expectedstring); } + /** + * * Test the retrieve_registration method with a successful response from the player. + * @return void + */ + public function testcmi5launch_retrieve_registration_with_get_pass() + { + // Arguments to be passed to the method under test. + $id = 0; + $registration = "testregistration"; + + // This is the url the stubbed method shopuld receive. + $urltosend = "http://test/launch.php/api/v1/registration/testregistration"; + + // The player returns a string. + $returnvalue = json_encode(array( + "actor" => "testtenantname", + "aus" => array( + "testau1", + "testau2" + ), + "code" => "testregistrationcode", + )); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_get')) + ->getMock(); - //Here we will mock cmi5_send_request_to_player and have it return a string again, because thje func actually under test is - // cmi5launch_create_tenant, so to test it we want to make sure it calls the other func and retutrns what it does - // or throws the specified error - public function testcmi5launch_create_tenant_pass() + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_get') + ->with("Testtoken", $urltosend) + ->willReturn($returnvalue); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_registration_with_get($registration, $id); + + // And the return should be a string. + $this->assertIsString($result); + $this->assertEquals($returnvalue, $result); + } + + + /** + * * Test the retrieve_registration method with a failed response from the player. + * @return void + */ + public function testcmi5launch_retrieve_registration_with_get_fail() { + // Error message for stubbed method to return. + $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + + // The expected error message to be output. + $expectedstring= "
Something went wrong retrieving the registration. CMI5 Player returned 404 error. With message 'testmessage'.
"; + + // The arguments to be passed to the method under test. + $id = 0; + $registration = "testregistration"; + + // This is the url the stubbed method shopuld receive. + $urltosend = "http://test/launch.php/api/v1/registration/testregistration"; - // global $auidForTest; - global $CFG; - - $urltosend = "playerwebaddress"; - $username = "testname"; - $password = "testpassword"; - $newtenantname = "testtenantname"; - - $returnvalue = array( - "code" => "testtenantname", - "id" => 9 - ); - $data = array ('code' => 'testtenantname'); - // Mock a cmi5 connector object but only stub ONE method, as we want to test the other - // Create a mock of the send_request class as we don't actually want - // to create a new course in the player. - $csc = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') - ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) - ->getMock(); - - // We will have the mock return a basic string, as it's not under test - // the string just needs to be returned as is. We do expect create_course to only call this once. - $csc->expects($this->once()) - ->method('cmi5launch_send_request_to_cmi5_player_post') - ->with(json_encode($data), 'playerwebaddress', 'testname', 'testpassword') // for tomorrow, is thi failing because with only evaluates strings? Like do we need to string the array out> - // IRL it returns something that needs to be json decoded, so lets pass somethin that is encoded> - ->willReturn('{ - "code": "testtenantname", - "id": 9 - }' ) - // ->with('Request sent to player') - ; - - // I think I need to say expect to be called with these, - // because for some reason it says paraaam 0 is not matching? - //Call the method under test. - $result =$csc->cmi5launch_create_tenant($urltosend, $username, $password, $newtenantname); - - // And the return should be a string (the original method returns what the player sends back or FALSE) - $this->assertIsArray($result); - $this->assertEquals( $returnvalue, $result); + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_get')) + ->getMock(); + + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_get') + ->with("Testtoken", $urltosend) + ->willReturn($errormessage); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_registration_with_get($registration, $id); + + // Result should be debug echo string and false + $this->assertNotTrue($result, "Expected retrieved object to be false"); + //And it should output this error message + $this->expectOutputString($expectedstring); + } + + /** + * * Test the retrieve_registration_with_post method with a successful response from the player. + * @return void + */ + public function testcmi5launch_retrieve_registration_with_post_pass() + { + // The arguments to be passed to the method under test. + $id = 0; + $courseid = 1; + $filetype = "json"; + + // The data to be passed to the mocked method. + $data = array( + "courseId" => $courseid, + "actor" => array( + "account" => array ( + "homePage" => "http://testhomepage.com", + "name" => "testname" + ), + ), + ); + + // This is the url the stubbed method shopuld receive. + $urltosend = "http://test/launch.php/api/v1/registration"; + + // The player returns a string. + $returnvalue = json_encode(array( + "actor" => "testtenantname", + "aus" => array( + "testau1", + "testau2" + ), + "code" => "testregistrationcode", + )); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) + ->getMock(); + + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with(json_encode($data), $urltosend, $filetype, "Testtoken") + ->willReturn($returnvalue); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_registration_with_post($courseid, $id); + + // And the return should be a string. + $this->assertIsString($result); + $this->assertEquals($result, "testregistrationcode"); } - public function testcmi5launch_create_tenant_fail() + /** + * * Test the retrieve_registration_with_post method with a failed response from the player. + * @return void + */ + public function testcmi5launch_retrieve_registration_with_post_fail() { + // Error message for stubbed method to return. + $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); - // global $auidForTest; - global $CFG; + // The expected error message to be output. + $expectedstring= "
Something went wrong retrieving the registration. CMI5 Player returned 404 error. With message 'testmessage'.
"; - $urltosend = "playerwebaddress"; - $username = "testname"; - $password = "testpassword"; - $newtenantname = "testtenantname"; + // The arguments to be passed to the method under test. + $id = 0; + $courseid = 1; + $filetype = "json"; + + // The data to be passed to the mocked method. + $data = array( + "courseId" => $courseid, + "actor" => array( + "account" => array ( + "homePage" => "http://testhomepage.com", + "name" => "testname" + ), + ), + ); - $returnvalue = array( - "code" => "testtenantname", - "id" => 9 + // This is the url the stubbed method shopuld receive. + $urltosend = "http://test/launch.php/api/v1/registration"; + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with(json_encode($data), $urltosend, $filetype, "Testtoken") + ->willReturn($errormessage); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_registration_with_post($courseid, $id); + + // Result should be debug echo string and false + $this->assertNotTrue($result, "Expected retrieved object to be false"); + //And it should output this error message + $this->expectOutputString($expectedstring); + } + + + /** + * * Test the retrieve_token method with a successful response from the player. + * @return void + */ + public function testcmi5launch_retrieve_token_pass() + { + // Arguments to be passed to the method under test. + $url = "http://test/launch.php"; + $username = "testname"; + $filetype = "json"; + $password = "testpassword"; + $audience = "testaudience"; + $tenantid = 0; + + // The data to be passed to the mocked method. + $data = array( + "tenantId" => $tenantid, + "audience" => $audience, ); - // Mock a cmi5 connector object but only stub ONE method, as we want to test the other. - // Create a mock of the send_request class as we don't actually want - // to create a new course in the player. - $csc = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') - ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) - ->getMock(); - - // This time we will have ti 'fail' so return a fail response from player - $csc->expects($this->once()) - ->method('cmi5launch_send_request_to_cmi5_player_post') - ->with(array ('code' => 'testtenantname'), 'playerwebaddress', 'testname', 'testpassword') - // IRL it returns something that needs to be json decoded, so lets pass somethin that is encoded> - ->willReturn(false) - // ->with('Request sent to player') - ; - - // I think I need to say expect to be called with these, - // because for some reason it says paraaam 0 is not matching? - //Call the method under test. - $result =$csc->cmi5launch_create_tenant($urltosend, $username, $password, $newtenantname); + // The player returns a json string. + $returnvalue = '{ + "token": "testtoken" + }'; + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) + ->getMock(); + + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with(json_encode($data), $url, $filetype, $username, $password) + ->willReturn($returnvalue); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_token($url, $username, $password, $audience, $tenantid); + + // And the return should be a string (the original method returns what the player sends back or FALSE. + $this->assertIsString($result); + $this->assertEquals($result, $returnvalue); + } + + /** + * * Test the retrieve_token method with a failed response from the player. + * @return void + */ + public function testcmi5launch_retrieve_token_fail() + { + // Error message for stubbed method to return. + $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + + // The expected error message to be output. + $expectedstring= "
Something went wrong retrieving the token. CMI5 Player returned 404 error. With message 'testmessage'.
"; + + // Arguments to be passed to the method under test. + $url = "http://test/launch.php"; + $username = "testname"; + $password = "testpassword"; + $audience = "testaudience"; + $tenantid = 0; + + // The data to be passed to the mocked method. + $filetype = "json"; + $data = array( + "tenantId" => $tenantid, + "audience" => $audience, + ); + + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with(json_encode($data), $url, $filetype, $username, $password) + ->willReturn($errormessage); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_token($url, $username, $password, $audience, $tenantid); + + // Result should be debug echo string and false. + $this->assertNotTrue($result, "Expected retrieved object to be false"); + // And it should output this error message. + $this->expectOutputString($expectedstring); + } + + /** + * * Test the retrieve_url (launchurl) method with a successful response from the player. + * @return void + */ + public function testcmi5launch_retrieve_url_pass() + { + global $cmi5launchid; + + // Arguments to be passed to the method under test. + $id = $cmi5launchid; + $auindex = 1; + $filetype = "json"; + $returnurl = "https://testmoodle.com"; + $registrationid = "testregistrationid"; + + //Retrieve settings like the method under test will. + $settings = cmi5launch_settings($id); + $playerurl = $settings['cmi5launchplayerurl']; + + // The data to be passed to the mocked method. + $data = array( + 'actor' => array( + 'account' => array( + "homePage" => "http://testhomepage.com", + "name" => "testname", + ), + ), + 'returnUrl' => $returnurl, + 'reg' => $registrationid, + ); + + // This is the url the stubbed method shopuld receive. + $urltosend = $playerurl . "/api/v1/course/" . "1" ."/launch-url/" . $auindex; + + // The player returns a string. + $returnvalue = json_encode(array( + "id" => 21, + "launchMethod" => "AnyWindow", + "url" => "http://testlaunchurl" + )); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with(json_encode($data), $urltosend, $filetype, "Testtoken") + ->willReturn(($returnvalue)); + + // Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_url($id, $auindex); + + // And the return should be an array because the method returns it json_decoded. + $this->assertIsArray($result); + $this->assertEquals($result, json_decode($returnvalue, true)); + } + + /** + * * Test the retrieve_url (launchurl) method with a failed response from the player. + * @return void + */ + public function testcmi5launch_retrieve_url_fail() + { + global $cmi5launchid; + + // Error message for stubbed method to return. + $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + + // The expected error message to be output. + $expectedstring= "
Something went wrong retrieving launch url. CMI5 Player returned 404 error. With message 'testmessage'.
"; + + // The id to be passed to method under test. + $id = $cmi5launchid; + $auindex = 1; + $filetype = "json"; + + //Retrieve settings like the method under test will. + $settings = cmi5launch_settings($id); + $playerurl = $settings['cmi5launchplayerurl']; + + $returnurl = "https://testmoodle.com"; + $registrationid = "testregistrationid"; + + // The data to be passed to the mocked method. + $data = array( + 'actor' => array( + 'account' => array( + "homePage" => "http://testhomepage.com", + "name" => "testname", + ), + ), + 'returnUrl' => $returnurl, + 'reg' => $registrationid, + ); + + // This is the url the stubbed method shopuld receive. + $urltosend = $playerurl . "/api/v1/course/" . "1" ."/launch-url/" . $auindex; + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the other. + // Create a mock of the send_request class as we don't actually want + // to create a new course in the player. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_post')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_post') + ->with(json_encode($data), $urltosend, $filetype, "Testtoken") + ->willReturn($errormessage); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_url($id, $auindex); // Result should be debug echo string and false $this->assertNotTrue($result, "Expected retrieved object to be false"); - //And it should output this error message - $this->expectOutputString("
Something went wrong creating the tenant. CMI5 Player returned ". $result . "
"); + //And it should output this error message + $this->expectOutputString($expectedstring); + } + + /** + * Test the send_request_to_cmi5_player_post method with one arg. + * This is what is used to retrieve info like tenant info. + * @return void + */ + public function testcmi5launch_send_request_to_cmi5_player_post_with_one_arg() + { + // The player returns a string under normal circumstances. + $returnvalue = json_encode(array( + "statusCode" => 200, + "Response" => "Successful Post", + )); + + // The data to be passed to the mocked method. + $data = array( + 'actor' => array( + 'account' => array( + "homePage" => "http://testhomepage.com", + "name" => "testname", + ), + ), + 'returnUrl' => 'returnurl', + 'reg' => 'registrationid', + ); + + // Fake arguments to pass in. + $filetype = "json"; + $url = "http://test/url.com"; + $contenttype = "application/json\r\n"; + + //The arguments to pass in, in this case one, a pretend token. + $token = "testtoken"; + + // Options that should be built in the method, and we need to make sure it goes + // to the right branch by matching it. + $options = array( + 'http' => array( + 'method' => 'POST', + 'ignore_errors' => true, + 'header' => array("Authorization: Bearer ". $token, + "Content-Type: " .$contenttype . + "Accept: " . $contenttype), + 'content' => ($data), + ), + ); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_stream_and_send')) + ->getMock(); + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_stream_and_send') + ->with($url, $options) + ->willReturn($returnvalue) ; + // Call the method under test. + $test = $mockedclass->cmi5launch_send_request_to_cmi5_player_post($data, $url, $filetype, $token); + + // And the return should be a string. + $this->assertIsString($test); + // And it should be the same as the return value. + $this->assertEquals($test, $returnvalue); } + /** + * * Test the send_request_to_cmi5_player_post method with two args. + * This is what is used to retrieve info like tenant info. + * @return void + */ + public function testcmi5launch_send_request_to_cmi5_player_post_with_two_args() + { + // The player returns a string under normal circumstances. + $returnvalue = json_encode(array( + "statusCode" => 200, + "Response" => "Successful Post", + )); + + // The data to be passed to the mocked method. + $data = array( + 'actor' => array( + 'account' => array( + "homePage" => "http://testhomepage.com", + "name" => "testname", + ), + ), + 'returnUrl' => 'returnurl', + 'reg' => 'registrationid', + ); + + // Fake arguments to pass in. + $filetype = "json"; + $url = "http://test/url.com"; + $contenttype = "application/json\r\n"; + + //The arguments to pass in, in this case one, a pretend username and password. + $username = "testname"; + $password = "testpassword"; + + // Options that should be built in the method, and we need to make sure it goes + // to the right branch by matching it. + $options = array( + 'http' => array( + 'method' => 'POST', + 'header' => array('Authorization: Basic '. base64_encode("$username:$password"), + "Content-Type: " .$contenttype . + "Accept: " . $contenttype), + 'content' => ($data), + ), + ); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_stream_and_send')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_stream_and_send') + ->with($url, $options) + ->willReturn($returnvalue) ; + + // Call the method under test. + $test = $mockedclass->cmi5launch_send_request_to_cmi5_player_post($data, $url, $filetype, $username, $password); - public function testcmi5launch_send_request_to_cmi5_player_post() + // And the return should be a string. + $this->assertIsString($test); + // And it should be the same as the return value. + $this->assertEquals($test, $returnvalue); + } + + /** + * * Test the send_request_to_cmi5_player_get + * This is what is used to retrieve info like tenant info. + * @return void + */ + public function testcmi5launch_send_request_to_cmi5_player_post_with_get_pass() { - // global $auidForTest; - global $CFG; + // The player returns a string under normal circumstances. + $returnvalue = json_encode(array( + "statusCode" => 200, + "Response" => "Successful Post", + )); + + //The arguments to pass in. + $token = "testtoken"; + $url = "http://test/url.com"; + + // Options that should be built in the method, we build here to pass to mock. + $options = array ( + 'http' => array ( + 'method' => 'GET', + 'ignore_errors' => true, + 'header' => array ("Authorization: Bearer ". $token, + "Content-Type: application/json\r\n" . + "Accept: application/json\r\n"), + ), + ); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the other. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_stream_and_send')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_stream_and_send') + ->with($url, $options) + ->willReturn($returnvalue) ; - $help = new cmi5TestHelpers(); - // $testHelp = new cmi5TestHelpers(); + // Call the method under test. + $test = $mockedclass->cmi5launch_send_request_to_cmi5_player_get($token, $url); - $databody = array ('code' => 'testtenantname'); - $urltosend = "playerwebaddress"; - $username = "testname"; - $password = "testpassword"; + // And the return should be an array. + $this->assertIsArray($test); + // And it should be the same as the return value. + $this->assertEquals($test, json_decode($returnvalue, true) ); + + } + + /** + * * Test the send_request_to_cmi5_player_get + * This is what is used to retrieve info like tenant info. + * This is meant to fail, we want it to act as if the player is unreachable + * @return void + */ + public function testcmi5launch_send_request_to_cmi5_player_post_with_get_fail() + { + + // Error message for stubbed method to return. + $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + + //The arguments to pass in, in this case one, a pretend token. $token = "testtoken"; + $url = "http://test/url.com"; + + // Options that should be built in the method, and we need to make sure it goes + // to the right branch by matching it. + $options = array ( + 'http' => array ( + 'method' => 'GET', + 'ignore_errors' => true, + 'header' => array ("Authorization: Bearer ". $token, + "Content-Type: application/json\r\n" . + "Accept: application/json\r\n"), + ), + ); + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the other. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_stream_and_send')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_stream_and_send') + ->with($url, $options) + ->willReturn(json_encode($errormessage) ) ; + + $test = $mockedclass->cmi5launch_send_request_to_cmi5_player_get($token, $url); + + // And the return should be an array since the method under test returns player message decoded. + $this->assertIsArray($test); + // And it should be the same as the return value. + $this->assertEquals($test, $errormessage); + } + + /** + * * Test the retrieve_session method with a successful response from the player. + * @return void + */ + public function testcmi5launch_session_info_from_player_pass() + { + global $cmi5launchid; + // The id to be passed to method under test. + $id = $cmi5launchid; + + // Retrieve settings like the method under test will. + $settings = cmi5launch_settings($id); + $playerurl = $settings['cmi5launchplayerurl']; + $token = $settings['cmi5launchtenanttoken']; + $sessionid = "testsessionid"; + + // This is the url the stubbed method shopuld receive. + $urltosend = $playerurl . "/api/v1/session/" . $sessionid; + + // The player returns a string, but the mocked method returns an array. $returnvalue = array( - "code" => "testtenantname", - "id" => 9 + "statusCode" => 200, + "launchMethod" => "AnyWindow", + "url" => "http://testlaunchurl" ); - // Mock a cmi5 connector object but only stub ONE method, as we want to test the other - // Create a mock of the send_request class as we don't actually want - // to create a new course in the player. - $csc = $this->getMockBuilder( __NAMESPACE__ . '\cmi5TestHelpers') - ->onlyMethods(array('file_get_contents')) - ->getMock(); - - // This time we will have ti 'fail' so return a fail response from player - $csc->expects($this->once()) - ->method('file_get_contents') - //->with(array ('code' => 'testtenantname'), 'playerwebaddress', 'testname', 'testpassword') - // IRL it returns something that needs to be json decoded, so lets pass somethin that is encoded> - ->willReturn("Test") - // ->with('Request sent to player') - ; - - // I think I need to say expect to be called with these, - // because for some reason it says paraaam 0 is not matching? - //Call the method under test. - $result =$csc->cmi5launch_send_request_to_cmi5_player_post($databody, $urltosend, $username, $password); + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_get')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_get') + ->with($token, $urltosend) + ->willReturn(json_encode($returnvalue)); + + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_session_info_from_player($sessionid, $id); + + // And the return should be an array (the original method returns what the player sends back json-decoded or FALSE) + $this->assertIsString($result); + $this->assertEquals($result, json_encode($returnvalue)); + } + + /** + * * Test the retrieve_session (launchurl) method with a failed response from the player. + * @return void + */ + public function testcmi5launch_session_info_from_player_fail() + { + global $cmi5launchid; + + // Error message for stubbed method to return. + $errormessage = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + + // The expected error message to be output. + $expectedstring= "
Something went wrong retrieving session info. CMI5 Player returned 404 error. With message 'testmessage'.
"; + + // The id to be passed to method under test. + $id = $cmi5launchid; + + //Retrieve settings like the method under test will. + $settings = cmi5launch_settings($id); + $playerurl = $settings['cmi5launchplayerurl']; + $sessionid = "testsessionid"; + $token = $settings['cmi5launchtenanttoken']; + // This is the url the stubbed method shopuld receive. + $urltosend = $playerurl . "/api/v1/session/" . $sessionid; + + // Mock a cmi5 connector object but only stub ONE method, as we want to test the others. + $mockedclass = $this->getMockBuilder('mod_cmi5launch\local\cmi5_connectors') + ->onlyMethods(array('cmi5launch_send_request_to_cmi5_player_get')) + ->getMock(); + + // Mock returns json encoded data, as it would be from the player. + $mockedclass->expects($this->once()) + ->method('cmi5launch_send_request_to_cmi5_player_get') + ->with($token, $urltosend) + ->willReturn(json_encode($errormessage)); + + //Call the method under test. + $result = $mockedclass->cmi5launch_retrieve_session_info_from_player($sessionid, $id); // Result should be debug echo string and false - // $this->assertNotTrue($result, "Expected retrieved object to be false"); - //And it should output this error message - $this->expectOutputString("Test"); + $this->assertNotTrue($result, "Expected retrieved object to be false"); + //And it should output this error message + $this->expectOutputString($expectedstring); + } + + /** + * * Test the cmi5launch_connectors method first if branch, which is when the player is not communicating. + * @return void + */ + public function testcmi5launch_connectors_error_message_path_one() + { + // The function takes a response mmessage and a string of 'type' which is what + // function failed, to fill in error message. + // In the case where the player is not on, it would simply be 'false'. + $response = false; + $type = "retreiving 'item'"; + // The expected error message to be output. + $errormessage ="
Something went wrong " . $type . ". CMI5 Player is not communicating. Is it running?
"; + // Call the method under test. + $result = cmi5_connectors::cmi5launch_connectors_error_message($response, $type); + + // Result should be debug echo string and false + $this->assertNotTrue($result, "Expected retrieved object to be false"); + //And it should output this error message + $this->expectOutputString($errormessage); + } + + /** + * * Test the cmi5launch_connectors method second if branch, which is when the player returns an error. + * @return void + */ + public function testcmi5launch_connectors_error_message_path_two() + { + // The function takes a response message and a string of 'type' which is what failed to be fetched. + $type = "retreiving 'item'"; + + // Error message for stubbed method to return. + $response = array("statusCode" => "404", "error" => "Not Found","message" => "testmessage" ); + + // The expected error message to be output. + $expectedstring ="
Something went wrong " . $type . ". CMI5 Player returned " . $response['statusCode'] . " error. With message '" . $response['message'] . "'.
"; + + // Call the method under test. + $result = cmi5_connectors::cmi5launch_connectors_error_message($response, $type); + + // Result should be debug echo string and false + $this->assertNotTrue($result, "Expected retrieved object to be false"); + //And it should output this error message + $this->expectOutputString($expectedstring); } + /** + * * Test the cmi5launch_connectors method last if branch, which is when the player returns a 200 + * message with requested info. No errors. + * @return void + */ + public function testcmi5launch_connectors_error_message_path_three() + { + // The function takes a response message and a string of 'type' which is what failed to be fetched. + $type = "retreiving 'item'"; + + // The player returns a string under normal circumstances. + $response = json_encode(array( + "statusCode" => 200, + "Response" => "Successful Post", + )); + + // Call the method under test. + $result = cmi5_connectors::cmi5launch_connectors_error_message($response, $type); + + // Result should be debug echo string and false + $this->assertTrue($result, "Expected retrieved object to be true"); + } - } \ No newline at end of file +} + \ No newline at end of file diff --git a/view.php b/view.php index 5419137..ce7855c 100755 --- a/view.php +++ b/view.php @@ -166,8 +166,7 @@ function mod_cmi5launch_launchexperience(registrationInfo) { $userscourse->id = $newid; } else { // Record exists. - echo" cm is $cm->instance"; - echo" cmi5launch id is $cmi5launch->id"; + // We have a record, so we need to retrieve it. $userscourse = $DB->get_record('cmi5launch_usercourse', ['courseid' => $record->courseid, 'userid' => $USER->id]);