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 - https://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 the {@link \tool_policy\test\helper} class.
  19   *
  20   * @package     tool_policy
  21   * @category    test
  22   * @copyright   2018 David Mudrák <david@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_policy\test;
  27  
  28  use tool_policy\api;
  29  use tool_policy\policy_version;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Provides some helper methods for unit-tests.
  35   *
  36   * @copyright 2018 David Mudrák <david@moodle.com>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class helper {
  40  
  41      /**
  42       * Helper method that creates a new policy for testing
  43       *
  44       * @param array $params
  45       * @return policy_version
  46       */
  47      public static function add_policy($params = []) {
  48          static $counter = 0;
  49          $counter++;
  50  
  51          $defaults = [
  52              'name' => 'Policy '.$counter,
  53              'summary_editor' => ['text' => "P$counter summary", 'format' => FORMAT_HTML, 'itemid' => 0],
  54              'content_editor' => ['text' => "P$counter content", 'format' => FORMAT_HTML, 'itemid' => 0],
  55          ];
  56  
  57          $params = (array)$params + $defaults;
  58          $formdata = api::form_policydoc_data(new policy_version(0));
  59          foreach ($params as $key => $value) {
  60              $formdata->$key = $value;
  61          }
  62          return api::form_policydoc_add($formdata);
  63      }
  64  
  65      /**
  66       * Helper method that prepare a policy document with some versions.
  67       *
  68       * @param int $numversions The number of policy versions to create.
  69       * @return array Array with all the policy versions created.
  70       */
  71      public static function create_versions($numversions = 2) {
  72          // Prepare a policy document with some versions.
  73          $policy = self::add_policy([
  74              'name' => 'Test policy',
  75              'revision' => 'v1',
  76          ]);
  77  
  78          for ($i = 2; $i <= $numversions; $i++) {
  79              $formdata = api::form_policydoc_data($policy);
  80              $formdata->revision = 'v'.$i;
  81              api::form_policydoc_update_new($formdata);
  82          }
  83  
  84          $list = api::list_policies($policy->get('policyid'));
  85  
  86          return $list[0]->draftversions;
  87      }
  88  }