Merge branch 'MDL-57455_master' of git://github.com/markn86/moodle
[moodle.git] / mod / data / tests / search_test.php
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/>.
17 /**
18  * Unit tests for data_get_all_recordsids(), data_get_advance_search_ids(), data_get_record_ids(),
19  * and data_get_advanced_search_sql()
20  *
21  * @package    mod_data
22  * @category   phpunit
23  * @copyright  2012 Adrian Greeve
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/data/lib.php');
31 require_once($CFG->dirroot . '/lib/datalib.php');
32 require_once($CFG->dirroot . '/lib/csvlib.class.php');
33 require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
34 require_once($CFG->dirroot . '/mod/data/tests/generator/lib.php');
36 /**
37  * Unit tests for {@see data_get_all_recordids()}.
38  *                {@see data_get_advanced_search_ids()}
39  *                {@see data_get_record_ids()}
40  *                {@see data_get_advanced_search_sql()}
41  *
42  * @package    mod_data
43  * @copyright  2012 Adrian Greeve
44  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45  */
46 class mod_data_search_test extends advanced_testcase {
47     /**
48      * @var stdObject $recorddata An object that holds information from the table data.
49      */
50     public $recorddata = null;
51     /**
52      * @var int $recordcontentid The content ID.
53      */
54     public $recordcontentid = null;
55     /**
56      * @var int $recordrecordid The record ID.
57      */
58     public $recordrecordid = null;
59     /**
60      * @var int $recordfieldid The field ID.
61      */
62     public $recordfieldid = null;
63     /**
64      * @var array $recordsearcharray An array of stdClass which contains search criteria.
65      */
66     public $recordsearcharray = null;
68     // CONSTANTS
70     /**
71      * @var int $datarecordcount   The number of records in the database.
72      */
73     public $datarecordcount = 100;
75     /**
76      * @var int $groupdatarecordcount  The number of records in the database in groups 0 and 1.
77      */
78     public $groupdatarecordcount = 75;
80     /**
81      * @var array $datarecordset   Expected record IDs.
82      */
83     public $datarecordset = array('0' => '6');
85     /**
86      * @var array $finalrecord   Final record for comparison with test four.
87      */
88     public $finalrecord = array();
90     /**
91      * @var int $approvedatarecordcount  The number of approved records in the database.
92      */
93     public $approvedatarecordcount = 89;
95     /**
96      * @var string Area id
97      */
98     protected $databaseentryareaid = null;
100     /**
101      * Set up function. In this instance we are setting up database
102      * records to be used in the unit tests.
103      */
104     protected function setUp() {
105         global $DB, $CFG;
106         parent::setUp();
108         $this->resetAfterTest(true);
110         set_config('enableglobalsearch', true);
112         $this->databaseentryareaid = \core_search\manager::generate_areaid('mod_data', 'entry');
114         // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
115         $search = testable_core_search::instance();
117     }
119     /**
120      * Test 1: The function data_get_all_recordids.
121      *
122      * Test 2: This tests the data_get_advance_search_ids() function. The function takes a set
123      * of all the record IDs in the database and then with the search details ($this->recordsearcharray)
124      * returns a comma seperated string of record IDs that match the search criteria.
125      *
126      * Test 3: This function tests data_get_recordids(). This is the function that is nested in the last
127      * function (see data_get_advance_search_ids). This function takes a couple of
128      * extra parameters. $alias is the field alias used in the sql query and $commaid
129      * is a comma seperated string of record IDs.
130      *
131      * Test 3.1: This tests that if no recordids are provided (In a situation where a search is done on an empty database)
132      * That an empty array is returned.
133      *
134      * Test 4: data_get_advanced_search_sql provides an array which contains an sql string to be used for displaying records
135      * to the user when they use the advanced search criteria and the parameters that go with the sql statement. This test
136      * takes that information and does a search on the database, returning a record.
137      *
138      * Test 5: Returning to data_get_all_recordids(). Here we are ensuring that the total amount of record ids is reduced to
139      * match the group conditions that are provided. There are 25 entries which relate to group 2. They are removed
140      * from the total so we should only have 75 records total.
141      *
142      * Test 6: data_get_all_recordids() again. This time we are testing approved database records. We only want to
143      * display the records that have been approved. In this record set we have 89 approved records.
144      */
145     public function test_advanced_search_sql_section() {
146         global $DB;
148         // we already have 2 users, we need 98 more - let's ignore the fact that guest can not post anywhere
149         // We reset the user sequence here to ensure we get the expected numbers.
150         // TODO: Invent a better way for managing data file input against database sequence id's.
151         $DB->get_manager()->reset_sequence('user');
152         for($i=3;$i<=100;$i++) {
153             $this->getDataGenerator()->create_user();
154         }
156         // create database module - there should be more of these I guess
157         $course = $this->getDataGenerator()->create_course();
158         $data = $this->getDataGenerator()->create_module('data', array('course'=>$course->id));
159         $this->recorddata = $data;
161         // Set up data for the test database.
162         $files = array(
163                 'data_fields'  => __DIR__.'/fixtures/test_data_fields.csv',
164                 'data_records' => __DIR__.'/fixtures/test_data_records.csv',
165                 'data_content' => __DIR__.'/fixtures/test_data_content.csv',
166         );
167         $this->loadDataSet($this->createCsvDataSet($files));
168         // Set dataid to the correct value now the data has been inserted by csv file.
169         $DB->execute('UPDATE {data_fields} SET dataid = ?', array($data->id));
170         $DB->execute('UPDATE {data_records} SET dataid = ?', array($data->id));
172         // Create the search array which contains our advanced search criteria.
173         $fieldinfo = array('0' => new stdClass(),
174                 '1' => new stdClass(),
175                 '2' => new stdClass(),
176                 '3' => new stdClass(),
177                 '4' => new stdClass());
178         $fieldinfo['0']->id = 1;
179         $fieldinfo['0']->data = '3.721,46.6126';
180         $fieldinfo['1']->id = 2;
181         $fieldinfo['1']->data = 'Hahn Premium';
182         $fieldinfo['2']->id = 5;
183         $fieldinfo['2']->data = 'Female';
184         $fieldinfo['3']->id = 7;
185         $fieldinfo['3']->data = 'kel';
186         $fieldinfo['4']->id = 9;
187         $fieldinfo['4']->data = 'VIC';
189         foreach($fieldinfo as $field) {
190             $searchfield = data_get_field_from_id($field->id, $data);
191             if ($field->id == 2) {
192                 $searchfield->field->param1 = 'Hahn Premium';
193                 $val = array();
194                 $val['selected'] = array('0' => 'Hahn Premium');
195                 $val['allrequired'] = 0;
196             } else {
197                 $val = $field->data;
198             }
199             $search_array[$field->id] = new stdClass();
200             list($search_array[$field->id]->sql, $search_array[$field->id]->params) = $searchfield->generate_sql('c' . $field->id, $val);
201         }
203         $this->recordsearcharray = $search_array;
205         // Setting up the comparison stdClass for the last test.
206         $user = $DB->get_record('user', array('id'=>6));
207         $this->finalrecord[6] = new stdClass();
208         $this->finalrecord[6]->id = 6;
209         $this->finalrecord[6]->approved = 1;
210         $this->finalrecord[6]->timecreated = 1234567891;
211         $this->finalrecord[6]->timemodified = 1234567892;
212         $this->finalrecord[6]->userid = 6;
213         $this->finalrecord[6]->firstname = $user->firstname;
214         $this->finalrecord[6]->lastname = $user->lastname;
215         $this->finalrecord[6]->firstnamephonetic = $user->firstnamephonetic;
216         $this->finalrecord[6]->lastnamephonetic = $user->lastnamephonetic;
217         $this->finalrecord[6]->middlename = $user->middlename;
218         $this->finalrecord[6]->alternatename = $user->alternatename;
219         $this->finalrecord[6]->picture = $user->picture;
220         $this->finalrecord[6]->imagealt = $user->imagealt;
221         $this->finalrecord[6]->email = $user->email;
223         // Test 1
224         $recordids = data_get_all_recordids($this->recorddata->id);
225         $this->assertEquals(count($recordids), $this->datarecordcount);
227         // Test 2
228         $key = array_keys($this->recordsearcharray);
229         $alias = $key[0];
230         $newrecordids = data_get_recordids($alias, $this->recordsearcharray, $this->recorddata->id, $recordids);
231         $this->assertEquals($this->datarecordset, $newrecordids);
233         // Test 3
234         $newrecordids = data_get_advance_search_ids($recordids, $this->recordsearcharray, $this->recorddata->id);
235         $this->assertEquals($this->datarecordset, $newrecordids);
237         // Test 3.1
238         $resultrecordids = data_get_advance_search_ids(array(), $this->recordsearcharray, $this->recorddata->id);
239         $this->assertEmpty($resultrecordids);
241         // Test 4
242         $sortorder = 'ORDER BY r.timecreated ASC , r.id ASC';
243         $html = data_get_advanced_search_sql('0', $this->recorddata, $newrecordids, '', $sortorder);
244         $allparams = array_merge($html['params'], array('dataid' => $this->recorddata->id));
245         $records = $DB->get_records_sql($html['sql'], $allparams);
246         $this->assertEquals($records, $this->finalrecord);
248         // Test 5
249         $groupsql = " AND (r.groupid = :currentgroup OR r.groupid = 0)";
250         $params = array('currentgroup' => 1);
251         $recordids = data_get_all_recordids($this->recorddata->id, $groupsql, $params);
252         $this->assertEquals($this->groupdatarecordcount, count($recordids));
254         // Test 6
255         $approvesql = ' AND r.approved=1 ';
256         $recordids = data_get_all_recordids($this->recorddata->id, $approvesql, $params);
257         $this->assertEquals($this->approvedatarecordcount, count($recordids));
258     }
260     public function test_advanced_search_tags() {
261         $this->resetAfterTest();
262         $this->setAdminUser();
264         // Setup test data.
265         $datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
266         $course1 = $this->getDataGenerator()->create_course();
268         $fieldrecord = new StdClass();
269         $fieldrecord->name = 'field-1';
270         $fieldrecord->type = 'text';
271         $fieldrecord->titlefield = true;
273         $data1 = $this->getDataGenerator()->create_module('data', array('course' => $course1->id, 'approval' => true));
274         $field1 = $datagenerator->create_field($fieldrecord, $data1);
276         $record11 = $datagenerator->create_entry($data1, [$field1->field->id => 'value11'], 0, ['Cats', 'Dogs']);
277         $record12 = $datagenerator->create_entry($data1, [$field1->field->id => 'value12'], 0, ['Cats', 'mice']);
278         $record13 = $datagenerator->create_entry($data1, [$field1->field->id => 'value13'], 0, ['Bats']);
280         $searcharray = [];
281         $searcharray[DATA_TAGS] = new stdClass();
282         $searcharray[DATA_TAGS]->params = [];
283         $searcharray[DATA_TAGS]->rawtagnames = ['Cats'];
284         $searcharray[DATA_TAGS]->sql = '';
286         $recordids = data_get_all_recordids($data1->id);
287         $newrecordids = data_get_advance_search_ids($recordids, $searcharray, $data1->id);
289         $this->assertContains($record11, $newrecordids);
290         $this->assertContains($record12, $newrecordids);
291         $this->assertNotContains($record13, $newrecordids);
292     }
294     /**
295      * Indexing database entries contents.
296      *
297      * @return void
298      */
299     public function test_data_entries_indexing() {
300         global $DB;
302         // Returns the instance as long as the area is supported.
303         $searcharea = \core_search\manager::get_search_area($this->databaseentryareaid);
304         $this->assertInstanceOf('\mod_data\search\entry', $searcharea);
306         $user1 = self::getDataGenerator()->create_user();
308         $course1 = self::getDataGenerator()->create_course();
310         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
312         $record = new stdClass();
313         $record->course = $course1->id;
315         $this->setUser($user1);
317         // Available for both student and teacher.
318         $data1 = $this->getDataGenerator()->create_module('data', $record);
320         // Excluding LatLong and Picture as we aren't indexing LatLong and Picture fields any way
321         // ...and they're complex and not of any use to consider for this test.
322         // Excluding File as we are indexing files seperately and its complex to implement.
323         $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
325         $this->create_default_data_fields($fieldtypes, $data1);
327         $data1record1id = $this->create_default_data_record($data1);
328         // All records.
329         $recordset = $searcharea->get_recordset_by_timestamp(0);
331         $this->assertTrue($recordset->valid());
333         $nrecords = 0;
334         foreach ($recordset as $record) {
335             $this->assertInstanceOf('stdClass', $record);
336             $doc = $searcharea->get_document($record);
337             $this->assertInstanceOf('\core_search\document', $doc);
338             $nrecords++;
339         }
341         // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
342         $recordset->close();
343         $this->assertEquals(1, $nrecords);
345         // The +2 is to prevent race conditions.
346         $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
348         // No new records.
349         $this->assertFalse($recordset->valid());
350         $recordset->close();
352         // Create a second database, also with one record.
353         $data2 = $this->getDataGenerator()->create_module('data', ['course' => $course1->id]);
354         $this->create_default_data_fields($fieldtypes, $data2);
355         $this->create_default_data_record($data2);
357         // Test indexing with contexts.
358         $rs = $searcharea->get_document_recordset(0, context_module::instance($data1->cmid));
359         $this->assertEquals(1, iterator_count($rs));
360         $rs->close();
361         $rs = $searcharea->get_document_recordset(0, context_module::instance($data2->cmid));
362         $this->assertEquals(1, iterator_count($rs));
363         $rs->close();
364         $rs = $searcharea->get_document_recordset(0, context_course::instance($course1->id));
365         $this->assertEquals(2, iterator_count($rs));
366         $rs->close();
367     }
369     /**
370      * Document contents.
371      *
372      * @return void
373      */
374     public function test_data_entries_document() {
375         global $DB;
377         // Returns the instance as long as the area is supported.
378         $searcharea = \core_search\manager::get_search_area($this->databaseentryareaid);
379         $this->assertInstanceOf('\mod_data\search\entry', $searcharea);
381         $user1 = self::getDataGenerator()->create_user();
383         $course = self::getDataGenerator()->create_course();
385         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
387         $record = new stdClass();
388         $record->course = $course->id;
390         $this->setAdminUser();
392         // First Case.
393         $data1 = $this->getDataGenerator()->create_module('data', $record);
395         $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
397         $this->create_default_data_fields($fieldtypes, $data1);
399         $data1record1id = $this->create_default_data_record($data1);
401         $data1entry1 = $this->get_entry_for_id($data1record1id);
403         $data1doc = $searcharea->get_document($data1entry1);
405         $this->assertEquals($data1doc->get('courseid'), $course->id);
406         $this->assertEquals($data1doc->get('title'), 'text for testing');
407         $this->assertEquals($data1doc->get('content'), 'menu1');
408         $this->assertEquals($data1doc->get('description1'), 'radioopt1');
409         $this->assertEquals($data1doc->get('description2'), 'opt1 opt2 opt3 opt4 multimenu1 multimenu2 multimenu3 multimenu4 text area testing http://example.url');
411         // Second Case.
412         $data2 = $this->getDataGenerator()->create_module('data', $record);
414         $fieldtypes = array(
415                 array('checkbox', 1),
416                 array('textarea', 0),
417                 array('menu', 0),
418                 array('number', 1),
419                 array('url', 0),
420                 array('text', 0)
421         );
423         $this->create_default_data_fields($fieldtypes, $data2);
425         $data2record1id = $this->create_default_data_record($data2);
427         $data2entry1 = $this->get_entry_for_id($data2record1id);
429         $data2doc = $searcharea->get_document($data2entry1);
431         $this->assertEquals($data2doc->get('courseid'), $course->id);
432         $this->assertEquals($data2doc->get('title'), 'opt1 opt2 opt3 opt4');
433         $this->assertEquals($data2doc->get('content'), 'text for testing');
434         $this->assertEquals($data2doc->get('description1'), 'menu1');
435         $this->assertEquals($data2doc->get('description2'), 'text area testing http://example.url');
437         // Third Case.
438         $data3 = $this->getDataGenerator()->create_module('data', $record);
440         $fieldtypes = array( 'url' );
442         $this->create_default_data_fields($fieldtypes, $data3);
444         $data3record1id = $this->create_default_data_record($data3);
446         $data3entry1 = $this->get_entry_for_id($data3record1id);
448         $this->assertFalse($searcharea->get_document($data3entry1));
450         // Fourth Case.
451         $data4 = $this->getDataGenerator()->create_module('data', $record);
453         $fieldtypes = array( array('date', 1), array('text', 1));
455         $this->create_default_data_fields($fieldtypes, $data4);
457         $data4record1id = $this->create_default_data_record($data4);
459         $data4entry1 = $this->get_entry_for_id($data4record1id);
461         $this->assertFalse($searcharea->get_document($data4entry1));
463         // Fifth Case.
464         $data5 = $this->getDataGenerator()->create_module('data', $record);
466         $fieldtypes = array(
467                 array('checkbox', 0),
468                 array('number', 1),
469                 array('text', 0),
470                 array('date', 1),
471                 array('textarea', 0),
472                 array('url', 1));
474         $this->create_default_data_fields($fieldtypes, $data5);
476         $data5record1id = $this->create_default_data_record($data5);
478         $data5entry1 = $this->get_entry_for_id($data5record1id);
480         $data5doc = $searcharea->get_document($data5entry1);
482         $this->assertEquals($data5doc->get('courseid'), $course->id);
483         $this->assertEquals($data5doc->get('title'), 'http://example.url');
484         $this->assertEquals($data5doc->get('content'), 'text for testing');
485         $this->assertEquals($data5doc->get('description1'), 'opt1 opt2 opt3 opt4');
486         $this->assertEquals($data5doc->get('description2'), 'text area testing');
488         // Sixth Case.
489         $data6 = $this->getDataGenerator()->create_module('data', $record);
491         $fieldtypes = array( array('date', 1), array('number', 1));
493         $this->create_default_data_fields($fieldtypes, $data6);
495         $data6record1id = $this->create_default_data_record($data6);
497         $data6entry1 = $this->get_entry_for_id($data6record1id);
499         $data6doc = $searcharea->get_document($data6entry1);
501         $this->assertFalse($data6doc);
503         // Seventh Case.
504         $data7 = $this->getDataGenerator()->create_module('data', $record);
506         $fieldtypes = array( array('date', 1), array('number', 1),
507                 array('text', 0), array('textarea', 0));
509         $this->create_default_data_fields($fieldtypes, $data7);
511         $data7record1id = $this->create_default_data_record($data7);
513         $data7entry1 = $this->get_entry_for_id($data7record1id);
515         $data7doc = $searcharea->get_document($data7entry1);
517         $this->assertEquals($data7doc->get('courseid'), $course->id);
518         $this->assertEquals($data7doc->get('title'), 'text for testing');
519         $this->assertEquals($data7doc->get('content'), 'text area testing');
521         // Eight Case.
522         $data8 = $this->getDataGenerator()->create_module('data', $record);
524         $fieldtypes = array('url', 'url', 'url', 'text');
526         $this->create_default_data_fields($fieldtypes, $data8);
528         $data8record1id = $this->create_default_data_record($data8);
530         $data8entry1 = $this->get_entry_for_id($data8record1id);
532         $data8doc = $searcharea->get_document($data8entry1);
534         $this->assertEquals($data8doc->get('courseid'), $course->id);
535         $this->assertEquals($data8doc->get('title'), 'text for testing');
536         $this->assertEquals($data8doc->get('content'), 'http://example.url');
537         $this->assertEquals($data8doc->get('description1'), 'http://example.url');
538         $this->assertEquals($data8doc->get('description2'), 'http://example.url');
540         // Ninth Case.
541         $data9 = $this->getDataGenerator()->create_module('data', $record);
543         $fieldtypes = array('radiobutton', 'menu', 'multimenu');
545         $this->create_default_data_fields($fieldtypes, $data9);
547         $data9record1id = $this->create_default_data_record($data9);
549         $data9entry1 = $this->get_entry_for_id($data9record1id);
551         $data9doc = $searcharea->get_document($data9entry1);
553         $this->assertEquals($data9doc->get('courseid'), $course->id);
554         $this->assertEquals($data9doc->get('title'), 'radioopt1');
555         $this->assertEquals($data9doc->get('content'), 'menu1');
556         $this->assertEquals($data9doc->get('description1'), 'multimenu1 multimenu2 multimenu3 multimenu4');
558         // Tenth Case.
559         $data10 = $this->getDataGenerator()->create_module('data', $record);
561         $fieldtypes = array('checkbox', 'textarea', 'multimenu');
563         $this->create_default_data_fields($fieldtypes, $data10);
565         $data10record1id = $this->create_default_data_record($data10);
567         $data10entry1 = $this->get_entry_for_id($data10record1id);
569         $data10doc = $searcharea->get_document($data10entry1);
571         $this->assertEquals($data10doc->get('courseid'), $course->id);
572         $this->assertEquals($data10doc->get('title'), 'opt1 opt2 opt3 opt4');
573         $this->assertEquals($data10doc->get('content'), 'text area testing');
574         $this->assertEquals($data10doc->get('description1'), 'multimenu1 multimenu2 multimenu3 multimenu4');
576     }
578     /**
579      * Document accesses.
580      *
581      * @return void
582      */
583     public function test_data_entries_access() {
584         global $DB;
586         // Returns the instance as long as the area is supported.
587         $searcharea = \core_search\manager::get_search_area($this->databaseentryareaid);
588         $this->assertInstanceOf('\mod_data\search\entry', $searcharea);
590         $user1 = self::getDataGenerator()->create_user();
591         $user2 = self::getDataGenerator()->create_user();
592         $user3 = self::getDataGenerator()->create_user();
593         $userteacher1 = self::getDataGenerator()->create_user();
595         $course1 = self::getDataGenerator()->create_course();
596         $course2 = self::getDataGenerator()->create_course();
598         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
599         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');
600         $this->getDataGenerator()->enrol_user($userteacher1->id, $course1->id, 'teacher');
602         $this->getDataGenerator()->enrol_user($user3->id, $course2->id, 'student');
604         $record = new stdClass();
605         $record->course = $course1->id;
607         $this->setUser($userteacher1);
609         $data1 = $this->getDataGenerator()->create_module('data', $record);
611         $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
613         $this->create_default_data_fields($fieldtypes, $data1);
615         $this->setUser($user1);
616         $data1record1id = $this->create_default_data_record($data1);
618         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data1record1id));
619         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-1));
621         $this->setUser($user2);
622         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data1record1id));
624         $this->setUser($user3);
625         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data1record1id));
627         $this->setUser($userteacher1);
628         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data1record1id));
630         $this->setAdminUser();
631         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data1record1id));
633         $this->setGuestUser();
634         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data1record1id));
636         // Case with groups.
637         $user1 = self::getDataGenerator()->create_user();
638         $user2 = self::getDataGenerator()->create_user();
639         $user3 = self::getDataGenerator()->create_user();
640         $userteacher1 = self::getDataGenerator()->create_user();
642         $course = self::getDataGenerator()->create_course(array('groupmode' => 1, 'groupmodeforce' => 1));
644         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
645         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
646         $this->getDataGenerator()->enrol_user($userteacher1->id, $course->id, 'teacher');
647         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
649         $groupa = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'name' => 'groupA'));
650         $groupb = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'name' => 'groupB'));
652         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $groupa->id));
653         $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $groupa->id));
654         $this->getDataGenerator()->create_group_member(array('userid' => $userteacher1->id, 'groupid' => $groupa->id));
656         $this->getDataGenerator()->create_group_member(array('userid' => $user3->id, 'groupid' => $groupb->id));
658         $record = new stdClass();
659         $record->course = $course->id;
661         $this->setUser($userteacher1);
663         $data2 = $this->getDataGenerator()->create_module('data', $record);
665         $cm = get_coursemodule_from_instance('data', $data2->id, $course->id);
666         $cm->groupmode = '1';
667         $cm->effectivegroupmode = '1';
669         $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
671         $this->create_default_data_fields($fieldtypes, $data2);
673         $this->setUser($user1);
675         $data2record1id = $this->create_default_data_record($data2, $groupa->id);
677         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data2record1id));
678         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-1));
680         $this->setUser($user2);
681         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data2record1id));
683         $this->setUser($user3);
684         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data2record1id));
686         $this->setUser($userteacher1);
687         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data2record1id));
689         $this->setAdminUser();
690         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data2record1id));
692         $this->setGuestUser();
693         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data2record1id));
695         // Case with approval.
696         $user1 = self::getDataGenerator()->create_user();
697         $user2 = self::getDataGenerator()->create_user();
698         $userteacher1 = self::getDataGenerator()->create_user();
700         $course = self::getDataGenerator()->create_course();
702         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
703         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
704         $this->getDataGenerator()->enrol_user($userteacher1->id, $course->id, 'teacher');
706         $record = new stdClass();
707         $record->course = $course->id;
709         $this->setUser($userteacher1);
711         $data3 = $this->getDataGenerator()->create_module('data', $record);
713         $DB->update_record('data', array('id' => $data3->id, 'approval' => 1));
715         $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
717         $this->create_default_data_fields($fieldtypes, $data3);
719         $this->setUser($user1);
721         $data3record1id = $this->create_default_data_record($data3);
723         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data3record1id));
724         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-1));
726         $this->setUser($user2);
727         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data3record1id));
729         $this->setUser($userteacher1);
730         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data3record1id));
732         $this->setAdminUser();
733         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data3record1id));
735         $this->setGuestUser();
736         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data3record1id));
738         $DB->update_record('data_records', array('id' => $data3record1id, 'approved' => 1));
740         $this->setUser($user1);
741         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data3record1id));
742         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-1));
744         $this->setUser($user2);
745         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data3record1id));
747         $this->setUser($userteacher1);
748         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data3record1id));
750         $this->setAdminUser();
751         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data3record1id));
753         $this->setGuestUser();
754         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data3record1id));
756         // Case with requiredentriestoview.
757         $this->setAdminUser();
759         $record->requiredentriestoview = 2;
761         $data4 = $this->getDataGenerator()->create_module('data', $record);
762         $fieldtypes = array( 'checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url' );
764         $this->create_default_data_fields($fieldtypes, $data4);
766         $data4record1id = $this->create_default_data_record($data4);
768         $this->setUser($user1);
769         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data4record1id));
771         $data4record2id = $this->create_default_data_record($data4);
772         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data4record1id));
773         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data4record2id));
774     }
776     /**
777      * Test for file contents.
778      *
779      * @return void
780      */
781     public function test_attach_files() {
782         global $DB, $USER;
784         $fs = get_file_storage();
786         // Returns the instance as long as the area is supported.
787         $searcharea = \core_search\manager::get_search_area($this->databaseentryareaid);
788         $this->assertInstanceOf('\mod_data\search\entry', $searcharea);
790         $user1 = self::getDataGenerator()->create_user();
792         $course = self::getDataGenerator()->create_course();
794         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
796         $record = new stdClass();
797         $record->course = $course->id;
799         $this->setAdminUser();
801         // Creating database activity instance.
802         $data1 = $this->getDataGenerator()->create_module('data', $record);
804         // Creating file field.
805         $record = new stdClass;
806         $record->type = 'file';
807         $record->dataid = $data1->id;
808         $record->required = 0;
809         $record->name = 'FileFld';
810         $record->description = 'Just another file field';
811         $record->param3 = 0;
812         $record->param1 = '';
813         $record->param2 = '';
815         $data1filefieldid = $DB->insert_record('data_fields', $record);
817         // Creating text field.
818         $record = new stdClass;
819         $record->type = 'text';
820         $record->dataid = $data1->id;
821         $record->required = 0;
822         $record->name = 'TextFld';
823         $record->description = 'Just another text field';
824         $record->param3 = 0;
825         $record->param1 = '';
826         $record->param2 = '';
828         $data1textfieldid = $DB->insert_record('data_fields', $record);
830         // Creating textarea field.
831         $record = new stdClass;
832         $record->type = 'textarea';
833         $record->dataid = $data1->id;
834         $record->required = 0;
835         $record->name = 'TextAreaFld';
836         $record->description = 'Just another textarea field';
837         $record->param1 = '';
838         $record->param2 = 60;
839         $record->param3 = 35;
840         $record->param3 = 1;
841         $record->param3 = 0;
843         $data1textareafieldid = $DB->insert_record('data_fields', $record);
845         // Creating 1st entry.
846         $record = new stdClass;
847         $record->userid = $USER->id;
848         $record->dataid = $data1->id;
849         $record->groupid = 0;
851         $data1record1id = $DB->insert_record('data_records', $record);
853         $filerecord = array(
854                 'contextid' => context_module::instance($data1->cmid)->id,
855                 'component' => 'mod_data',
856                 'filearea'  => 'content',
857                 'itemid'    => $data1record1id,
858                 'filepath'  => '/',
859                 'filename'  => 'myfile1.txt'
860         );
862         $data1record1file = $fs->create_file_from_string($filerecord, 'Some contents 1');
864         $record = new stdClass;
865         $record->fieldid = $data1filefieldid;
866         $record->recordid = $data1record1id;
867         $record->content = 'myfile1.txt';
868         $DB->insert_record('data_content', $record);
870         $record = new stdClass;
871         $record->fieldid = $data1textfieldid;
872         $record->recordid = $data1record1id;
873         $record->content = 'sample text';
874         $DB->insert_record('data_content', $record);
876         $record = new stdClass;
877         $record->fieldid = $data1textareafieldid;
878         $record->recordid = $data1record1id;
879         $record->content = '<br>sample text<p /><br/>';
880         $record->content1 = 1;
881         $DB->insert_record('data_content', $record);
883         // Creating 2nd entry.
884         $record = new stdClass;
885         $record->userid = $USER->id;
886         $record->dataid = $data1->id;
887         $record->groupid = 0;
888         $data1record2id = $DB->insert_record('data_records', $record);
890         $filerecord['itemid'] = $data1record2id;
891         $filerecord['filename'] = 'myfile2.txt';
892         $data1record2file = $fs->create_file_from_string($filerecord, 'Some contents 2');
894         $record = new stdClass;
895         $record->fieldid = $data1filefieldid;
896         $record->recordid = $data1record2id;
897         $record->content = 'myfile2.txt';
898         $DB->insert_record('data_content', $record);
900         $record = new stdClass;
901         $record->fieldid = $data1textfieldid;
902         $record->recordid = $data1record2id;
903         $record->content = 'sample text';
904         $DB->insert_record('data_content', $record);
906         $record = new stdClass;
907         $record->fieldid = $data1textareafieldid;
908         $record->recordid = $data1record2id;
909         $record->content = '<br>sample text<p /><br/>';
910         $record->content1 = 1;
911         $DB->insert_record('data_content', $record);
913         // Now get all the posts and see if they have the right files attached.
914         $searcharea = \core_search\manager::get_search_area($this->databaseentryareaid);
915         $recordset = $searcharea->get_recordset_by_timestamp(0);
916         $nrecords = 0;
917         foreach ($recordset as $record) {
918             $doc = $searcharea->get_document($record);
919             $searcharea->attach_files($doc);
920             $files = $doc->get_files();
921             // Now check that each doc has the right files on it.
922             switch ($doc->get('itemid')) {
923                 case ($data1record1id):
924                     $this->assertCount(1, $files);
925                     $this->assertEquals($data1record1file->get_id(), $files[$data1record1file->get_id()]->get_id());
926                     break;
927                 case ($data1record2id):
928                     $this->assertCount(1, $files);
929                     $this->assertEquals($data1record2file->get_id(), $files[$data1record2file->get_id()]->get_id());
930                     break;
931                 default:
932                     $this->fail('Unexpected entry returned');
933                     break;
934             }
935             $nrecords++;
936         }
937         $recordset->close();
938         $this->assertEquals(2, $nrecords);
939     }
941     /**
942      * Creates default fields for a database instance
943      *
944      * @param array $fieldtypes
945      * @param mod_data $data
946      * @return void
947      */
948     protected function create_default_data_fields($fieldtypes = array(), $data) {
949         $count = 1;
951         // Creating test Fields with default parameter values.
952         foreach ($fieldtypes as $fieldtype) {
954             // Creating variables dynamically.
955             $fieldname = 'field-'.$count;
956             $record = new stdClass();
957             $record->name = $fieldname;
959             if (is_array($fieldtype)) {
960                 $record->type = $fieldtype[0];
961                 $record->required = $fieldtype[1];
962             } else {
963                 $record->type = $fieldtype;
964                 $record->required = 0;
965             }
967             ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
968             $count++;
969         }
970     }
972     /**
973      * Creates default database entry content values for default field param values
974      *
975      * @param mod_data $data
976      * @param int $groupid
977      * @return int
978      */
979     protected function create_default_data_record($data, $groupid = 0) {
980         global $DB;
982         $fields = $DB->get_records('data_fields', array('dataid' => $data->id));
984         $fieldcontents = array();
985         foreach ($fields as $fieldrecord) {
986             switch ($fieldrecord->type) {
987                 case 'checkbox':
988                     $fieldcontents[$fieldrecord->id] = array('opt1', 'opt2', 'opt3', 'opt4');
989                     break;
991                 case 'multimenu':
992                     $fieldcontents[$fieldrecord->id] = array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4');
993                     break;
995                 case 'date':
996                     $fieldcontents[$fieldrecord->id] = '27-07-2016';
997                     break;
999                 case 'menu':
1000                     $fieldcontents[$fieldrecord->id] = 'menu1';
1001                     break;
1003                 case 'radiobutton':
1004                     $fieldcontents[$fieldrecord->id] = 'radioopt1';
1005                     break;
1007                 case 'number':
1008                     $fieldcontents[$fieldrecord->id] = '12345';
1009                     break;
1011                 case 'text':
1012                     $fieldcontents[$fieldrecord->id] = 'text for testing';
1013                     break;
1015                 case 'textarea':
1016                     $fieldcontents[$fieldrecord->id] = '<p>text area testing<br /></p>';
1017                     break;
1019                 case 'url':
1020                     $fieldcontents[$fieldrecord->id] = array('example.url', 'sampleurl');
1021                     break;
1023                 default:
1024                     $this->fail('Unexpected field type');
1025                     break;
1026             }
1028         }
1030         return $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data, $fieldcontents, $groupid);
1031     }
1033     /**
1034      * Creates default database entry content values for default field param values
1035      *
1036      * @param int $recordid
1037      * @return stdClass
1038      */
1039     protected function get_entry_for_id($recordid ) {
1040         global $DB;
1042         $sql = "SELECT dr.*, d.course
1043                   FROM {data_records} dr
1044                   JOIN {data} d ON d.id = dr.dataid
1045                  WHERE dr.id = :drid";
1046         return $DB->get_record_sql($sql, array('drid' => $recordid));
1047     }