Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * PHPUnit data generator tests.
  19   *
  20   * @package mod_resource
  21   * @category phpunit
  22   * @copyright 2013 The Open University
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * PHPUnit data generator testcase.
  31   *
  32   * @package    mod_resource
  33   * @category phpunit
  34   * @covers \mod_resource_generator
  35   * @copyright 2013 The Open University
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class mod_resource_generator_testcase extends advanced_testcase {
  39  
  40      public function test_generator() {
  41          global $DB, $SITE;
  42  
  43          $this->resetAfterTest(true);
  44  
  45          // Must be a non-guest user to create resources.
  46          $this->setAdminUser();
  47  
  48          // There are 0 resources initially.
  49          $this->assertEquals(0, $DB->count_records('resource'));
  50  
  51          // Create the generator object and do standard checks.
  52          $generator = $this->getDataGenerator()->get_plugin_generator('mod_resource');
  53          $this->assertInstanceOf('mod_resource_generator', $generator);
  54          $this->assertEquals('resource', $generator->get_modulename());
  55  
  56          // Create three instances in the site course.
  57          $generator->create_instance(array('course' => $SITE->id));
  58          $generator->create_instance(array('course' => $SITE->id));
  59          $resource = $generator->create_instance(array('course' => $SITE->id));
  60          $this->assertEquals(3, $DB->count_records('resource'));
  61  
  62          // Check the course-module is correct.
  63          $cm = get_coursemodule_from_instance('resource', $resource->id);
  64          $this->assertEquals($resource->id, $cm->instance);
  65          $this->assertEquals('resource', $cm->modname);
  66          $this->assertEquals($SITE->id, $cm->course);
  67  
  68          // Check the context is correct.
  69          $context = context_module::instance($cm->id);
  70          $this->assertEquals($resource->cmid, $context->instanceid);
  71  
  72          // Check that generated resource module contains a file.
  73          $fs = get_file_storage();
  74          $files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false);
  75          $file = array_values($files)[0];
  76          $this->assertCount(1, $files);
  77          $this->assertEquals('resource3.txt', $file->get_filename());
  78          $this->assertEquals('Test resource resource3.txt file', $file->get_content());
  79  
  80          // Create a new resource specifying the file name.
  81          $resource = $generator->create_instance(['course' => $SITE->id, 'defaultfilename' => 'myfile.pdf']);
  82  
  83          // Check that generated resource module contains a file with the specified name.
  84          $cm = get_coursemodule_from_instance('resource', $resource->id);
  85          $context = \context_module::instance($cm->id);
  86          $files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false);
  87          $file = array_values($files)[0];
  88          $this->assertCount(1, $files);
  89          $this->assertEquals('myfile.pdf', $file->get_filename());
  90          $this->assertEquals('Test resource myfile.pdf file', $file->get_content());
  91  
  92          // Create a new resource uploading a file.
  93          $resource = $generator->create_instance([
  94              'course' => $SITE->id,
  95              'uploaded' => true,
  96              'defaultfilename' => 'mod/resource/tests/fixtures/samplefile.txt',
  97          ]);
  98  
  99          // Check that generated resource module contains the uploaded samplefile.txt.
 100          $cm = get_coursemodule_from_instance('resource', $resource->id);
 101          $context = \context_module::instance($cm->id);
 102          $files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false);
 103          $file = array_values($files)[0];
 104          $this->assertCount(1, $files);
 105          $this->assertEquals('samplefile.txt', $file->get_filename());
 106          $this->assertEquals('Hello!', $file->get_content());
 107  
 108          // Try to generate a resource with uploaded file without specifying the file.
 109          try {
 110              $resource = $generator->create_instance([
 111                  'course' => $SITE->id,
 112                  'uploaded' => true,
 113              ]);
 114              $this->assertTrue(false, 'coding_exception expected, defaultfilename is required');
 115          } catch (\Exception $e) {
 116              $this->assertInstanceOf(\coding_exception::class, $e);
 117              $this->assertStringContainsString('defaultfilename option is required', $e->getMessage());
 118          }
 119  
 120          // Try to generate a resource with uploaded file pointing to non-existing file.
 121          try {
 122              $resource = $generator->create_instance([
 123                  'course' => $SITE->id,
 124                  'uploaded' => true,
 125                  'defaultfilename' => 'mod/resource/tests/fixtures/doesnotexist.txt',
 126              ]);
 127              $this->assertTrue(false, 'coding_exception expected, defaultfilename must point to an existing file');
 128          } catch (\Exception $e) {
 129              $this->assertInstanceOf(\coding_exception::class, $e);
 130              $this->assertStringContainsString('defaultfilename option must point to an existing file', $e->getMessage());
 131          }
 132      }
 133  }