/question/engine/simpletest/testdatalib.php

https://github.com/nigeldaley/moodle · PHP · 150 lines · 98 code · 24 blank · 28 comment · 1 complexity · 2cb079bc783d26c3eaa6119b0eeef7ea 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. require_once(dirname(__FILE__) . '/../lib.php');
  26. /**
  27. * Unit tests for some of the code in ../datalib.php.
  28. *
  29. * @copyright 2009 The Open University
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class qubaid_condition_test extends UnitTestCaseUsingDatabase {
  33. public function setUp() {
  34. parent::setUp();
  35. $this->switch_to_test_db();
  36. }
  37. public function tearDown() {
  38. $this->revert_to_real_db();
  39. parent::tearDown();
  40. }
  41. protected function check_typical_question_attempts_query(
  42. qubaid_condition $qubaids, $expectedsql, $expectedparams) {
  43. $sql = "SELECT qa.id, qa.maxmark
  44. FROM {$qubaids->from_question_attempts('qa')}
  45. WHERE {$qubaids->where()} AND qa.slot = :slot";
  46. $this->assertEqual($expectedsql, $sql);
  47. $params = $qubaids->from_where_params();
  48. $params['slot'] = 1;
  49. $this->assertEqual($expectedparams, $params);
  50. }
  51. protected function check_typical_in_query(qubaid_condition $qubaids,
  52. $expectedsql, $expectedparams) {
  53. $sql = "SELECT qa.id, qa.maxmark
  54. FROM {question_attempts} qa
  55. WHERE qa.questionusageid {$qubaids->usage_id_in()}";
  56. $this->assertEqual($expectedsql, $sql);
  57. $this->assertEqual($expectedparams, $qubaids->usage_id_in_params());
  58. }
  59. public function test_qubaid_list_one_join() {
  60. $qubaids = new qubaid_list(array(1));
  61. $this->check_typical_question_attempts_query($qubaids,
  62. "SELECT qa.id, qa.maxmark
  63. FROM {question_attempts} qa
  64. WHERE qa.questionusageid = :qubaid1 AND qa.slot = :slot",
  65. array('qubaid1' => 1, 'slot' => 1));
  66. }
  67. public function test_qubaid_list_several_join() {
  68. $qubaids = new qubaid_list(array(1, 3, 7));
  69. $this->check_typical_question_attempts_query($qubaids,
  70. "SELECT qa.id, qa.maxmark
  71. FROM {question_attempts} qa
  72. WHERE qa.questionusageid IN (:qubaid2,:qubaid3,:qubaid4) AND qa.slot = :slot",
  73. array('qubaid2' => 1, 'qubaid3' => 3, 'qubaid4' => 7, 'slot' => 1));
  74. }
  75. public function test_qubaid_join() {
  76. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1');
  77. $this->check_typical_question_attempts_query($qubaids,
  78. "SELECT qa.id, qa.maxmark
  79. FROM {other_table} ot
  80. JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid
  81. WHERE ot.id = 1 AND qa.slot = :slot", array('slot' => 1));
  82. }
  83. public function test_qubaid_join_no_where_join() {
  84. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid');
  85. $this->check_typical_question_attempts_query($qubaids,
  86. "SELECT qa.id, qa.maxmark
  87. FROM {other_table} ot
  88. JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid
  89. WHERE 1 = 1 AND qa.slot = :slot", array('slot' => 1));
  90. }
  91. public function test_qubaid_list_one_in() {
  92. global $CFG;
  93. $qubaids = new qubaid_list(array(1));
  94. $this->check_typical_in_query($qubaids,
  95. "SELECT qa.id, qa.maxmark
  96. FROM {question_attempts} qa
  97. WHERE qa.questionusageid = :qubaid5", array('qubaid5' => 1));
  98. }
  99. public function test_qubaid_list_several_in() {
  100. global $CFG;
  101. $qubaids = new qubaid_list(array(1, 2, 3));
  102. $this->check_typical_in_query($qubaids,
  103. "SELECT qa.id, qa.maxmark
  104. FROM {question_attempts} qa
  105. WHERE qa.questionusageid IN (:qubaid6,:qubaid7,:qubaid8)",
  106. array('qubaid6' => 1, 'qubaid7' => 2, 'qubaid8' => 3));
  107. }
  108. public function test_qubaid_join_in() {
  109. global $CFG;
  110. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1');
  111. $this->check_typical_in_query($qubaids,
  112. "SELECT qa.id, qa.maxmark
  113. FROM {question_attempts} qa
  114. WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE ot.id = 1)",
  115. array());
  116. }
  117. public function test_qubaid_join_no_where_in() {
  118. global $CFG;
  119. $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid');
  120. $this->check_typical_in_query($qubaids,
  121. "SELECT qa.id, qa.maxmark
  122. FROM {question_attempts} qa
  123. WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE 1 = 1)",
  124. array());
  125. }
  126. }