Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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  namespace core_adminpresets\local\setting;
  18  
  19  /**
  20   * Tests for the adminpresets_admin_setting_sitesettext class.
  21   *
  22   * @package    core_adminpresets
  23   * @category   test
  24   * @copyright  2021 Sara Arjona (sara@moodle.com)
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @coversDefaultClass \core_adminpresets\local\setting\adminpresets_admin_setting_sitesettext
  27   */
  28  class adminpresets_admin_setting_sitesettext_test extends \advanced_testcase {
  29  
  30      /**
  31       * Test the behaviour of save_value() method.
  32       *
  33       * @covers ::save_value
  34       * @dataProvider save_value_provider
  35       *
  36       * @param string $settingname Setting name to save.
  37       * @param string $settingvalue Setting value to be saved.
  38       * @param bool $expectedsaved Whether the setting will be saved or not.
  39       */
  40      public function test_save_value(string $settingname, string $settingvalue, bool $expectedsaved): void {
  41          global $DB;
  42  
  43          $this->resetAfterTest();
  44  
  45          // Login as admin, to access all the settings.
  46          $this->setAdminUser();
  47  
  48          // Get the setting and save the value.
  49          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  50          $setting = $generator->get_admin_preset_setting('frontpagesettings', $settingname);
  51          $result = $setting->save_value(false, $settingvalue);
  52  
  53          // Check the result is the expected (saved when it has a different value and ignored when the value is the same).
  54          if ($expectedsaved) {
  55              $this->assertCount(1, $DB->get_records('config_log', ['id' => $result]));
  56              // Specific from the save_value in adminpresets_admin_setting_sitesettext.
  57              $sitecourse = $DB->get_record('course', ['id' => 1]);
  58              $this->assertEquals($settingvalue, $sitecourse->{$settingname});
  59          } else {
  60              $this->assertFalse($result);
  61          }
  62      }
  63  
  64      /**
  65       * Data provider for test_save_value().
  66       *
  67       * @return array
  68       */
  69      public function save_value_provider(): array {
  70          return [
  71              'Fullname: different value' => [
  72                  'settingname' => 'fullname',
  73                  'setttingvalue' => 'New site fullname',
  74                  'expectedsaved' => true,
  75              ],
  76              'Fullname: same value' => [
  77                  'settingname' => 'fullname',
  78                  'setttingvalue' => 'PHPUnit test site',
  79                  'expectedsaved' => false,
  80              ],
  81              'Summary: different value' => [
  82                  'settingname' => 'summary',
  83                  'setttingvalue' => 'This is a new site summary.',
  84                  'expectedsaved' => true,
  85              ],
  86              'Summary: same value' => [
  87                  'settingname' => 'summary',
  88                  'setttingvalue' => '',
  89                  'expectedsaved' => false,
  90              ],
  91          ];
  92      }
  93  
  94  }