/backup/util/plan/tests/task_test.php

https://bitbucket.org/kudutest1/moodlegit · PHP · 140 lines · 72 code · 23 blank · 45 comment · 1 complexity · 02b06d6fdb1846f75769b2e222609de1 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. require_once(__DIR__.'/fixtures/plan_fixtures.php');
  24. /**
  25. * task tests (all)
  26. */
  27. class backup_task_testcase extends advanced_testcase {
  28. protected $moduleid; // course_modules id used for testing
  29. protected $sectionid; // course_sections id used for testing
  30. protected $courseid; // course id used for testing
  31. protected $userid; // user record used for testing
  32. protected function setUp() {
  33. global $DB, $CFG;
  34. parent::setUp();
  35. $this->resetAfterTest(true);
  36. $course = $this->getDataGenerator()->create_course();
  37. $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  38. $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  39. $this->moduleid = $coursemodule->id;
  40. $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
  41. $this->courseid = $coursemodule->course;
  42. $this->userid = 2; // admin
  43. // Disable all loggers
  44. $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  45. $CFG->backup_file_logger_level = backup::LOG_NONE;
  46. $CFG->backup_database_logger_level = backup::LOG_NONE;
  47. $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  48. }
  49. /**
  50. * test base_task class
  51. */
  52. function test_base_task() {
  53. $bp = new mock_base_plan('planname'); // We need one plan
  54. // Instantiate
  55. $bt = new mock_base_task('taskname', $bp);
  56. $this->assertTrue($bt instanceof base_task);
  57. $this->assertEquals($bt->get_name(), 'taskname');
  58. $this->assertTrue(is_array($bt->get_settings()));
  59. $this->assertEquals(count($bt->get_settings()), 0);
  60. $this->assertTrue(is_array($bt->get_steps()));
  61. $this->assertEquals(count($bt->get_steps()), 0);
  62. }
  63. /**
  64. * test backup_task class
  65. */
  66. function test_backup_task() {
  67. // We need one (non interactive) controller for instatiating plan
  68. $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  69. backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  70. // We need one plan
  71. $bp = new backup_plan($bc);
  72. // Instantiate task
  73. $bt = new mock_backup_task('taskname', $bp);
  74. $this->assertTrue($bt instanceof backup_task);
  75. $this->assertEquals($bt->get_name(), 'taskname');
  76. // Calculate checksum and check it
  77. $checksum = $bt->calculate_checksum();
  78. $this->assertTrue($bt->is_checksum_correct($checksum));
  79. }
  80. /**
  81. * wrong base_task class tests
  82. */
  83. function test_base_task_wrong() {
  84. // Try to pass one wrong plan
  85. try {
  86. $bt = new mock_base_task('tasktest', new stdclass());
  87. $this->assertTrue(false, 'base_task_exception expected');
  88. } catch (exception $e) {
  89. $this->assertTrue($e instanceof base_task_exception);
  90. $this->assertEquals($e->errorcode, 'wrong_base_plan_specified');
  91. }
  92. // Add wrong step to task
  93. $bp = new mock_base_plan('planname'); // We need one plan
  94. // Instantiate
  95. $bt = new mock_base_task('taskname', $bp);
  96. try {
  97. $bt->add_step(new stdclass());
  98. $this->assertTrue(false, 'base_task_exception expected');
  99. } catch (exception $e) {
  100. $this->assertTrue($e instanceof base_task_exception);
  101. $this->assertEquals($e->errorcode, 'wrong_base_step_specified');
  102. }
  103. }
  104. /**
  105. * wrong backup_task class tests
  106. */
  107. function test_backup_task_wrong() {
  108. // Try to pass one wrong plan
  109. try {
  110. $bt = new mock_backup_task('tasktest', new stdclass());
  111. $this->assertTrue(false, 'backup_task_exception expected');
  112. } catch (exception $e) {
  113. $this->assertTrue($e instanceof backup_task_exception);
  114. $this->assertEquals($e->errorcode, 'wrong_backup_plan_specified');
  115. }
  116. }
  117. }