Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 400 and 403] [Versions 401 and 403]

   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 qbank_editquestion;
  18  
  19  use core_question\local\bank\question_version_status;
  20  use qbank_editquestion\external\update_question_version_status;
  21  
  22  /**
  23   * Submit status external api test.
  24   *
  25   * @package    qbank_editquestion
  26   * @copyright  2021 Catalyst IT Australia Pty Ltd
  27   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   * @coversDefaultClass \core_question\local\bank\question_version_status
  30   * @coversDefaultClass \qbank_editquestion\external\update_question_version_status
  31   * @coversDefaultClass \qbank_editquestion\editquestion_helper
  32   */
  33  class update_question_version_status_test extends \advanced_testcase {
  34  
  35      /** @var \stdClass course record. */
  36      protected $course;
  37  
  38      /** @var mixed. */
  39      protected $user;
  40  
  41      /**
  42       * Called before every test.
  43       */
  44      public function setUp(): void {
  45          global $USER;
  46          parent::setUp();
  47          $this->setAdminUser();
  48          $this->course = $this->getDataGenerator()->create_course();
  49          $this->user = $USER;
  50      }
  51  
  52      /**
  53       * Test if the submit status webservice changes the status of the question.
  54       *
  55       * @covers ::execute
  56       * @covers ::get_question_status_string
  57       */
  58      public function test_submit_status_updates_the_question_status() {
  59          global $DB;
  60          $this->resetAfterTest();
  61          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  62          $cat = $questiongenerator->create_question_category();
  63          $numq = $questiongenerator->create_question('essay', null,
  64              ['category' => $cat->id, 'name' => 'This is the first version']);
  65          $result = update_question_version_status::execute($numq->id, 'draft');
  66          // Test if the version actually changed.
  67          $currentstatus = $DB->get_record('question_versions', ['questionid' => $numq->id]);
  68          $this->assertEquals(editquestion_helper::get_question_status_string($currentstatus->status), $result['statusname']);
  69      }
  70  
  71      /**
  72       * Test submit status webservice only takes an existing parameter status.
  73       *
  74       * @covers ::execute
  75       */
  76      public function test_submit_status_error() {
  77          global $DB;
  78          $this->resetAfterTest();
  79          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  80          $cat = $questiongenerator->create_question_category();
  81          $numq = $questiongenerator->create_question('essay', null,
  82              ['category' => $cat->id, 'name' => 'This is the first version']);
  83          // Passing a wrong status to web service.
  84          $result = update_question_version_status::execute($numq->id, 'frog');
  85          // Tests web service returns error.
  86          $this->assertEquals(false, $result['status']);
  87          $this->assertEquals('', $result['statusname']);
  88          $this->assertEquals(get_string('unrecognizedstatus', 'qbank_editquestion'), $result['error']);
  89          // Test version did not change.
  90          $currentstatus = $DB->get_record('question_versions', ['questionid' => $numq->id]);
  91          $this->assertEquals(question_version_status::QUESTION_STATUS_READY, $currentstatus->status);
  92      }
  93  
  94      /**
  95       * Test that updating the status does not create a new version.
  96       *
  97       * @covers ::execute
  98       */
  99      public function test_submit_status_does_not_create_a_new_version() {
 100          global $DB;
 101          $this->resetAfterTest();
 102  
 103          // Find out the start count in 'question_versions' table.
 104          $versioncount = $DB->count_records('question_versions');
 105  
 106          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 107          $cat = $questiongenerator->create_question_category();
 108          $numq = $questiongenerator->create_question('essay', null,
 109              ['category' => $cat->id, 'name' => 'This is the first version']);
 110          $countcurrentrecords = $DB->count_records('question_versions');
 111          // New version count should be equal to start + 1.
 112          $this->assertEquals($versioncount + 1, $countcurrentrecords);
 113  
 114          $result = update_question_version_status::execute($numq->id, 'draft');
 115          $countafterupdate = $DB->count_records('question_versions');
 116          $this->assertEquals($countcurrentrecords, $countafterupdate);
 117      }
 118  }