MDL-32323 convert mod_data search test

This commit is contained in:
Petr Skoda
2012-04-10 15:27:13 +02:00
parent 713d20918f
commit 5b8d5c19fd
4 changed files with 2352 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+21
View File
@@ -0,0 +1,21 @@
id,dataid,type,name,description,param1,param2,param3,param4,param5
1,1,latlong,location,Your current location,,-1,,NULL,NULL
2,1,multimenu,beer,Which beer is good?,,,,NULL,NULL
3,1,checkbox,month,One month or Two,,,,NULL,NULL
4,1,number,tfn,Tax file number,,,,NULL,NULL
5,1,radiobutton,gender,Male or Female,,,,NULL,NULL
6,1,url,webpage,Your webpage,,,,NULL,NULL
7,1,text,firstname,Your first name,,,,NULL,NULL
8,1,textarea,about,About you,,60,35,1,NULL
9,1,menu,state,State,,,,NULL,NULL
10,1,text,address,address of the participant,,,,NULL,NULL
11,1,menu,Sport,Favourite sport,,,,NULL,NULL
12,1,text,suburb,suburb,,,,NULL,NULL
13,1,text,mobile,mobile number,,,,NULL,NULL
14,1,text,phone,phone number,,,,NULL,NULL
15,1,text,email,email address,,,,NULL,NULL
16,1,text,license,Driver's license,,,,NULL,NULL
17,1,text,passport,Passport number,,,,NULL,NULL
18,1,menu,browser,prefered browser,,,,NULL,NULL
19,1,menu,OS,Operating System,,,,NULL,NULL
20,1,text,nickname,Nick name,,,,NULL,NULL
1 id dataid type name description param1 param2 param3 param4 param5
2 1 1 latlong location Your current location -1 NULL NULL
3 2 1 multimenu beer Which beer is good? NULL NULL
4 3 1 checkbox month One month or Two NULL NULL
5 4 1 number tfn Tax file number NULL NULL
6 5 1 radiobutton gender Male or Female NULL NULL
7 6 1 url webpage Your webpage NULL NULL
8 7 1 text firstname Your first name NULL NULL
9 8 1 textarea about About you 60 35 1 NULL
10 9 1 menu state State NULL NULL
11 10 1 text address address of the participant NULL NULL
12 11 1 menu Sport Favourite sport NULL NULL
13 12 1 text suburb suburb NULL NULL
14 13 1 text mobile mobile number NULL NULL
15 14 1 text phone phone number NULL NULL
16 15 1 text email email address NULL NULL
17 16 1 text license Driver's license NULL NULL
18 17 1 text passport Passport number NULL NULL
19 18 1 menu browser prefered browser NULL NULL
20 19 1 menu OS Operating System NULL NULL
21 20 1 text nickname Nick name NULL NULL
+101
View File
@@ -0,0 +1,101 @@
id,userid,groupid,dataid,timecreated,timemodified,approved
1,1,0,1,1234567891,1234567892,1
2,2,0,1,1234567891,1234567892,1
3,3,0,1,1234567891,1234567892,1
4,4,0,1,1234567891,1234567892,1
5,5,0,1,1234567891,1234567892,1
6,6,0,1,1234567891,1234567892,1
7,7,0,1,1234567891,1234567892,1
8,8,0,1,1234567891,1234567892,1
9,9,0,1,1234567891,1234567892,1
10,10,0,1,1234567891,1234567892,1
11,11,0,1,1234567891,1234567892,1
12,12,0,1,1234567891,1234567892,1
13,13,0,1,1234567891,1234567892,1
14,14,0,1,1234567891,1234567892,1
15,15,0,1,1234567891,1234567892,1
16,16,0,1,1234567891,1234567892,1
17,17,0,1,1234567891,1234567892,1
18,18,0,1,1234567891,1234567892,1
19,19,0,1,1234567891,1234567892,1
20,20,0,1,1234567891,1234567892,1
21,21,0,1,1234567891,1234567892,1
22,22,0,1,1234567891,1234567892,1
23,23,0,1,1234567891,1234567892,1
24,24,0,1,1234567891,1234567892,1
25,25,0,1,1234567891,1234567892,1
26,26,0,1,1234567891,1234567892,1
27,27,0,1,1234567891,1234567892,1
28,28,0,1,1234567891,1234567892,1
29,29,0,1,1234567891,1234567892,1
30,30,0,1,1234567891,1234567892,1
31,31,0,1,1234567891,1234567892,1
32,32,0,1,1234567891,1234567892,1
33,33,0,1,1234567891,1234567892,1
34,34,0,1,1234567891,1234567892,1
35,35,0,1,1234567891,1234567892,1
36,36,0,1,1234567891,1234567892,1
37,37,0,1,1234567891,1234567892,1
38,38,0,1,1234567891,1234567892,1
39,39,0,1,1234567891,1234567892,1
40,40,0,1,1234567891,1234567892,1
41,41,0,1,1234567891,1234567892,1
42,42,0,1,1234567891,1234567892,1
43,43,0,1,1234567891,1234567892,1
44,44,0,1,1234567891,1234567892,1
45,45,0,1,1234567891,1234567892,1
46,46,0,1,1234567891,1234567892,1
47,47,0,1,1234567891,1234567892,1
48,48,0,1,1234567891,1234567892,1
49,49,0,1,1234567891,1234567892,1
50,50,0,1,1234567891,1234567892,1
51,51,0,1,1234567891,1234567892,1
52,52,0,1,1234567891,1234567892,1
53,53,0,1,1234567891,1234567892,1
54,54,0,1,1234567891,1234567892,1
55,55,0,1,1234567891,1234567892,1
56,56,0,1,1234567891,1234567892,1
57,57,0,1,1234567891,1234567892,1
58,58,0,1,1234567891,1234567892,1
59,59,0,1,1234567891,1234567892,1
60,60,0,1,1234567891,1234567892,1
61,61,0,1,1234567891,1234567892,1
62,62,0,1,1234567891,1234567892,1
63,63,0,1,1234567891,1234567892,1
64,64,0,1,1234567891,1234567892,1
65,65,0,1,1234567891,1234567892,1
66,66,0,1,1234567891,1234567892,1
67,67,0,1,1234567891,1234567892,1
68,68,0,1,1234567891,1234567892,1
69,69,0,1,1234567891,1234567892,1
70,70,0,1,1234567891,1234567892,1
71,71,0,1,1234567891,1234567892,1
72,72,0,1,1234567891,1234567892,1
73,73,0,1,1234567891,1234567892,1
74,74,0,1,1234567891,1234567892,1
75,75,0,1,1234567891,1234567892,1
76,76,0,1,1234567891,1234567892,1
77,77,0,1,1234567891,1234567892,1
78,78,0,1,1234567891,1234567892,1
79,79,0,1,1234567891,1234567892,1
80,80,0,1,1234567891,1234567892,1
81,81,0,1,1234567891,1234567892,1
82,82,0,1,1234567891,1234567892,1
83,83,0,1,1234567891,1234567892,1
84,84,0,1,1234567891,1234567892,1
85,85,0,1,1234567891,1234567892,1
86,86,0,1,1234567891,1234567892,1
87,87,0,1,1234567891,1234567892,1
88,88,0,1,1234567891,1234567892,1
89,89,0,1,1234567891,1234567892,1
90,90,0,1,1234567891,1234567892,1
91,91,0,1,1234567891,1234567892,1
92,92,0,1,1234567891,1234567892,1
93,93,0,1,1234567891,1234567892,1
94,94,0,1,1234567891,1234567892,1
95,95,0,1,1234567891,1234567892,1
96,96,0,1,1234567891,1234567892,1
97,97,0,1,1234567891,1234567892,1
98,98,0,1,1234567891,1234567892,1
99,99,0,1,1234567891,1234567892,1
100,100,0,1,1234567891,1234567892,1
1 id userid groupid dataid timecreated timemodified approved
2 1 1 0 1 1234567891 1234567892 1
3 2 2 0 1 1234567891 1234567892 1
4 3 3 0 1 1234567891 1234567892 1
5 4 4 0 1 1234567891 1234567892 1
6 5 5 0 1 1234567891 1234567892 1
7 6 6 0 1 1234567891 1234567892 1
8 7 7 0 1 1234567891 1234567892 1
9 8 8 0 1 1234567891 1234567892 1
10 9 9 0 1 1234567891 1234567892 1
11 10 10 0 1 1234567891 1234567892 1
12 11 11 0 1 1234567891 1234567892 1
13 12 12 0 1 1234567891 1234567892 1
14 13 13 0 1 1234567891 1234567892 1
15 14 14 0 1 1234567891 1234567892 1
16 15 15 0 1 1234567891 1234567892 1
17 16 16 0 1 1234567891 1234567892 1
18 17 17 0 1 1234567891 1234567892 1
19 18 18 0 1 1234567891 1234567892 1
20 19 19 0 1 1234567891 1234567892 1
21 20 20 0 1 1234567891 1234567892 1
22 21 21 0 1 1234567891 1234567892 1
23 22 22 0 1 1234567891 1234567892 1
24 23 23 0 1 1234567891 1234567892 1
25 24 24 0 1 1234567891 1234567892 1
26 25 25 0 1 1234567891 1234567892 1
27 26 26 0 1 1234567891 1234567892 1
28 27 27 0 1 1234567891 1234567892 1
29 28 28 0 1 1234567891 1234567892 1
30 29 29 0 1 1234567891 1234567892 1
31 30 30 0 1 1234567891 1234567892 1
32 31 31 0 1 1234567891 1234567892 1
33 32 32 0 1 1234567891 1234567892 1
34 33 33 0 1 1234567891 1234567892 1
35 34 34 0 1 1234567891 1234567892 1
36 35 35 0 1 1234567891 1234567892 1
37 36 36 0 1 1234567891 1234567892 1
38 37 37 0 1 1234567891 1234567892 1
39 38 38 0 1 1234567891 1234567892 1
40 39 39 0 1 1234567891 1234567892 1
41 40 40 0 1 1234567891 1234567892 1
42 41 41 0 1 1234567891 1234567892 1
43 42 42 0 1 1234567891 1234567892 1
44 43 43 0 1 1234567891 1234567892 1
45 44 44 0 1 1234567891 1234567892 1
46 45 45 0 1 1234567891 1234567892 1
47 46 46 0 1 1234567891 1234567892 1
48 47 47 0 1 1234567891 1234567892 1
49 48 48 0 1 1234567891 1234567892 1
50 49 49 0 1 1234567891 1234567892 1
51 50 50 0 1 1234567891 1234567892 1
52 51 51 0 1 1234567891 1234567892 1
53 52 52 0 1 1234567891 1234567892 1
54 53 53 0 1 1234567891 1234567892 1
55 54 54 0 1 1234567891 1234567892 1
56 55 55 0 1 1234567891 1234567892 1
57 56 56 0 1 1234567891 1234567892 1
58 57 57 0 1 1234567891 1234567892 1
59 58 58 0 1 1234567891 1234567892 1
60 59 59 0 1 1234567891 1234567892 1
61 60 60 0 1 1234567891 1234567892 1
62 61 61 0 1 1234567891 1234567892 1
63 62 62 0 1 1234567891 1234567892 1
64 63 63 0 1 1234567891 1234567892 1
65 64 64 0 1 1234567891 1234567892 1
66 65 65 0 1 1234567891 1234567892 1
67 66 66 0 1 1234567891 1234567892 1
68 67 67 0 1 1234567891 1234567892 1
69 68 68 0 1 1234567891 1234567892 1
70 69 69 0 1 1234567891 1234567892 1
71 70 70 0 1 1234567891 1234567892 1
72 71 71 0 1 1234567891 1234567892 1
73 72 72 0 1 1234567891 1234567892 1
74 73 73 0 1 1234567891 1234567892 1
75 74 74 0 1 1234567891 1234567892 1
76 75 75 0 1 1234567891 1234567892 1
77 76 76 0 1 1234567891 1234567892 1
78 77 77 0 1 1234567891 1234567892 1
79 78 78 0 1 1234567891 1234567892 1
80 79 79 0 1 1234567891 1234567892 1
81 80 80 0 1 1234567891 1234567892 1
82 81 81 0 1 1234567891 1234567892 1
83 82 82 0 1 1234567891 1234567892 1
84 83 83 0 1 1234567891 1234567892 1
85 84 84 0 1 1234567891 1234567892 1
86 85 85 0 1 1234567891 1234567892 1
87 86 86 0 1 1234567891 1234567892 1
88 87 87 0 1 1234567891 1234567892 1
89 88 88 0 1 1234567891 1234567892 1
90 89 89 0 1 1234567891 1234567892 1
91 90 90 0 1 1234567891 1234567892 1
92 91 91 0 1 1234567891 1234567892 1
93 92 92 0 1 1234567891 1234567892 1
94 93 93 0 1 1234567891 1234567892 1
95 94 94 0 1 1234567891 1234567892 1
96 95 95 0 1 1234567891 1234567892 1
97 96 96 0 1 1234567891 1234567892 1
98 97 97 0 1 1234567891 1234567892 1
99 98 98 0 1 1234567891 1234567892 1
100 99 99 0 1 1234567891 1234567892 1
101 100 100 0 1 1234567891 1234567892 1
+229
View File
@@ -0,0 +1,229 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for data_get_all_recordsids(), data_get_advance_search_ids(), data_get_record_ids(),
* and data_get_advanced_search_sql()
*
* @package mod_data
* @category phpunit
* @copyright 2012 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/data/lib.php');
require_once($CFG->dirroot . '/lib/csvlib.class.php');
/**
* Unit tests for {@see data_get_all_recordids()}.
* {@see data_get_advanced_search_ids()}
* {@see data_get_record_ids()}
* {@see data_get_advanced_search_sql()}
*
* @package mod_data
* @copyright 2012 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class data_advanced_search_sql_test extends advanced_testcase {
/**
* @var stdObject $recorddata An object that holds information from the table data.
*/
public $recorddata = null;
/**
* @var int $recordcontentid The content ID.
*/
public $recordcontentid = null;
/**
* @var int $recordrecordid The record ID.
*/
public $recordrecordid = null;
/**
* @var int $recordfieldid The field ID.
*/
public $recordfieldid = null;
/**
* @var array $recordsearcharray An array of stdClass which contains search criteria.
*/
public $recordsearcharray = null;
// CONSTANTS
/**
* @var int $datarecordcount The number of records in the database.
*/
public $datarecordcount = 100;
/**
* @var array $datarecordset Expected record IDs.
*/
public $datarecordset = array('0' => '6');
/**
* @var array $finalrecord Final record for comparison with test four.
*/
public $finalrecord = array();
/**
* Set up function. In this instance we are setting up database
* records to be used in the unit tests.
*/
protected function setUp() {
global $DB, $CFG;
parent::setUp();
$this->resetAfterTest(true);
// Set up data for the test database.
$tablenames = array(
'data_fields',
'data_records',
'data_content',
);
// we already have 2 users, we need 98 more - let's ignore the fact that guest can not post anywhere
for($i=3;$i<=100;$i++) {
$this->getDataGenerator()->create_user();
}
// create database module - there should be more of these I guess
$course = $this->getDataGenerator()->create_course();
$data = $this->getDataGenerator()->create_module('data', array('course'=>$course->id));
$this->recorddata = $data;
foreach ($tablenames as $tablename) {
$filename = __DIR__. '/fixtures/test_' . $tablename . '.csv';
if (file_exists($filename)) {
$file = file_get_contents($filename);
}
$this->insert_data_from_csv($file, $tablename);
}
// Create the search array which contains our advanced search criteria.
$fieldinfo = array('0' => new stdClass(),
'1' => new stdClass(),
'2' => new stdClass(),
'3' => new stdClass(),
'4' => new stdClass());
$fieldinfo['0']->id = 1;
$fieldinfo['0']->data = '3.721,46.6126';
$fieldinfo['1']->id = 2;
$fieldinfo['1']->data = 'Hahn Premium';
$fieldinfo['2']->id = 5;
$fieldinfo['2']->data = 'Female';
$fieldinfo['3']->id = 7;
$fieldinfo['3']->data = 'kel';
$fieldinfo['4']->id = 9;
$fieldinfo['4']->data = 'VIC';
foreach($fieldinfo as $field) {
$searchfield = data_get_field_from_id($field->id, $data);
if ($field->id == 2) {
$searchfield->field->param1 = 'Hahn Premium';
$val = array();
$val['selected'] = array('0' => 'Hahn Premium');
$val['allrequired'] = 0;
} else {
$val = $field->data;
}
$search_array[$field->id] = new stdClass();
list($search_array[$field->id]->sql, $search_array[$field->id]->params) = $searchfield->generate_sql('c' . $field->id, $val);
}
$this->recordsearcharray = $search_array;
// Setting up the comparison stdClass for the last test.
$user = $DB->get_record('user', array('id'=>6));
$this->finalrecord[6] = new stdClass();
$this->finalrecord[6]->id = 6;
$this->finalrecord[6]->approved = 1;
$this->finalrecord[6]->timecreated = 1234567891;
$this->finalrecord[6]->timemodified = 1234567892;
$this->finalrecord[6]->userid = 6;
$this->finalrecord[6]->firstname = $user->firstname;
$this->finalrecord[6]->lastname = $user->lastname;
}
/**
* Test 1: The function data_get_all_recordids.
*
* Test 2: This tests the data_get_advance_search_ids() function. The function takes a set
* of all the record IDs in the database and then with the search details ($this->recordsearcharray)
* returns a comma seperated string of record IDs that match the search criteria.
*
* Test 3: This function tests data_get_recordids(). This is the function that is nested in the last
* function (@see data_get_advance_search_ids). This function takes a couple of
* extra parameters. $alias is the field alias used in the sql query and $commaid
* is a comma seperated string of record IDs.
*
* Test 4: data_get_advanced_search_sql provides an array which contains an sql string to be used for displaying records
* to the user when they use the advanced search criteria and the parameters that go with the sql statement. This test
* takes that information and does a search on the database, returning a record.
*/
function test_advanced_search_sql_section() {
global $DB;
// Test 1
$recordids = data_get_all_recordids($this->recorddata->id);
$this->assertEquals(count($recordids), $this->datarecordcount);
// Test 2
$key = array_keys($this->recordsearcharray);
$alias = $key[0];
$newrecordids = data_get_recordids($alias, $this->recordsearcharray, $this->recorddata->id, $recordids);
$this->assertEquals($this->datarecordset, $newrecordids);
// Test 3
$newrecordids = data_get_advance_search_ids($recordids, $this->recordsearcharray, $this->recorddata->id);
$this->assertEquals($this->datarecordset, $newrecordids);
// Test 4
$sortorder = 'ORDER BY r.timecreated ASC , r.id ASC';
$html = data_get_advanced_search_sql('0', $this->recorddata, $newrecordids, '', $sortorder);
$allparams = array_merge($html['params'], array('dataid' => $this->recorddata->id));
$records = $DB->get_records_sql($html['sql'], $allparams);
$this->assertEquals($records, $this->finalrecord);
}
/**
* Inserts data from a csv file into the data module table specified.
*
* @param string $file comma seperated value file
* @param string $tablename name of the table for the data to be inserted into.
*/
function insert_data_from_csv($file, $tablename) {
global $DB;
$iid = csv_import_reader::get_new_iid('moddata');
$csvdata = new csv_import_reader($iid, 'moddata');
$fielddata = $csvdata->load_csv_content($file, 'utf-8', 'comma');
$columns = $csvdata->get_columns();
$columncount = count($columns);
$csvdata->init();
$fieldinfo = array();
for ($j = 0; $j < $fielddata; $j++) {
$thing = $csvdata->next();
$fieldinfo[$j] = new stdClass();
for ($i = 0; $i < $columncount; $i++) {
$fieldinfo[$j]->$columns[$i] = $thing[$i];
}
$DB->insert_record($tablename, $fieldinfo[$j], false);
}
}
}