/** * Test for data_connector::get_resourcelink_from_context() */ public function test_get_resourcelink_from_context() { $dc = new data_connector(); $consumer = new ToolConsumer(null, $dc); $consumer->name = 'TestName'; $consumer->setKey('TestKey'); $consumer->secret = 'TestSecret'; $consumer->save(); $settings = ['a', 'b', 'c']; $lticontextid = 'testlticontextid'; $context = Context::fromConsumer($consumer, $lticontextid); $context->settings = $settings; $context->save(); // No ResourceLink associated with the Context yet. $this->assertNull($dc->get_resourcelink_from_context($context)); // Create and save ResourceLink from the Context. $resourcelink = ResourceLink::fromContext($context, 'testresourcelinkid'); $resourcelink->save(); $dc->loadResourceLink($resourcelink); // Assert that the resource link and the one fetched by get_resourcelink_from_context() are the same. $this->assertEquals($resourcelink, $dc->get_resourcelink_from_context($context)); }
public function setUp() { $this->resetAfterTest(); // Set this user as the admin. $this->setAdminUser(); $this->task = new dummy_sync_members_task(); $generator = $this->getDataGenerator(); $course = $generator->create_course(); $tooldata = ['courseid' => $course->id, 'membersyncmode' => helper::MEMBER_SYNC_ENROL_AND_UNENROL, 'membersync' => 1]; $tool = $generator->create_lti_tool((object) $tooldata); $this->tool = helper::get_lti_tool($tool->id); $dataconnector = $this->task->get_dataconnector(); $this->consumer = new ToolConsumer('Consumer1Key', $dataconnector); $this->consumer->name = 'Consumer1'; $this->consumer->secret = 'Consumer1Secret'; $this->consumer->save(); $toolprovider = new tool_provider($this->tool->id); $toolprovider->consumer = $this->consumer; $toolprovider->map_tool_to_consumer(); $imageurl = $this->getExternalTestFileUrl('test.jpg'); $count = 10; $this->members = []; for ($i = 1; $i <= $count; $i++) { $user = new User(); $user->firstname = 'Firstname' . $i; $user->lastname = 'Lastname' . $i; $user->ltiUserId = 'user' . $i; // Set user image values for some users. if ($i % 3 == 0) { $user->image = $imageurl; } $this->members[] = $user; } $this->context = Context::fromConsumer($this->consumer, 'testlticontextid'); $this->context->save(); $this->resourcelink = ResourceLink::fromContext($this->context, 'testresourcelinkid'); $this->resourcelink->save(); }
/** * Fetches a resource link record that is associated with a Context object. * * @param Context $context * @return ResourceLink */ public function get_resourcelink_from_context(Context $context) { global $DB; $resourcelink = null; if ($resourcelinkrecord = $DB->get_record($this->resourcelinktable, ['contextid' => $context->getRecordId()], 'ltiresourcelinkkey')) { $resourcelink = ResourceLink::fromContext($context, $resourcelinkrecord->ltiresourcelinkkey); } return $resourcelink; }