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
namespace mod_data;
17
 
18
use mod_data\external\record_exporter;
19
 
20
defined('MOODLE_INTERNAL') || die();
21
 
22
global $CFG;
23
require_once($CFG->dirroot . '/mod/data/locallib.php');
24
 
25
/**
26
 * Unit tests for locallib.php
27
 *
28
 * @package    mod_data
29
 * @copyright  2022 Laurent David <laurent.david@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
1441 ariadna 32
final class locallib_test extends \advanced_testcase {
1 efrain 33
 
34
    /**
35
     * Confirms that search is working
36
     * @covers ::data_search_entries
37
     */
11 efrain 38
    public function test_data_search_entries(): void {
1 efrain 39
        $this->resetAfterTest();
40
        $this->setAdminUser();
41
        $course = $this->getDataGenerator()->create_course();
42
        $record = new \stdClass();
43
        $record->course = $course->id;
44
        $record->name = "Mod data delete test";
45
        $record->intro = "Some intro of some sort";
46
 
47
        $module = $this->getDataGenerator()->create_module('data', $record);
48
        $titlefield = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field(
49
            (object) [
50
                'name' => 'title',
51
                'type' => 'text',
52
                'required' => 1
53
            ],
54
            $module);
55
        $captionfield = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field(
56
            (object) [
57
                'name' => 'caption',
58
                'type' => 'text',
59
                'required' => 1
60
            ],
61
            $module);
62
        $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
63
            $titlefield->field->id => 'Entry 1',
64
            $captionfield->field->id => 'caption'
65
        ]);
66
        $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
67
            $titlefield->field->id => 'Entry 2',
68
            $captionfield->field->id => ''
69
        ]);
70
        $cm = get_coursemodule_from_id('data', $module->cmid);
71
        // Search for entries without any search query set, we should return them all.
72
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
73
            data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0);
74
        $this->assertCount(2, $records);
75
        // Search for entries for "caption" we should return only one of them.
76
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
77
            data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0, 'caption');
78
        $this->assertCount(1, $records);
79
        // Same search but we order by title.
80
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
81
            data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0, 'caption',
82
                $titlefield->field->id, 'ASC');
83
        $this->assertCount(1, $records);
84
        $this->assert_record_entries_contains($records, $captionfield->field->id, 'caption');
85
 
86
        // Now with advanced search.
87
        $defaults = [];
88
        $fn = $ln = ''; // Defaults for first and last name.
89
        // Force value for advanced search.
90
        $_GET['f_' . $captionfield->field->id] = 'caption';
91
        list($searcharray, $searchtext) = data_build_search_array($module, false, [], $defaults, $fn, $ln);
92
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
93
            data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0, $searchtext,
94
                $titlefield->field->id, 'ASC', 0, 0, true, $searcharray);
95
        $this->assertCount(1, $records);
96
        $this->assert_record_entries_contains($records, $captionfield->field->id, 'caption');
97
    }
98
 
99
    /**
1441 ariadna 100
     * Confirms that search is working with groups
101
     * @covers ::data_search_entries
102
     */
103
    public function test_data_search_entries_with_groups(): void {
104
        $this->resetAfterTest();
105
        $this->setAdminUser();
106
        $course = $this->getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1]);
107
        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
108
        $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
109
        $student1 = $this->getDataGenerator()->create_and_enrol($course);
110
        $student2 = $this->getDataGenerator()->create_and_enrol($course);
111
        $student3 = $this->getDataGenerator()->create_and_enrol($course);
112
        $teacher1 = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
113
        $teacher2 = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
114
        $teacher3 = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
115
        groups_add_member($group1->id, $student1->id);
116
        groups_add_member($group1->id, $teacher1->id);
117
        groups_add_member($group2->id, $student3->id);
118
        groups_add_member($group2->id, $teacher3->id);
119
 
120
        $record = new \stdClass();
121
        $record->course = $course->id;
122
        $record->name = "Mod data delete test";
123
        $record->intro = "Some intro of some sort";
124
        $module = $this->getDataGenerator()->create_module('data', $record);
125
        $titlefield = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field(
126
            (object) [
127
                'name' => 'title',
128
                'type' => 'text',
129
                'required' => 1,
130
            ],
131
            $module);
132
        $captionfield = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field(
133
            (object) [
134
                'name' => 'caption',
135
                'type' => 'text',
136
                'required' => 1,
137
            ],
138
            $module);
139
        $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
140
            $titlefield->field->id => 'Entry 1 - group 1',
141
            $captionfield->field->id => 'caption',
142
        ],
143
            $group1->id,
144
            [],
145
            null,
146
            $student1->id
147
        );
148
        $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
149
            $titlefield->field->id => 'Entry 2 - group 1',
150
            $captionfield->field->id => 'caption',
151
        ],
152
            $group1->id,
153
            [],
154
            null,
155
            $student1->id
156
        );
157
        $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
158
            $titlefield->field->id => 'Entry 3 - group 2',
159
            $captionfield->field->id => '',
160
        ],
161
            $group2->id,
162
            [],
163
            null,
164
            $student3->id
165
        );
166
        $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
167
            $titlefield->field->id => 'Entry 3 - no group',
168
            $captionfield->field->id => '',
169
        ],
170
            0,
171
            [],
172
            null,
173
            $student2->id
174
        );
175
        $cm = get_coursemodule_from_id('data', $module->cmid);
176
        $this->setUser($teacher1);
177
        // As a non editing teacher in group 1, I should see only the entries for group 1.
178
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
179
            data_search_entries($module, $cm, \context_course::instance($course->id), 'list', $group1->id);
180
        $this->assertCount(3, $records); // Record with group 1 and record with no group.
181
        // As a non editing teacher not in a group, I should see the entry from users not in a group.
182
        $this->setUser($teacher3);
183
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
184
            data_search_entries($module, $cm, \context_course::instance($course->id), 'list', $group2->id);
185
        $this->assertCount(2, $records); // Record with group 2 and record with no group.
186
        // As a non editing teacher not in a group, I should see the entry from users not in a group.
187
        $this->setUser($teacher2);
188
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
189
            data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0);
190
        $this->assertCount(1, $records); // Just the record with no group.
191
        $this->assert_record_entries_contains($records, $titlefield->field->id, 'Entry 3 - no group');
192
    }
193
 
194
    /**
1 efrain 195
     * Assert that all records contains a value for the matching field id.
196
     *
197
     * @param array $records
198
     * @param int $fieldid
199
     * @param string $content
200
     * @return void
201
     */
202
    private function assert_record_entries_contains($records, $fieldid, $content) {
203
        global $DB;
204
        foreach ($records as $record) {
205
            $fieldscontent = $DB->get_records('data_content', ['recordid' => $record->id]);
206
            foreach ($fieldscontent as $fieldcontent) {
207
                if ($fieldcontent->id == $fieldid) {
208
                    $this->assertStringContainsString($fieldcontent->content, $content);
209
                }
210
            }
211
        }
212
    }
213
}