Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [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   * Tests for mod/assign/submission/onlinetext/locallib.php
  19   *
  20   * @package   assignsubmission_onlinetext
  21   * @copyright 2016 Cameron Ball
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace assignsubmission_onlinetext;
  25  
  26  use mod_assign_test_generator;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  32  
  33  /**
  34   * Unit tests for mod/assign/submission/onlinetext/locallib.php
  35   *
  36   * @copyright  2016 Cameron Ball
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class locallib_test extends \advanced_testcase {
  40  
  41      // Use the generator helper.
  42      use mod_assign_test_generator;
  43  
  44      /**
  45       * Test submission_is_empty
  46       *
  47       * @dataProvider submission_is_empty_testcases
  48       * @param string $submissiontext The online text submission text
  49       * @param bool $expected The expected return value
  50       */
  51      public function test_submission_is_empty($submissiontext, $expected) {
  52          $this->resetAfterTest();
  53  
  54          $course = $this->getDataGenerator()->create_course();
  55          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  56          $assign = $this->create_instance($course, [
  57                  'assignsubmission_onlinetext_enabled' => true,
  58              ]);
  59  
  60          $this->setUser($student->id);
  61  
  62          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
  63          $result = $plugin->submission_is_empty((object) [
  64                  'onlinetext_editor' => [
  65                      'text' => $submissiontext,
  66                  ],
  67              ]);
  68          $this->assertTrue($result === $expected);
  69      }
  70  
  71      /**
  72       * Test new_submission_empty
  73       *
  74       * @dataProvider submission_is_empty_testcases
  75       * @param string $submissiontext The file submission data
  76       * @param bool $expected The expected return value
  77       */
  78      public function test_new_submission_empty($submissiontext, $expected) {
  79          $this->resetAfterTest();
  80  
  81          $course = $this->getDataGenerator()->create_course();
  82          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  83          $assign = $this->create_instance($course, [
  84                  'assignsubmission_onlinetext_enabled' => true,
  85              ]);
  86  
  87          $this->setUser($student->id);
  88  
  89          $result = $assign->new_submission_empty((object) [
  90                  'onlinetext_editor' => [
  91                      'text' => $submissiontext,
  92                  ],
  93              ]);
  94  
  95          $this->assertTrue($result === $expected);
  96      }
  97  
  98      /**
  99       * Dataprovider for the test_submission_is_empty testcase
 100       *
 101       * @return array of testcases
 102       */
 103      public function submission_is_empty_testcases() {
 104          return [
 105              'Empty submission string' => ['', true],
 106              'Empty submission null' => [null, true],
 107              'Value 0' => [0, false],
 108              'String 0' => ['0', false],
 109              'Text' => ['Ai! laurië lantar lassi súrinen, yéni únótimë ve rámar aldaron!', false],
 110              'Image' => ['<img src="test.jpg" />', false],
 111              'Video' => ['<video controls="true"><source src="test.mp4"></video>', false],
 112              'Audio' => ['<audio controls="true"><source src="test.mp3"></audio>', false],
 113          ];
 114      }
 115  }