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
 * 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
 */
1441 ariadna 41
final class search_test extends \advanced_testcase {
1 efrain 42
 
43
    /**
44
     * @var string Area id
45
     */
46
    protected $folderareaid = 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->folderareaid = \core_search\manager::generate_areaid('mod_folder', '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 folder 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
        );
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 folder 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 folder file 5');
97
 
98
        $this->getDataGenerator()->create_module('folder', $record);
99
 
100
        // Returns the instance as long as the area is supported.
101
        $searcharea = \core_search\manager::get_search_area($this->folderareaid);
102
        $this->assertInstanceOf('\mod_folder\search\activity', $searcharea);
103
 
104
        $recordset = $searcharea->get_recordset_by_timestamp(0);
105
        $nrecords = 0;
106
        foreach ($recordset as $record) {
107
            $doc = $searcharea->get_document($record);
108
            $searcharea->attach_files($doc);
109
            $files = $doc->get_files();
110
 
111
            // Folder should return all files attached.
112
            $this->assertCount(5, $files);
113
 
114
            // We don't know the order, so get all the names, then sort, then check.
115
            $filenames = array();
116
            foreach ($files as $file) {
117
                $filenames[] = $file->get_filename();
118
            }
119
            sort($filenames);
120
 
121
            for ($i = 1; $i <= 5; $i++) {
122
                $this->assertEquals('myfile'.$i, $filenames[($i - 1)]);
123
            }
124
 
125
            $nrecords++;
126
        }
127
 
128
        // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
129
        $recordset->close();
130
        $this->assertEquals(1, $nrecords);
131
    }
132
 
133
}