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 401 and 402] [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      /**
  36       * Called before every test.
  37       */
  38      public function setUp(): void {
  39          global $USER;
  40          parent::setUp();
  41          $this->setAdminUser();
  42          $this->course = $this->getDataGenerator()->create_course();
  43          $this->user = $USER;
  44      }
  45  
  46      /**
  47       * Test if the submit status webservice changes the status of the question.
  48       *
  49       * @covers ::execute
  50       * @covers ::get_question_status_string
  51       */
  52      public function test_submit_status_updates_the_question_status() {
  53          global $DB;
  54          $this->resetAfterTest();
  55          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  56          $cat = $questiongenerator->create_question_category();
  57          $numq = $questiongenerator->create_question('essay', null,
  58              ['category' => $cat->id, 'name' => 'This is the first version']);
  59          $result = update_question_version_status::execute($numq->id, 'draft');
  60          // Test if the version actually changed.
  61          $currentstatus = $DB->get_record('question_versions', ['questionid' => $numq->id]);
  62          $this->assertEquals(editquestion_helper::get_question_status_string($currentstatus->status), $result['statusname']);
  63      }
  64  
  65      /**
  66       * Test submit status webservice only takes an existing parameter status.
  67       *
  68       * @covers ::execute
  69       */
  70      public function test_submit_status_error() {
  71          global $DB;
  72          $this->resetAfterTest();
  73          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  74          $cat = $questiongenerator->create_question_category();
  75          $numq = $questiongenerator->create_question('essay', null,
  76              ['category' => $cat->id, 'name' => 'This is the first version']);
  77          // Passing a wrong status to web service.
  78          $result = update_question_version_status::execute($numq->id, 'frog');
  79          // Tests web service returns error.
  80          $this->assertEquals(false, $result['status']);
  81          $this->assertEquals('', $result['statusname']);
  82          $this->assertEquals(get_string('unrecognizedstatus', 'qbank_editquestion'), $result['error']);
  83          // Test version did not change.
  84          $currentstatus = $DB->get_record('question_versions', ['questionid' => $numq->id]);
  85          $this->assertEquals(question_version_status::QUESTION_STATUS_READY, $currentstatus->status);
  86      }
  87  
  88      /**
  89       * Test that updating the status does not create a new version.
  90       *
  91       * @covers ::execute
  92       */
  93      public function test_submit_status_does_not_create_a_new_version() {
  94          global $DB;
  95          $this->resetAfterTest();
  96  
  97          // Find out the start count in 'question_versions' table.
  98          $versioncount = $DB->count_records('question_versions');
  99  
 100          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 101          $cat = $questiongenerator->create_question_category();
 102          $numq = $questiongenerator->create_question('essay', null,
 103              ['category' => $cat->id, 'name' => 'This is the first version']);
 104          $countcurrentrecords = $DB->count_records('question_versions');
 105          // New version count should be equal to start + 1.
 106          $this->assertEquals($versioncount + 1, $countcurrentrecords);
 107  
 108          $result = update_question_version_status::execute($numq->id, 'draft');
 109          $countafterupdate = $DB->count_records('question_versions');
 110          $this->assertEquals($countcurrentrecords, $countafterupdate);
 111      }
 112  }