Esempio n. 1
0
 /**
  * Test that we can read plans.
  */
 public function test_read_plans()
 {
     global $OUTPUT;
     $this->setUser($this->creator);
     $syscontext = context_system::instance();
     $plan1 = $this->create_plan(1, $this->user->id, 0, plan::STATUS_DRAFT, 0);
     $plan2 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
     $plan3 = $this->create_plan(3, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
     external::complete_plan($plan3->id);
     $plan3 = (object) external::read_plan($plan3->id);
     $data = external::read_plan($plan1->id);
     $this->assertEquals((array) $plan1, external::read_plan($plan1->id));
     $data = external::read_plan($plan2->id);
     $this->assertEquals((array) $plan2, external::read_plan($plan2->id));
     $data = external::read_plan($plan3->id);
     $this->assertEquals((array) $plan3, external::read_plan($plan3->id));
     $this->setUser($this->user);
     // The normal user can not edit these plans.
     $plan1->canmanage = false;
     $plan2->canmanage = false;
     $plan3->canmanage = false;
     $plan1->canbeedited = false;
     $plan2->canbeedited = false;
     $plan3->canbeedited = false;
     $plan1->canrequestreview = true;
     $plan2->canrequestreview = true;
     $plan3->canrequestreview = true;
     $plan1->canreview = false;
     $plan2->canreview = false;
     $plan3->canreview = false;
     $plan1->iscompleteallowed = false;
     $plan2->iscompleteallowed = false;
     $plan3->iscompleteallowed = false;
     $plan1->isrequestreviewallowed = true;
     $plan2->isrequestreviewallowed = true;
     $plan3->isrequestreviewallowed = true;
     $plan1->isapproveallowed = false;
     $plan2->isapproveallowed = false;
     $plan3->isapproveallowed = false;
     $plan1->isunapproveallowed = false;
     $plan2->isunapproveallowed = false;
     $plan3->isunapproveallowed = false;
     $plan3->isreopenallowed = false;
     $plan1->commentarea['canpost'] = false;
     $plan1->commentarea['canview'] = true;
     // Prevent the user from seeing their own non-draft plans.
     assign_capability('moodle/competency:plancommentown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true);
     accesslib_clear_all_caches_for_unit_testing();
     $this->assertEquals((array) $plan1, external::read_plan($plan1->id));
     try {
         external::read_plan($plan2->id);
         $this->fail('Exception expected due to not permissions to read plan');
     } catch (moodle_exception $e) {
         $this->assertEquals('nopermissions', $e->errorcode);
     }
     try {
         external::read_plan($plan3->id);
         $this->fail('Exception expected due to not permissions to read plan');
     } catch (moodle_exception $e) {
         $this->assertEquals('nopermissions', $e->errorcode);
     }
     // Allow user to see their plan.
     assign_capability('moodle/competency:plancommentown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planviewown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
     accesslib_clear_all_caches_for_unit_testing();
     $plan1->commentarea['canpost'] = true;
     $plan1->commentarea['canview'] = true;
     $plan2->commentarea['canpost'] = true;
     $plan2->isrequestreviewallowed = false;
     $plan3->commentarea['canpost'] = true;
     $plan3->isrequestreviewallowed = false;
     $plan1->commentarea['canpostorhascomments'] = true;
     $plan2->commentarea['canpostorhascomments'] = true;
     $plan3->commentarea['canpostorhascomments'] = true;
     $this->assertEquals((array) $plan1, external::read_plan($plan1->id));
     $this->assertEquals((array) $plan2, external::read_plan($plan2->id));
     $this->assertEquals((array) $plan3, external::read_plan($plan3->id));
     // Allow use to manage their own draft plan.
     assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planmanageown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true);
     accesslib_clear_all_caches_for_unit_testing();
     $plan1->canmanage = true;
     $plan1->canbeedited = true;
     $plan1->canrequestreview = true;
     $plan1->isrequestreviewallowed = true;
     $this->assertEquals((array) $plan1, external::read_plan($plan1->id));
     try {
         external::read_plan($plan2->id);
         $this->fail('Exception expected due to not permissions to read plan');
     } catch (moodle_exception $e) {
         $this->assertEquals('nopermissions', $e->errorcode);
     }
     try {
         external::read_plan($plan3->id);
         $this->fail('Exception expected due to not permissions to read plan');
     } catch (moodle_exception $e) {
         $this->assertEquals('nopermissions', $e->errorcode);
     }
     // Allow use to manage their plan.
     assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
     assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
     accesslib_clear_all_caches_for_unit_testing();
     $plan1->canmanage = false;
     $plan1->canbeedited = false;
     $plan1->canrequestreview = true;
     $plan1->canreview = true;
     $plan1->isrequestreviewallowed = true;
     $plan1->isapproveallowed = true;
     $plan1->iscompleteallowed = false;
     $plan2->canmanage = true;
     $plan2->canbeedited = true;
     $plan2->canreview = true;
     $plan2->iscompleteallowed = true;
     $plan2->isunapproveallowed = true;
     $plan3->canmanage = true;
     $plan3->canreview = true;
     $plan3->isreopenallowed = true;
     $this->assertEquals((array) $plan1, external::read_plan($plan1->id));
     $this->assertEquals((array) $plan2, external::read_plan($plan2->id));
     $this->assertEquals((array) $plan3, external::read_plan($plan3->id));
 }