Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  18   * Concept fetching and caching tests.
  19   *
  20   * @package    mod_glossary
  21   * @category   test
  22   * @copyright  2014 Petr Skoda
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * Concept fetching and caching tests.
  28   *
  29   * @package    mod_glossary
  30   * @category   test
  31   * @copyright  2014 Petr Skoda
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class mod_glossary_concept_cache_testcase extends advanced_testcase {
  35      /**
  36       * Test convect fetching.
  37       */
  38      public function test_concept_fetching() {
  39          global $CFG, $DB;
  40          $this->resetAfterTest(true);
  41          $this->setAdminUser();
  42  
  43          $CFG->glossary_linkbydefault = 1;
  44          $CFG->glossary_linkentries = 0;
  45  
  46          // Create a test courses.
  47          $course1 = $this->getDataGenerator()->create_course();
  48          $course2 = $this->getDataGenerator()->create_course();
  49          $site = $DB->get_record('course', array('id' => SITEID));
  50  
  51          // Create a glossary.
  52          $glossary1a = $this->getDataGenerator()->create_module('glossary',
  53              array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1));
  54          $glossary1b = $this->getDataGenerator()->create_module('glossary',
  55              array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1));
  56          $glossary1c = $this->getDataGenerator()->create_module('glossary',
  57              array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 0));
  58          $glossary2 = $this->getDataGenerator()->create_module('glossary',
  59              array('course' => $course2->id, 'mainglossary' => 1, 'usedynalink' => 1));
  60          $glossary3 = $this->getDataGenerator()->create_module('glossary',
  61              array('course' => $site->id, 'mainglossary' => 1, 'usedynalink' => 1, 'globalglossary' => 1));
  62  
  63          /** @var mod_glossary_generator $generator */
  64          $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
  65          $entry1a1 = $generator->create_content($glossary1a, array('concept' => 'first', 'usedynalink' => 1), array('prvni', 'erste'));
  66          $entry1a2 = $generator->create_content($glossary1a, array('concept' => 'A&B', 'usedynalink' => 1));
  67          $entry1a3 = $generator->create_content($glossary1a, array('concept' => 'neee', 'usedynalink' => 0));
  68          $entry1b1 = $generator->create_content($glossary1b, array('concept' => 'second', 'usedynalink' => 1));
  69          $entry1c1 = $generator->create_content($glossary1c, array('concept' => 'third', 'usedynalink' => 1));
  70          $entry31 = $generator->create_content($glossary3, array('concept' => 'global', 'usedynalink' => 1), array('globalni'));
  71  
  72          $cat1 = $generator->create_category($glossary1a, array('name' => 'special'), array($entry1a1, $entry1a2));
  73  
  74          \mod_glossary\local\concept_cache::reset_caches();
  75  
  76          $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
  77          $this->assertCount(3, $concepts1[0]);
  78          $this->arrayHasKey($concepts1[0], $glossary1a->id);
  79          $this->arrayHasKey($concepts1[0], $glossary1b->id);
  80          $this->arrayHasKey($concepts1[0], $glossary3->id);
  81          $this->assertCount(3, $concepts1[1]);
  82          $this->arrayHasKey($concepts1[1], $glossary1a->id);
  83          $this->arrayHasKey($concepts1[1], $glossary1b->id);
  84          $this->arrayHasKey($concepts1[0], $glossary3->id);
  85          $this->assertCount(5, $concepts1[1][$glossary1a->id]);
  86          foreach($concepts1[1][$glossary1a->id] as $concept) {
  87              $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array)$concept));
  88              if ($concept->concept === 'first') {
  89                  $this->assertEquals($entry1a1->id, $concept->id);
  90                  $this->assertEquals($glossary1a->id, $concept->glossaryid);
  91                  $this->assertEquals(0, $concept->category);
  92              } else if ($concept->concept === 'prvni') {
  93                  $this->assertEquals($entry1a1->id, $concept->id);
  94                  $this->assertEquals($glossary1a->id, $concept->glossaryid);
  95                  $this->assertEquals(0, $concept->category);
  96              } else if ($concept->concept === 'erste') {
  97                  $this->assertEquals($entry1a1->id, $concept->id);
  98                  $this->assertEquals($glossary1a->id, $concept->glossaryid);
  99                  $this->assertEquals(0, $concept->category);
 100              } else if ($concept->concept === 'A&amp;B') {
 101                  $this->assertEquals($entry1a2->id, $concept->id);
 102                  $this->assertEquals($glossary1a->id, $concept->glossaryid);
 103                  $this->assertEquals(0, $concept->category);
 104              } else if ($concept->concept === 'special') {
 105                  $this->assertEquals($cat1->id, $concept->id);
 106                  $this->assertEquals($glossary1a->id, $concept->glossaryid);
 107                  $this->assertEquals(1, $concept->category);
 108              } else {
 109                  $this->fail('Unexpected concept: ' . $concept->concept);
 110              }
 111          }
 112          $this->assertCount(1, $concepts1[1][$glossary1b->id]);
 113          foreach($concepts1[1][$glossary1b->id] as $concept) {
 114              $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array)$concept));
 115              if ($concept->concept === 'second') {
 116                  $this->assertEquals($entry1b1->id, $concept->id);
 117                  $this->assertEquals($glossary1b->id, $concept->glossaryid);
 118                  $this->assertEquals(0, $concept->category);
 119              } else {
 120                  $this->fail('Unexpected concept: ' . $concept->concept);
 121              }
 122          }
 123          $this->assertCount(2, $concepts1[1][$glossary3->id]);
 124          foreach($concepts1[1][$glossary3->id] as $concept) {
 125              $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array)$concept));
 126              if ($concept->concept === 'global') {
 127                  $this->assertEquals($entry31->id, $concept->id);
 128                  $this->assertEquals($glossary3->id, $concept->glossaryid);
 129                  $this->assertEquals(0, $concept->category);
 130              } else if ($concept->concept === 'globalni') {
 131                  $this->assertEquals($entry31->id, $concept->id);
 132                  $this->assertEquals($glossary3->id, $concept->glossaryid);
 133                  $this->assertEquals(0, $concept->category);
 134              } else {
 135                  $this->fail('Unexpected concept: ' . $concept->concept);
 136              }
 137          }
 138  
 139          $concepts3 = \mod_glossary\local\concept_cache::get_concepts($site->id);
 140          $this->assertCount(1, $concepts3[0]);
 141          $this->arrayHasKey($concepts3[0], $glossary3->id);
 142          $this->assertCount(1, $concepts3[1]);
 143          $this->arrayHasKey($concepts3[0], $glossary3->id);
 144          foreach($concepts3[1][$glossary3->id] as $concept) {
 145              $this->assertSame(array('id', 'glossaryid', 'concept', 'casesensitive', 'category', 'fullmatch'), array_keys((array)$concept));
 146              if ($concept->concept === 'global') {
 147                  $this->assertEquals($entry31->id, $concept->id);
 148                  $this->assertEquals($glossary3->id, $concept->glossaryid);
 149                  $this->assertEquals(0, $concept->category);
 150              } else if ($concept->concept === 'globalni') {
 151                  $this->assertEquals($entry31->id, $concept->id);
 152                  $this->assertEquals($glossary3->id, $concept->glossaryid);
 153                  $this->assertEquals(0, $concept->category);
 154              } else {
 155                  $this->fail('Unexpected concept: ' . $concept->concept);
 156              }
 157          }
 158  
 159          $concepts2 = \mod_glossary\local\concept_cache::get_concepts($course2->id);
 160          $this->assertEquals($concepts3, $concepts2);
 161  
 162          // Test uservisible flag.
 163          set_config('enableavailability', 1);
 164          $glossary1d = $this->getDataGenerator()->create_module('glossary',
 165                  array('course' => $course1->id, 'mainglossary' => 1, 'usedynalink' => 1,
 166                  'availability' => json_encode(\core_availability\tree::get_root_json(
 167                          array(\availability_group\condition::get_json())))));
 168          $entry1d1 = $generator->create_content($glossary1d, array('concept' => 'membersonly', 'usedynalink' => 1));
 169          $user = $this->getDataGenerator()->create_user();
 170          $this->getDataGenerator()->enrol_user($user->id, $course1->id);
 171          $this->getDataGenerator()->enrol_user($user->id, $course2->id);
 172          \mod_glossary\local\concept_cache::reset_caches();
 173          $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
 174          $this->assertCount(4, $concepts1[0]);
 175          $this->assertCount(4, $concepts1[1]);
 176          $this->setUser($user);
 177          course_modinfo::clear_instance_cache();
 178          \mod_glossary\local\concept_cache::reset_caches();
 179          $concepts1 = \mod_glossary\local\concept_cache::get_concepts($course1->id);
 180          $this->assertCount(3, $concepts1[0]);
 181          $this->assertCount(3, $concepts1[1]);
 182      }
 183  }