[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/submission/file/tests/ -> locallib_test.php (source)

   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/file/locallib.php
  19   *
  20   * @package   assignsubmission_file
  21   * @copyright 2016 Cameron Ball
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
  29  
  30  /**
  31   * Unit tests for mod/assign/submission/file/locallib.php
  32   *
  33   * @copyright  2016 Cameron Ball
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class assignsubmission_file_locallib_testcase extends advanced_testcase {
  37  
  38      /** @var stdClass $user A user to submit an assignment. */
  39      protected $user;
  40  
  41      /** @var stdClass $course New course created to hold the assignment activity. */
  42      protected $course;
  43  
  44      /** @var stdClass $cm A context module object. */
  45      protected $cm;
  46  
  47      /** @var stdClass $context Context of the assignment activity. */
  48      protected $context;
  49  
  50      /** @var stdClass $assign The assignment object. */
  51      protected $assign;
  52  
  53      /**
  54       * Setup all the various parts of an assignment activity including creating an onlinetext submission.
  55       */
  56      protected function setUp() {
  57          $this->user = $this->getDataGenerator()->create_user();
  58          $this->course = $this->getDataGenerator()->create_course();
  59          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  60          $params = [
  61              'course' => $this->course->id,
  62              'assignsubmission_file_enabled' => 1,
  63              'assignsubmission_file_maxfiles' => 12,
  64              'assignsubmission_file_maxsizebytes' => 10,
  65          ];
  66          $instance = $generator->create_instance($params);
  67          $this->cm = get_coursemodule_from_instance('assign', $instance->id);
  68          $this->context = context_module::instance($this->cm->id);
  69          $this->assign = new testable_assign($this->context, $this->cm, $this->course);
  70          $this->setUser($this->user->id);
  71      }
  72  
  73      /**
  74       * Test submission_is_empty
  75       *
  76       * @dataProvider submission_is_empty_testcases
  77       * @param string $data The file submission data
  78       * @param bool $expected The expected return value
  79       */
  80      public function test_submission_is_empty($data, $expected) {
  81          $this->resetAfterTest();
  82  
  83          $itemid = file_get_unused_draft_itemid();
  84          $submission = (object)['files_filemanager' => $itemid];
  85          $plugin = $this->assign->get_submission_plugin_by_type('file');
  86  
  87          if ($data) {
  88              $data += ['contextid' => context_user::instance($this->user->id)->id, 'itemid' => $itemid];
  89              $fs = get_file_storage();
  90              $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
  91          }
  92  
  93          $result = $plugin->submission_is_empty($submission);
  94          $this->assertTrue($result === $expected);
  95      }
  96  
  97      /**
  98       * Test new_submission_empty
  99       *
 100       * @dataProvider submission_is_empty_testcases
 101       * @param string $data The file submission data
 102       * @param bool $expected The expected return value
 103       */
 104      public function test_new_submission_empty($data, $expected) {
 105          $this->resetAfterTest();
 106  
 107          $itemid = file_get_unused_draft_itemid();
 108          $submission = (object)['files_filemanager' => $itemid];
 109  
 110          if ($data) {
 111              $data += ['contextid' => context_user::instance($this->user->id)->id, 'itemid' => $itemid];
 112              $fs = get_file_storage();
 113              $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
 114          }
 115  
 116          $result = $this->assign->new_submission_empty($submission);
 117          $this->assertTrue($result === $expected);
 118      }
 119  
 120      /**
 121       * Dataprovider for the test_submission_is_empty testcase
 122       *
 123       * @return array of testcases
 124       */
 125      public function submission_is_empty_testcases() {
 126          return [
 127              'With file' => [
 128                  [
 129                      'component' => 'user',
 130                      'filearea' => 'draft',
 131                      'filepath' => '/',
 132                      'filename' => 'not_a_virus.exe'
 133                  ],
 134                  false
 135              ],
 136              'Without file' => [null, true]
 137          ];
 138      }
 139  
 140  
 141  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1