Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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  namespace mod_book;
  18  
  19  use externallib_advanced_testcase;
  20  use mod_book_external;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  
  26  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  27  
  28  /**
  29   * External mod_book functions unit tests
  30   *
  31   * @package    mod_book
  32   * @category   external
  33   * @copyright  2015 Juan Leyva <juan@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since      Moodle 3.0
  36   */
  37  class externallib_test extends externallib_advanced_testcase {
  38  
  39      /**
  40       * Test view_book
  41       */
  42      public function test_view_book() {
  43          global $DB;
  44  
  45          $this->resetAfterTest(true);
  46  
  47          $this->setAdminUser();
  48          // Setup test data.
  49          $course = $this->getDataGenerator()->create_course();
  50          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  51          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
  52          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
  53          $chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1));
  54  
  55          $context = \context_module::instance($book->cmid);
  56          $cm = get_coursemodule_from_instance('book', $book->id);
  57  
  58          // Test invalid instance id.
  59          try {
  60              mod_book_external::view_book(0);
  61              $this->fail('Exception expected due to invalid mod_book instance id.');
  62          } catch (\moodle_exception $e) {
  63              $this->assertEquals('invalidrecord', $e->errorcode);
  64          }
  65  
  66          // Test not-enrolled user.
  67          $user = self::getDataGenerator()->create_user();
  68          $this->setUser($user);
  69          try {
  70              mod_book_external::view_book($book->id, 0);
  71              $this->fail('Exception expected due to not enrolled user.');
  72          } catch (\moodle_exception $e) {
  73              $this->assertEquals('requireloginerror', $e->errorcode);
  74          }
  75  
  76          // Test user with full capabilities.
  77          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  78          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  79  
  80          // Trigger and capture the event.
  81          $sink = $this->redirectEvents();
  82  
  83          $result = mod_book_external::view_book($book->id, 0);
  84          $result = \external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
  85  
  86          $events = $sink->get_events();
  87          $this->assertCount(2, $events);
  88          $event = array_shift($events);
  89  
  90          // Checking that the event contains the expected values.
  91          $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
  92          $this->assertEquals($context, $event->get_context());
  93          $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id));
  94          $this->assertEquals($moodleurl, $event->get_url());
  95          $this->assertEventContextNotUsed($event);
  96          $this->assertNotEmpty($event->get_name());
  97  
  98          $event = array_shift($events);
  99          $this->assertInstanceOf('\mod_book\event\chapter_viewed', $event);
 100          $this->assertEquals($chapter->id, $event->objectid);
 101  
 102          $result = mod_book_external::view_book($book->id, $chapter->id);
 103          $result = \external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result);
 104  
 105          $events = $sink->get_events();
 106          // We expect a total of 3 events.
 107          $this->assertCount(3, $events);
 108  
 109          // Try to view a hidden chapter.
 110          try {
 111              mod_book_external::view_book($book->id, $chapterhidden->id);
 112              $this->fail('Exception expected due to missing capability.');
 113          } catch (\moodle_exception $e) {
 114              $this->assertEquals('errorchapter', $e->errorcode);
 115          }
 116  
 117          // Test user with no capabilities.
 118          // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
 119          assign_capability('mod/book:read', CAP_PROHIBIT, $studentrole->id, $context->id);
 120          accesslib_clear_all_caches_for_unit_testing();
 121  
 122          try {
 123              mod_book_external::view_book($book->id, 0);
 124              $this->fail('Exception expected due to missing capability.');
 125          } catch (\moodle_exception $e) {
 126              $this->assertEquals('nopermissions', $e->errorcode);
 127          }
 128  
 129      }
 130  
 131      /**
 132       * Test get_books_by_courses
 133       */
 134      public function test_get_books_by_courses() {
 135          global $DB, $USER;
 136          $this->resetAfterTest(true);
 137          $this->setAdminUser();
 138          $course1 = self::getDataGenerator()->create_course();
 139          $bookoptions1 = array(
 140                                'course' => $course1->id,
 141                                'name' => 'First Book'
 142                               );
 143          $book1 = self::getDataGenerator()->create_module('book', $bookoptions1);
 144          $course2 = self::getDataGenerator()->create_course();
 145          $bookoptions2 = array(
 146                                'course' => $course2->id,
 147                                'name' => 'Second Book'
 148                               );
 149          $book2 = self::getDataGenerator()->create_module('book', $bookoptions2);
 150          $student1 = $this->getDataGenerator()->create_user();
 151          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 152  
 153          // Enroll Student1 in Course1.
 154          self::getDataGenerator()->enrol_user($student1->id,  $course1->id, $studentrole->id);
 155          $this->setUser($student1);
 156  
 157          $books = mod_book_external::get_books_by_courses();
 158          // We need to execute the return values cleaning process to simulate the web service server.
 159          $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
 160          $this->assertCount(1, $books['books']);
 161          $this->assertEquals('First Book', $books['books'][0]['name']);
 162          // We see 10 fields.
 163          $this->assertCount(10, $books['books'][0]);
 164  
 165          // As Student you cannot see some book properties like 'section'.
 166          $this->assertFalse(isset($books['books'][0]['section']));
 167  
 168          // Student1 is not enrolled in course2. The webservice will return a warning!
 169          $books = mod_book_external::get_books_by_courses(array($course2->id));
 170          // We need to execute the return values cleaning process to simulate the web service server.
 171          $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
 172          $this->assertCount(0, $books['books']);
 173          $this->assertEquals(1, $books['warnings'][0]['warningcode']);
 174  
 175          // Now as admin.
 176          $this->setAdminUser();
 177          // As Admin we can see this book.
 178          $books = mod_book_external::get_books_by_courses(array($course2->id));
 179          // We need to execute the return values cleaning process to simulate the web service server.
 180          $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
 181  
 182          $this->assertCount(1, $books['books']);
 183          $this->assertEquals('Second Book', $books['books'][0]['name']);
 184          // We see 17 fields.
 185          $this->assertCount(17, $books['books'][0]);
 186          // As an Admin you can see some book properties like 'section'.
 187          $this->assertEquals(0, $books['books'][0]['section']);
 188  
 189          // Enrol student in the second course.
 190          self::getDataGenerator()->enrol_user($student1->id,  $course2->id, $studentrole->id);
 191          $this->setUser($student1);
 192          $books = mod_book_external::get_books_by_courses();
 193          $books = \external_api::clean_returnvalue(mod_book_external::get_books_by_courses_returns(), $books);
 194          $this->assertCount(2, $books['books']);
 195  
 196      }
 197  }