Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Resource search unit tests.
19
 *
20
 * @package     mod_resource
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_resource\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_resource
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
 */
1441 ariadna 41
final class search_test extends \advanced_testcase {
1 efrain 42
 
43
    /**
44
     * @var string Area id
45
     */
46
    protected $resourceareaid = null;
47
 
48
    public function setUp(): void {
1441 ariadna 49
        parent::setUp();
1 efrain 50
        $this->resetAfterTest(true);
51
        set_config('enableglobalsearch', true);
52
 
53
        $this->resourceareaid = \core_search\manager::generate_areaid('mod_resource', '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
 
59
    /**
60
     * Test for resource file attachments.
61
     *
62
     * @return void
63
     */
11 efrain 64
    public function test_attach_files(): void {
1 efrain 65
        global $USER;
66
 
67
        $this->setAdminUser();
68
        // Setup test data.
69
        $course = $this->getDataGenerator()->create_course();
70
 
71
        $fs = get_file_storage();
72
        $usercontext = \context_user::instance($USER->id);
73
 
74
        $record = new \stdClass();
75
        $record->course = $course->id;
76
        $record->files = file_get_unused_draft_itemid();
77
 
78
        // Attach the main file. We put them in the draft area, create_module will move them.
79
        $filerecord = array(
80
            'contextid' => $usercontext->id,
81
            'component' => 'user',
82
            'filearea'  => 'draft',
83
            'itemid'    => $record->files,
84
            'filepath'  => '/',
85
            'filename'  => 'mainfile',
86
            'sortorder' => 1
87
        );
88
        $fs->create_file_from_string($filerecord, 'Test resource file');
89
 
90
        // Attach a second file.
91
        $filerecord['filename'] = 'extrafile';
92
        $filerecord['sortorder'] = 0;
93
        $fs->create_file_from_string($filerecord, 'Test resource file 2');
94
 
95
        $resource = $this->getDataGenerator()->create_module('resource', $record);
96
 
97
        $searcharea = \core_search\manager::get_search_area($this->resourceareaid);
98
        $this->assertInstanceOf('\mod_resource\search\activity', $searcharea);
99
 
100
        $recordset = $searcharea->get_recordset_by_timestamp(0);
101
        $nrecords = 0;
102
        foreach ($recordset as $record) {
103
            $doc = $searcharea->get_document($record);
104
            $searcharea->attach_files($doc);
105
            $files = $doc->get_files();
106
 
107
            // Resources should return all added files.
108
            $this->assertCount(2, $files);
109
 
110
            $filenames = array();
111
            foreach ($files as $file) {
112
                $filenames[] = $file->get_filename();
113
            }
114
            $this->assertContains('mainfile', $filenames);
115
            $this->assertContains('extrafile', $filenames);
116
 
117
            $nrecords++;
118
        }
119
 
120
        $recordset->close();
121
        $this->assertEquals(1, $nrecords);
122
    }
123
 
124
}