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.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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 test for the site generator
  19   *
  20   * @package tool_generator
  21   * @copyright 2013 David Monllaó
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Silly class to access site_backend internal methods.
  29   *
  30   * @package tool_generator
  31   * @copyright 2013 David Monllaó
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class testable_tool_generator_site_backend extends tool_generator_site_backend {
  35  
  36      /**
  37       * Public accessor.
  38       *
  39       * @return int
  40       */
  41      public static function get_last_testcourse_id() {
  42          return parent::get_last_testcourse_id();
  43      }
  44  }
  45  
  46  /**
  47   * Unit test for the site generator
  48   *
  49   * @package tool_generator
  50   * @copyright 2013 David Monllaó
  51   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  52   */
  53  class tool_generator_maketestsite_testcase extends advanced_testcase {
  54  
  55      /**
  56       * Checks that site courses shortnames are properly generated.
  57       */
  58      public function test_shortnames_generation() {
  59  
  60          $this->resetAfterTest();
  61          $this->setAdminUser();
  62  
  63          $generator = $this->getDataGenerator();
  64  
  65          // Shortname common prefix.
  66          $prefix = tool_generator_site_backend::SHORTNAMEPREFIX;
  67  
  68          $record = array();
  69  
  70          // Without courses will be 0.
  71          $lastshortname = testable_tool_generator_site_backend::get_last_testcourse_id();
  72          $this->assertEquals(0, $lastshortname);
  73  
  74          // Without {$prefix} + {no integer} courses will be 0.
  75          $record['shortname'] = $prefix . 'AA';
  76          $generator->create_course($record);
  77          $record['shortname'] = $prefix . '__';
  78          $generator->create_course($record);
  79          $record['shortname'] = $prefix . '12.2';
  80          $generator->create_course($record);
  81  
  82          $lastshortname = testable_tool_generator_site_backend::get_last_testcourse_id();
  83          $this->assertEquals(0, $lastshortname);
  84  
  85          // With {$prefix} + {integer} courses will be the higher one.
  86          $record['shortname'] = $prefix . '2';
  87          $generator->create_course($record);
  88          $record['shortname'] = $prefix . '20';
  89          $generator->create_course($record);
  90          $record['shortname'] = $prefix . '8';
  91          $generator->create_course($record);
  92  
  93          $lastshortname = testable_tool_generator_site_backend::get_last_testcourse_id();
  94          $this->assertEquals(20, $lastshortname);
  95  
  96          // Numeric order.
  97          for ($i = 9; $i < 14; $i++) {
  98              $record['shortname'] = $prefix . $i;
  99              $generator->create_course($record);
 100          }
 101  
 102          $lastshortname = testable_tool_generator_site_backend::get_last_testcourse_id();
 103          $this->assertEquals(20, $lastshortname);
 104      }
 105  
 106  }