Exemplo n.º 1
0
 /**
  * Test that we can add related competencies.
  *
  * @return void
  */
 public function test_add_related_competency()
 {
     global $DB;
     $this->setUser($this->creator);
     $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
     $framework = $lpg->create_framework();
     $framework2 = $lpg->create_framework();
     $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
     $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
     $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
     $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework2->get_id()));
     // The lower one always as competencyid.
     $result = external::add_related_competency($competency1->get_id(), $competency2->get_id());
     $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
     $this->assertTrue($result);
     $this->assertTrue($DB->record_exists_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('cid' => $competency1->get_id(), 'rid' => $competency2->get_id())));
     $this->assertFalse($DB->record_exists_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('cid' => $competency2->get_id(), 'rid' => $competency1->get_id())));
     $result = external::add_related_competency($competency3->get_id(), $competency1->get_id());
     $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
     $this->assertTrue($result);
     $this->assertTrue($DB->record_exists_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('cid' => $competency1->get_id(), 'rid' => $competency3->get_id())));
     $this->assertFalse($DB->record_exists_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('cid' => $competency3->get_id(), 'rid' => $competency1->get_id())));
     // We can not allow a duplicate relation, not even in the other direction.
     $this->assertEquals(1, $DB->count_records_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('cid' => $competency1->get_id(), 'rid' => $competency2->get_id())));
     $this->assertEquals(0, $DB->count_records_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('rid' => $competency1->get_id(), 'cid' => $competency2->get_id())));
     $result = external::add_related_competency($competency2->get_id(), $competency1->get_id());
     $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
     $this->assertTrue($result);
     $this->assertEquals(1, $DB->count_records_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('cid' => $competency1->get_id(), 'rid' => $competency2->get_id())));
     $this->assertEquals(0, $DB->count_records_select(related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid', array('rid' => $competency1->get_id(), 'cid' => $competency2->get_id())));
     // Check that we cannot create links across frameworks.
     try {
         external::add_related_competency($competency1->get_id(), $competency4->get_id());
         $this->fail('Exception expected due mis-use of shared competencies');
     } catch (invalid_persistent_exception $e) {
         // Yay!
     }
     // User without permission.
     $this->setUser($this->user);
     // Check we can not add the related competency now.
     try {
         external::add_related_competency($competency1->get_id(), $competency3->get_id());
         $this->fail('Exception expected due to not permissions to manage template competencies');
     } catch (moodle_exception $e) {
         $this->assertEquals('nopermissions', $e->errorcode);
     }
 }