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.

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

   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_wiki;
  18  
  19  /**
  20   * Genarator tests class for mod_wiki.
  21   *
  22   * @package    mod_wiki
  23   * @category   test
  24   * @copyright  2013 Marina Glancy
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class generator_test extends \advanced_testcase {
  28  
  29      public function test_create_instance() {
  30          global $DB;
  31          $this->resetAfterTest();
  32          $this->setAdminUser();
  33  
  34          $course = $this->getDataGenerator()->create_course();
  35  
  36          $this->assertFalse($DB->record_exists('wiki', array('course' => $course->id)));
  37          $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course));
  38          $records = $DB->get_records('wiki', array('course' => $course->id), 'id');
  39          $this->assertEquals(1, count($records));
  40          $this->assertTrue(array_key_exists($wiki->id, $records));
  41  
  42          $params = array('course' => $course->id, 'name' => 'Another wiki');
  43          $wiki = $this->getDataGenerator()->create_module('wiki', $params);
  44          $records = $DB->get_records('wiki', array('course' => $course->id), 'id');
  45          $this->assertEquals(2, count($records));
  46          $this->assertEquals('Another wiki', $records[$wiki->id]->name);
  47      }
  48  
  49      public function test_create_content() {
  50          global $DB;
  51          $this->resetAfterTest();
  52          $this->setAdminUser();
  53  
  54          $course = $this->getDataGenerator()->create_course();
  55          $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course));
  56          $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
  57  
  58          $page1 = $wikigenerator->create_first_page($wiki);
  59          $page2 = $wikigenerator->create_content($wiki);
  60          $page3 = $wikigenerator->create_content($wiki, array('title' => 'Custom title', 'tags' => array('Cats', 'mice')));
  61          unset($wiki->cmid);
  62          $page4 = $wikigenerator->create_content($wiki, array('tags' => 'Cats, dogs'));
  63          $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
  64          $this->assertEquals(1, count($subwikis));
  65          $subwikiid = key($subwikis);
  66          $records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
  67          $this->assertEquals(4, count($records));
  68          $this->assertEquals($page1->id, $records[$page1->id]->id);
  69          $this->assertEquals($page2->id, $records[$page2->id]->id);
  70          $this->assertEquals($page3->id, $records[$page3->id]->id);
  71          $this->assertEquals('Custom title', $records[$page3->id]->title);
  72          $this->assertEquals(array('Cats', 'mice'),
  73                  array_values(\core_tag_tag::get_item_tags_array('mod_wiki', 'wiki_pages', $page3->id)));
  74          $this->assertEquals(array('Cats', 'dogs'),
  75                  array_values(\core_tag_tag::get_item_tags_array('mod_wiki', 'wiki_pages', $page4->id)));
  76      }
  77  
  78      public function test_create_content_individual() {
  79          global $DB;
  80          $this->resetAfterTest();
  81          $this->setAdminUser();
  82  
  83          $course = $this->getDataGenerator()->create_course();
  84          $wiki = $this->getDataGenerator()->create_module('wiki',
  85                  array('course' => $course, 'wikimode' => 'individual'));
  86          $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
  87  
  88          $page1 = $wikigenerator->create_first_page($wiki);
  89          $page2 = $wikigenerator->create_content($wiki);
  90          $page3 = $wikigenerator->create_content($wiki, array('title' => 'Custom title for admin'));
  91          $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
  92          $this->assertEquals(1, count($subwikis));
  93          $subwikiid = key($subwikis);
  94          $records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
  95          $this->assertEquals(3, count($records));
  96          $this->assertEquals($page1->id, $records[$page1->id]->id);
  97          $this->assertEquals($page2->id, $records[$page2->id]->id);
  98          $this->assertEquals($page3->id, $records[$page3->id]->id);
  99          $this->assertEquals('Custom title for admin', $records[$page3->id]->title);
 100  
 101          $user = $this->getDataGenerator()->create_user();
 102          $role = $DB->get_record('role', array('shortname' => 'student'));
 103          $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
 104          $this->setUser($user);
 105  
 106          $page1s = $wikigenerator->create_first_page($wiki);
 107          $page2s = $wikigenerator->create_content($wiki);
 108          $page3s = $wikigenerator->create_content($wiki, array('title' => 'Custom title for student'));
 109          $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
 110          $this->assertEquals(2, count($subwikis));
 111          next($subwikis);
 112          $subwikiid = key($subwikis);
 113          $records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
 114          $this->assertEquals(3, count($records));
 115          $this->assertEquals($page1s->id, $records[$page1s->id]->id);
 116          $this->assertEquals($page2s->id, $records[$page2s->id]->id);
 117          $this->assertEquals($page3s->id, $records[$page3s->id]->id);
 118          $this->assertEquals('Custom title for student', $records[$page3s->id]->title);
 119      }
 120  }