[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/data/tests/ -> search_test.php (source)

   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   * 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   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/mod/data/lib.php');
  31  require_once($CFG->dirroot . '/lib/csvlib.class.php');
  32  
  33  
  34  /**
  35   * Unit tests for {@see data_get_all_recordids()}.
  36   *                {@see data_get_advanced_search_ids()}
  37   *                {@see data_get_record_ids()}
  38   *                {@see data_get_advanced_search_sql()}
  39   *
  40   * @package    mod_data
  41   * @copyright  2012 Adrian Greeve
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class data_advanced_search_sql_test extends advanced_testcase {
  45      /**
  46       * @var stdObject $recorddata An object that holds information from the table data.
  47       */
  48      public $recorddata = null;
  49      /**
  50       * @var int $recordcontentid The content ID.
  51       */
  52      public $recordcontentid = null;
  53      /**
  54       * @var int $recordrecordid The record ID.
  55       */
  56      public $recordrecordid = null;
  57      /**
  58       * @var int $recordfieldid The field ID.
  59       */
  60      public $recordfieldid = null;
  61      /**
  62       * @var array $recordsearcharray An array of stdClass which contains search criteria.
  63       */
  64      public $recordsearcharray = null;
  65  
  66      // CONSTANTS
  67  
  68      /**
  69       * @var int $datarecordcount   The number of records in the database.
  70       */
  71      public $datarecordcount = 100;
  72  
  73      /**
  74       * @var int $groupdatarecordcount  The number of records in the database in groups 0 and 1.
  75       */
  76      public $groupdatarecordcount = 75;
  77  
  78      /**
  79       * @var array $datarecordset   Expected record IDs.
  80       */
  81      public $datarecordset = array('0' => '6');
  82  
  83      /**
  84       * @var array $finalrecord   Final record for comparison with test four.
  85       */
  86      public $finalrecord = array();
  87  
  88      /**
  89       * @var int $approvedatarecordcount  The number of approved records in the database.
  90       */
  91      public $approvedatarecordcount = 89;
  92  
  93      /**
  94       * Set up function. In this instance we are setting up database
  95       * records to be used in the unit tests.
  96       */
  97      protected function setUp() {
  98          global $DB, $CFG;
  99          parent::setUp();
 100  
 101          $this->resetAfterTest(true);
 102  
 103  
 104          // we already have 2 users, we need 98 more - let's ignore the fact that guest can not post anywhere
 105          // We reset the user sequence here to ensure we get the expected numbers.
 106          // TODO: Invent a better way for managing data file input against database sequence id's.
 107          $DB->get_manager()->reset_sequence('user');
 108          for($i=3;$i<=100;$i++) {
 109              $this->getDataGenerator()->create_user();
 110          }
 111  
 112          // create database module - there should be more of these I guess
 113          $course = $this->getDataGenerator()->create_course();
 114          $data = $this->getDataGenerator()->create_module('data', array('course'=>$course->id));
 115          $this->recorddata = $data;
 116  
 117          // Set up data for the test database.
 118          $files = array(
 119              'data_fields'  => __DIR__.'/fixtures/test_data_fields.csv',
 120              'data_records' => __DIR__.'/fixtures/test_data_records.csv',
 121              'data_content' => __DIR__.'/fixtures/test_data_content.csv',
 122          );
 123          $this->loadDataSet($this->createCsvDataSet($files));
 124          // Set dataid to the correct value now the data has been inserted by csv file.
 125          $DB->execute('UPDATE {data_fields} SET dataid = ?', array($data->id));
 126          $DB->execute('UPDATE {data_records} SET dataid = ?', array($data->id));
 127  
 128          // Create the search array which contains our advanced search criteria.
 129          $fieldinfo = array('0' => new stdClass(),
 130              '1' => new stdClass(),
 131              '2' => new stdClass(),
 132              '3' => new stdClass(),
 133              '4' => new stdClass());
 134          $fieldinfo['0']->id = 1;
 135          $fieldinfo['0']->data = '3.721,46.6126';
 136          $fieldinfo['1']->id = 2;
 137          $fieldinfo['1']->data = 'Hahn Premium';
 138          $fieldinfo['2']->id = 5;
 139          $fieldinfo['2']->data = 'Female';
 140          $fieldinfo['3']->id = 7;
 141          $fieldinfo['3']->data = 'kel';
 142          $fieldinfo['4']->id = 9;
 143          $fieldinfo['4']->data = 'VIC';
 144  
 145          foreach($fieldinfo as $field) {
 146              $searchfield = data_get_field_from_id($field->id, $data);
 147              if ($field->id == 2) {
 148                  $searchfield->field->param1 = 'Hahn Premium';
 149                  $val = array();
 150                  $val['selected'] = array('0' => 'Hahn Premium');
 151                  $val['allrequired'] = 0;
 152              } else {
 153                  $val = $field->data;
 154              }
 155              $search_array[$field->id] = new stdClass();
 156              list($search_array[$field->id]->sql, $search_array[$field->id]->params) = $searchfield->generate_sql('c' . $field->id, $val);
 157          }
 158  
 159          $this->recordsearcharray = $search_array;
 160  
 161          // Setting up the comparison stdClass for the last test.
 162          $user = $DB->get_record('user', array('id'=>6));
 163          $this->finalrecord[6] = new stdClass();
 164          $this->finalrecord[6]->id = 6;
 165          $this->finalrecord[6]->approved = 1;
 166          $this->finalrecord[6]->timecreated = 1234567891;
 167          $this->finalrecord[6]->timemodified = 1234567892;
 168          $this->finalrecord[6]->userid = 6;
 169          $this->finalrecord[6]->firstname = $user->firstname;
 170          $this->finalrecord[6]->lastname = $user->lastname;
 171          $this->finalrecord[6]->firstnamephonetic = $user->firstnamephonetic;
 172          $this->finalrecord[6]->lastnamephonetic = $user->lastnamephonetic;
 173          $this->finalrecord[6]->middlename = $user->middlename;
 174          $this->finalrecord[6]->alternatename = $user->alternatename;
 175          $this->finalrecord[6]->picture = $user->picture;
 176          $this->finalrecord[6]->imagealt = $user->imagealt;
 177          $this->finalrecord[6]->email = $user->email;
 178      }
 179  
 180      /**
 181       * Test 1: The function data_get_all_recordids.
 182       *
 183       * Test 2: This tests the data_get_advance_search_ids() function. The function takes a set
 184       * of all the record IDs in the database and then with the search details ($this->recordsearcharray)
 185       * returns a comma seperated string of record IDs that match the search criteria.
 186       *
 187       * Test 3: This function tests data_get_recordids(). This is the function that is nested in the last
 188       * function (@see data_get_advance_search_ids). This function takes a couple of
 189       * extra parameters. $alias is the field alias used in the sql query and $commaid
 190       * is a comma seperated string of record IDs.
 191       *
 192       * Test 3.1: This tests that if no recordids are provided (In a situation where a search is done on an empty database)
 193       * That an empty array is returned.
 194       *
 195       * Test 4: data_get_advanced_search_sql provides an array which contains an sql string to be used for displaying records
 196       * to the user when they use the advanced search criteria and the parameters that go with the sql statement. This test
 197       * takes that information and does a search on the database, returning a record.
 198       *
 199       * Test 5: Returning to data_get_all_recordids(). Here we are ensuring that the total amount of record ids is reduced to
 200       * match the group conditions that are provided. There are 25 entries which relate to group 2. They are removed
 201       * from the total so we should only have 75 records total.
 202       *
 203       * Test 6: data_get_all_recordids() again. This time we are testing approved database records. We only want to
 204       * display the records that have been approved. In this record set we have 89 approved records.
 205       */
 206      function test_advanced_search_sql_section() {
 207          global $DB;
 208  
 209          // Test 1
 210          $recordids = data_get_all_recordids($this->recorddata->id);
 211          $this->assertEquals(count($recordids), $this->datarecordcount);
 212  
 213          // Test 2
 214          $key = array_keys($this->recordsearcharray);
 215          $alias = $key[0];
 216          $newrecordids = data_get_recordids($alias, $this->recordsearcharray, $this->recorddata->id, $recordids);
 217          $this->assertEquals($this->datarecordset, $newrecordids);
 218  
 219          // Test 3
 220          $newrecordids = data_get_advance_search_ids($recordids, $this->recordsearcharray, $this->recorddata->id);
 221          $this->assertEquals($this->datarecordset, $newrecordids);
 222  
 223          // Test 3.1
 224          $resultrecordids = data_get_advance_search_ids(array(), $this->recordsearcharray, $this->recorddata->id);
 225          $this->assertEmpty($resultrecordids);
 226  
 227          // Test 4
 228          $sortorder = 'ORDER BY r.timecreated ASC , r.id ASC';
 229          $html = data_get_advanced_search_sql('0', $this->recorddata, $newrecordids, '', $sortorder);
 230          $allparams = array_merge($html['params'], array('dataid' => $this->recorddata->id));
 231          $records = $DB->get_records_sql($html['sql'], $allparams);
 232          $this->assertEquals($records, $this->finalrecord);
 233  
 234          // Test 5
 235          $groupsql = " AND (r.groupid = :currentgroup OR r.groupid = 0)";
 236          $params = array('currentgroup' => 1);
 237          $recordids = data_get_all_recordids($this->recorddata->id, $groupsql, $params);
 238          $this->assertEquals($this->groupdatarecordcount, count($recordids));
 239  
 240          // Test 6
 241          $approvesql = ' AND r.approved=1 ';
 242          $recordids = data_get_all_recordids($this->recorddata->id, $approvesql, $params);
 243          $this->assertEquals($this->approvedatarecordcount, count($recordids));
 244      }
 245  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1