Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [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  namespace block_myoverview;
  18  
  19  /**
  20   * Online users testcase
  21   *
  22   * @package    block_myoverview
  23   * @category   test
  24   * @copyright  2019 Juan Leyva <juan@moodle.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class myoverview_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test getting block configuration
  31       */
  32      public function test_get_block_config_for_external() {
  33          global $PAGE, $CFG, $OUTPUT;
  34          require_once($CFG->dirroot . '/my/lib.php');
  35  
  36          $this->resetAfterTest(true);
  37  
  38          $user = $this->getDataGenerator()->create_user();
  39  
  40          $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']);
  41  
  42          $customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text',
  43              'categoryid' => $fieldcategory->get('id')];
  44          $field = self::getDataGenerator()->create_custom_field($customfield);
  45  
  46          $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value I'];
  47          $course1  = self::getDataGenerator()->create_course(['customfields' => [$customfieldvalue]]);
  48          $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value II'];
  49          $course2  = self::getDataGenerator()->create_course(['customfields' => [$customfieldvalue]]);
  50          $this->getDataGenerator()->enrol_user($user->id, $course1->id, 'student');
  51          $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
  52  
  53          // Force a setting change to check the returned blocks settings.
  54          set_config('displaygroupingcustomfield', 1, 'block_myoverview');
  55          set_config('customfiltergrouping', $field->get('shortname'), 'block_myoverview');
  56  
  57          $this->setUser($user);
  58          $context = \context_user::instance($user->id);
  59  
  60          if (!$currentpage = my_get_page($user->id, MY_PAGE_PUBLIC, MY_PAGE_COURSES)) {
  61              throw new \moodle_exception('mymoodlesetup');
  62          }
  63  
  64          $PAGE->set_url('/my/courses.php');    // Need this because some internal API calls require the $PAGE url to be set.
  65          $PAGE->set_context($context);
  66          $PAGE->set_pagelayout('mydashboard');
  67          $PAGE->set_pagetype('my-index');
  68          $PAGE->blocks->add_region('content');   // Need to add this special region to retrieve the central blocks.
  69          $PAGE->set_subpage($currentpage->id);
  70  
  71          // Load the block instances for all the regions.
  72          $PAGE->blocks->load_blocks();
  73          $PAGE->blocks->create_all_block_instances();
  74  
  75          $blocks = $PAGE->blocks->get_content_for_all_regions($OUTPUT);
  76          $configs = null;
  77          foreach ($blocks as $region => $regionblocks) {
  78              $regioninstances = $PAGE->blocks->get_blocks_for_region($region);
  79  
  80              foreach ($regioninstances as $ri) {
  81                  // Look for myoverview block only.
  82                  if ($ri->instance->blockname == 'myoverview') {
  83                      $configs = $ri->get_config_for_external();
  84                      break 2;
  85                  }
  86              }
  87          }
  88  
  89          // Test we receive all we expect (exact number and values of settings).
  90          $this->assertNotEmpty($configs);
  91          $this->assertEmpty((array) $configs->instance);
  92          $this->assertCount(13, (array) $configs->plugin);
  93          $this->assertEquals('test', $configs->plugin->customfiltergrouping);
  94          // Test default values.
  95          $this->assertEquals(1, $configs->plugin->displaycategories);
  96          $this->assertEquals(1, $configs->plugin->displaygroupingall);
  97          $this->assertEquals(0, $configs->plugin->displaygroupingallincludinghidden);
  98          $this->assertEquals(1, $configs->plugin->displaygroupingcustomfield);
  99          $this->assertEquals(1, $configs->plugin->displaygroupingfuture);
 100          $this->assertEquals(1, $configs->plugin->displaygroupinghidden);
 101          $this->assertEquals(1, $configs->plugin->displaygroupinginprogress);
 102          $this->assertEquals(1, $configs->plugin->displaygroupingpast);
 103          $this->assertEquals(1, $configs->plugin->displaygroupingfavourites);
 104          $this->assertEquals('card,list,summary', $configs->plugin->layouts);
 105          $this->assertEquals(get_config('block_myoverview', 'version'), $configs->plugin->version);
 106          // Test custom fields.
 107          $this->assertJson($configs->plugin->customfieldsexport);
 108          $fields = json_decode($configs->plugin->customfieldsexport);
 109          $this->assertEquals('Test value I', $fields[0]->name);
 110          $this->assertEquals('Test value I', $fields[0]->value);
 111          $this->assertFalse($fields[0]->active);
 112          $this->assertEquals('Test value II', $fields[1]->name);
 113          $this->assertEquals('Test value II', $fields[1]->value);
 114          $this->assertFalse($fields[1]->active);
 115          $this->assertEquals('No Custom field', $fields[2]->name);
 116          $this->assertFalse($fields[2]->active);
 117      }
 118  }