2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Assign search unit tests.
22 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
. '/search/tests/fixtures/testable_core_search.php');
30 require_once($CFG->dirroot
. '/mod/assign/locallib.php');
33 * Provides the unit tests for forum search.
37 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class mod_assign_search_testcase
extends advanced_testcase
{
43 * Test for assign file attachments.
47 public function test_attach_files() {
50 $this->resetAfterTest(true);
51 set_config('enableglobalsearch', true);
53 $assignareaid = \core_search\manager
::generate_areaid('mod_assign', 'activity');
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();
58 $this->setAdminUser();
60 $course = $this->getDataGenerator()->create_course();
62 $fs = get_file_storage();
63 $usercontext = context_user
::instance($USER->id
);
65 $record = new stdClass();
66 $record->course
= $course->id
;
68 $assign = $this->getDataGenerator()->create_module('assign', $record);
69 $context = context_module
::instance($assign->cmid
);
71 // Attach the main file. We put them in the draft area, create_module will move them.
73 'contextid' => $context->id
,
74 'component' => 'mod_assign',
75 'filearea' => ASSIGN_INTROATTACHMENT_FILEAREA
,
81 for ($i = 1; $i <= 4; $i++
) {
82 $filerecord['filename'] = 'myfile'.$i;
83 $fs->create_file_from_string($filerecord, 'Test assign file '.$i);
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');
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);
95 $recordset = $searcharea->get_recordset_by_timestamp(0);
97 foreach ($recordset as $record) {
98 $doc = $searcharea->get_document($record);
99 $searcharea->attach_files($doc);
100 $files = $doc->get_files();
102 // Assign should return all files attached.
103 $this->assertCount(5, $files);
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();
112 for ($i = 1; $i <= 5; $i++
) {
113 $this->assertEquals('myfile'.$i, $filenames[($i - 1)]);
119 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
121 $this->assertEquals(1, $nrecords);