Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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   * External course functions unit tests
      19   *
      20   * @package    core_course
      21   * @category   external
      22   * @copyright  2012 Jerome Mouneyrac
      23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      24   */
      25  
      26  defined('MOODLE_INTERNAL') || die();
      27  
      28  global $CFG;
      29  
      30  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
      31  
      32  /**
      33   * External course functions unit tests
      34   *
      35   * @package    core_course
      36   * @category   external
      37   * @copyright  2012 Jerome Mouneyrac
      38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      39   */
      40  class core_course_externallib_testcase extends externallib_advanced_testcase {
      41  
      42      /**
      43       * Tests set up
      44       */
      45      protected function setUp() {
      46          global $CFG;
      47          require_once($CFG->dirroot . '/course/externallib.php');
      48      }
      49  
      50      /**
      51       * Tidy up open files that may be left open.
      52       */
      53      protected function tearDown() {
      54          gc_collect_cycles();
      55      }
      56  
      57      /**
      58       * Test create_categories
      59       */
      60      public function test_create_categories() {
      61  
      62          global $DB;
      63  
      64          $this->resetAfterTest(true);
      65  
      66          // Set the required capabilities by the external function
      67          $contextid = context_system::instance()->id;
      68          $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
      69  
      70          // Create base categories.
      71          $category1 = new stdClass();
      72          $category1->name = 'Root Test Category 1';
      73          $category2 = new stdClass();
      74          $category2->name = 'Root Test Category 2';
      75          $category2->idnumber = 'rootcattest2';
      76          $category2->desc = 'Description for root test category 1';
      77          $category2->theme = 'base';
      78          $categories = array(
      79              array('name' => $category1->name, 'parent' => 0),
      80              array('name' => $category2->name, 'parent' => 0, 'idnumber' => $category2->idnumber,
      81                  'description' => $category2->desc, 'theme' => $category2->theme)
      82          );
      83  
      84          $createdcats = core_course_external::create_categories($categories);
      85  
      86          // We need to execute the return values cleaning process to simulate the web service server.
      87          $createdcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdcats);
      88  
      89          // Initially confirm that base data was inserted correctly.
      90          $this->assertEquals($category1->name, $createdcats[0]['name']);
      91          $this->assertEquals($category2->name, $createdcats[1]['name']);
      92  
      93          // Save the ids.
      94          $category1->id = $createdcats[0]['id'];
      95          $category2->id = $createdcats[1]['id'];
      96  
      97          // Create on sub category.
      98          $category3 = new stdClass();
      99          $category3->name = 'Sub Root Test Category 3';
     100          $subcategories = array(
     101              array('name' => $category3->name, 'parent' => $category1->id)
     102          );
     103  
     104          $createdsubcats = core_course_external::create_categories($subcategories);
     105  
     106          // We need to execute the return values cleaning process to simulate the web service server.
     107          $createdsubcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdsubcats);
     108  
     109          // Confirm that sub categories were inserted correctly.
     110          $this->assertEquals($category3->name, $createdsubcats[0]['name']);
     111  
     112          // Save the ids.
     113          $category3->id = $createdsubcats[0]['id'];
     114  
     115          // Calling the ws function should provide a new sortorder to give category1,
     116          // category2, category3. New course categories are ordered by id not name.
     117          $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
     118          $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
     119          $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
     120  
     121          // sortorder sequence (and sortorder) must be:
     122          // category 1
     123          //   category 3
     124          // category 2
     125          $this->assertGreaterThan($category1->sortorder, $category3->sortorder);
     126          $this->assertGreaterThan($category3->sortorder, $category2->sortorder);
     127  
     128          // Call without required capability
     129          $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
     130          $this->setExpectedException('required_capability_exception');
     131          $createdsubcats = core_course_external::create_categories($subcategories);
     132  
     133      }
     134  
     135      /**
     136       * Test delete categories
     137       */
     138      public function test_delete_categories() {
     139          global $DB;
     140  
     141          $this->resetAfterTest(true);
     142  
     143          // Set the required capabilities by the external function
     144          $contextid = context_system::instance()->id;
     145          $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
     146  
     147          $category1  = self::getDataGenerator()->create_category();
     148          $category2  = self::getDataGenerator()->create_category(
     149                  array('parent' => $category1->id));
     150          $category3  = self::getDataGenerator()->create_category();
     151          $category4  = self::getDataGenerator()->create_category(
     152                  array('parent' => $category3->id));
     153          $category5  = self::getDataGenerator()->create_category(
     154                  array('parent' => $category4->id));
     155  
     156          //delete category 1 and 2 + delete category 4, category 5 moved under category 3
     157          core_course_external::delete_categories(array(
     158              array('id' => $category1->id, 'recursive' => 1),
     159              array('id' => $category4->id)
     160          ));
     161  
     162          //check $category 1 and 2 are deleted
     163          $notdeletedcount = $DB->count_records_select('course_categories',
     164              'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')');
     165          $this->assertEquals(0, $notdeletedcount);
     166  
     167          //check that $category5 as $category3 for parent
     168          $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id));
     169          $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id);
     170  
     171           // Call without required capability
     172          $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
     173          $this->setExpectedException('required_capability_exception');
     174          $createdsubcats = core_course_external::delete_categories(
     175                  array(array('id' => $category3->id)));
     176      }
     177  
     178      /**
     179       * Test get categories
     180       */
     181      public function test_get_categories() {
     182          global $DB;
     183  
     184          $this->resetAfterTest(true);
     185  
     186          $generatedcats = array();
     187          $category1data['idnumber'] = 'idnumbercat1';
     188          $category1data['name'] = 'Category 1 for PHPunit test';
     189          $category1data['description'] = 'Category 1 description';
     190          $category1data['descriptionformat'] = FORMAT_MOODLE;
     191          $category1  = self::getDataGenerator()->create_category($category1data);
     192          $generatedcats[$category1->id] = $category1;
     193          $category2  = self::getDataGenerator()->create_category(
     194                  array('parent' => $category1->id));
     195          $generatedcats[$category2->id] = $category2;
     196          $category6  = self::getDataGenerator()->create_category(
     197                  array('parent' => $category1->id, 'visible' => 0));
     198          $generatedcats[$category6->id] = $category6;
     199          $category3  = self::getDataGenerator()->create_category();
     200          $generatedcats[$category3->id] = $category3;
     201          $category4  = self::getDataGenerator()->create_category(
     202                  array('parent' => $category3->id));
     203          $generatedcats[$category4->id] = $category4;
     204          $category5  = self::getDataGenerator()->create_category(
     205                  array('parent' => $category4->id));
     206          $generatedcats[$category5->id] = $category5;
     207  
     208          // Set the required capabilities by the external function.
     209          $context = context_system::instance();
     210          $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
     211  
     212          // Retrieve category1 + sub-categories except not visible ones
     213          $categories = core_course_external::get_categories(array(
     214              array('key' => 'id', 'value' => $category1->id),
     215              array('key' => 'visible', 'value' => 1)), 1);
     216  
     217          // We need to execute the return values cleaning process to simulate the web service server.
     218          $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
     219  
     220          // Check we retrieve the good total number of categories.
     221          $this->assertEquals(2, count($categories));
     222  
     223          // Check the return values
     224          foreach ($categories as $category) {
     225              $generatedcat = $generatedcats[$category['id']];
     226              $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
     227              $this->assertEquals($category['name'], $generatedcat->name);
     228              // Description was converted to the HTML format.
     229              $this->assertEquals($category['description'], format_text($generatedcat->description, FORMAT_MOODLE, array('para' => false)));
     230              $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
     231          }
     232  
     233          // Check different params.
     234          $categories = core_course_external::get_categories(array(
     235              array('key' => 'id', 'value' => $category1->id),
     236              array('key' => 'idnumber', 'value' => $category1->idnumber),
     237              array('key' => 'visible', 'value' => 1)), 0);
     238  
     239          // We need to execute the return values cleaning process to simulate the web service server.
     240          $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
     241  
     242          $this->assertEquals(1, count($categories));
     243  
     244          // Retrieve categories from parent.
     245          $categories = core_course_external::get_categories(array(
     246              array('key' => 'parent', 'value' => $category3->id)), 1);
     247          $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
     248  
     249          $this->assertEquals(2, count($categories));
     250  
     251          // Retrieve all categories.
     252          $categories = core_course_external::get_categories();
     253  
     254          // We need to execute the return values cleaning process to simulate the web service server.
     255          $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
     256  
     257          $this->assertEquals($DB->count_records('course_categories'), count($categories));
     258  
     259          // Call without required capability (it will fail cause of the search on idnumber).
     260          $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
     261          $this->setExpectedException('moodle_exception');
     262          $categories = core_course_external::get_categories(array(
     263              array('key' => 'id', 'value' => $category1->id),
     264              array('key' => 'idnumber', 'value' => $category1->idnumber),
     265              array('key' => 'visible', 'value' => 1)), 0);
     266      }
     267  
     268      /**
     269       * Test update_categories
     270       */
     271      public function test_update_categories() {
     272          global $DB;
     273  
     274          $this->resetAfterTest(true);
     275  
     276          // Set the required capabilities by the external function
     277          $contextid = context_system::instance()->id;
     278          $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
     279  
     280          // Create base categories.
     281          $category1data['idnumber'] = 'idnumbercat1';
     282          $category1data['name'] = 'Category 1 for PHPunit test';
     283          $category1data['description'] = 'Category 1 description';
     284          $category1data['descriptionformat'] = FORMAT_MOODLE;
     285          $category1  = self::getDataGenerator()->create_category($category1data);
     286          $category2  = self::getDataGenerator()->create_category(
     287                  array('parent' => $category1->id));
     288          $category3  = self::getDataGenerator()->create_category();
     289          $category4  = self::getDataGenerator()->create_category(
     290                  array('parent' => $category3->id));
     291          $category5  = self::getDataGenerator()->create_category(
     292                  array('parent' => $category4->id));
     293  
     294          // We update all category1 attribut.
     295          // Then we move cat4 and cat5 parent: cat3 => cat1
     296          $categories = array(
     297              array('id' => $category1->id,
     298                  'name' => $category1->name . '_updated',
     299                  'idnumber' => $category1->idnumber . '_updated',
     300                  'description' => $category1->description . '_updated',
     301                  'descriptionformat' => FORMAT_HTML,
     302                  'theme' => $category1->theme),
     303              array('id' => $category4->id, 'parent' => $category1->id));
     304  
     305          core_course_external::update_categories($categories);
     306  
     307          // Check the values were updated.
     308          $dbcategories = $DB->get_records_select('course_categories',
     309                  'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
     310                  . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
     311          $this->assertEquals($category1->name . '_updated',
     312                  $dbcategories[$category1->id]->name);
     313          $this->assertEquals($category1->idnumber . '_updated',
     314                  $dbcategories[$category1->id]->idnumber);
     315          $this->assertEquals($category1->description . '_updated',
     316                  $dbcategories[$category1->id]->description);
     317          $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
     318  
     319          // Check that category4 and category5 have been properly moved.
     320          $this->assertEquals('/' . $category1->id . '/' . $category4->id,
     321                  $dbcategories[$category4->id]->path);
     322          $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
     323                  $dbcategories[$category5->id]->path);
     324  
     325          // Call without required capability.
     326          $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
     327          $this->setExpectedException('required_capability_exception');
     328          core_course_external::update_categories($categories);
     329      }
     330  
     331      /**
     332       * Test create_courses
     333       */
     334      public function test_create_courses() {
     335          global $DB;
     336  
     337          $this->resetAfterTest(true);
     338  
     339          // Enable course completion.
     340          set_config('enablecompletion', 1);
     341          // Enable course themes.
     342          set_config('allowcoursethemes', 1);
     343  
     344          // Set the required capabilities by the external function
     345          $contextid = context_system::instance()->id;
     346          $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
     347          $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
     348  
     349          $category  = self::getDataGenerator()->create_category();
     350  
     351          // Create base categories.
     352          $course1['fullname'] = 'Test course 1';
     353          $course1['shortname'] = 'Testcourse1';
     354          $course1['categoryid'] = $category->id;
     355          $course2['fullname'] = 'Test course 2';
     356          $course2['shortname'] = 'Testcourse2';
     357          $course2['categoryid'] = $category->id;
     358          $course2['idnumber'] = 'testcourse2idnumber';
     359          $course2['summary'] = 'Description for course 2';
     360          $course2['summaryformat'] = FORMAT_MOODLE;
     361          $course2['format'] = 'weeks';
     362          $course2['showgrades'] = 1;
     363          $course2['newsitems'] = 3;
     364          $course2['startdate'] = 1420092000; // 01/01/2015
     365          $course2['numsections'] = 4;
     366          $course2['maxbytes'] = 100000;
     367          $course2['showreports'] = 1;
     368          $course2['visible'] = 0;
     369          $course2['hiddensections'] = 0;
     370          $course2['groupmode'] = 0;
     371          $course2['groupmodeforce'] = 0;
     372          $course2['defaultgroupingid'] = 0;
     373          $course2['enablecompletion'] = 1;
     374          $course2['completionnotify'] = 1;
     375          $course2['lang'] = 'en';
     376          $course2['forcetheme'] = 'base';
     377          $course3['fullname'] = 'Test course 3';
     378          $course3['shortname'] = 'Testcourse3';
     379          $course3['categoryid'] = $category->id;
     380          $course3['format'] = 'topics';
     381          $course3options = array('numsections' => 8,
     382              'hiddensections' => 1,
     383              'coursedisplay' => 1);
     384          $course3['courseformatoptions'] = array();
     385          foreach ($course3options as $key => $value) {
     386              $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
     387          }
     388          $courses = array($course1, $course2, $course3);
     389  
     390          $createdcourses = core_course_external::create_courses($courses);
     391  
     392          // We need to execute the return values cleaning process to simulate the web service server.
     393          $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
     394  
     395          // Check that right number of courses were created.
     396          $this->assertEquals(3, count($createdcourses));
     397  
     398          // Check that the courses were correctly created.
     399          foreach ($createdcourses as $createdcourse) {
     400              $courseinfo = course_get_format($createdcourse['id'])->get_course();
     401  
     402              if ($createdcourse['shortname'] == $course2['shortname']) {
     403                  $this->assertEquals($courseinfo->fullname, $course2['fullname']);
     404                  $this->assertEquals($courseinfo->shortname, $course2['shortname']);
     405                  $this->assertEquals($courseinfo->category, $course2['categoryid']);
     406                  $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
     407                  $this->assertEquals($courseinfo->summary, $course2['summary']);
     408                  $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
     409                  $this->assertEquals($courseinfo->format, $course2['format']);
     410                  $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
     411                  $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
     412                  $this->assertEquals($courseinfo->startdate, $course2['startdate']);
     413                  $this->assertEquals($courseinfo->numsections, $course2['numsections']);
     414                  $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
     415                  $this->assertEquals($courseinfo->showreports, $course2['showreports']);
     416                  $this->assertEquals($courseinfo->visible, $course2['visible']);
     417                  $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
     418                  $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
     419                  $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
     420                  $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
     421                  $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
     422                  $this->assertEquals($courseinfo->lang, $course2['lang']);
     423                  $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
     424  
     425                  // We enabled completion at the beginning of the test.
     426                  $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
     427  
     428              } else if ($createdcourse['shortname'] == $course1['shortname']) {
     429                  $courseconfig = get_config('moodlecourse');
     430                  $this->assertEquals($courseinfo->fullname, $course1['fullname']);
     431                  $this->assertEquals($courseinfo->shortname, $course1['shortname']);
     432                  $this->assertEquals($courseinfo->category, $course1['categoryid']);
     433                  $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
     434                  $this->assertEquals($courseinfo->format, $courseconfig->format);
     435                  $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
     436                  $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
     437                  $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
     438                  $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
     439                  $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
     440                  $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
     441                  $this->assertEquals($courseinfo->defaultgroupingid, 0);
     442              } else if ($createdcourse['shortname'] == $course3['shortname']) {
     443                  $this->assertEquals($courseinfo->fullname, $course3['fullname']);
     444                  $this->assertEquals($courseinfo->shortname, $course3['shortname']);
     445                  $this->assertEquals($courseinfo->category, $course3['categoryid']);
     446                  $this->assertEquals($courseinfo->format, $course3['format']);
     447                  $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
     448                  $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
     449                  $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
     450              } else {
     451                  throw moodle_exception('Unexpected shortname');
     452              }
     453          }
     454  
     455          // Call without required capability
     456          $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
     457          $this->setExpectedException('required_capability_exception');
     458          $createdsubcats = core_course_external::create_courses($courses);
     459      }
     460  
     461      /**
     462       * Test delete_courses
     463       */
     464      public function test_delete_courses() {
     465          global $DB, $USER;
     466  
     467          $this->resetAfterTest(true);
     468  
     469          // Admin can delete a course.
     470          $this->setAdminUser();
     471          // Validate_context() will fail as the email is not set by $this->setAdminUser().
     472          $USER->email = 'emailtopass@example.com';
     473  
     474          $course1  = self::getDataGenerator()->create_course();
     475          $course2  = self::getDataGenerator()->create_course();
     476          $course3  = self::getDataGenerator()->create_course();
     477  
     478          // Delete courses.
     479          core_course_external::delete_courses(array($course1->id, $course2->id));
     480  
     481          // Check $course 1 and 2 are deleted.
     482          $notdeletedcount = $DB->count_records_select('course',
     483              'id IN ( ' . $course1->id . ',' . $course2->id . ')');
     484          $this->assertEquals(0, $notdeletedcount);
     485  
     486           // Fail when the user is not allow to access the course (enrolled) or is not admin.
     487          $this->setGuestUser();
     488          $this->setExpectedException('require_login_exception');
     489          $createdsubcats = core_course_external::delete_courses(array($course3->id));
     490      }
     491  
     492      /**
     493       * Test get_courses
     494       */
     495      public function test_get_courses () {
     496          global $DB;
     497  
     498          $this->resetAfterTest(true);
     499  
     500          $generatedcourses = array();
     501          $coursedata['idnumber'] = 'idnumbercourse1';
     502          $coursedata['fullname'] = 'Course 1 for PHPunit test';
     503          $coursedata['summary'] = 'Course 1 description';
     504          $coursedata['summaryformat'] = FORMAT_MOODLE;
     505          $course1  = self::getDataGenerator()->create_course($coursedata);
     506          $generatedcourses[$course1->id] = $course1;
     507          $course2  = self::getDataGenerator()->create_course();
     508          $generatedcourses[$course2->id] = $course2;
     509          $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
     510          $generatedcourses[$course3->id] = $course3;
     511  
     512          // Set the required capabilities by the external function.
     513          $context = context_system::instance();
     514          $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
     515          $this->assignUserCapability('moodle/course:update',
     516                  context_course::instance($course1->id)->id, $roleid);
     517          $this->assignUserCapability('moodle/course:update',
     518                  context_course::instance($course2->id)->id, $roleid);
     519          $this->assignUserCapability('moodle/course:update',
     520                  context_course::instance($course3->id)->id, $roleid);
     521  
     522          $courses = core_course_external::get_courses(array('ids' =>
     523              array($course1->id, $course2->id)));
     524  
     525          // We need to execute the return values cleaning process to simulate the web service server.
     526          $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
     527  
     528          // Check we retrieve the good total number of categories.
     529          $this->assertEquals(2, count($courses));
     530  
     531          foreach ($courses as $course) {
     532              $dbcourse = $generatedcourses[$course['id']];
     533              $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
     534              $this->assertEquals($course['fullname'], $dbcourse->fullname);
     535              // Summary was converted to the HTML format.
     536              $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
     537              $this->assertEquals($course['summaryformat'], FORMAT_HTML);
     538              $this->assertEquals($course['shortname'], $dbcourse->shortname);
     539              $this->assertEquals($course['categoryid'], $dbcourse->category);
     540              $this->assertEquals($course['format'], $dbcourse->format);
     541              $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
     542              $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
     543              $this->assertEquals($course['startdate'], $dbcourse->startdate);
     544              $this->assertEquals($course['numsections'], $dbcourse->numsections);
     545              $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
     546              $this->assertEquals($course['showreports'], $dbcourse->showreports);
     547              $this->assertEquals($course['visible'], $dbcourse->visible);
     548              $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
     549              $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
     550              $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
     551              $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
     552              $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
     553              $this->assertEquals($course['lang'], $dbcourse->lang);
     554              $this->assertEquals($course['forcetheme'], $dbcourse->theme);
     555              $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
     556              if ($dbcourse->format === 'topics') {
     557                  $this->assertEquals($course['courseformatoptions'], array(
     558                      array('name' => 'numsections', 'value' => $dbcourse->numsections),
     559                      array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
     560                      array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
     561                  ));
     562              }
     563          }
     564  
     565          // Get all courses in the DB
     566          $courses = core_course_external::get_courses(array());
     567  
     568          // We need to execute the return values cleaning process to simulate the web service server.
     569          $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
     570  
     571          $this->assertEquals($DB->count_records('course'), count($courses));
     572      }
     573  
     574      /**
     575       * Test get_course_contents
     576       */
     577      public function test_get_course_contents() {
     578          $this->resetAfterTest(true);
     579  
     580          $course  = self::getDataGenerator()->create_course();
     581          $forumdescription = 'This is the forum description';
     582          $forum = $this->getDataGenerator()->create_module('forum',
     583              array('course'=>$course->id, 'intro' => $forumdescription),
     584              array('showdescription' => true));
     585          $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
     586          $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
     587          $datacm = get_coursemodule_from_instance('page', $data->id);
     588          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
     589          $pagecm = get_coursemodule_from_instance('page', $page->id);
     590          $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
     591                  So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
     592          $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
     593              'intro' => $labeldescription));
     594          $labelcm = get_coursemodule_from_instance('label', $label->id);
     595          $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
     596              'name' => 'URL: % & $ ../'));
     597  
     598          // Set the required capabilities by the external function.
     599          $context = context_course::instance($course->id);
     600          $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
     601          $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
     602  
     603          $sections = core_course_external::get_course_contents($course->id, array());
     604  
     605          // We need to execute the return values cleaning process to simulate the web service server.
     606          $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
     607  
     608          // Check that forum and label descriptions are correctly returned.
     609          $firstsection = array_pop($sections);
     610          $modinfo = get_fast_modinfo($course);
     611          $testexecuted = 0;
     612          foreach($firstsection['modules'] as $module) {
     613              if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
     614                  $cm = $modinfo->cms[$forumcm->id];
     615                  $formattedtext = format_text($cm->content, FORMAT_HTML,
     616                      array('noclean' => true, 'para' => false, 'filter' => false));
     617                  $this->assertEquals($formattedtext, $module['description']);
     618                  $this->assertEquals($forumcm->instance, $module['instance']);
     619                  $testexecuted = $testexecuted + 1;
     620              } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
     621                  $cm = $modinfo->cms[$labelcm->id];
     622                  $formattedtext = format_text($cm->content, FORMAT_HTML,
     623                      array('noclean' => true, 'para' => false, 'filter' => false));
     624                  $this->assertEquals($formattedtext, $module['description']);
     625                  $this->assertEquals($labelcm->instance, $module['instance']);
     626                  $testexecuted = $testexecuted + 1;
     627              }
     628          }
     629          $this->assertEquals(2, $testexecuted);
     630  
     631          // Check that the only return section has the 5 created modules
     632          $this->assertEquals(5, count($firstsection['modules']));
     633      }
     634  
     635      /**
     636       * Test duplicate_course
     637       */
     638      public function test_duplicate_course() {
     639          $this->resetAfterTest(true);
     640  
     641          // Create one course with three modules.
     642          $course  = self::getDataGenerator()->create_course();
     643          $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
     644          $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
     645          $forumcontext = context_module::instance($forum->cmid);
     646          $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
     647          $datacontext = context_module::instance($data->cmid);
     648          $datacm = get_coursemodule_from_instance('page', $data->id);
     649          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
     650          $pagecontext = context_module::instance($page->cmid);
     651          $pagecm = get_coursemodule_from_instance('page', $page->id);
     652  
     653          // Set the required capabilities by the external function.
     654          $coursecontext = context_course::instance($course->id);
     655          $categorycontext = context_coursecat::instance($course->category);
     656          $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
     657          $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
     658          $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
     659          $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
     660          $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
     661          // Optional capabilities to copy user data.
     662          $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
     663          $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
     664  
     665          $newcourse['fullname'] = 'Course duplicate';
     666          $newcourse['shortname'] = 'courseduplicate';
     667          $newcourse['categoryid'] = $course->category;
     668          $newcourse['visible'] = true;
     669          $newcourse['options'][] = array('name' => 'users', 'value' => true);
     670  
     671          $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
     672                  $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
     673  
     674          // We need to execute the return values cleaning process to simulate the web service server.
     675          $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
     676  
     677          // Check that the course has been duplicated.
     678          $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
     679      }
     680  
     681      /**
     682       * Test update_courses
     683       */
     684      public function test_update_courses() {
     685          global $DB, $CFG, $USER, $COURSE;
     686  
     687          // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
     688          // trick because we are both updating and getting (for testing) course information
     689          // in the same request and core_course_external::update_courses()
     690          // is overwriting $COURSE all over the time with OLD values, so later
     691          // use of get_course() fetches those OLD values instead of the updated ones.
     692          // See MDL-39723 for more info.
     693          $origcourse = clone($COURSE);
     694  
     695          $this->resetAfterTest(true);
     696  
     697          // Set the required capabilities by the external function.
     698          $contextid = context_system::instance()->id;
     699          $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
     700          $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
     701          $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
     702          $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
     703          $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
     704          $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
     705          $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
     706          $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
     707  
     708          // Create category and course.
     709          $category1  = self::getDataGenerator()->create_category();
     710          $category2  = self::getDataGenerator()->create_category();
     711          $originalcourse1 = self::getDataGenerator()->create_course();
     712          self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
     713          $originalcourse2 = self::getDataGenerator()->create_course();
     714          self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
     715  
     716          // Course values to be updated.
     717          $course1['id'] = $originalcourse1->id;
     718          $course1['fullname'] = 'Updated test course 1';
     719          $course1['shortname'] = 'Udestedtestcourse1';
     720          $course1['categoryid'] = $category1->id;
     721          $course2['id'] = $originalcourse2->id;
     722          $course2['fullname'] = 'Updated test course 2';
     723          $course2['shortname'] = 'Updestedtestcourse2';
     724          $course2['categoryid'] = $category2->id;
     725          $course2['idnumber'] = 'Updatedidnumber2';
     726          $course2['summary'] = 'Updaated description for course 2';
     727          $course2['summaryformat'] = FORMAT_HTML;
     728          $course2['format'] = 'topics';
     729          $course2['showgrades'] = 1;
     730          $course2['newsitems'] = 3;
     731          $course2['startdate'] = 1420092000; // 01/01/2015.
     732          $course2['numsections'] = 4;
     733          $course2['maxbytes'] = 100000;
     734          $course2['showreports'] = 1;
     735          $course2['visible'] = 0;
     736          $course2['hiddensections'] = 0;
     737          $course2['groupmode'] = 0;
     738          $course2['groupmodeforce'] = 0;
     739          $course2['defaultgroupingid'] = 0;
     740          $course2['enablecompletion'] = 1;
     741          $course2['lang'] = 'en';
     742          $course2['forcetheme'] = 'base';
     743          $courses = array($course1, $course2);
     744  
     745          $updatedcoursewarnings = core_course_external::update_courses($courses);
     746          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     747                                                                      $updatedcoursewarnings);
     748          $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
     749  
     750          // Check that right number of courses were created.
     751          $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
     752  
     753          // Check that the courses were correctly created.
     754          foreach ($courses as $course) {
     755              $courseinfo = course_get_format($course['id'])->get_course();
     756              if ($course['id'] == $course2['id']) {
     757                  $this->assertEquals($course2['fullname'], $courseinfo->fullname);
     758                  $this->assertEquals($course2['shortname'], $courseinfo->shortname);
     759                  $this->assertEquals($course2['categoryid'], $courseinfo->category);
     760                  $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
     761                  $this->assertEquals($course2['summary'], $courseinfo->summary);
     762                  $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
     763                  $this->assertEquals($course2['format'], $courseinfo->format);
     764                  $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
     765                  $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
     766                  $this->assertEquals($course2['startdate'], $courseinfo->startdate);
     767                  $this->assertEquals($course2['numsections'], $courseinfo->numsections);
     768                  $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
     769                  $this->assertEquals($course2['showreports'], $courseinfo->showreports);
     770                  $this->assertEquals($course2['visible'], $courseinfo->visible);
     771                  $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
     772                  $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
     773                  $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
     774                  $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
     775                  $this->assertEquals($course2['lang'], $courseinfo->lang);
     776  
     777                  if (!empty($CFG->allowcoursethemes)) {
     778                      $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
     779                  }
     780  
     781                  if (completion_info::is_enabled_for_site()) {
     782                      $this->assertEquals($course2['enabledcompletion'], $courseinfo->enablecompletion);
     783                  }
     784              } else if ($course['id'] == $course1['id']) {
     785                  $this->assertEquals($course1['fullname'], $courseinfo->fullname);
     786                  $this->assertEquals($course1['shortname'], $courseinfo->shortname);
     787                  $this->assertEquals($course1['categoryid'], $courseinfo->category);
     788                  $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
     789                  $this->assertEquals('topics', $courseinfo->format);
     790                  $this->assertEquals(5, $courseinfo->numsections);
     791                  $this->assertEquals(0, $courseinfo->newsitems);
     792                  $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
     793              } else {
     794                  throw moodle_exception('Unexpected shortname');
     795              }
     796          }
     797  
     798          $courses = array($course1);
     799          // Try update course without update capability.
     800          $user = self::getDataGenerator()->create_user();
     801          $this->setUser($user);
     802          $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
     803          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     804          $updatedcoursewarnings = core_course_external::update_courses($courses);
     805          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     806                                                                      $updatedcoursewarnings);
     807          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     808  
     809          // Try update course category without capability.
     810          $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
     811          $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
     812          $user = self::getDataGenerator()->create_user();
     813          $this->setUser($user);
     814          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     815          $course1['categoryid'] = $category2->id;
     816          $courses = array($course1);
     817          $updatedcoursewarnings = core_course_external::update_courses($courses);
     818          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     819                                                                      $updatedcoursewarnings);
     820          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     821  
     822          // Try update course fullname without capability.
     823          $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
     824          $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
     825          $user = self::getDataGenerator()->create_user();
     826          $this->setUser($user);
     827          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     828          $updatedcoursewarnings = core_course_external::update_courses($courses);
     829          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     830                                                                      $updatedcoursewarnings);
     831          $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
     832          $course1['fullname'] = 'Testing fullname without permission';
     833          $courses = array($course1);
     834          $updatedcoursewarnings = core_course_external::update_courses($courses);
     835          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     836                                                                      $updatedcoursewarnings);
     837          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     838  
     839          // Try update course shortname without capability.
     840          $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
     841          $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
     842          $user = self::getDataGenerator()->create_user();
     843          $this->setUser($user);
     844          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     845          $updatedcoursewarnings = core_course_external::update_courses($courses);
     846          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     847                                                                      $updatedcoursewarnings);
     848          $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
     849          $course1['shortname'] = 'Testing shortname without permission';
     850          $courses = array($course1);
     851          $updatedcoursewarnings = core_course_external::update_courses($courses);
     852          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     853                                                                      $updatedcoursewarnings);
     854          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     855  
     856          // Try update course idnumber without capability.
     857          $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
     858          $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
     859          $user = self::getDataGenerator()->create_user();
     860          $this->setUser($user);
     861          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     862          $updatedcoursewarnings = core_course_external::update_courses($courses);
     863          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     864                                                                      $updatedcoursewarnings);
     865          $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
     866          $course1['idnumber'] = 'NEWIDNUMBER';
     867          $courses = array($course1);
     868          $updatedcoursewarnings = core_course_external::update_courses($courses);
     869          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     870                                                                      $updatedcoursewarnings);
     871          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     872  
     873          // Try update course summary without capability.
     874          $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
     875          $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
     876          $user = self::getDataGenerator()->create_user();
     877          $this->setUser($user);
     878          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     879          $updatedcoursewarnings = core_course_external::update_courses($courses);
     880          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     881                                                                      $updatedcoursewarnings);
     882          $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
     883          $course1['summary'] = 'New summary';
     884          $courses = array($course1);
     885          $updatedcoursewarnings = core_course_external::update_courses($courses);
     886          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     887                                                                      $updatedcoursewarnings);
     888          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     889  
     890          // Try update course with invalid summary format.
     891          $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
     892          $user = self::getDataGenerator()->create_user();
     893          $this->setUser($user);
     894          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     895          $updatedcoursewarnings = core_course_external::update_courses($courses);
     896          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     897                                                                      $updatedcoursewarnings);
     898          $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
     899          $course1['summaryformat'] = 10;
     900          $courses = array($course1);
     901          $updatedcoursewarnings = core_course_external::update_courses($courses);
     902          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     903                                                                      $updatedcoursewarnings);
     904          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     905  
     906          // Try update course visibility without capability.
     907          $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
     908          $user = self::getDataGenerator()->create_user();
     909          $this->setUser($user);
     910          self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
     911          $course1['summaryformat'] = FORMAT_MOODLE;
     912          $courses = array($course1);
     913          $updatedcoursewarnings = core_course_external::update_courses($courses);
     914          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     915                                                                      $updatedcoursewarnings);
     916          $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
     917          $course1['visible'] = 0;
     918          $courses = array($course1);
     919          $updatedcoursewarnings = core_course_external::update_courses($courses);
     920          $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
     921                                                                      $updatedcoursewarnings);
     922          $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
     923      }
     924  
     925      /**
     926       * Test delete course_module.
     927       */
     928      public function test_delete_modules() {
     929          global $DB;
     930  
     931          // Ensure we reset the data after this test.
     932          $this->resetAfterTest(true);
     933  
     934          // Create a user.
     935          $user = self::getDataGenerator()->create_user();
     936  
     937          // Set the tests to run as the user.
     938          self::setUser($user);
     939  
     940          // Create a course to add the modules.
     941          $course = self::getDataGenerator()->create_course();
     942  
     943          // Create two test modules.
     944          $record = new stdClass();
     945          $record->course = $course->id;
     946          $module1 = self::getDataGenerator()->create_module('forum', $record);
     947          $module2 = self::getDataGenerator()->create_module('assign', $record);
     948  
     949          // Check the forum was correctly created.
     950          $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
     951  
     952          // Check the assignment was correctly created.
     953          $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
     954  
     955          // Check data exists in the course modules table.
     956          $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
     957                  array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
     958  
     959          // Enrol the user in the course.
     960          $enrol = enrol_get_plugin('manual');
     961          $enrolinstances = enrol_get_instances($course->id, true);
     962          foreach ($enrolinstances as $courseenrolinstance) {
     963              if ($courseenrolinstance->enrol == "manual") {
     964                  $instance = $courseenrolinstance;
     965                  break;
     966              }
     967          }
     968          $enrol->enrol_user($instance, $user->id);
     969  
     970          // Assign capabilities to delete module 1.
     971          $modcontext = context_module::instance($module1->cmid);
     972          $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
     973  
     974          // Assign capabilities to delete module 2.
     975          $modcontext = context_module::instance($module2->cmid);
     976          $newrole = create_role('Role 2', 'role2', 'Role 2 description');
     977          $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
     978  
     979          // Deleting these module instances.
     980          core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
     981  
     982          // Check the forum was deleted.
     983          $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
     984  
     985          // Check the assignment was deleted.
     986          $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
     987  
     988          // Check we retrieve no data in the course modules table.
     989          $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
     990                  array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
     991  
     992          // Call with non-existent course module id and ensure exception thrown.
     993          try {
     994              core_course_external::delete_modules(array('1337'));
     995              $this->fail('Exception expected due to missing course module.');
     996          } catch (dml_missing_record_exception $e) {
     997              $this->assertEquals('invalidcoursemodule', $e->errorcode);
     998          }
     999  
    1000          // Create two modules.
    1001          $module1 = self::getDataGenerator()->create_module('forum', $record);
    1002          $module2 = self::getDataGenerator()->create_module('assign', $record);
    1003  
    1004          // Since these modules were recreated the user will not have capabilities
    1005          // to delete them, ensure exception is thrown if they try.
    1006          try {
    1007              core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
    1008              $this->fail('Exception expected due to missing capability.');
    1009          } catch (moodle_exception $e) {
    1010              $this->assertEquals('nopermissions', $e->errorcode);
    1011          }
    1012  
    1013          // Unenrol user from the course.
    1014          $enrol->unenrol_user($instance, $user->id);
    1015  
    1016          // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
    1017          try {
    1018              core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
    1019              $this->fail('Exception expected due to being unenrolled from the course.');
    1020          } catch (moodle_exception $e) {
    1021              $this->assertEquals('requireloginerror', $e->errorcode);
    1022          }
    1023      }
    1024  
    1025      /**
    1026       * Test import_course into an empty course
    1027       */
    1028      public function test_import_course_empty() {
    1029          global $USER;
    1030  
    1031          $this->resetAfterTest(true);
    1032  
    1033          $course1  = self::getDataGenerator()->create_course();
    1034          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
    1035          $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
    1036  
    1037          $course2  = self::getDataGenerator()->create_course();
    1038  
    1039          $course1cms = get_fast_modinfo($course1->id)->get_cms();
    1040          $course2cms = get_fast_modinfo($course2->id)->get_cms();
    1041  
    1042          // Verify the state of the courses before we do the import.
    1043          $this->assertCount(2, $course1cms);
    1044          $this->assertEmpty($course2cms);
    1045  
    1046          // Setup the user to run the operation (ugly hack because validate_context() will
    1047          // fail as the email is not set by $this->setAdminUser()).
    1048          $this->setAdminUser();
    1049          $USER->email = 'emailtopass@example.com';
    1050  
    1051          // Import from course1 to course2.
    1052          core_course_external::import_course($course1->id, $course2->id, 0);
    1053  
    1054          // Verify that now we have two modules in both courses.
    1055          $course1cms = get_fast_modinfo($course1->id)->get_cms();
    1056          $course2cms = get_fast_modinfo($course2->id)->get_cms();
    1057          $this->assertCount(2, $course1cms);
    1058          $this->assertCount(2, $course2cms);
    1059  
    1060          // Verify that the names transfered across correctly.
    1061          foreach ($course2cms as $cm) {
    1062              if ($cm->modname === 'page') {
    1063                  $this->assertEquals($cm->name, $page->name);
    1064              } else if ($cm->modname === 'forum') {
    1065                  $this->assertEquals($cm->name, $forum->name);
    1066              } else {
    1067                  $this->fail('Unknown CM found.');
    1068              }
    1069          }
    1070      }
    1071  
    1072      /**
    1073       * Test import_course into an filled course
    1074       */
    1075      public function test_import_course_filled() {
    1076          global $USER;
    1077  
    1078          $this->resetAfterTest(true);
    1079  
    1080          // Add forum and page to course1.
    1081          $course1  = self::getDataGenerator()->create_course();
    1082          $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
    1083          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
    1084  
    1085          // Add quiz to course 2.
    1086          $course2  = self::getDataGenerator()->create_course();
    1087          $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
    1088  
    1089          $course1cms = get_fast_modinfo($course1->id)->get_cms();
    1090          $course2cms = get_fast_modinfo($course2->id)->get_cms();
    1091  
    1092          // Verify the state of the courses before we do the import.
    1093          $this->assertCount(2, $course1cms);
    1094          $this->assertCount(1, $course2cms);
    1095  
    1096          // Setup the user to run the operation (ugly hack because validate_context() will
    1097          // fail as the email is not set by $this->setAdminUser()).
    1098          $this->setAdminUser();
    1099          $USER->email = 'emailtopass@example.com';
    1100  
    1101          // Import from course1 to course2 without deleting content.
    1102          core_course_external::import_course($course1->id, $course2->id, 0);
    1103  
    1104          $course2cms = get_fast_modinfo($course2->id)->get_cms();
    1105  
    1106          // Verify that now we have three modules in course2.
    1107          $this->assertCount(3, $course2cms);
    1108  
    1109          // Verify that the names transfered across correctly.
    1110          foreach ($course2cms as $cm) {
    1111              if ($cm->modname === 'page') {
    1112                  $this->assertEquals($cm->name, $page->name);
    1113              } else if ($cm->modname === 'forum') {
    1114                  $this->assertEquals($cm->name, $forum->name);
    1115              } else if ($cm->modname === 'quiz') {
    1116                  $this->assertEquals($cm->name, $quiz->name);
    1117              } else {
    1118                  $this->fail('Unknown CM found.');
    1119              }
    1120          }
    1121      }
    1122  
    1123      /**
    1124       * Test import_course with only blocks set to backup
    1125       */
    1126      public function test_import_course_blocksonly() {
    1127          global $USER, $DB;
    1128  
    1129          $this->resetAfterTest(true);
    1130  
    1131          // Add forum and page to course1.
    1132          $course1  = self::getDataGenerator()->create_course();
    1133          $course1ctx = context_course::instance($course1->id);
    1134          $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
    1135          $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
    1136  
    1137          $course2  = self::getDataGenerator()->create_course();
    1138          $course2ctx = context_course::instance($course2->id);
    1139          $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
    1140          $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
    1141  
    1142          // Setup the user to run the operation (ugly hack because validate_context() will
    1143          // fail as the email is not set by $this->setAdminUser()).
    1144          $this->setAdminUser();
    1145          $USER->email = 'emailtopass@example.com';
    1146  
    1147          // Import from course1 to course2 without deleting content, but excluding
    1148          // activities.
    1149          $options = array(
    1150              array('name' => 'activities', 'value' => 0),
    1151              array('name' => 'blocks', 'value' => 1),
    1152              array('name' => 'filters', 'value' => 0),
    1153          );
    1154  
    1155          core_course_external::import_course($course1->id, $course2->id, 0, $options);
    1156  
    1157          $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
    1158          $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
    1159          // Check that course modules haven't changed, but that blocks have.
    1160          $this->assertEquals($initialcmcount, $newcmcount);
    1161          $this->assertEquals(($initialblockcount + 1), $newblockcount);
    1162      }
    1163  
    1164      /**
    1165       * Test import_course into an filled course, deleting content.
    1166       */
    1167      public function test_import_course_deletecontent() {
    1168          global $USER;
    1169          $this->resetAfterTest(true);
    1170  
    1171          // Add forum and page to course1.
    1172          $course1  = self::getDataGenerator()->create_course();
    1173          $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
    1174          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
    1175  
    1176          // Add quiz to course 2.
    1177          $course2  = self::getDataGenerator()->create_course();
    1178          $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
    1179  
    1180          $course1cms = get_fast_modinfo($course1->id)->get_cms();
    1181          $course2cms = get_fast_modinfo($course2->id)->get_cms();
    1182  
    1183          // Verify the state of the courses before we do the import.
    1184          $this->assertCount(2, $course1cms);
    1185          $this->assertCount(1, $course2cms);
    1186  
    1187          // Setup the user to run the operation (ugly hack because validate_context() will
    1188          // fail as the email is not set by $this->setAdminUser()).
    1189          $this->setAdminUser();
    1190          $USER->email = 'emailtopass@example.com';
    1191  
    1192          // Import from course1 to course2,  deleting content.
    1193          core_course_external::import_course($course1->id, $course2->id, 1);
    1194  
    1195          $course2cms = get_fast_modinfo($course2->id)->get_cms();
    1196  
    1197          // Verify that now we have two modules in course2.
    1198          $this->assertCount(2, $course2cms);
    1199  
    1200          // Verify that the course only contains the imported modules.
    1201          foreach ($course2cms as $cm) {
    1202              if ($cm->modname === 'page') {
    1203                  $this->assertEquals($cm->name, $page->name);
    1204              } else if ($cm->modname === 'forum') {
    1205                  $this->assertEquals($cm->name, $forum->name);
    1206              } else {
    1207                  $this->fail('Unknown CM found: '.$cm->name);
    1208              }
    1209          }
    1210      }
    1211  
    1212      /**
    1213       * Ensure import_course handles incorrect deletecontent option correctly.
    1214       */
    1215      public function test_import_course_invalid_deletecontent_option() {
    1216          $this->resetAfterTest(true);
    1217  
    1218          $course1  = self::getDataGenerator()->create_course();
    1219          $course2  = self::getDataGenerator()->create_course();
    1220  
    1221          $this->setExpectedException('moodle_exception', get_string('invalidextparam', 'webservice', -1));
    1222          // Import from course1 to course2, with invalid option
    1223          core_course_external::import_course($course1->id, $course2->id, -1);;
    1224      }
    1225  
    1226  }
    

    Search This Site: