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  /**
  18   * Generator tests.
  19   *
  20   * @package    core_notes
  21   * @copyright  2013 Ankit Agarwal
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_notes;
  26  
  27  /**
  28   * Generator tests class.
  29   *
  30   * @package    core_notes
  31   * @copyright  2013 Ankit Agarwal
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class generator_test extends \advanced_testcase {
  35  
  36      /** Test create_instance method */
  37      public function test_create_instance() {
  38          global $DB;
  39          $this->resetAfterTest();
  40          $this->setAdminUser();
  41  
  42          $course = $this->getDataGenerator()->create_course();
  43          $user = $this->getDataGenerator()->create_user();
  44          $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
  45  
  46          $this->assertFalse($DB->record_exists('post', array('courseid' => $course->id)));
  47          $note = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id));
  48          $this->assertEquals(1, $DB->count_records('post', array('courseid' => $course->id, 'userid' => $user->id)));
  49          $this->assertTrue($DB->record_exists('post', array('id' => $note->id)));
  50  
  51          $params = array('courseid' => $course->id, 'userid' => $user->id, 'publishstate' => NOTES_STATE_DRAFT);
  52          $note = $gen->create_instance($params);
  53          $this->assertEquals(2, $DB->count_records('post', array('courseid' => $course->id, 'userid' => $user->id)));
  54          $this->assertEquals(NOTES_STATE_DRAFT, $DB->get_field_select('post', 'publishstate', 'id = :id',
  55                  array('id' => $note->id)));
  56      }
  57  
  58      /** Test Exceptions thrown by create_instance method */
  59      public function test_create_instance_exceptions() {
  60          $this->resetAfterTest();
  61  
  62          $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
  63  
  64          // Test not setting userid.
  65          try {
  66              $gen->create_instance(array('courseid' => 2));
  67              $this->fail('A note should not be allowed to be created without associcated userid');
  68          } catch (\coding_exception $e) {
  69              $this->assertStringContainsString('Module generator requires $record->userid', $e->getMessage());
  70          }
  71  
  72          // Test not setting courseid.
  73          try {
  74              $gen->create_instance(array('userid' => 2));
  75              $this->fail('A note should not be allowed to be created without associcated courseid');
  76          } catch (\coding_exception $e) {
  77              $this->assertStringContainsString('Module generator requires $record->courseid', $e->getMessage());
  78          }
  79      }
  80  
  81  }
  82