Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  namespace core_adminpresets\local\setting;
  18  
  19  /**
  20   * Tests for the adminpresets_admin_setting_bloglevel 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_bloglevel
  27   */
  28  class adminpresets_admin_setting_bloglevel_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 int $settingvalue Setting value to be saved.
  37       * @param bool $expectedsaved Whether the setting will be saved or not.
  38       */
  39      public function test_save_value(int $settingvalue, bool $expectedsaved): void {
  40          global $DB;
  41  
  42          $this->resetAfterTest();
  43  
  44          // Login as admin, to access all the settings.
  45          $this->setAdminUser();
  46  
  47          // Set the config values (to confirm they change after applying the preset).
  48          set_config('bloglevel', BLOG_SITE_LEVEL); // All site users can see all blog entries.
  49  
  50          // Get the setting and save the value.
  51          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  52          $setting = $generator->get_admin_preset_setting('blog', 'bloglevel');
  53          $result = $setting->save_value(false, $settingvalue);
  54  
  55          // Check the result is the expected (saved when it has a different value and ignored when the value is the same).
  56          if ($expectedsaved) {
  57              $this->assertCount(1, $DB->get_records('config_log', ['id' => $result]));
  58              // Specific from the save_value in adminpresets_admin_setting_bloglevel.
  59              if ($settingvalue != 0) {
  60                  $this->assertTrue((bool) $DB->get_field('block', 'visible', ['name' => 'blog_menu']));
  61              } else {
  62                  $this->assertFalse((bool) $DB->get_field('block', 'visible', ['name' => 'blog_menu']));
  63              }
  64          } else {
  65              $this->assertFalse($result);
  66          }
  67          $this->assertEquals($settingvalue, get_config('core', 'bloglevel'));
  68      }
  69  
  70      /**
  71       * Data provider for test_save_value().
  72       *
  73       * @return array
  74       */
  75      public function save_value_provider(): array {
  76          return [
  77              'Save the bloglevel and set blog_menu block visibility to true' => [
  78                  'setttingvalue' => BLOG_USER_LEVEL,
  79                  'expectedsaved' => true,
  80              ],
  81              'Same value to bloglevel, so it will not be saved' => [
  82                  'setttingvalue' => BLOG_SITE_LEVEL,
  83                  'expectedsaved' => false,
  84              ],
  85              'Save the bloglevel and set blog_menu block visibility to false' => [
  86                  'setttingvalue' => 0,
  87                  'expectedsaved' => true,
  88              ],
  89          ];
  90      }
  91  
  92  }