PageRenderTime 24ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/question/engine/tests/datalib_test.php

https://github.com/mayankgupta/moodle
PHP | 161 lines | 106 code | 25 blank | 30 comment | 2 complexity | b2ffdf8ae913606a788ddbd5f24758d4 MD5 | raw file
  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. * This file contains tests for some of the code in ../datalib.php.
  18. *
  19. * @package moodlecore
  20. * @subpackage questionengine
  21. * @copyright 2009 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once(dirname(__FILE__) . '/../lib.php');
  27. /**
  28. * Unit tests for some of the code in ../datalib.php.
  29. *
  30. * @copyright 2009 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class qubaid_condition_test extends advanced_testcase {
  34. protected function normalize_sql($sql, $params) {
  35. $newparams = array();
  36. preg_match_all('/(?<!:):([a-z][a-z0-9_]*)/', $sql, $named_matches);
  37. foreach($named_matches[1] as $param) {
  38. if (array_key_exists($param, $params)) {
  39. $newparams[] = $params[$param];
  40. }
  41. }
  42. $newsql = preg_replace('/(?<!:):[a-z][a-z0-9_]*/', '?', $sql);
  43. return array($newsql, $newparams);
  44. }
  45. protected function check_typical_question_attempts_query(
  46. qubaid_condition $qubaids, $expectedsql, $expectedparams) {
  47. $sql = "SELECT qa.id, qa.maxmark
  48. FROM {$qubaids->from_question_attempts('qa')}
  49. WHERE {$qubaids->where()} AND qa.slot = :slot";
  50. $params = $qubaids->from_where_params();
  51. $params['slot'] = 1;
  52. // NOTE: parameter names may change thanks to $DB->inorequaluniqueindex, normal comparison is very wrong!!
  53. list($sql, $params) = $this->normalize_sql($sql, $params);
  54. list($expectedsql, $expectedparams) = $this->normalize_sql($expectedsql, $expectedparams);
  55. $this->assertEquals($expectedsql, $sql);
  56. $this->assertEquals($expectedparams, $params);
  57. }
  58. protected function check_typical_in_query(qubaid_condition $qubaids,
  59. $expectedsql, $expectedparams) {
  60. $sql = "SELECT qa.id, qa.maxmark
  61. FROM {question_attempts} qa
  62. WHERE qa.questionusageid {$qubaids->usage_id_in()}";
  63. // NOTE: parameter names may change thanks to $DB->inorequaluniqueindex, normal comparison is very wrong!!
  64. list($sql, $params) = $this->normalize_sql($sql, $qubaids->usage_id_in_params());
  65. list($expectedsql, $expectedparams) = $this->normalize_sql($expectedsql, $expectedparams);
  66. $this->assertEquals($expectedsql, $sql);
  67. $this->assertEquals($expectedparams, $params);
  68. }
  69. public function test_qubaid_list_one_join() {
  70. $qubaids = new qubaid_list(array(1));
  71. $this->check_typical_question_attempts_query($qubaids,
  72. "SELECT qa.id, qa.maxmark
  73. FROM {question_attempts} qa
  74. WHERE qa.questionusageid = :qubaid1 AND qa.slot = :slot",
  75. array('qubaid1' => 1, 'slot' => 1));
  76. }
  77. public function test_qubaid_list_several_join() {
  78. $qubaids = new qubaid_list(array(1, 3, 7));
  79. $this->check_typical_question_attempts_query($qubaids,
  80. "SELECT qa.id, qa.maxmark
  81. FROM {question_attempts} qa
  82. WHERE qa.questionusageid IN (:qubaid2,:qubaid3,:qubaid4) AND qa.slot = :slot",
  83. array('qubaid2' => 1, 'qubaid3' => 3, 'qubaid4' => 7, 'slot' => 1));
  84. }
  85. public function test_qubaid_join() {
  86. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1');
  87. $this->check_typical_question_attempts_query($qubaids,
  88. "SELECT qa.id, qa.maxmark
  89. FROM {other_table} ot
  90. JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid
  91. WHERE ot.id = 1 AND qa.slot = :slot", array('slot' => 1));
  92. }
  93. public function test_qubaid_join_no_where_join() {
  94. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid');
  95. $this->check_typical_question_attempts_query($qubaids,
  96. "SELECT qa.id, qa.maxmark
  97. FROM {other_table} ot
  98. JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid
  99. WHERE 1 = 1 AND qa.slot = :slot", array('slot' => 1));
  100. }
  101. public function test_qubaid_list_one_in() {
  102. global $CFG;
  103. $qubaids = new qubaid_list(array(1));
  104. $this->check_typical_in_query($qubaids,
  105. "SELECT qa.id, qa.maxmark
  106. FROM {question_attempts} qa
  107. WHERE qa.questionusageid = :qubaid5", array('qubaid5' => 1));
  108. }
  109. public function test_qubaid_list_several_in() {
  110. global $CFG;
  111. $qubaids = new qubaid_list(array(1, 2, 3));
  112. $this->check_typical_in_query($qubaids,
  113. "SELECT qa.id, qa.maxmark
  114. FROM {question_attempts} qa
  115. WHERE qa.questionusageid IN (:qubaid6,:qubaid7,:qubaid8)",
  116. array('qubaid6' => 1, 'qubaid7' => 2, 'qubaid8' => 3));
  117. }
  118. public function test_qubaid_join_in() {
  119. global $CFG;
  120. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1');
  121. $this->check_typical_in_query($qubaids,
  122. "SELECT qa.id, qa.maxmark
  123. FROM {question_attempts} qa
  124. WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE ot.id = 1)",
  125. array());
  126. }
  127. public function test_qubaid_join_no_where_in() {
  128. global $CFG;
  129. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid');
  130. $this->check_typical_in_query($qubaids,
  131. "SELECT qa.id, qa.maxmark
  132. FROM {question_attempts} qa
  133. WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE 1 = 1)",
  134. array());
  135. }
  136. }