[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/glossary/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   * Glossary lib tests.
  19   *
  20   * @package    mod_glossary
  21   * @copyright  2015 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/mod/glossary/lib.php');
  29  
  30  /**
  31   * Glossary lib testcase.
  32   *
  33   * @package    mod_glossary
  34   * @copyright  2015 Frédéric Massart - FMCorz.net
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_glossary_lib_testcase extends advanced_testcase {
  38  
  39      public function test_glossary_view() {
  40          global $CFG;
  41          $origcompletion = $CFG->enablecompletion;
  42          $CFG->enablecompletion = true;
  43          $this->resetAfterTest(true);
  44  
  45          // Generate all the things.
  46          $c1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  47          $g1 = $this->getDataGenerator()->create_module('glossary', array(
  48              'course' => $c1->id,
  49              'completion' => COMPLETION_TRACKING_AUTOMATIC,
  50              'completionview' => 1
  51          ));
  52          $g2 = $this->getDataGenerator()->create_module('glossary', array(
  53              'course' => $c1->id,
  54              'completion' => COMPLETION_TRACKING_AUTOMATIC,
  55              'completionview' => 1
  56          ));
  57          $u1 = $this->getDataGenerator()->create_user();
  58          $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
  59          $modinfo = course_modinfo::instance($c1->id);
  60          $cm1 = $modinfo->get_cm($g1->cmid);
  61          $cm2 = $modinfo->get_cm($g2->cmid);
  62          $ctx1 = $cm1->context;
  63          $completion = new completion_info($c1);
  64  
  65          $this->setUser($u1);
  66  
  67          // Confirm what we've set up.
  68          $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
  69          $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
  70          $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
  71          $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
  72  
  73          // Simulate the view call.
  74          $sink = $this->redirectEvents();
  75          glossary_view($g1, $c1, $cm1, $ctx1, 'letter');
  76          $events = $sink->get_events();
  77  
  78          // Assertions.
  79          $this->assertCount(3, $events);
  80          $this->assertEquals('\core\event\course_module_completion_updated', $events[0]->eventname);
  81          $this->assertEquals('\core\event\course_module_completion_updated', $events[1]->eventname);
  82          $this->assertEquals('\mod_glossary\event\course_module_viewed', $events[2]->eventname);
  83          $this->assertEquals($g1->id, $events[2]->objectid);
  84          $this->assertEquals('letter', $events[2]->other['mode']);
  85          $this->assertEquals(COMPLETION_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
  86          $this->assertEquals(COMPLETION_COMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
  87          $this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
  88          $this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
  89  
  90          // Tear down.
  91          $sink->close();
  92          $CFG->enablecompletion = $origcompletion;
  93      }
  94  
  95      public function test_glossary_entry_view() {
  96          $this->resetAfterTest(true);
  97  
  98          // Generate all the things.
  99          $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
 100          $c1 = $this->getDataGenerator()->create_course();
 101          $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
 102          $e1 = $gg->create_content($g1);
 103          $u1 = $this->getDataGenerator()->create_user();
 104          $ctx = context_module::instance($g1->cmid);
 105          $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
 106  
 107          // Assertions.
 108          $sink = $this->redirectEvents();
 109          glossary_entry_view($e1, $ctx);
 110          $events = $sink->get_events();
 111          $this->assertCount(1, $events);
 112          $this->assertEquals('\mod_glossary\event\entry_viewed', $events[0]->eventname);
 113          $this->assertEquals($e1->id, $events[0]->objectid);
 114          $sink->close();
 115      }
 116  
 117  }


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