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   * mod_glossary generator tests
  19   *
  20   * @package    mod_glossary
  21   * @category   test
  22   * @copyright  2013 Marina Glancy
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * Genarator tests class for mod_glossary.
  28   *
  29   * @package    mod_glossary
  30   * @category   test
  31   * @copyright  2013 Marina Glancy
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class mod_glossary_generator_testcase extends advanced_testcase {
  35  
  36      public function test_create_instance() {
  37          global $DB;
  38          $this->resetAfterTest();
  39          $this->setAdminUser();
  40  
  41          $course = $this->getDataGenerator()->create_course();
  42  
  43          $this->assertFalse($DB->record_exists('glossary', array('course' => $course->id)));
  44          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
  45          $records = $DB->get_records('glossary', array('course' => $course->id), 'id');
  46          $this->assertCount(1, $records);
  47          $this->assertTrue(array_key_exists($glossary->id, $records));
  48  
  49          $params = array('course' => $course->id, 'name' => 'Another glossary');
  50          $glossary = $this->getDataGenerator()->create_module('glossary', $params);
  51          $records = $DB->get_records('glossary', array('course' => $course->id), 'id');
  52          $this->assertCount(2, $records);
  53          $this->assertEquals('Another glossary', $records[$glossary->id]->name);
  54      }
  55  
  56      public function test_create_content() {
  57          global $DB;
  58          $this->resetAfterTest();
  59          $this->setAdminUser();
  60  
  61          $course = $this->getDataGenerator()->create_course();
  62          $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
  63          /** @var mod_glossary_generator $glossarygenerator */
  64          $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
  65  
  66          $entry1 = $glossarygenerator->create_content($glossary);
  67          $entry2 = $glossarygenerator->create_content($glossary,
  68              array('concept' => 'Custom concept', 'tags' => array('Cats', 'mice')), array('alias1', 'alias2'));
  69          $records = $DB->get_records('glossary_entries', array('glossaryid' => $glossary->id), 'id');
  70          $this->assertCount(2, $records);
  71          $this->assertEquals($entry1->id, $records[$entry1->id]->id);
  72          $this->assertEquals($entry2->id, $records[$entry2->id]->id);
  73          $this->assertEquals('Custom concept', $records[$entry2->id]->concept);
  74          $this->assertEquals(array('Cats', 'mice'),
  75              array_values(core_tag_tag::get_item_tags_array('mod_glossary', 'glossary_entries', $entry2->id)));
  76          $aliases = $DB->get_records_menu('glossary_alias', array('entryid' => $entry2->id), 'id ASC', 'id, alias');
  77          $this->assertSame(array('alias1', 'alias2'), array_values($aliases));
  78  
  79          // Test adding of category to entry.
  80          $categories = $DB->get_records('glossary_categories', array('glossaryid' => $glossary->id));
  81          $this->assertCount(0, $categories);
  82          $entry3 = $glossarygenerator->create_content($glossary, array('concept' => 'In category'));
  83          $category1 = $glossarygenerator->create_category($glossary, array());
  84          $categories = $DB->get_records('glossary_categories', array('glossaryid' => $glossary->id));
  85          $this->assertCount(1, $categories);
  86          $category2 = $glossarygenerator->create_category($glossary, array('name' => 'Some category'), array($entry2, $entry3));
  87          $categories = $DB->get_records('glossary_categories', array('glossaryid' => $glossary->id));
  88          $this->assertCount(2, $categories);
  89          $members = $DB->get_records_menu('glossary_entries_categories', array('categoryid' => $category2->id), 'id ASC', 'id, entryid');
  90          $this->assertSame(array($entry2->id, $entry3->id), array_values($members));
  91      }
  92  }