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