[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/tests/ -> search_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   * Assign search unit tests.
  19   *
  20   * @package     mod_assign
  21   * @category    test
  22   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
  30  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  31  
  32  /**
  33   * Provides the unit tests for forum search.
  34   *
  35   * @package     mod_assign
  36   * @category    test
  37   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class mod_assign_search_testcase extends advanced_testcase {
  41  
  42      /**
  43       * @var string Area id
  44       */
  45      protected $assignareaid = null;
  46  
  47      public function setUp() {
  48          $this->resetAfterTest(true);
  49          set_config('enableglobalsearch', true);
  50  
  51          $this->assignareaid = \core_search\manager::generate_areaid('mod_assign', 'activity');
  52  
  53          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  54          $search = testable_core_search::instance();
  55      }
  56  
  57      /**
  58       * Test for assign file attachments.
  59       *
  60       * @return void
  61       */
  62      public function test_attach_files() {
  63          global $USER;
  64  
  65          $this->setAdminUser();
  66          // Setup test data.
  67          $course = $this->getDataGenerator()->create_course();
  68  
  69          $fs = get_file_storage();
  70          $usercontext = context_user::instance($USER->id);
  71  
  72          $record = new stdClass();
  73          $record->course = $course->id;
  74  
  75          $assign = $this->getDataGenerator()->create_module('assign', $record);
  76          $context = context_module::instance($assign->cmid);
  77  
  78          // Attach the main file. We put them in the draft area, create_module will move them.
  79          $filerecord = array(
  80              'contextid' => $context->id,
  81              'component' => 'mod_assign',
  82              'filearea'  => ASSIGN_INTROATTACHMENT_FILEAREA,
  83              'itemid'    => 0,
  84              'filepath'  => '/'
  85          );
  86  
  87          // Attach 4 files.
  88          for ($i = 1; $i <= 4; $i++) {
  89              $filerecord['filename'] = 'myfile'.$i;
  90              $fs->create_file_from_string($filerecord, 'Test assign file '.$i);
  91          }
  92  
  93          // And a fifth in a sub-folder.
  94          $filerecord['filename'] = 'myfile5';
  95          $filerecord['filepath'] = '/subfolder/';
  96          $fs->create_file_from_string($filerecord, 'Test assign file 5');
  97  
  98          // Returns the instance as long as the area is supported.
  99          $searcharea = \core_search\manager::get_search_area($this->assignareaid);
 100          $this->assertInstanceOf('\mod_assign\search\activity', $searcharea);
 101  
 102          $recordset = $searcharea->get_recordset_by_timestamp(0);
 103          $nrecords = 0;
 104          foreach ($recordset as $record) {
 105              $doc = $searcharea->get_document($record);
 106              $searcharea->attach_files($doc);
 107              $files = $doc->get_files();
 108  
 109              // Assign should return all files attached.
 110              $this->assertCount(5, $files);
 111  
 112              // We don't know the order, so get all the names, then sort, then check.
 113              $filenames = array();
 114              foreach ($files as $file) {
 115                  $filenames[] = $file->get_filename();
 116              }
 117              sort($filenames);
 118  
 119              for ($i = 1; $i <= 5; $i++) {
 120                  $this->assertEquals('myfile'.$i, $filenames[($i - 1)]);
 121              }
 122  
 123              $nrecords++;
 124          }
 125  
 126          // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
 127          $recordset->close();
 128          $this->assertEquals(1, $nrecords);
 129      }
 130  
 131  }


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