PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/backup/util/checks/tests/checks_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 136 lines | 81 code | 20 blank | 35 comment | 1 complexity | b50e6b6a898262369f013cd6427d020d 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. * @package core_backup
  18. * @category phpunit
  19. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. defined('MOODLE_INTERNAL') || die();
  23. // Include all the needed stuff
  24. global $CFG;
  25. require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  26. /*
  27. * check tests (all)
  28. */
  29. class backup_check_testcase extends advanced_testcase {
  30. protected $moduleid; // course_modules id used for testing
  31. protected $sectionid; // course_sections id used for testing
  32. protected $courseid; // course id used for testing
  33. protected $userid; // user record id
  34. protected function setUp() {
  35. global $DB, $CFG;
  36. parent::setUp();
  37. $this->resetAfterTest(true);
  38. $course = $this->getDataGenerator()->create_course(array(), array('createsections' => true));
  39. $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  40. $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  41. $this->moduleid = $coursemodule->id;
  42. $this->sectionid = $coursemodule->section;
  43. $this->courseid = $coursemodule->course;
  44. $this->userid = 2; // admin
  45. $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  46. $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
  47. $CFG->backup_file_logger_level = backup::LOG_NONE;
  48. $CFG->backup_database_logger_level = backup::LOG_NONE;
  49. unset($CFG->backup_file_logger_extra);
  50. $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  51. }
  52. /*
  53. * test backup_check class
  54. */
  55. public function test_backup_check() {
  56. // Check against existing course module/section course or fail
  57. $this->assertTrue(backup_check::check_id(backup::TYPE_1ACTIVITY, $this->moduleid));
  58. $this->assertTrue(backup_check::check_id(backup::TYPE_1SECTION, $this->sectionid));
  59. $this->assertTrue(backup_check::check_id(backup::TYPE_1COURSE, $this->courseid));
  60. $this->assertTrue(backup_check::check_user($this->userid));
  61. // Check against non-existing course module/section/course (0)
  62. try {
  63. backup_check::check_id(backup::TYPE_1ACTIVITY, 0);
  64. $this->assertTrue(false, 'backup_controller_exception expected');
  65. } catch (exception $e) {
  66. $this->assertTrue($e instanceof backup_controller_exception);
  67. $this->assertEquals($e->errorcode, 'backup_check_module_not_exists');
  68. }
  69. try {
  70. backup_check::check_id(backup::TYPE_1SECTION, 0);
  71. $this->assertTrue(false, 'backup_controller_exception expected');
  72. } catch (exception $e) {
  73. $this->assertTrue($e instanceof backup_controller_exception);
  74. $this->assertEquals($e->errorcode, 'backup_check_section_not_exists');
  75. }
  76. try {
  77. backup_check::check_id(backup::TYPE_1COURSE, 0);
  78. $this->assertTrue(false, 'backup_controller_exception expected');
  79. } catch (exception $e) {
  80. $this->assertTrue($e instanceof backup_controller_exception);
  81. $this->assertEquals($e->errorcode, 'backup_check_course_not_exists');
  82. }
  83. // Try wrong type
  84. try {
  85. backup_check::check_id(12345678,0);
  86. $this->assertTrue(false, 'backup_controller_exception expected');
  87. } catch (exception $e) {
  88. $this->assertTrue($e instanceof backup_controller_exception);
  89. $this->assertEquals($e->errorcode, 'backup_check_incorrect_type');
  90. }
  91. // Test non-existing user
  92. $userid = 0;
  93. try {
  94. backup_check::check_user($userid);
  95. $this->assertTrue(false, 'backup_controller_exception expected');
  96. } catch (exception $e) {
  97. $this->assertTrue($e instanceof backup_controller_exception);
  98. $this->assertEquals($e->errorcode, 'backup_check_user_not_exists');
  99. }
  100. // Security check tests
  101. // Try to pass wrong controller
  102. try {
  103. backup_check::check_security(new stdclass(), true);
  104. $this->assertTrue(false, 'backup_controller_exception expected');
  105. } catch (exception $e) {
  106. $this->assertTrue($e instanceof backup_controller_exception);
  107. $this->assertEquals($e->errorcode, 'backup_check_security_requires_backup_controller');
  108. }
  109. // Pass correct controller, check must return true in any case with $apply enabled
  110. // and $bc must continue being mock_backup_controller
  111. $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  112. backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  113. $this->assertTrue(backup_check::check_security($bc, true));
  114. $this->assertTrue($bc instanceof backup_controller);
  115. }
  116. }