Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  /**
  20   * Generator tests class.
  21   *
  22   * @package    mod_book
  23   * @copyright  2013 Frédéric Massart
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class generator_test extends \advanced_testcase {
  27  
  28      public function test_create_instance() {
  29          global $DB;
  30          $this->resetAfterTest();
  31          $this->setAdminUser();
  32  
  33          $course = $this->getDataGenerator()->create_course();
  34  
  35          $this->assertFalse($DB->record_exists('book', array('course' => $course->id)));
  36          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  37          $this->assertEquals(1, $DB->count_records('book', array('course' => $course->id)));
  38          $this->assertTrue($DB->record_exists('book', array('course' => $course->id, 'id' => $book->id)));
  39  
  40          $params = array('course' => $course->id, 'name' => 'One more book');
  41          $book = $this->getDataGenerator()->create_module('book', $params);
  42          $this->assertEquals(2, $DB->count_records('book', array('course' => $course->id)));
  43          $this->assertEquals('One more book', $DB->get_field_select('book', 'name', 'id = :id', array('id' => $book->id)));
  44      }
  45  
  46      public function test_create_chapter() {
  47          global $DB;
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50  
  51          $course = $this->getDataGenerator()->create_course();
  52          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  53          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
  54  
  55          $this->assertFalse($DB->record_exists('book_chapters', array('bookid' => $book->id)));
  56          $bookgenerator->create_chapter(array('bookid' => $book->id));
  57          $this->assertTrue($DB->record_exists('book_chapters', array('bookid' => $book->id)));
  58  
  59          $chapter = $bookgenerator->create_chapter(
  60              array('bookid' => $book->id, 'content' => 'Yay!', 'title' => 'Oops', 'tags' => array('Cats', 'mice')));
  61          $this->assertEquals(2, $DB->count_records('book_chapters', array('bookid' => $book->id)));
  62          $this->assertEquals('Oops', $DB->get_field_select('book_chapters', 'title', 'id = :id', array('id' => $chapter->id)));
  63          $this->assertEquals('Yay!', $DB->get_field_select('book_chapters', 'content', 'id = :id', array('id' => $chapter->id)));
  64          $this->assertEquals(array('Cats', 'mice'),
  65              array_values(\core_tag_tag::get_item_tags_array('mod_book', 'book_chapters', $chapter->id)));
  66  
  67          $chapter = $bookgenerator->create_content($book);
  68          $this->assertEquals(3, $DB->count_records('book_chapters', array('bookid' => $book->id)));
  69      }
  70  
  71  }