Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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   * Provides \core\update\testable_api class.
  19   *
  20   * @package     core_plugin
  21   * @subpackage  fixtures
  22   * @category    test
  23   * @copyright   2015 David Mudrak <david@moodle.com>
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace core\update;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Testable variant of \core\update\api class.
  33   *
  34   * Provides access to some protected methods we want to explicitly test and
  35   * bypass the actual cURL calls by providing fake responses.
  36   *
  37   * @copyright 2015 David Mudrak <david@moodle.com>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class testable_api extends api {
  41  
  42      /**
  43       * Provides access to the parent protected method.
  44       *
  45       * @param int $branch
  46       * @return string
  47       */
  48      public function convert_branch_numbering_format($branch) {
  49          return parent::convert_branch_numbering_format($branch);
  50      }
  51  
  52      /**
  53       * Returns fake URL of the pluginfo.php API end-point.
  54       *
  55       * @return string
  56       */
  57      protected function get_serviceurl_pluginfo() {
  58          return 'http://testab.le/api/pluginfo.php';
  59      }
  60  
  61      /**
  62       * Mimics the call to the given end-point service with the given parameters.
  63       *
  64       * This simulates a hypothetical plugins directory with a single plugin
  65       * 'foo_bar' available (with a single release).
  66       *
  67       * @param string $serviceurl
  68       * @param array $params
  69       * @return stdClass|bool
  70       */
  71      protected function call_service($serviceurl, array $params=array()) {
  72  
  73          $response = (object)array(
  74              'data' => null,
  75              'info' => null,
  76              'status' => null,
  77          );
  78  
  79          $foobarinfo = (object)array(
  80              'status' => 'OK',
  81              'apiver' => '1.3',
  82              'pluginfo' => (object)array(
  83                  'id' => 42,
  84                  'name' => 'Foo bar',
  85                  'component' => 'foo_bar',
  86                  'source' => '',
  87                  'doc' => '',
  88                  'bugs' => '',
  89                  'discussion' => '',
  90                  'version' => false,
  91              ),
  92          );
  93  
  94          $version2015093000info = (object)array(
  95              'id' => '6765',
  96              'version' => '2015093000',
  97              'release' => '1.0',
  98              'maturity' => '200',
  99              'downloadurl' => 'http://mood.le/plugins/foo_bar/2015093000.zip',
 100              'downloadmd5' => 'd41d8cd98f00b204e9800998ecf8427e',
 101              'vcssystem' => '',
 102              'vcssystemother' => '',
 103              'vcsrepositoryurl' => '',
 104              'vcsbranch' => '',
 105              'vcstag' => '',
 106              'supportedmoodles' => array(
 107                  (object)array(
 108                      'version' => '2015041700',
 109                      'release' => '2.9'
 110                  ),
 111                  (object)array(
 112                      'version' => '2015110900',
 113                      'release' => '3.0'
 114                  ),
 115              )
 116          );
 117  
 118          $version2015100400info = (object)array(
 119              'id' => '6796',
 120              'version' => '2015100400',
 121              'release' => '1.1',
 122              'maturity' => '200',
 123              'downloadurl' => 'http://mood.le/plugins/foo_bar/2015100400.zip',
 124              'downloadmd5' => 'd41d8cd98f00b204e9800998ecf8427e',
 125              'vcssystem' => '',
 126              'vcssystemother' => '',
 127              'vcsrepositoryurl' => '',
 128              'vcsbranch' => '',
 129              'vcstag' => '',
 130              'supportedmoodles' => array(
 131                  (object)array(
 132                      'version' => '2015110900',
 133                      'release' => '3.0'
 134                  ),
 135              )
 136          );
 137  
 138          $version2015100500info = (object)array(
 139              'id' => '6799',
 140              'version' => '2015100500',
 141              'release' => '2.0beta',
 142              'maturity' => '100',
 143              'downloadurl' => 'http://mood.le/plugins/foo_bar/2015100500.zip',
 144              'downloadmd5' => 'd41d8cd98f00b204e9800998ecf8427e',
 145              'vcssystem' => '',
 146              'vcssystemother' => '',
 147              'vcsrepositoryurl' => '',
 148              'vcsbranch' => '',
 149              'vcstag' => '',
 150              'supportedmoodles' => array(
 151                  (object)array(
 152                      'version' => '2015110900',
 153                      'release' => '3.0'
 154                  ),
 155              )
 156          );
 157  
 158          if ($serviceurl === 'http://testab.le/api/pluginfo.php') {
 159              if (strpos($params['plugin'], 'foo_bar@') === 0) {
 160                  $response->data = $foobarinfo;
 161                  $response->info = array(
 162                      'http_code' => 200,
 163                  );
 164                  $response->status = '200 OK';
 165  
 166                  if (substr($params['plugin'], -11) === '@2015093000') {
 167                      $response->data->pluginfo->version = $version2015093000info;
 168                  }
 169  
 170                  if (substr($params['plugin'], -11) === '@2015100400') {
 171                      $response->data->pluginfo->version = $version2015100400info;
 172                  }
 173  
 174                  if (substr($params['plugin'], -11) === '@2015100500') {
 175                      $response->data->pluginfo->version = $version2015100500info;
 176                  }
 177  
 178              } else if ($params['plugin'] === 'foo_bar' and isset($params['branch']) and isset($params['minversion'])) {
 179                  $response->data = $foobarinfo;
 180                  $response->info = array(
 181                      'http_code' => 200,
 182                  );
 183                  $response->status = '200 OK';
 184  
 185                  if ($params['minversion'] <= 2015100400) {
 186                      // If two stable versions fullfilling the required version are
 187                      // available, the /1.3/pluginfo.php API returns the more recent one.
 188                      $response->data->pluginfo->version = $version2015100400info;
 189  
 190                  } else if ($params['minversion'] <= 2015100500) {
 191                      // The /1.3/pluginfo.php API returns versions with lower
 192                      // maturity if it is the only way how to fullfil the
 193                      // required minimal version.
 194                      $response->data->pluginfo->version = $version2015100500info;
 195                  }
 196  
 197              } else {
 198                  $response->info = array(
 199                      'http_code' => 404,
 200                  );
 201                  $response->status = '404 Not Found (unknown plugin)';
 202              }
 203  
 204              return $response;
 205  
 206          } else {
 207              return 'This should not happen';
 208          }
 209      }
 210  }