1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace enrol_lti\local\ltiadvantage\repository; 18 use enrol_lti\local\ltiadvantage\entity\application_registration; 19 use enrol_lti\local\ltiadvantage\entity\deployment; 20 21 /** 22 * Tests for deployment_repository. 23 * 24 * @package enrol_lti 25 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 * @coversDefaultClass \enrol_lti\local\ltiadvantage\repository\deployment_repository 28 */ 29 class deployment_repository_test extends \advanced_testcase { 30 /** 31 * Helper to create test deployment objects for use with the repository tests. 32 * 33 * @param string $deploymentid the string id of the deployment. 34 * @param int|null $appregistrationid the id of the application registration to which this deployment belongs. 35 * @return deployment the deployment. 36 */ 37 protected function create_test_deployment(string $deploymentid = 'DeployID123', 38 int $appregistrationid = null): deployment { 39 40 if (is_null($appregistrationid)) { 41 $registration = application_registration::create( 42 'Test', 43 'a2c94a2c94', 44 new \moodle_url('http://lms.example.org'), 45 'clientid_123', 46 new \moodle_url('https://example.org/authrequesturl'), 47 new \moodle_url('https://example.org/jwksurl'), 48 new \moodle_url('https://example.org/accesstokenurl') 49 ); 50 $registrationrepo = new application_registration_repository(); 51 $createdregistration = $registrationrepo->save($registration); 52 $appregistrationid = $createdregistration->get_id(); 53 } 54 return deployment::create( 55 $appregistrationid, 56 $deploymentid, 57 'Tool deployment on platform x', 58 ); 59 } 60 61 /** 62 * Helper to assert that all the key elements of two deployments (i.e. excluding id) are equal. 63 * 64 * @param deployment $expected the deployment whose values are deemed correct. 65 * @param deployment $check the deployment to check. 66 */ 67 protected function assert_same_deployment_values(deployment $expected, deployment $check): void { 68 $this->assertEquals($expected->get_deploymentname(), $check->get_deploymentname()); 69 $this->assertEquals($expected->get_deploymentid(), $check->get_deploymentid()); 70 $this->assertEquals($expected->get_registrationid(), $check->get_registrationid()); 71 $this->assertEquals($expected->get_legacy_consumer_key(), $check->get_legacy_consumer_key()); 72 } 73 74 /** 75 * Helper to assert that all the key elements of a deployment are present in the DB. 76 * 77 * @param deployment $expected the deployment whose values are deemed correct. 78 */ 79 protected function assert_deployment_db_values(deployment $expected) { 80 global $DB; 81 $checkrecord = $DB->get_record('enrol_lti_deployment', ['id' => $expected->get_id()]); 82 $this->assertEquals($expected->get_id(), $checkrecord->id); 83 $this->assertEquals($expected->get_deploymentname(), $checkrecord->name); 84 $this->assertEquals($expected->get_deploymentid(), $checkrecord->deploymentid); 85 $this->assertEquals($expected->get_registrationid(), $checkrecord->platformid); 86 $this->assertEquals($expected->get_legacy_consumer_key(), $checkrecord->legacyconsumerkey); 87 $this->assertNotEmpty($checkrecord->timecreated); 88 $this->assertNotEmpty($checkrecord->timemodified); 89 } 90 91 /** 92 * Test saving a new deployment. 93 * 94 * @covers ::save 95 */ 96 public function test_save_new() { 97 $this->resetAfterTest(); 98 $deploymentrepo = new deployment_repository(); 99 $deployment = $this->create_test_deployment(); 100 $deployment->set_legacy_consumer_key('test-consumer-key'); 101 $saved = $deploymentrepo->save($deployment); 102 103 $this->assertIsInt($saved->get_id()); 104 $this->assert_same_deployment_values($deployment, $saved); 105 $this->assert_deployment_db_values($saved); 106 } 107 108 /** 109 * Test saving an existing deployment. 110 * 111 * @covers ::save 112 */ 113 public function test_save_existing() { 114 $this->resetAfterTest(); 115 $deploymentrepo = new deployment_repository(); 116 $deployment = $this->create_test_deployment(); 117 $saved = $deploymentrepo->save($deployment); 118 119 $saved->set_legacy_consumer_key('added-consumer-key'); 120 $saved2 = $deploymentrepo->save($saved); 121 122 $this->assertEquals($saved->get_id(), $saved2->get_id()); 123 $this->assert_same_deployment_values($saved, $saved2); 124 $this->assert_deployment_db_values($saved2); 125 } 126 127 /** 128 * Test trying to save two deployments of identical nature in sequence. 129 * 130 * @covers ::save 131 */ 132 public function test_save_unique_constraints_not_met() { 133 $this->resetAfterTest(); 134 $deployment1 = $this->create_test_deployment('Deploy_ID_123'); 135 $deployment2 = $this->create_test_deployment('Deploy_ID_123', $deployment1->get_registrationid()); 136 $deploymentrepo = new deployment_repository(); 137 138 $this->assertInstanceOf(deployment::class, $deploymentrepo->save($deployment1)); 139 $this->expectException(\dml_exception::class); 140 $deploymentrepo->save($deployment2); 141 } 142 143 /** 144 * Test existence of a deployment within the repository. 145 * 146 * @covers ::exists 147 */ 148 public function test_exists() { 149 $this->resetAfterTest(); 150 $deploymentrepo = new deployment_repository(); 151 $deployment = $this->create_test_deployment(); 152 $saveddeployment = $deploymentrepo->save($deployment); 153 154 $this->assertTrue($deploymentrepo->exists($saveddeployment->get_id())); 155 $this->assertFalse($deploymentrepo->exists(0)); 156 } 157 158 /** 159 * Test finding a deployment in the repository. 160 * 161 * @covers ::find 162 */ 163 public function test_find() { 164 $this->resetAfterTest(); 165 $deployment = $this->create_test_deployment(); 166 $deploymentrepo = new deployment_repository(); 167 $saveddeployment = $deploymentrepo->save($deployment); 168 169 $founddeployment = $deploymentrepo->find($saveddeployment->get_id()); 170 $this->assertEquals($saveddeployment->get_id(), $founddeployment->get_id()); 171 $this->assert_same_deployment_values($saveddeployment, $founddeployment); 172 $this->assertNull($deploymentrepo->find(0)); 173 } 174 175 /** 176 * Test deleting a deployment object from the repository. 177 * 178 * @covers ::delete 179 */ 180 public function test_delete() { 181 $this->resetAfterTest(); 182 $deployment = $this->create_test_deployment(); 183 $deploymentrepo = new deployment_repository(); 184 $saveddeployment = $deploymentrepo->save($deployment); 185 $this->assertTrue($deploymentrepo->exists($saveddeployment->get_id())); 186 187 $deploymentrepo->delete($saveddeployment->get_id()); 188 $this->assertFalse($deploymentrepo->exists($saveddeployment->get_id())); 189 190 $this->assertNull($deploymentrepo->delete($saveddeployment->get_id())); 191 } 192 193 /** 194 * Test deleting a deployment by registration. 195 * 196 * @covers ::delete_by_registration 197 */ 198 public function test_delete_by_registration() { 199 $this->resetAfterTest(); 200 $deployment = $this->create_test_deployment(); 201 $deploymentrepo = new deployment_repository(); 202 $saveddeployment = $deploymentrepo->save($deployment); 203 $deployment2 = deployment::create($saveddeployment->get_registrationid(), 'another-deployment-id-1', 204 'another deployment 1'); 205 $saveddeployment2 = $deploymentrepo->save($deployment2); 206 $deployment3 = deployment::create($saveddeployment->get_registrationid() + 1, 'another-deployment-id-2', 207 'another deployment 2'); 208 $saveddeployment3 = $deploymentrepo->save($deployment3); 209 $this->assertTrue($deploymentrepo->exists($saveddeployment->get_id())); 210 $this->assertTrue($deploymentrepo->exists($saveddeployment2->get_id())); 211 $this->assertTrue($deploymentrepo->exists($saveddeployment3->get_id())); 212 213 $deploymentrepo->delete_by_registration($saveddeployment->get_registrationid()); 214 $this->assertFalse($deploymentrepo->exists($saveddeployment->get_id())); 215 $this->assertFalse($deploymentrepo->exists($saveddeployment2->get_id())); 216 $this->assertTrue($deploymentrepo->exists($saveddeployment3->get_id())); 217 218 $this->assertNull($deploymentrepo->delete($saveddeployment->get_id())); 219 } 220 221 /** 222 * Test counting the number of deployments for a given registration. 223 * 224 * @covers ::count_by_registration 225 */ 226 public function test_count_by_registration() { 227 $this->resetAfterTest(); 228 $deployment = $this->create_test_deployment(); 229 $deploymentrepo = new deployment_repository(); 230 $saveddeployment = $deploymentrepo->save($deployment); 231 $deployment2 = deployment::create($saveddeployment->get_registrationid(), 'another-deployment-id-1', 232 'another deployment 1'); 233 $saveddeployment2 = $deploymentrepo->save($deployment2); 234 $deployment3 = deployment::create($saveddeployment->get_registrationid() + 1, 'another-deployment-id-2', 235 'another deployment 2'); 236 $saveddeployment3 = $deploymentrepo->save($deployment3); 237 238 $this->assertEquals(2, $deploymentrepo->count_by_registration($saveddeployment->get_registrationid())); 239 $this->assertEquals(1, $deploymentrepo->count_by_registration($saveddeployment3->get_registrationid())); 240 $this->assertEquals(0, $deploymentrepo->count_by_registration(0)); 241 } 242 243 /** 244 * Test confirming a deployment can be found by registration and deploymentid. 245 * 246 * @covers ::find_by_registration 247 */ 248 public function test_find_by_registration() { 249 $this->resetAfterTest(); 250 $deployment = $this->create_test_deployment(); 251 $deploymentrepo = new deployment_repository(); 252 $saveddeployment = $deploymentrepo->save($deployment); 253 $regid = $saveddeployment->get_registrationid(); 254 255 // Existing registration. 256 $this->assertInstanceOf(deployment::class, 257 $deploymentrepo->find_by_registration($regid, $saveddeployment->get_deploymentid())); 258 259 // A non-existent registration. 260 $this->assertNull($deploymentrepo->find_by_registration($regid, 'NonExistentDeploymentId')); 261 } 262 263 /** 264 * Testing that all deployments for a given registration can be fetched. 265 * 266 * @covers ::find_all_by_registration 267 */ 268 public function test_find_all_by_registration() { 269 $this->resetAfterTest(); 270 $registration1 = application_registration::create( 271 'Test', 272 'a2c94a2c94', 273 new \moodle_url('http://lms.example.org'), 274 'clientid_123', 275 new \moodle_url('https://example.org/authrequesturl'), 276 new \moodle_url('https://example.org/jwksurl'), 277 new \moodle_url('https://example.org/accesstokenurl') 278 ); 279 $registration2 = application_registration::create( 280 'Test 2', 281 'c56bcdc56bcd', 282 new \moodle_url('http://lms2.example.org'), 283 'clientid_345', 284 new \moodle_url('https://example.org/authrequesturl'), 285 new \moodle_url('https://example.org/jwksurl'), 286 new \moodle_url('https://example.org/accesstokenurl') 287 ); 288 $registrationrepo = new application_registration_repository(); 289 $deploymentrepo = new deployment_repository(); 290 $createdregistration1 = $registrationrepo->save($registration1); 291 $createdregistration2 = $registrationrepo->save($registration2); 292 $deployment1 = $createdregistration1->add_tool_deployment('Deployment 1', 'reg1_deploy1'); 293 $deployment2 = $createdregistration1->add_tool_deployment('Deployment 2', 'reg1_deploy2'); 294 $deployment3 = $createdregistration2->add_tool_deployment('Deployment 3', 'reg2_deploy1'); 295 $saveddeployment1 = $deploymentrepo->save($deployment1); 296 $saveddeployment2 = $deploymentrepo->save($deployment2); 297 $saveddeployment3 = $deploymentrepo->save($deployment3); 298 $reg1saveddeployments = [ 299 $saveddeployment1->get_id() => $saveddeployment1, 300 $saveddeployment2->get_id() => $saveddeployment2 301 ]; 302 $reg2saveddeployments = [ 303 $saveddeployment3->get_id() => $saveddeployment3 304 ]; 305 306 // Registration 1. 307 $reg1founddeployments = $deploymentrepo->find_all_by_registration($createdregistration1->get_id()); 308 $this->assertCount(2, $reg1founddeployments); 309 foreach ($reg1founddeployments as $reg1founddeployment) { 310 $this->assertEquals($reg1saveddeployments[$reg1founddeployment->get_id()], $reg1founddeployment); 311 } 312 313 // Registration 2. 314 $reg2founddeployments = $deploymentrepo->find_all_by_registration($createdregistration2->get_id()); 315 $this->assertCount(1, $deploymentrepo->find_all_by_registration($createdregistration2->get_id())); 316 foreach ($reg2founddeployments as $reg2founddeployment) { 317 $this->assertEquals($reg2saveddeployments[$reg2founddeployment->get_id()], $reg2founddeployment); 318 } 319 320 // A non-existent registration. 321 $nonexistentregdeployments = $deploymentrepo->find_all_by_registration(0); 322 $this->assertEmpty($nonexistentregdeployments); 323 } 324 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body