Ignore:
Timestamp:
May 2, 2016, 12:09:23 PM (3 years ago)
Author:
jrpelegrina
Message:

Updated to moodle 3.0.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • moodle/trunk/fuentes/group/tests/externallib_test.php

    r136 r1331  
    5151        $group1['descriptionformat'] = FORMAT_MOODLE;
    5252        $group1['enrolmentkey'] = 'Test group enrol secret phrase';
     53        $group1['idnumber'] = 'TEST1';
    5354        $group2 = array();
    5455        $group2['courseid'] = $course->id;
     
    5960        $group3['name'] = 'Group Test 3';
    6061        $group3['description'] = 'Group Test 3 description';
     62        $group3['idnumber'] = 'TEST1';
     63        $group4 = array();
     64        $group4['courseid'] = $course->id;
     65        $group4['name'] = 'Group Test 4';
     66        $group4['description'] = 'Group Test 4 description';
    6167
    6268        // Set the required capabilities by the external function
     
    8187                    $this->assertEquals($dbgroup->descriptionformat, $group1['descriptionformat']);
    8288                    $this->assertEquals($dbgroup->enrolmentkey, $group1['enrolmentkey']);
     89                    $this->assertEquals($dbgroup->idnumber, $group1['idnumber']);
    8390                    break;
    8491                case $group2['name']:
     
    94101        }
    95102
     103        try {
     104            $froups = core_group_external::create_groups(array($group3));
     105            $this->fail('Exception expected due to already existing idnumber.');
     106        } catch (moodle_exception $e) {
     107            $this->assertInstanceOf('invalid_parameter_exception', $e);
     108            $this->assertEquals('Invalid parameter value detected (Group with the same idnumber already exists)',
     109                $e->getMessage());
     110        }
     111
    96112        // Call without required capability
    97113        $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
    98114        $this->setExpectedException('required_capability_exception');
    99         $froups = core_group_external::create_groups(array($group3));
     115        $froups = core_group_external::create_groups(array($group4));
    100116    }
    101117
     
    115131        $group1data['descriptionformat'] = FORMAT_MOODLE;
    116132        $group1data['enrolmentkey'] = 'Test group enrol secret phrase';
     133        $group1data['idnumber'] = 'TEST1';
    117134        $group2data = array();
    118135        $group2data['courseid'] = $course->id;
     
    143160                    $this->assertEquals($dbgroup->descriptionformat, $group1->descriptionformat);
    144161                    $this->assertEquals($dbgroup->enrolmentkey, $group1->enrolmentkey);
     162                    $this->assertEquals($dbgroup->idnumber, $group1->idnumber);
    145163                    break;
    146164                case $group2->name:
     
    211229
    212230    /**
     231     * Test create and update groupings.
     232     * @return void
     233     */
     234    public function test_create_update_groupings() {
     235        global $DB;
     236
     237        $this->resetAfterTest(true);
     238
     239        $this->setAdminUser();
     240
     241        $course = self::getDataGenerator()->create_course();
     242
     243        $grouping1data = array();
     244        $grouping1data['courseid'] = $course->id;
     245        $grouping1data['name'] = 'Grouping 1 Test';
     246        $grouping1data['description'] = 'Grouping 1 Test description';
     247        $grouping1data['descriptionformat'] = FORMAT_MOODLE;
     248        $grouping1data['idnumber'] = 'TEST';
     249
     250        $grouping1 = self::getDataGenerator()->create_grouping($grouping1data);
     251
     252        $grouping1data['name'] = 'Another group';
     253
     254        try {
     255            $groupings = core_group_external::create_groupings(array($grouping1data));
     256            $this->fail('Exception expected due to already existing idnumber.');
     257        } catch (moodle_exception $e) {
     258            $this->assertInstanceOf('invalid_parameter_exception', $e);
     259            $this->assertEquals('Invalid parameter value detected (Grouping with the same idnumber already exists)',
     260                $e->getMessage());
     261        }
     262
     263        // No exception should be triggered.
     264        $grouping1data['id'] = $grouping1->id;
     265        $grouping1data['idnumber'] = 'CHANGED';
     266        unset($grouping1data['courseid']);
     267        core_group_external::update_groupings(array($grouping1data));
     268
     269        $grouping2data = array();
     270        $grouping2data['courseid'] = $course->id;
     271        $grouping2data['name'] = 'Grouping 2 Test';
     272        $grouping2data['description'] = 'Grouping 2 Test description';
     273        $grouping2data['descriptionformat'] = FORMAT_MOODLE;
     274        $grouping2data['idnumber'] = 'TEST';
     275
     276        $grouping2 = self::getDataGenerator()->create_grouping($grouping2data);
     277
     278        $grouping2data['id'] = $grouping2->id;
     279        $grouping2data['idnumber'] = 'CHANGED';
     280        unset($grouping2data['courseid']);
     281        try {
     282            $groupings = core_group_external::update_groupings(array($grouping2data));
     283            $this->fail('Exception expected due to already existing idnumber.');
     284        } catch (moodle_exception $e) {
     285            $this->assertInstanceOf('invalid_parameter_exception', $e);
     286            $this->assertEquals('Invalid parameter value detected (A different grouping with the same idnumber already exists)',
     287                $e->getMessage());
     288        }
     289    }
     290
     291    /**
    213292     * Test get_groupings
    214293     */
     
    286365        }
    287366    }
     367
     368    /**
     369     * Test get_groups
     370     */
     371    public function test_get_course_user_groups() {
     372        global $DB;
     373
     374        $this->resetAfterTest(true);
     375
     376        $student1 = self::getDataGenerator()->create_user();
     377        $student2 = self::getDataGenerator()->create_user();
     378        $teacher = self::getDataGenerator()->create_user();
     379
     380        $course = self::getDataGenerator()->create_course();
     381        $emptycourse = self::getDataGenerator()->create_course();
     382
     383        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     384        $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
     385        $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
     386
     387        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
     388        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
     389        $this->getDataGenerator()->enrol_user($teacher->id, $emptycourse->id, $teacherrole->id);
     390
     391        $group1data = array();
     392        $group1data['courseid'] = $course->id;
     393        $group1data['name'] = 'Group Test 1';
     394        $group1data['description'] = 'Group Test 1 description';
     395        $group1data['idnumber'] = 'TEST1';
     396        $group2data = array();
     397        $group2data['courseid'] = $course->id;
     398        $group2data['name'] = 'Group Test 2';
     399        $group2data['description'] = 'Group Test 2 description';
     400        $group1 = self::getDataGenerator()->create_group($group1data);
     401        $group2 = self::getDataGenerator()->create_group($group2data);
     402
     403        groups_add_member($group1->id, $student1->id);
     404        groups_add_member($group1->id, $student2->id);
     405        groups_add_member($group2->id, $student1->id);
     406
     407        $this->setUser($student1);
     408
     409        $groups = core_group_external::get_course_user_groups($course->id, $student1->id);
     410        $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
     411        // Check that I see my groups.
     412        $this->assertCount(2, $groups['groups']);
     413
     414        $this->setUser($student2);
     415        $groups = core_group_external::get_course_user_groups($course->id, $student2->id);
     416        $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
     417        // Check that I see my groups.
     418        $this->assertCount(1, $groups['groups']);
     419
     420        $this->assertEquals($group1data['name'], $groups['groups'][0]['name']);
     421        $this->assertEquals($group1data['description'], $groups['groups'][0]['description']);
     422        $this->assertEquals($group1data['idnumber'], $groups['groups'][0]['idnumber']);
     423
     424        $this->setUser($teacher);
     425        $groups = core_group_external::get_course_user_groups($course->id, $student1->id);
     426        $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
     427        // Check that a teacher can see student groups.
     428        $this->assertCount(2, $groups['groups']);
     429
     430        $groups = core_group_external::get_course_user_groups($course->id, $student2->id);
     431        $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
     432        // Check that a teacher can see student groups.
     433        $this->assertCount(1, $groups['groups']);
     434
     435        // Check permissions.
     436        $this->setUser($student1);
     437        try {
     438            $groups = core_group_external::get_course_user_groups($course->id, $student2->id);
     439        } catch (moodle_exception $e) {
     440            $this->assertEquals('accessdenied', $e->errorcode);
     441        }
     442
     443        try {
     444            $groups = core_group_external::get_course_user_groups($emptycourse->id, $student2->id);
     445        } catch (moodle_exception $e) {
     446            $this->assertEquals('requireloginerror', $e->errorcode);
     447        }
     448
     449        $this->setUser($teacher);
     450        // Check warnings.
     451        $groups = core_group_external::get_course_user_groups($emptycourse->id, $student1->id);
     452        $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
     453        $this->assertCount(1, $groups['warnings']);
     454
     455    }
     456
     457    /**
     458     * Test get_activity_allowed_groups
     459     */
     460    public function test_get_activity_allowed_groups() {
     461        global $DB;
     462
     463        $this->resetAfterTest(true);
     464
     465        $generator = self::getDataGenerator();
     466
     467        $student = $generator->create_user();
     468        $otherstudent = $generator->create_user();
     469        $teacher = $generator->create_user();
     470        $course = $generator->create_course();
     471        $othercourse = $generator->create_course();
     472
     473        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     474        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
     475        $generator->enrol_user($student->id, $course->id, $studentrole->id);
     476        $generator->enrol_user($otherstudent->id, $othercourse->id, $studentrole->id);
     477        $generator->enrol_user($teacher->id, $course->id, $teacherrole->id);
     478
     479        $forum1 = $generator->create_module("forum", array('course' => $course->id), array('groupmode' => VISIBLEGROUPS));
     480        $forum2 = $generator->create_module("forum", array('course' => $othercourse->id));
     481        $forum3 = $generator->create_module("forum", array('course' => $course->id), array('visible' => 0));
     482
     483        // Request data for tests.
     484        $cm1 = get_coursemodule_from_instance("forum", $forum1->id);
     485        $cm2 = get_coursemodule_from_instance("forum", $forum2->id);
     486        $cm3 = get_coursemodule_from_instance("forum", $forum3->id);
     487
     488        $group1data = array();
     489        $group1data['courseid'] = $course->id;
     490        $group1data['name'] = 'Group Test 1';
     491        $group1data['description'] = 'Group Test 1 description';
     492        $group1data['idnumber'] = 'TEST1';
     493        $group2data = array();
     494        $group2data['courseid'] = $course->id;
     495        $group2data['name'] = 'Group Test 2';
     496        $group2data['description'] = 'Group Test 2 description';
     497        $group2data['idnumber'] = 'TEST2';
     498        $group1 = $generator->create_group($group1data);
     499        $group2 = $generator->create_group($group2data);
     500
     501        groups_add_member($group1->id, $student->id);
     502        groups_add_member($group2->id, $student->id);
     503
     504        $this->setUser($student);
     505
     506        // First try possible errors.
     507        try {
     508            $data = core_group_external::get_activity_allowed_groups($cm2->id);
     509        } catch (moodle_exception $e) {
     510            $this->assertEquals('requireloginerror', $e->errorcode);
     511        }
     512
     513        try {
     514            $data = core_group_external::get_activity_allowed_groups($cm3->id);
     515        } catch (moodle_exception $e) {
     516            $this->assertEquals('requireloginerror', $e->errorcode);
     517        }
     518
     519        // Retrieve my groups.
     520        $groups = core_group_external::get_activity_allowed_groups($cm1->id);
     521        $groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups);
     522        $this->assertCount(2, $groups['groups']);
     523
     524        foreach ($groups['groups'] as $group) {
     525            if ($group['name'] == $group1data['name']) {
     526                $this->assertEquals($group1data['description'], $group['description']);
     527                $this->assertEquals($group1data['idnumber'], $group['idnumber']);
     528            } else {
     529                $this->assertEquals($group2data['description'], $group['description']);
     530                $this->assertEquals($group2data['idnumber'], $group['idnumber']);
     531            }
     532        }
     533
     534        $this->setUser($teacher);
     535        // Retrieve other users groups.
     536        $groups = core_group_external::get_activity_allowed_groups($cm1->id, $student->id);
     537        $groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups);
     538        $this->assertCount(2, $groups['groups']);
     539
     540        // Check warnings. Trying to get groups for a user not enrolled in course.
     541        $groups = core_group_external::get_activity_allowed_groups($cm1->id, $otherstudent->id);
     542        $groups = external_api::clean_returnvalue(core_group_external::get_activity_allowed_groups_returns(), $groups);
     543        $this->assertCount(1, $groups['warnings']);
     544
     545    }
     546
     547    /**
     548     * Test get_activity_groupmode
     549     */
     550    public function test_get_activity_groupmode() {
     551        global $DB;
     552
     553        $this->resetAfterTest(true);
     554
     555        $generator = self::getDataGenerator();
     556
     557        $student = $generator->create_user();
     558        $course = $generator->create_course();
     559        $othercourse = $generator->create_course();
     560
     561        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     562        $generator->enrol_user($student->id, $course->id, $studentrole->id);
     563
     564        $forum1 = $generator->create_module("forum", array('course' => $course->id), array('groupmode' => VISIBLEGROUPS));
     565        $forum2 = $generator->create_module("forum", array('course' => $othercourse->id));
     566        $forum3 = $generator->create_module("forum", array('course' => $course->id), array('visible' => 0));
     567
     568        // Request data for tests.
     569        $cm1 = get_coursemodule_from_instance("forum", $forum1->id);
     570        $cm2 = get_coursemodule_from_instance("forum", $forum2->id);
     571        $cm3 = get_coursemodule_from_instance("forum", $forum3->id);
     572
     573        $this->setUser($student);
     574
     575        $data = core_group_external::get_activity_groupmode($cm1->id);
     576        $data = external_api::clean_returnvalue(core_group_external::get_activity_groupmode_returns(), $data);
     577        $this->assertEquals(VISIBLEGROUPS, $data['groupmode']);
     578
     579        try {
     580            $data = core_group_external::get_activity_groupmode($cm2->id);
     581        } catch (moodle_exception $e) {
     582            $this->assertEquals('requireloginerror', $e->errorcode);
     583        }
     584
     585        try {
     586            $data = core_group_external::get_activity_groupmode($cm3->id);
     587        } catch (moodle_exception $e) {
     588            $this->assertEquals('requireloginerror', $e->errorcode);
     589        }
     590
     591    }
    288592}
Note: See TracChangeset for help on using the changeset viewer.