Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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_survey;
  18  
  19  /**
  20   * Genarator tests class for mod_survey.
  21   *
  22   * @package    mod_survey
  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('survey', array('course' => $course->id)));
  37          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
  38          $records = $DB->get_records('survey', array('course' => $course->id), 'id');
  39          $this->assertEquals(1, count($records));
  40          $this->assertTrue(array_key_exists($survey->id, $records));
  41  
  42          $params = array('course' => $course->id, 'name' => 'Another survey');
  43          $survey = $this->getDataGenerator()->create_module('survey', $params);
  44          $records = $DB->get_records('survey', array('course' => $course->id), 'id');
  45          $this->assertEquals(2, count($records));
  46          $this->assertEquals('Another survey', $records[$survey->id]->name);
  47      }
  48  
  49      public function test_create_instance_with_template() {
  50          global $DB;
  51          $this->resetAfterTest();
  52          $this->setAdminUser();
  53  
  54          $course = $this->getDataGenerator()->create_course();
  55          $templates = $DB->get_records_menu('survey', array('template' => 0), 'name', 'id, name');
  56          $firsttemplateid = key($templates);
  57  
  58          // By default survey is created with the first available template.
  59          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
  60          $record = $DB->get_record('survey', array('id' => $survey->id));
  61          $this->assertEquals($firsttemplateid, $record->template);
  62  
  63          // Survey can be created specifying the template id.
  64          $tmplid = array_search('ciqname', $templates);
  65          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
  66              'template' => $tmplid));
  67          $record = $DB->get_record('survey', array('id' => $survey->id));
  68          $this->assertEquals($tmplid, $record->template);
  69  
  70          // Survey can be created specifying the template name instead of id.
  71          $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
  72              'template' => 'collesaname'));
  73          $record = $DB->get_record('survey', array('id' => $survey->id));
  74          $this->assertEquals(array_search('collesaname', $templates), $record->template);
  75  
  76          // Survey can not be created specifying non-existing template id or name.
  77          try {
  78              $this->getDataGenerator()->create_module('survey', array('course' => $course,
  79                  'template' => 87654));
  80              $this->fail('Exception about non-existing numeric template is expected');
  81          } catch (\Exception $e) {}
  82          try {
  83              $this->getDataGenerator()->create_module('survey', array('course' => $course,
  84                  'template' => 'nonexistingcode'));
  85              $this->fail('Exception about non-existing string template is expected');
  86          } catch (\Exception $e) {}
  87      }
  88  }