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.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

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