Esempio n. 1
0
 /**
  * Test for enrol_lti_plugin::delete_instance().
  */
 public function test_delete_instance()
 {
     global $DB;
     // Create tool enrolment instance.
     $data = new stdClass();
     $data->enrolstartdate = time();
     $data->secret = 'secret';
     $tool = $this->getDataGenerator()->create_lti_tool($data);
     // Create consumer and related data.
     $dataconnector = new data_connector();
     $consumer = new ToolConsumer('testkey', $dataconnector);
     $consumer->secret = $tool->secret;
     $consumer->ltiVersion = ToolProvider::LTI_VERSION1;
     $consumer->name = 'TEST CONSUMER NAME';
     $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
     $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
     $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
     $consumer->enabled = true;
     $consumer->protected = true;
     $consumer->save();
     $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid');
     $resourcelink->save();
     $ltiuser = User::fromResourceLink($resourcelink, '');
     $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId';
     $ltiuser->ltiUserId = 'testuserid';
     $ltiuser->email = '*****@*****.**';
     $ltiuser->save();
     $tp = new tool_provider($tool->id);
     $tp->user = $ltiuser;
     $tp->resourceLink = $resourcelink;
     $tp->consumer = $consumer;
     $tp->map_tool_to_consumer();
     $mappingparams = ['toolid' => $tool->id, 'consumerid' => $tp->consumer->getRecordId()];
     // Check first that the related records exist.
     $this->assertTrue($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams));
     $this->assertTrue($DB->record_exists('enrol_lti_lti2_consumer', ['id' => $consumer->getRecordId()]));
     $this->assertTrue($DB->record_exists('enrol_lti_lti2_resource_link', ['id' => $resourcelink->getRecordId()]));
     $this->assertTrue($DB->record_exists('enrol_lti_lti2_user_result', ['id' => $ltiuser->getRecordId()]));
     // Perform deletion.
     $enrollti = new enrol_lti_plugin();
     $instance = $DB->get_record('enrol', ['id' => $tool->enrolid]);
     $enrollti->delete_instance($instance);
     // Check that the related records have been deleted.
     $this->assertFalse($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams));
     $this->assertFalse($DB->record_exists('enrol_lti_lti2_consumer', ['id' => $consumer->getRecordId()]));
     $this->assertFalse($DB->record_exists('enrol_lti_lti2_resource_link', ['id' => $resourcelink->getRecordId()]));
     $this->assertFalse($DB->record_exists('enrol_lti_lti2_user_result', ['id' => $ltiuser->getRecordId()]));
     // Check that the enrolled users and the tool instance has been deleted.
     $this->assertFalse($DB->record_exists('enrol_lti_users', ['toolid' => $tool->id]));
     $this->assertFalse($DB->record_exists('enrol_lti_tools', ['id' => $tool->id]));
     $this->assertFalse($DB->record_exists('enrol', ['id' => $instance->id]));
 }
Esempio n. 2
0
 /**
  * Test for data_connector::deleteUser().
  */
 public function test_delete_user()
 {
     $dc = new data_connector();
     $consumer = new ToolConsumer(null, $dc);
     $consumer->name = 'TestName';
     $consumer->setKey('TestKey');
     $consumer->secret = 'TestSecret';
     $consumer->save();
     $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid');
     $resourcelink->save();
     $user = User::fromResourceLink($resourcelink, '');
     $user->ltiResultSourcedId = 'testLtiResultSourcedId';
     $user->firstname = 'First';
     $user->lastname = 'Last';
     $user->fullname = 'Full name';
     $user->email = '*****@*****.**';
     $user->roles = ['a', 'b'];
     $user->groups = ['1', '2'];
     $user->save();
     // Delete user.
     $this->assertTrue($dc->deleteUser($user));
     // User record should have been deleted from the DB.
     $this->assertFalse($dc->loadUser($user));
     // User object should have been initialised().
     $this->assertEmpty($user->firstname);
     $this->assertEmpty($user->lastname);
     $this->assertEmpty($user->fullname);
     $this->assertEmpty($user->email);
     $this->assertEmpty($user->roles);
     $this->assertEmpty($user->groups);
     $this->assertNull($user->ltiResultSourcedId);
     $this->assertNull($user->created);
     $this->assertNull($user->updated);
 }
Esempio n. 3
0
 /**
  * Check if a share arrangement is in place.
  *
  * @return boolean True if no error is reported
  */
 private function checkForShare()
 {
     $ok = true;
     $doSaveResourceLink = true;
     $id = $this->resourceLink->primaryResourceLinkId;
     $shareRequest = isset($_POST['custom_share_key']) && !empty($_POST['custom_share_key']);
     if ($shareRequest) {
         if (!$this->allowSharing) {
             $ok = false;
             $this->reason = 'Your sharing request has been refused because sharing is not being permitted.';
         } else {
             // Check if this is a new share key
             $shareKey = new ResourceLinkShareKey($this->resourceLink, $_POST['custom_share_key']);
             if (!is_null($shareKey->primaryConsumerKey) && !is_null($shareKey->primaryResourceLinkId)) {
                 // Update resource link with sharing primary resource link details
                 $key = $shareKey->primaryConsumerKey;
                 $id = $shareKey->primaryResourceLinkId;
                 $ok = $key !== $this->consumer->getKey() || $id != $this->resourceLink->getId();
                 if ($ok) {
                     $this->resourceLink->primaryConsumerKey = $key;
                     $this->resourceLink->primaryResourceLinkId = $id;
                     $this->resourceLink->shareApproved = $shareKey->autoApprove;
                     $ok = $this->resourceLink->save();
                     if ($ok) {
                         $doSaveResourceLink = false;
                         $this->user->getResourceLink()->primaryConsumerKey = $key;
                         $this->user->getResourceLink()->primaryResourceLinkId = $id;
                         $this->user->getResourceLink()->shareApproved = $shareKey->autoApprove;
                         $this->user->getResourceLink()->updated = time();
                         // Remove share key
                         $shareKey->delete();
                     } else {
                         $this->reason = 'An error occurred initialising your share arrangement.';
                     }
                 } else {
                     $this->reason = 'It is not possible to share your resource link with yourself.';
                 }
             }
             if ($ok) {
                 $ok = !is_null($key);
                 if (!$ok) {
                     $this->reason = 'You have requested to share a resource link but none is available.';
                 } else {
                     $ok = !is_null($this->user->getResourceLink()->shareApproved) && $this->user->getResourceLink()->shareApproved;
                     if (!$ok) {
                         $this->reason = 'Your share request is waiting to be approved.';
                     }
                 }
             }
         }
     } else {
         // Check no share is in place
         $ok = is_null($id);
         if (!$ok) {
             $this->reason = 'You have not requested to share a resource link but an arrangement is currently in place.';
         }
     }
     // Look up primary resource link
     if ($ok && !is_null($id)) {
         $consumer = new ToolConsumer($key, $this->dataConnector);
         $ok = !is_null($consumer->created);
         if ($ok) {
             $resourceLink = ResourceLink::fromConsumer($consumer, $id);
             $ok = !is_null($resourceLink->created);
         }
         if ($ok) {
             if ($doSaveResourceLink) {
                 $this->resourceLink->save();
             }
             $this->resourceLink = $resourceLink;
         } else {
             $this->reason = 'Unable to load resource link being shared.';
         }
     }
     return $ok;
 }
Esempio n. 4
0
 /**
  * Builds a dummy tool provider object.
  *
  * @param string $secret Consumer secret.
  * @param array|stdClass $proxy Tool proxy data.
  * @param null $resourcelinksettings Key-value array for resource link settings.
  * @return dummy_tool_provider
  */
 protected function build_dummy_tp($secret = null, $proxy = null, $resourcelinksettings = null)
 {
     $tool = $this->tool;
     $dataconnector = new data_connector();
     $consumer = new ToolConsumer('testkey', $dataconnector);
     $ltiversion = ToolProvider::LTI_VERSION2;
     if ($secret === null && $proxy === null) {
         $consumer->secret = $tool->secret;
         $ltiversion = ToolProvider::LTI_VERSION1;
     } else {
         $consumer->secret = $secret;
     }
     $consumer->ltiVersion = $ltiversion;
     $consumer->name = 'TEST CONSUMER NAME';
     $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
     $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
     $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
     $consumer->enabled = true;
     $consumer->protected = true;
     if ($proxy !== null) {
         $consumer->toolProxy = json_encode($proxy);
     }
     $consumer->save();
     $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid');
     if (!empty($resourcelinksettings)) {
         foreach ($resourcelinksettings as $setting => $value) {
             $resourcelink->setSetting($setting, $value);
         }
     }
     $resourcelink->save();
     $ltiuser = User::fromResourceLink($resourcelink, '');
     $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId';
     $ltiuser->ltiUserId = 'testuserid';
     $ltiuser->email = '*****@*****.**';
     $ltiuser->save();
     $tp = new dummy_tool_provider($tool->id);
     $tp->user = $ltiuser;
     $tp->resourceLink = $resourcelink;
     $tp->consumer = $consumer;
     return $tp;
 }
Esempio n. 5
0
 /**
  * Test for data_connector::get_resourcelink_from_consumer()
  */
 public function test_get_resourcelink_from_consumer()
 {
     $dc = new data_connector();
     $consumer = new ToolConsumer(null, $dc);
     $consumer->name = 'TestName';
     $consumer->setKey('TestKey');
     $consumer->secret = 'TestSecret';
     $consumer->save();
     // No ResourceLink associated with the ToolConsumer yet.
     $this->assertNull($dc->get_resourcelink_from_consumer($consumer));
     // Create and save ResourceLink from ToolConsumer.
     $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid');
     $resourcelink->save();
     $dc->loadResourceLink($resourcelink);
     // Assert that the resource link and the one fetched by get_resourcelink_from_consumer() are the same.
     $this->assertEquals($resourcelink, $dc->get_resourcelink_from_consumer($consumer));
 }
Esempio n. 6
0
 /**
  * Fetches a resource link record that is associated with a ToolConsumer.
  *
  * @param ToolConsumer $consumer
  * @return ResourceLink
  */
 public function get_resourcelink_from_consumer(ToolConsumer $consumer)
 {
     global $DB;
     $resourcelink = null;
     if ($resourcelinkrecord = $DB->get_record($this->resourcelinktable, ['consumerid' => $consumer->getRecordId()], 'ltiresourcelinkkey')) {
         $resourcelink = ResourceLink::fromConsumer($consumer, $resourcelinkrecord->ltiresourcelinkkey);
     }
     return $resourcelink;
 }