[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/manual/tests/ -> externallib_test.php (source)

   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  /**
  18   * Enrol manual external PHPunit tests
  19   *
  20   * @package    enrol_manual
  21   * @category   phpunit
  22   * @copyright  2012 Jerome Mouneyrac
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since Moodle 2.4
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  require_once($CFG->dirroot . '/enrol/manual/externallib.php');
  33  
  34  class enrol_manual_externallib_testcase extends externallib_advanced_testcase {
  35  
  36      /**
  37       * Test get_enrolled_users
  38       */
  39      public function test_enrol_users() {
  40          global $DB;
  41  
  42          $this->resetAfterTest(true);
  43  
  44          $user = self::getDataGenerator()->create_user();
  45          $this->setUser($user);
  46  
  47          $course1 = self::getDataGenerator()->create_course();
  48          $course2 = self::getDataGenerator()->create_course();
  49          $user1 = self::getDataGenerator()->create_user();
  50          $user2 = self::getDataGenerator()->create_user();
  51  
  52          $context1 = context_course::instance($course1->id);
  53          $context2 = context_course::instance($course2->id);
  54          $instance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  55          $instance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  56  
  57          // Set the required capabilities by the external function.
  58          $roleid = $this->assignUserCapability('enrol/manual:enrol', $context1->id);
  59          $this->assignUserCapability('moodle/course:view', $context1->id, $roleid);
  60          $this->assignUserCapability('moodle/role:assign', $context1->id, $roleid);
  61          $this->assignUserCapability('enrol/manual:enrol', $context2->id, $roleid);
  62          $this->assignUserCapability('moodle/course:view', $context2->id, $roleid);
  63          $this->assignUserCapability('moodle/role:assign', $context2->id, $roleid);
  64  
  65          allow_assign($roleid, 3);
  66  
  67          // Call the external function.
  68          enrol_manual_external::enrol_users(array(
  69              array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
  70              array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course1->id),
  71          ));
  72  
  73          $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
  74          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
  75          $this->assertTrue(is_enrolled($context1, $user1));
  76          $this->assertTrue(is_enrolled($context1, $user2));
  77  
  78          // Call without required capability.
  79          $DB->delete_records('user_enrolments');
  80          $this->unassignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
  81          try {
  82              enrol_manual_external::enrol_users(array(
  83                  array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
  84              ));
  85              $this->fail('Exception expected if not having capability to enrol');
  86          } catch (moodle_exception $e) {
  87              $this->assertInstanceOf('required_capability_exception', $e);
  88              $this->assertSame('nopermissions', $e->errorcode);
  89          }
  90          $this->assignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
  91          $this->assertEquals(0, $DB->count_records('user_enrolments'));
  92  
  93          // Call with forbidden role.
  94          try {
  95              enrol_manual_external::enrol_users(array(
  96                  array('roleid' => 1, 'userid' => $user1->id, 'courseid' => $course1->id),
  97              ));
  98              $this->fail('Exception expected if not allowed to assign role.');
  99          } catch (moodle_exception $e) {
 100              $this->assertSame('wsusercannotassign', $e->errorcode);
 101          }
 102          $this->assertEquals(0, $DB->count_records('user_enrolments'));
 103  
 104          // Call for course without manual instance.
 105          $DB->delete_records('user_enrolments');
 106          $DB->delete_records('enrol', array('courseid' => $course2->id));
 107          try {
 108              enrol_manual_external::enrol_users(array(
 109                  array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
 110                  array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course2->id),
 111              ));
 112              $this->fail('Exception expected if course does not have manual instance');
 113          } catch (moodle_exception $e) {
 114              $this->assertSame('wsnoinstance', $e->errorcode);
 115          }
 116      }
 117  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1