/** * Test for data_connector::deleteContext(). */ public function test_delete_context() { $dc = new data_connector(); $consumer = new ToolConsumer(null, $dc); $consumer->name = 'testconsumername'; $consumer->setKey('TestKey'); $consumer->secret = 'testsecret'; $consumer->save(); $title = 'testcontexttitle'; $settings = ['a', 'b', 'c']; $lticontextid = 'testlticontextid'; $context = Context::fromConsumer($consumer, $lticontextid); $context->title = $title; $context->settings = $settings; // Save the context. $this->assertTrue($dc->saveContext($context)); $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); $this->assertTrue($dc->deleteContext($context)); // Context properties should have been reset. $this->assertEmpty($context->title); $this->assertEmpty($context->settings); $this->assertNull($context->created); $this->assertNull($context->updated); // Context record should have already been deleted from the DB. $this->assertFalse($dc->loadContext($context)); // 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)); // 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); }