/** * Test for data_connector::deleteToolConsumer(). */ public function test_delete_tool_consumer() { $dc = new data_connector(); $data = ['name' => 'TestName', 'secret' => 'TestSecret', 'ltiversion' => ToolProvider::LTI_VERSION1]; $consumer = new ToolConsumer(null, $dc); $consumer->name = $data['name']; $consumer->setKey('TestKey'); $consumer->secret = $data['secret']; $consumer->save(); $nonce = new ConsumerNonce($consumer, 'testnonce'); $nonce->save(); $context = Context::fromConsumer($consumer, 'testlticontext'); $context->save(); $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid'); $resourcelink->setContextId($context->getRecordId()); $resourcelink->save(); $this->assertEquals($consumer->getRecordId(), $resourcelink->getConsumer()->getRecordId()); $resourcelinkchild = ResourceLink::fromConsumer($consumer, 'testresourcelinkchildid'); $resourcelinkchild->primaryResourceLinkId = $resourcelink->getRecordId(); $resourcelinkchild->shareApproved = true; $resourcelinkchild->setContextId($context->getRecordId()); $resourcelinkchild->save(); $this->assertEquals($consumer->getRecordId(), $resourcelinkchild->getConsumer()->getRecordId()); $this->assertEquals($resourcelink->getRecordId(), $resourcelinkchild->primaryResourceLinkId); $this->assertTrue($resourcelinkchild->shareApproved); $resourcelinkchild2 = clone $resourcelink; $resourcelinkchild2->setRecordId(null); $resourcelinkchild2->setConsumerId(null); $resourcelinkchild2->setContextId(0); $resourcelinkchild2->primaryResourceLinkId = $resourcelink->getRecordId(); $resourcelinkchild2->shareApproved = true; $resourcelinkchild2->save(); $this->assertNull($resourcelinkchild2->getConsumer()->getRecordId()); $this->assertEquals(0, $resourcelinkchild2->getContextId()); $this->assertNotEquals($resourcelink->getRecordId(), $resourcelinkchild2->getRecordId()); $resourcelinksharekey = new ResourceLinkShareKey($resourcelink); $resourcelinksharekey->save(); $user = User::fromResourceLink($resourcelink, ''); $user->ltiResultSourcedId = 'testLtiResultSourcedId'; $dc->saveUser($user); // Confirm that tool consumer deletion processing ends successfully. $this->assertTrue($dc->deleteToolConsumer($consumer)); // Consumer object should have been initialised. foreach ($consumer as $key => $value) { $this->assertTrue(empty($value)); } // Nonce record should have been deleted. $this->assertFalse($dc->loadConsumerNonce($nonce)); // Share key record record should have been deleted. $this->assertFalse($dc->loadResourceLinkShareKey($resourcelinksharekey)); // Resource record link should have been deleted. $this->assertFalse($dc->loadResourceLink($resourcelink)); // Consumer record should have been deleted. $this->assertFalse($dc->loadToolConsumer($consumer)); // Resource links for contexts in this consumer should have been deleted. Even child ones. $this->assertFalse($dc->loadResourceLink($resourcelinkchild)); // Child resource link primaryResourceLinkId and shareApproved attributes should have been set to null. $this->assertTrue($dc->loadResourceLink($resourcelinkchild2)); $this->assertNull($resourcelinkchild2->primaryResourceLinkId); $this->assertNull($resourcelinkchild2->shareApproved); }