/** * Test launch_sco */ public function test_launch_sco() { global $DB; // Test invalid instance id. try { mod_scorm_external::launch_sco(0); $this->fail('Exception expected due to invalid mod_scorm instance id.'); } catch (moodle_exception $e) { $this->assertEquals('invalidrecord', $e->errorcode); } // Test not-enrolled user. $user = self::getDataGenerator()->create_user(); $this->setUser($user); try { mod_scorm_external::launch_sco($this->scorm->id); $this->fail('Exception expected due to not enrolled user.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } // Test user with full capabilities. $this->setUser($this->student); // Trigger and capture the event. $sink = $this->redirectEvents(); $scoes = scorm_get_scoes($this->scorm->id); foreach ($scoes as $sco) { // Find launchable SCO. if ($sco->launch != '') { break; } } $result = mod_scorm_external::launch_sco($this->scorm->id, $sco->id); $result = external_api::clean_returnvalue(mod_scorm_external::launch_sco_returns(), $result); $events = $sink->get_events(); $this->assertCount(1, $events); $event = array_shift($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_scorm\\event\\sco_launched', $event); $this->assertEquals($this->context, $event->get_context()); $moodleurl = new \moodle_url('/mod/scorm/player.php', array('id' => $this->cm->id, 'scoid' => $sco->id)); $this->assertEquals($moodleurl, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); // Invalid SCO. try { mod_scorm_external::launch_sco($this->scorm->id, -1); $this->fail('Exception expected due to invalid SCO id.'); } catch (moodle_exception $e) { $this->assertEquals('cannotfindsco', $e->errorcode); } }