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