[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/book/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/book/lib.php.
  19   *
  20   * @package    mod_book
  21   * @category   phpunit
  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/book/lib.php');
  31  
  32  /**
  33   * Unit tests for (some of) mod/book/lib.php.
  34   *
  35   * @package    mod_book
  36   * @category   phpunit
  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_book_lib_testcase extends advanced_testcase {
  41  
  42      public function test_export_contents() {
  43          global $DB;
  44  
  45          $this->resetAfterTest(true);
  46  
  47          $user = $this->getDataGenerator()->create_user();
  48          $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
  49          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  50          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  51  
  52          // Test book with 3 chapters.
  53          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  54          $cm = get_coursemodule_from_id('book', $book->cmid);
  55  
  56          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
  57          $chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 1));
  58          $chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 2));
  59          $subchapter = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 3, "subchapter" => 1));
  60          $chapter3 = $bookgenerator->create_chapter(array('bookid' => $book->id, "pagenum" => 4, "hidden" => 1));
  61  
  62          $this->setUser($user);
  63  
  64          $contents = book_export_contents($cm, '');
  65          // The hidden chapter must not be included, and additional page with the structure must be included.
  66          $this->assertCount(4, $contents);
  67  
  68          $this->assertEquals('structure', $contents[0]['filename']);
  69          $this->assertEquals('index.html', $contents[1]['filename']);
  70          $this->assertEquals('Chapter 1', $contents[1]['content']);
  71          $this->assertEquals('index.html', $contents[2]['filename']);
  72          $this->assertEquals('Chapter 2', $contents[2]['content']);
  73          $this->assertEquals('index.html', $contents[3]['filename']);
  74          $this->assertEquals('Chapter 3', $contents[3]['content']);
  75  
  76          // Test empty book.
  77          $emptybook = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  78          $cm = get_coursemodule_from_id('book', $emptybook->cmid);
  79          $contents = book_export_contents($cm, '');
  80  
  81          $this->assertCount(1, $contents);
  82          $this->assertEquals('structure', $contents[0]['filename']);
  83          $this->assertEquals(json_encode(array()), $contents[0]['content']);
  84  
  85      }
  86  
  87      /**
  88       * Test book_view
  89       * @return void
  90       */
  91      public function test_book_view() {
  92          global $CFG, $DB;
  93  
  94          $CFG->enablecompletion = 1;
  95          $this->resetAfterTest();
  96  
  97          $this->setAdminUser();
  98          // Setup test data.
  99          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 100          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id),
 101                                                              array('completion' => 2, 'completionview' => 1));
 102          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
 103          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
 104  
 105          $context = context_module::instance($book->cmid);
 106          $cm = get_coursemodule_from_instance('book', $book->id);
 107  
 108          // Trigger and capture the event.
 109          $sink = $this->redirectEvents();
 110  
 111          // Check just opening the book.
 112          book_view($book, 0, false, $course, $cm, $context);
 113  
 114          $events = $sink->get_events();
 115          $this->assertCount(1, $events);
 116          $event = array_shift($events);
 117  
 118          // Checking that the event contains the expected values.
 119          $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
 120          $this->assertEquals($context, $event->get_context());
 121          $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
 122          $this->assertEquals($moodleurl, $event->get_url());
 123          $this->assertEventContextNotUsed($event);
 124          $this->assertNotEmpty($event->get_name());
 125  
 126          // Check viewing one book chapter (the only one so it will be the first and last).
 127          book_view($book, $chapter, true, $course, $cm, $context);
 128  
 129          $events = $sink->get_events();
 130          // We expect a total of 4 events. One for module viewed, one for chapter viewed and two belonging to completion.
 131          $this->assertCount(4, $events);
 132  
 133          // Check completion status.
 134          $completion = new completion_info($course);
 135          $completiondata = $completion->get_data($cm);
 136          $this->assertEquals(1, $completiondata->completionstate);
 137  
 138      }
 139  }


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