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 - 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 <https://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Tests for behat_form_text class
  19   *
  20   * @copyright  2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
  21   * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  namespace tool_behat;
  25  
  26  use behat_form_text;
  27  use Behat\Mink\Session;
  28  use Behat\Mink\Element\NodeElement;
  29  use core_string_manager_standard;
  30  
  31  defined('MOODLE_INTERNAL') || die;
  32  
  33  global $CFG;
  34  require_once($CFG->libdir . '/behat/classes/behat_session_interface.php');
  35  require_once($CFG->libdir . '/behat/classes/behat_session_trait.php');
  36  require_once($CFG->libdir . '/behat/form_field/behat_form_text.php');
  37  
  38  /**
  39   * Tests for the behat_form_text class
  40   *
  41   * @package    tool_behat
  42   * @category   test
  43   * @copyright  2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
  44   * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   *
  46   * @covers \behat_form_text
  47   * @covers \behat_form_field
  48   */
  49  class behat_form_text_test extends \basic_testcase {
  50  
  51      /**
  52       * Data provider for the test_set_get_value() method.
  53       *
  54       * @return array of value and expectation pairs to be tested.
  55       */
  56      public function provider_test_set_get_value() {
  57          return [
  58              'null' => [null, null],
  59              'int' => [3, 3],
  60              'float' => [3.14, 3.14],
  61              'string' => ['hello', 'hello'],
  62              'utf8' => ['你好', '你好'],
  63          ];
  64      }
  65  
  66      /**
  67       * Test the set_value() and get_value() methods.
  68       *
  69       * @param mixed $value value to be set.
  70       * @param mixed $expectation value to be checked.
  71       * @dataProvider provider_test_set_get_value()
  72       */
  73      public function test_set_get_value($value, $expectation) {
  74          $session = $this->createMock(Session::class);
  75          $node = $this->createMock(NodeElement::class);
  76          $node->method('getValue')->willReturn($value);
  77          $field = new behat_form_text($session, $node);
  78  
  79          $field->set_value($value);
  80          $this->assertEquals($expectation, $field->get_value());
  81      }
  82  
  83      /**
  84       * Data provider for the test_text_matches() method.
  85       *
  86       * @return array of decsep, value, match and result pairs to be tested.
  87       */
  88      public function provider_test_matches() {
  89          return [
  90              'lazy true' => ['.', 'hello', 'hello', true],
  91              'lazy false' => ['.', 'hello', 'bye', false],
  92              'float true' => ['.', '3.14', '3.1400', true],
  93              'float false' => ['.', '3.14', '3.1401', false],
  94              'float and float string true' => ['.', 3.14, '3.1400', true],
  95              'float and unrelated string false' => ['.', 3.14, 'hello', false],
  96              'float hash decsep true' => ['#', '3#14', '3#1400', true],
  97              'float hash decsep false' => ['#', '3#14', '3#1401', false],
  98              'float and float string hash decsep true' => ['#', 3.14, '3.1400', true],
  99              'float and unrelated string hash decsep false' => ['#', 3.14, 'hello', false],
 100              'float custom-default decsep mix1 true' => ['#', '3#14', '3.1400', true],
 101              'float custom-default decsep mix2 true' => ['#', '3.14', '3#1400', true],
 102              'float 2-custom decsep mix1 false' => ['#', '3#14', '3,1400', false],
 103              'float 2-custom decsep mix2 false' => [',', '3#14', '3,1400', false],
 104              'float default-custom decsep mix1 false' => ['.', '3#14', '3.1400', false],
 105              'float default-custom decsep mix2 false' => ['.', '3.14', '3#1400', false],
 106          ];
 107      }
 108  
 109      /**
 110       * Test the matches() method.
 111       *
 112       * @param string $decsep decimal separator to use.
 113       * @param mixed $value value to be set.
 114       * @param mixed $match value to be matched.
 115       * @param bool  $result expected return status of the function.
 116       * @dataProvider provider_test_matches()
 117       */
 118      public function test_matches($decsep, $value, $match, $result) {
 119          global $CFG;
 120  
 121          // Switch of string manager to avoid having to (slow) customise the lang file.
 122          $origcustom = $CFG->config_php_settings['customstringmanager'] ?? null;
 123          $CFG->config_php_settings['customstringmanager'] = '\tool_behat\phpunit_string_manager';
 124          $manager = get_string_manager(true);
 125          $manager->set_string('decsep', 'langconfig', $decsep);
 126  
 127          $session = $this->createMock(Session::class);
 128          $node = $this->createMock(NodeElement::class);
 129          $node->method('getValue')->willReturn($value);
 130  
 131          $field = new behat_form_text($session, $node);
 132  
 133          $field->set_value($value);
 134          $this->assertSame($result, $field->matches($match));
 135  
 136          // Switch back to the original string manager.
 137          if (is_null($origcustom)) {
 138              unset($CFG->config_php_settings['customstringmanager']);
 139          } else {
 140              $CFG->config_php_settings['customstringmanager'] = $origcustom;
 141          }
 142          $manager = get_string_manager(true);
 143      }
 144  }
 145  
 146  /**
 147   * Customised values that will be used instead of standard manager one.
 148   *
 149   * If an existing component/identifier is found, return it instead of the real
 150   * one from language files. Note this doesn't support place holders or another niceties.
 151   *
 152   * @package    tool_behat
 153   * @category   test
 154   * @copyright  2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
 155   * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 156   */
 157  class phpunit_string_manager extends core_string_manager_standard {
 158  
 159      /** @var array language customisations provided by the manager without asking for real contents */
 160      protected $customstrings = [];
 161  
 162      /**
 163       * Get String returns a requested string
 164       *
 165       * @param string $identifier The identifier of the string to search for
 166       * @param string $component The module the string is associated with
 167       * @param string|object|array $a An object, string or number that can be used
 168       *      within translation strings
 169       * @param string $lang moodle translation language, null means use current
 170       * @return string The String !
 171       */
 172      public function get_string($identifier, $component = '', $a = null, $lang = null) {
 173          $key = trim($component) . '/' . trim($identifier);
 174          if (isset($this->customstrings[$key])) {
 175              return $this->customstrings[$key];
 176          }
 177          return parent::get_string($identifier, $component, $a, $lang);
 178      }
 179  
 180      /**
 181       * Sets a custom string to be returned by the string manager instead of the language file one.
 182       *
 183       * @param string $identifier The identifier of the string to search for
 184       * @param string $component The module the string is associated with
 185       * @param string $value the contents of the language string to be returned by get_string()
 186       */
 187      public function set_string($identifier, $component, $value) {
 188          $key = trim($component) . '/' . trim($identifier);
 189          $this->customstrings[$key] = $value;
 190      }
 191  }