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  /**
  18   * PHPUnit tool_brickfield tests
  19   *
  20   * @package   tool_brickfield
  21   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  22   * @author     Mike Churchward (mike@brickfieldlabs.ie)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace tool_brickfield;
  26  
  27  /**
  28   * Unit tests for {@registration tool_brickfield\registration.php}.
  29   * @group tool_brickfield
  30   */
  31  class registration_test extends \advanced_testcase {
  32      public static function setUpBeforeClass(): void {
  33          global $CFG;
  34  
  35          require_once($CFG->dirroot . '/admin/tool/brickfield/tests/generator/mock_registration.php');
  36          require_once($CFG->dirroot . '/admin/tool/brickfield/tests/generator/mock_brickfieldconnect.php');
  37      }
  38  
  39      /**
  40       * Tests the state of the registration system when first installed.
  41       * @throws \dml_exception
  42       */
  43      public function test_initial_state() {
  44          $this->resetAfterTest();
  45          $regobj = new mock_registration();
  46  
  47          // Initial state of system.
  48          $this->assertFalse($regobj->toolkit_is_active());
  49          $this->assertFalse($regobj->validation_pending());
  50          $this->assertFalse($regobj->validation_error());
  51          $this->assertEmpty($regobj->get_api_key());
  52          $this->assertEmpty($regobj->get_secret_key());
  53      }
  54  
  55      /**
  56       * Test the various states for setting registration keys.
  57       * @throws \dml_exception
  58       */
  59      public function test_set_keys_for_registration() {
  60          $this->resetAfterTest();
  61          $regobj = new mock_registration();
  62  
  63          // State when invalid format keys are sent.
  64          $this->assertFalse($regobj->set_keys_for_registration('123', 'abc'));
  65          $this->assertTrue($regobj->is_not_entered());
  66          $this->assertFalse($regobj->validation_pending());
  67          $this->assertEmpty($regobj->get_api_key());
  68          $this->assertEmpty($regobj->get_secret_key());
  69  
  70          // State when valid format keys are sent.
  71          $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
  72              mock_brickfieldconnect::VALIDSECRETKEY));
  73          $this->assertTrue($regobj->validation_pending());
  74          $this->assertEquals($regobj->get_api_key(), mock_brickfieldconnect::VALIDAPIKEY);
  75          $this->assertEquals($regobj->get_secret_key(), mock_brickfieldconnect::VALIDSECRETKEY);
  76      }
  77  
  78      /**
  79       * Test the validation system through its several states.
  80       * @throws \dml_exception
  81       */
  82      public function test_validation() {
  83          $this->resetAfterTest();
  84          $regobj = new mock_registration();
  85  
  86          // Set invalid format keys and validate the system.
  87          $this->assertFalse($regobj->set_keys_for_registration('123', 'abc'));
  88          // Run validate function. State should end up as 'NOT_ENTERED'.
  89          $this->assertFalse($regobj->validate());
  90          $this->assertTrue($regobj->is_not_entered());
  91  
  92          // Set valid keys and validate the system.
  93          $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
  94              mock_brickfieldconnect::VALIDSECRETKEY));
  95          // Run validate function. State should end up as valid, 'VALIDATED'.
  96          $this->assertTrue($regobj->validate());
  97          $this->assertTrue($regobj->toolkit_is_active());
  98          $this->assertFalse($regobj->validation_pending());
  99          $this->assertFalse($regobj->validation_error());
 100  
 101          // Set invalid keys and validate the system.
 102          $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
 103              'cd123456789012345678901234567890'));
 104          // Run validate function. State should end up as valid, not validated, 'ERROR'.
 105          $this->assertTrue($regobj->validate());
 106          $this->assertTrue($regobj->toolkit_is_active());
 107          $this->assertTrue($regobj->validation_pending());
 108          $this->assertTrue($regobj->validation_error());
 109      }
 110  
 111      /**
 112       * Tests the system after validation grace periods expire.
 113       * @throws \dml_exception
 114       */
 115      public function test_validation_time_expiry() {
 116          $this->resetAfterTest();
 117          $regobj = new mock_registration();
 118  
 119          // Set valid keys and validate the system.
 120          $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
 121              mock_brickfieldconnect::VALIDSECRETKEY));
 122          // Run validate function. State should end up as valid, 'VALIDATED'.
 123          $this->assertTrue($regobj->validate());
 124          $this->assertTrue($regobj->toolkit_is_active());
 125  
 126          // Invalidate the validation time.
 127          $regobj->invalidate_validation_time();
 128          // Run validate function. State should end up as valid, 'VALIDATED'.
 129          $this->assertTrue($regobj->validate());
 130          $this->assertTrue($regobj->toolkit_is_active());
 131  
 132          // Set invalid keys and validate the system.
 133          $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
 134              'cd123456789012345678901234567890'));
 135          // Run validate function. State should end up as valid, not validated, 'ERROR'.
 136          $this->assertTrue($regobj->validate());
 137          $this->assertTrue($regobj->toolkit_is_active());
 138          $this->assertTrue($regobj->validation_pending());
 139          $this->assertTrue($regobj->validation_error());
 140  
 141          // Invalidate the validation time.
 142          $regobj->invalidate_validation_time();
 143          // Run validate function. State should end up as  not valid.
 144          $this->assertFalse($regobj->validate());
 145          $this->assertFalse($regobj->toolkit_is_active());
 146      }
 147  
 148      /**
 149       * Tests the system after summary data time periods expire.
 150       * @throws \dml_exception
 151       */
 152      public function test_summary_time_expiry() {
 153          $this->resetAfterTest();
 154          $regobj = new mock_registration();
 155  
 156          // Set valid keys and validate the system.
 157          $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
 158              mock_brickfieldconnect::VALIDSECRETKEY));
 159          // Run validate function. State should end up as valid, 'VALIDATED'.
 160          $this->assertTrue($regobj->validate());
 161          $this->assertTrue($regobj->toolkit_is_active());
 162  
 163          // Invalidate the summary time.
 164          $regobj->invalidate_summary_time();
 165          // Run validate function. State should end up as not valid.
 166          $this->assertFalse($regobj->validate());
 167          $this->assertFalse($regobj->toolkit_is_active());
 168  
 169          // Set invalid keys and validate the system.
 170          $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
 171              'cd123456789012345678901234567890'));
 172          // Run validate function. State should end up as invalid.
 173          $this->assertFalse($regobj->validate());
 174          $this->assertFalse($regobj->toolkit_is_active());
 175  
 176          // Set invalid keys and validate the system.
 177          $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
 178              'cd123456789012345678901234567890'));
 179          // Mark the summary data as sent, and revalidate the system.
 180          $regobj->mark_summary_data_sent();
 181          // Run validate function. State should end up as valid, not validated, 'ERROR'.
 182          $this->assertTrue($regobj->validate());
 183          $this->assertTrue($regobj->toolkit_is_active());
 184          $this->assertTrue($regobj->validation_pending());
 185          $this->assertTrue($regobj->validation_error());
 186      }
 187  }