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.
   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 tool_brickfield;
  18  
  19  /**
  20   * Unit tests for {@scheduler tool_brickfield\scheduler.php}.
  21   *
  22   * @package   tool_brickfield
  23   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  24   * @author     Jay Churchward (jay@brickfieldlabs.ie)
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class scheduler_test extends \advanced_testcase {
  28  
  29      public function test_request_analysis() {
  30          $this->resetAfterTest();
  31  
  32          // I believe there is a bug where the code won't work with the default constructor values.
  33          // Can't find data record in database table course.
  34          // (SELECT id,category FROM {course} WHERE id = ?[array (0 => 0,)])
  35          // There is no course with an id of 0 so it throws an error, however it is never used like this in the current code.
  36  
  37          $object = new scheduler(1);
  38          $output = $object->request_analysis();
  39          $this->assertTrue($output);
  40  
  41          $object = new scheduler(1, 1);
  42          $output = $object->request_analysis();
  43          $this->assertTrue($output);
  44  
  45          $object = new scheduler(0, 2);
  46          $output = $object->request_analysis();
  47          $this->assertTrue($output);
  48  
  49      }
  50  
  51      public function test_mark_analyzed() {
  52          $this->resetAfterTest();
  53          $object = new scheduler();
  54          $output = $object->mark_analyzed();
  55          $this->assertTrue($output);
  56  
  57          $object = new scheduler(1, 1);
  58          $output = $object->mark_analyzed();
  59          $this->assertTrue($output);
  60      }
  61  
  62      public function test_create_schedule() {
  63          global $DB;
  64  
  65          $this->resetAfterTest();
  66          $object = new scheduler();
  67          $output = $object->create_schedule();
  68          $record = $DB->get_record($object::DATA_TABLE, ['contextlevel' => 50]);
  69          $this->assertTrue($output);
  70          $this->assertEquals($record->instanceid, 0);
  71  
  72          $object = new scheduler(1, 1);
  73          $output = $object->mark_analyzed();
  74          $record = $DB->get_record($object::DATA_TABLE, ['contextlevel' => 1]);
  75          $this->assertTrue($output);
  76          $this->assertEquals($record->instanceid, 1);
  77      }
  78  
  79      public function test_delete_schedule() {
  80          global $DB;
  81  
  82          // Call create_record() to insert a record into the table.
  83          $this->resetAfterTest();
  84          $object = new scheduler();
  85          $object->create_schedule();
  86          $record = $DB->get_record($object::DATA_TABLE, ['contextlevel' => 50]);
  87  
  88          // Assert that the record is in the table.
  89          $this->assertEquals($record->instanceid, 0);
  90  
  91          // Assert that the record is deleted after calling delete_schedule().
  92          $output = $object->delete_schedule();
  93          $record = $DB->get_record($object::DATA_TABLE, ['contextlevel' => 50]);
  94          $this->assertTrue($output);
  95          $this->assertFalse($record);
  96      }
  97  
  98      public function test_is_in_schedule() {
  99          $this->resetAfterTest();
 100  
 101          // This should assert to false as no record has been inserted.
 102          $object = new scheduler();
 103          $output = $object->is_in_schedule();
 104          $this->assertFalse($output);
 105  
 106          // This should assert to true because create_schedule inserts a record to the table.
 107          $object->create_schedule();
 108          $output = $object->is_in_schedule();
 109          $this->assertTrue($output);
 110      }
 111  
 112      public function test_is_scheduled() {
 113          $this->resetAfterTest();
 114  
 115          // This should assert to false as no record has been inserted.
 116          $object = new scheduler(1, 1);
 117          $output = $object->is_scheduled();
 118          $this->assertFalse($output);
 119  
 120          // This should assert to false because the record has been created but not requested.
 121          $object->create_schedule();
 122          $output = $object->is_scheduled();
 123          $this->assertFalse($output);
 124  
 125          // This should assert to true because the record has been created and requested.
 126          $object->create_schedule();
 127          $object->request_analysis();
 128          $output = $object->is_scheduled();
 129          $this->assertTrue($output);
 130      }
 131  
 132      public function test_is_submitted() {
 133          $this->resetAfterTest();
 134  
 135          // This should assert to false as no record has been inserted.
 136          $object = new scheduler(1, 1);
 137          $output = $object->is_submitted();
 138          $this->assertFalse($output);
 139  
 140          // This should assert to false because the record has been created but not submitted.
 141          $object->create_schedule();
 142          $output = $object->is_submitted();
 143          $this->assertFalse($output);
 144  
 145          // This should assert to true because the record has been created and submitted.
 146          $object->create_schedule();
 147          $object->mark_analyzed();
 148          $output = $object->is_submitted();
 149          $this->assertTrue($output);
 150      }
 151  
 152      public function test_is_analyzed() {
 153          $this->resetAfterTest();
 154  
 155          // This should assert to false as no record has been inserted.
 156          $object = new scheduler(1, 1);
 157          $output = $object->is_analyzed();
 158          $this->assertFalse($output);
 159  
 160          // This should assert to false because the record has been created but not submitted.
 161          $object->create_schedule();
 162          $output = $object->is_analyzed();
 163          $this->assertFalse($output);
 164  
 165          // This should assert to true because the record has been created and submitted.
 166          $object->create_schedule();
 167          $object->mark_analyzed();
 168          $output = $object->is_analyzed();
 169          $this->assertTrue($output);
 170      }
 171  
 172      // Can't test because it's a protected function.
 173      public function test_standard_search_params() {
 174      }
 175  
 176      // Can't test because it's a protected function.
 177      public function test_get_contextid() {
 178      }
 179  
 180      public function test_get_datarecord() {
 181          $this->resetAfterTest();
 182  
 183          $object = new scheduler();
 184          $output = $object->get_datarecord();
 185          $this->assertEquals($output->contextlevel, 50);
 186          $this->assertEquals($output->instanceid, 0);
 187          $this->assertEquals($output->status, 0);
 188  
 189          $object = new scheduler(1, 1);
 190          $output = $object->get_datarecord(2);
 191          $this->assertEquals($output->contextlevel, 1);
 192          $this->assertEquals($output->instanceid, 1);
 193          $this->assertEquals($output->status, 2);
 194  
 195          $object = new scheduler(10, 143);
 196          $output = $object->get_datarecord(5);
 197          $this->assertEquals($output->contextlevel, 143);
 198          $this->assertEquals($output->instanceid, 10);
 199          $this->assertEquals($output->status, 5);
 200      }
 201  
 202      // No return statement.
 203      public function test_process_scheduled_requests() {
 204  
 205      }
 206  
 207      public function test_initialize_schedule() {
 208          global $DB;
 209          $this->resetAfterTest();
 210  
 211          $output = scheduler::initialize_schedule();
 212          $record = $DB->get_record(scheduler::DATA_TABLE, ['contextlevel' => 50]);
 213          $this->assertTrue($output);
 214          $this->assertEquals($record->contextlevel, 50);
 215  
 216          $output = scheduler::initialize_schedule(20);
 217          $record = $DB->get_record(scheduler::DATA_TABLE, ['contextlevel' => 20]);
 218          $this->assertTrue($output);
 219          $this->assertEquals($record->contextlevel, 20);
 220      }
 221  
 222      public function test_request_course_analysis() {
 223          $this->resetAfterTest();
 224  
 225          $output = scheduler::request_course_analysis(1);
 226          $this->assertTrue($output);
 227      }
 228  
 229      public function test_create_course_schedule() {
 230          global $DB;
 231          $this->resetAfterTest();
 232  
 233          $output = scheduler::create_course_schedule(1);
 234          $record = $DB->get_record(scheduler::DATA_TABLE, ['contextlevel' => 50]);
 235          $this->assertTrue($output);
 236          $this->assertEquals($record->instanceid, 1);
 237  
 238      }
 239  
 240      public function test_delete_course_schedule() {
 241          global $DB;
 242          $this->resetAfterTest();
 243  
 244          scheduler::create_course_schedule(1);
 245          $record = $DB->get_record(scheduler::DATA_TABLE, ['contextlevel' => 50]);
 246          $this->assertEquals($record->instanceid, 1);
 247  
 248          $output = scheduler::delete_course_schedule(1);
 249          $record = $DB->get_record(scheduler::DATA_TABLE, ['contextlevel' => 50]);
 250          $this->assertTrue($output);
 251          $this->assertFalse($record);
 252      }
 253  
 254      public function test_is_course_in_schedule() {
 255          $this->resetAfterTest();
 256  
 257          // This should assert to false as no record has been inserted.
 258          $output = scheduler::is_course_in_schedule(1);
 259          $this->assertFalse($output);
 260  
 261          // This should assert to true because create_schedule inserts a record to the table.
 262          scheduler::create_course_schedule(1);
 263          $output = scheduler::is_course_in_schedule(1);
 264          $this->assertTrue($output);
 265      }
 266  
 267      public function test_is_course_scheduled() {
 268          $this->resetAfterTest();
 269  
 270          // This should assert to false as no record has been inserted.
 271          $output = scheduler::is_course_scheduled(1);
 272          $this->assertFalse($output);
 273  
 274          // This should assert to false because the record has been created but not requested.
 275          scheduler::create_course_schedule(1);
 276          $output = scheduler::is_course_scheduled(1);
 277          $this->assertFalse($output);
 278  
 279          // This should assert to true because the record has been created and requested.
 280          scheduler::create_course_schedule(1);
 281          scheduler::request_course_analysis(1);
 282          $output = scheduler::is_course_scheduled(1);
 283          $this->assertTrue($output);
 284      }
 285  
 286      public function test_is_course_analyzed() {
 287          $this->resetAfterTest();
 288          $object = new scheduler(10, 1);
 289  
 290          // This should assert to false as no record has been inserted.
 291          $output = scheduler::is_course_analyzed(10);
 292          $this->assertFalse($output);
 293  
 294          // This should assert to false because the record has been created but not submitted.
 295          scheduler::create_course_schedule(10);
 296          $output = scheduler::is_course_analyzed(10);
 297          $this->assertFalse($output);
 298  
 299          // This should assert to true because the record has been created and submitted.
 300          $object->create_schedule();
 301          $object->mark_analyzed();
 302          $output = $object->is_analyzed();
 303          $this->assertTrue($output);
 304      }
 305  }