[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/imscp/tests/ -> lib_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for (some of) mod/imscp/lib.php.
  19   *
  20   * @package    mod_imscp
  21   * @category   test
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/mod/imscp/lib.php');
  31  
  32  /**
  33   * Unit tests for (some of) mod/imscp/lib.php.
  34   *
  35   * @package    mod_imscp
  36   * @category   test
  37   * @copyright  2015 Juan Leyva <juan@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class mod_imscp_lib_testcase extends advanced_testcase {
  41  
  42      public function test_export_contents() {
  43          global $DB, $USER;
  44  
  45          $this->resetAfterTest(true);
  46  
  47          $user = $this->getDataGenerator()->create_user();
  48          $course = $this->getDataGenerator()->create_course();
  49          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  50          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  51  
  52          $this->setAdminUser();
  53          $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
  54          $cm = get_coursemodule_from_id('imscp', $imscp->cmid);
  55  
  56          $this->setUser($user);
  57          $contents = imscp_export_contents($cm, '');
  58  
  59          // The test package contains 47 files.
  60          $this->assertCount(47, $contents);
  61          // The structure is present.
  62          $this->assertEquals('structure', $contents[0]['filename']);
  63          // The structure is returned and it maches the expected one.
  64          $this->assertEquals(json_encode(unserialize($imscp->structure)), $contents[0]['content']);
  65  
  66      }
  67  
  68      /**
  69       * Test imscp_view
  70       * @return void
  71       */
  72      public function test_imscp_view() {
  73          global $CFG;
  74  
  75          $CFG->enablecompletion = 1;
  76          $this->resetAfterTest();
  77  
  78          $this->setAdminUser();
  79          // Setup test data.
  80          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  81          $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
  82                                                              array('completion' => 2, 'completionview' => 1));
  83          $context = context_module::instance($imscp->cmid);
  84          $cm = get_coursemodule_from_instance('imscp', $imscp->id);
  85  
  86          // Trigger and capture the event.
  87          $sink = $this->redirectEvents();
  88  
  89          imscp_view($imscp, $course, $cm, $context);
  90  
  91          $events = $sink->get_events();
  92          // 2 additional events thanks to completion.
  93          $this->assertCount(3, $events);
  94          $event = array_shift($events);
  95  
  96          // Checking that the event contains the expected values.
  97          $this->assertInstanceOf('\mod_imscp\event\course_module_viewed', $event);
  98          $this->assertEquals($context, $event->get_context());
  99          $moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id));
 100          $this->assertEquals($moodleurl, $event->get_url());
 101          $this->assertEventContextNotUsed($event);
 102          $this->assertNotEmpty($event->get_name());
 103  
 104          // Check completion status.
 105          $completion = new completion_info($course);
 106          $completiondata = $completion->get_data($cm);
 107          $this->assertEquals(1, $completiondata->completionstate);
 108  
 109      }
 110  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1