Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Unit tests for the import_info class.
  19   *
  20   * @package    tool_moodlenet
  21   * @category   test
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace tool_moodlenet\local\tests;
  26  
  27  use tool_moodlenet\local\import_info;
  28  use tool_moodlenet\local\remote_resource;
  29  use tool_moodlenet\local\url;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Class tool_moodlenet_import_info_testcase, providing test cases for the import_info class.
  35   */
  36  class tool_moodlenet_import_info_testcase extends \advanced_testcase {
  37  
  38      /**
  39       * Create some test objects.
  40       *
  41       * @return array
  42       */
  43      protected function create_test_info(): array {
  44          $user = $this->getDataGenerator()->create_user();
  45          $resource = new remote_resource(new \curl(),
  46              new url('http://example.org'),
  47              (object) [
  48                  'name' => 'Resource name',
  49                  'description' => 'Resource summary'
  50              ]
  51          );
  52          $importinfo = new import_info($user->id, $resource, (object)[]);
  53  
  54          return [$user, $resource, $importinfo];
  55      }
  56  
  57      /**
  58       * Test for creation and getters.
  59       */
  60      public function test_getters() {
  61          $this->resetAfterTest();
  62          [$user, $resource, $importinfo] = $this->create_test_info();
  63  
  64          $this->assertEquals($resource, $importinfo->get_resource());
  65          $this->assertEquals(new \stdClass(), $importinfo->get_config());
  66          $this->assertNotEmpty($importinfo->get_id());
  67      }
  68  
  69      /**
  70       * Test for setters.
  71       */
  72      public function test_set_config() {
  73          $this->resetAfterTest();
  74          [$user, $resource, $importinfo] = $this->create_test_info();
  75  
  76          $config = $importinfo->get_config();
  77          $this->assertEquals(new \stdClass(), $config);
  78          $config->course = 3;
  79          $config->section = 1;
  80          $importinfo->set_config($config);
  81          $this->assertEquals((object) ['course' => 3, 'section' => 1], $importinfo->get_config());
  82      }
  83  
  84      /**
  85       * Verify the object can be stored and loaded.
  86       */
  87      public function test_persistence() {
  88          $this->resetAfterTest();
  89          [$user, $resource, $importinfo] = $this->create_test_info();
  90  
  91          // Nothing to load initially since nothing has been saved.
  92          $loadedinfo = import_info::load($importinfo->get_id());
  93          $this->assertNull($loadedinfo);
  94  
  95          // Now, save and confirm we can load the data into a new object.
  96          $importinfo->save();
  97          $loadedinfo2 = import_info::load($importinfo->get_id());
  98          $this->assertEquals($importinfo, $loadedinfo2);
  99  
 100          // Purge and confirm the load returns null now.
 101          $importinfo->purge();
 102          $loadedinfo3 = import_info::load($importinfo->get_id());
 103          $this->assertNull($loadedinfo3);
 104      }
 105  }