Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Unit tests for get_entity_generator web service
  19   *
  20   * @package   tool_behat
  21   * @copyright 2022 onwards Catalyst IT EU {@link https://catalyst-eu.net}
  22   * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_behat\external;
  27  
  28  /**
  29   * Tests for get_entity_generator web service
  30   *
  31   * @covers \tool_behat\external\get_entity_generator
  32   */
  33  class get_entity_generator_test extends \advanced_testcase {
  34  
  35      /**
  36       * Log in as admin
  37       *
  38       * @return void
  39       */
  40      public function setUp(): void {
  41          $this->resetAfterTest();
  42          $this->setAdminUser();
  43      }
  44  
  45      /**
  46       * Get the generator for a core entity.
  47       *
  48       * @return void
  49       */
  50      public function test_execute_core_entity(): void {
  51          $generator = get_entity_generator::execute('users');
  52          $this->assertEquals(['required' => ['username']], $generator);
  53      }
  54  
  55      /**
  56       * Get the generator for the plugin entity.
  57       *
  58       * @return void
  59       */
  60      public function test_execute_plugin_entity(): void {
  61          $generator = get_entity_generator::execute('mod_book > chapters');
  62          $this->assertEquals(['required' => ['book', 'title', 'content']], $generator);
  63      }
  64  
  65      /**
  66       * Get the generator for an entity with no required fields.
  67       *
  68       * @return void
  69       */
  70      public function test_execute_no_requried(): void {
  71          $generator = get_entity_generator::execute('mod_forum > posts');
  72          $this->assertEquals(['required' => []], $generator);
  73      }
  74  
  75      /**
  76       * Attempt to get the generator for a core entity that does not exist.
  77       *
  78       * @return void
  79       */
  80      public function test_execute_invalid_entity(): void {
  81          $this->expectException('coding_exception');
  82          get_entity_generator::execute('foo');
  83      }
  84  
  85      /**
  86       * Attempt to get a generator form a plugin that does not exist.
  87       *
  88       * @return void
  89       */
  90      public function test_execute_invalid_plugin(): void {
  91          $this->expectException('coding_exception');
  92          get_entity_generator::execute('foo > bar');
  93      }
  94  
  95      /**
  96       * Attempt to get a generator for an entity that does not exist, from a plugin that does.
  97       *
  98       * @return void
  99       */
 100      public function test_execute_invalid_plugin_entity(): void {
 101          $this->expectException('coding_exception');
 102          get_entity_generator::execute('mod_book > bar');
 103      }
 104  }