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 * Test for assign file attachments. 44 * 45 * @return void 46 */ 47 public function test_attach_files() { 48 global $USER; 49 50 $this->resetAfterTest(true); 51 set_config('enableglobalsearch', true); 52 53 $assignareaid = \core_search\manager::generate_areaid('mod_assign', 'activity'); 54 55 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this. 56 $search = testable_core_search::instance(); 57 58 $this->setAdminUser(); 59 // Setup test data. 60 $course = $this->getDataGenerator()->create_course(); 61 62 $fs = get_file_storage(); 63 $usercontext = context_user::instance($USER->id); 64 65 $record = new stdClass(); 66 $record->course = $course->id; 67 68 $assign = $this->getDataGenerator()->create_module('assign', $record); 69 $context = context_module::instance($assign->cmid); 70 71 // Attach the main file. We put them in the draft area, create_module will move them. 72 $filerecord = array( 73 'contextid' => $context->id, 74 'component' => 'mod_assign', 75 'filearea' => ASSIGN_INTROATTACHMENT_FILEAREA, 76 'itemid' => 0, 77 'filepath' => '/' 78 ); 79 80 // Attach 4 files. 81 for ($i = 1; $i <= 4; $i++) { 82 $filerecord['filename'] = 'myfile'.$i; 83 $fs->create_file_from_string($filerecord, 'Test assign file '.$i); 84 } 85 86 // And a fifth in a sub-folder. 87 $filerecord['filename'] = 'myfile5'; 88 $filerecord['filepath'] = '/subfolder/'; 89 $fs->create_file_from_string($filerecord, 'Test assign file 5'); 90 91 // Returns the instance as long as the area is supported. 92 $searcharea = \core_search\manager::get_search_area($assignareaid); 93 $this->assertInstanceOf('\mod_assign\search\activity', $searcharea); 94 95 $recordset = $searcharea->get_recordset_by_timestamp(0); 96 $nrecords = 0; 97 foreach ($recordset as $record) { 98 $doc = $searcharea->get_document($record); 99 $searcharea->attach_files($doc); 100 $files = $doc->get_files(); 101 102 // Assign should return all files attached. 103 $this->assertCount(5, $files); 104 105 // We don't know the order, so get all the names, then sort, then check. 106 $filenames = array(); 107 foreach ($files as $file) { 108 $filenames[] = $file->get_filename(); 109 } 110 sort($filenames); 111 112 for ($i = 1; $i <= 5; $i++) { 113 $this->assertEquals('myfile'.$i, $filenames[($i - 1)]); 114 } 115 116 $nrecords++; 117 } 118 119 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown. 120 $recordset->close(); 121 $this->assertEquals(1, $nrecords); 122 } 123 124 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body