[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/util/plan/tests/ -> step_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   * @package    core_backup
  19   * @category   phpunit
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  require_once (__DIR__.'/fixtures/plan_fixtures.php');
  27  
  28  
  29  /*
  30   * step tests (all)
  31   */
  32  class backup_step_testcase extends advanced_testcase {
  33  
  34      protected $moduleid;  // course_modules id used for testing
  35      protected $sectionid; // course_sections id used for testing
  36      protected $courseid;  // course id used for testing
  37      protected $userid;      // user record used for testing
  38  
  39      protected function setUp() {
  40          global $DB, $CFG;
  41          parent::setUp();
  42  
  43          $this->resetAfterTest(true);
  44  
  45          $course = $this->getDataGenerator()->create_course();
  46          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  47          $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  48  
  49          $this->moduleid  = $coursemodule->id;
  50          $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
  51          $this->courseid  = $coursemodule->course;
  52          $this->userid = 2; // admin
  53  
  54          // Disable all loggers
  55          $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  56          $CFG->backup_file_logger_level = backup::LOG_NONE;
  57          $CFG->backup_database_logger_level = backup::LOG_NONE;
  58          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  59      }
  60  
  61      /**
  62       * test base_step class
  63       */
  64      function test_base_step() {
  65  
  66          $bp = new mock_base_plan('planname'); // We need one plan
  67          $bt = new mock_base_task('taskname', $bp); // We need one task
  68          // Instantiate
  69          $bs = new mock_base_step('stepname', $bt);
  70          $this->assertTrue($bs instanceof base_step);
  71          $this->assertEquals($bs->get_name(), 'stepname');
  72      }
  73  
  74      /**
  75       * test backup_step class
  76       */
  77      function test_backup_step() {
  78  
  79          // We need one (non interactive) controller for instatiating plan
  80          $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  81              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  82          // We need one plan
  83          $bp = new backup_plan($bc);
  84          // We need one task
  85          $bt = new mock_backup_task('taskname', $bp);
  86          // Instantiate step
  87          $bs = new mock_backup_step('stepname', $bt);
  88          $this->assertTrue($bs instanceof backup_step);
  89          $this->assertEquals($bs->get_name(), 'stepname');
  90  
  91          $bc->destroy();
  92      }
  93  
  94      /**
  95       * test backup_structure_step class
  96       */
  97      function test_backup_structure_step() {
  98          global $CFG;
  99  
 100          $file = $CFG->tempdir . '/test/test_backup_structure_step.txt';
 101          // Remove the test dir and any content
 102          @remove_dir(dirname($file));
 103          // Recreate test dir
 104          if (!check_dir_exists(dirname($file), true, true)) {
 105              throw new moodle_exception('error_creating_temp_dir', 'error', dirname($file));
 106          }
 107  
 108          // We need one (non interactive) controller for instatiating plan
 109          $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 110              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
 111          // We need one plan
 112          $bp = new backup_plan($bc);
 113          // We need one task with mocked basepath
 114          $bt = new mock_backup_task_basepath('taskname');
 115          $bp->add_task($bt);
 116          // Instantiate backup_structure_step (and add it to task)
 117          $bs = new mock_backup_structure_step('steptest', basename($file), $bt);
 118          // Execute backup_structure_step
 119          $bs->execute();
 120  
 121          // Test file has been created
 122          $this->assertTrue(file_exists($file));
 123  
 124          // Some simple tests with contents
 125          $contents = file_get_contents($file);
 126          $this->assertTrue(strpos($contents, '<?xml version="1.0"') !== false);
 127          $this->assertTrue(strpos($contents, '<test id="1">') !== false);
 128          $this->assertTrue(strpos($contents, '<field1>value1</field1>') !== false);
 129          $this->assertTrue(strpos($contents, '<field2>value2</field2>') !== false);
 130          $this->assertTrue(strpos($contents, '</test>') !== false);
 131  
 132          $bc->destroy();
 133  
 134          unlink($file); // delete file
 135  
 136          // Remove the test dir and any content
 137          @remove_dir(dirname($file));
 138      }
 139  
 140  
 141      /**
 142       * Verify the add_plugin_structure() backup method behavior and created structures.
 143       */
 144      public function test_backup_structure_step_add_plugin_structure() {
 145          // Create mocked task, step and element.
 146          $bt = new mock_backup_task_basepath('taskname');
 147          $bs = new mock_backup_structure_step('steptest', null, $bt);
 148          $el = new backup_nested_element('question', array('id'), array('one', 'two', 'qtype'));
 149          // Wrong plugintype.
 150          try {
 151              $bs->add_plugin_structure('fakeplugin', $el, true);
 152              $this->assertTrue(false, 'base_step_exception expected');
 153          } catch (exception $e) {
 154              $this->assertTrue($e instanceof backup_step_exception);
 155              $this->assertEquals('incorrect_plugin_type', $e->errorcode);
 156          }
 157          // Correct plugintype qtype call (@ 'question' level).
 158          $bs->add_plugin_structure('qtype', $el, false);
 159          $ch = $el->get_children();
 160          $this->assertEquals(1, count($ch));
 161          $og = reset($ch);
 162          $this->assertTrue($og instanceof backup_optigroup);
 163          $ch = $og->get_children();
 164          $this->assertTrue(array_key_exists('optigroup_qtype_calculatedsimple_question', $ch));
 165          $this->assertTrue($ch['optigroup_qtype_calculatedsimple_question'] instanceof backup_plugin_element);
 166      }
 167  
 168      /**
 169       * Verify the add_subplugin_structure() backup method behavior and created structures.
 170       */
 171      public function test_backup_structure_step_add_subplugin_structure() {
 172          // Create mocked task, step and element.
 173          $bt = new mock_backup_task_basepath('taskname');
 174          $bs = new mock_backup_structure_step('steptest', null, $bt);
 175          $el = new backup_nested_element('workshop', array('id'), array('one', 'two', 'qtype'));
 176          // Wrong plugin type.
 177          try {
 178              $bs->add_subplugin_structure('fakesubplugin', $el, true, 'fakeplugintype', 'fakepluginname');
 179              $this->assertTrue(false, 'base_step_exception expected');
 180          } catch (exception $e) {
 181              $this->assertTrue($e instanceof backup_step_exception);
 182              $this->assertEquals('incorrect_plugin_type', $e->errorcode);
 183          }
 184          // Wrong plugin type.
 185          try {
 186              $bs->add_subplugin_structure('fakesubplugin', $el, true, 'mod', 'fakepluginname');
 187              $this->assertTrue(false, 'base_step_exception expected');
 188          } catch (exception $e) {
 189              $this->assertTrue($e instanceof backup_step_exception);
 190              $this->assertEquals('incorrect_plugin_name', $e->errorcode);
 191          }
 192          // Wrong plugin not having subplugins.
 193          try {
 194              $bs->add_subplugin_structure('fakesubplugin', $el, true, 'mod', 'page');
 195              $this->assertTrue(false, 'base_step_exception expected');
 196          } catch (exception $e) {
 197              $this->assertTrue($e instanceof backup_step_exception);
 198              $this->assertEquals('plugin_missing_subplugins_php_file', $e->errorcode);
 199          }
 200          // Wrong BC (defaulting to mod and modulename) use not having subplugins.
 201          try {
 202              $bt->set_modulename('page');
 203              $bs->add_subplugin_structure('fakesubplugin', $el, true);
 204              $this->assertTrue(false, 'base_step_exception expected');
 205          } catch (exception $e) {
 206              $this->assertTrue($e instanceof backup_step_exception);
 207              $this->assertEquals('plugin_missing_subplugins_php_file', $e->errorcode);
 208          }
 209          // Wrong subplugin type.
 210          try {
 211              $bs->add_subplugin_structure('fakesubplugin', $el, true, 'mod', 'workshop');
 212              $this->assertTrue(false, 'base_step_exception expected');
 213          } catch (exception $e) {
 214              $this->assertTrue($e instanceof backup_step_exception);
 215              $this->assertEquals('incorrect_subplugin_type', $e->errorcode);
 216          }
 217          // Wrong BC subplugin type.
 218          try {
 219              $bt->set_modulename('workshop');
 220              $bs->add_subplugin_structure('fakesubplugin', $el, true);
 221              $this->assertTrue(false, 'base_step_exception expected');
 222          } catch (exception $e) {
 223              $this->assertTrue($e instanceof backup_step_exception);
 224              $this->assertEquals('incorrect_subplugin_type', $e->errorcode);
 225          }
 226          // Correct call to workshopform subplugin (@ 'workshop' level).
 227          $bs->add_subplugin_structure('workshopform', $el, true, 'mod', 'workshop');
 228          $ch = $el->get_children();
 229          $this->assertEquals(1, count($ch));
 230          $og = reset($ch);
 231          $this->assertTrue($og instanceof backup_optigroup);
 232          $ch = $og->get_children();
 233          $this->assertTrue(array_key_exists('optigroup_workshopform_accumulative_workshop', $ch));
 234          $this->assertTrue($ch['optigroup_workshopform_accumulative_workshop'] instanceof backup_subplugin_element);
 235  
 236          // Correct BC call to workshopform subplugin (@ 'assessment' level).
 237          $el = new backup_nested_element('assessment', array('id'), array('one', 'two', 'qtype'));
 238          $bt->set_modulename('workshop');
 239          $bs->add_subplugin_structure('workshopform', $el, true);
 240          $ch = $el->get_children();
 241          $this->assertEquals(1, count($ch));
 242          $og = reset($ch);
 243          $this->assertTrue($og instanceof backup_optigroup);
 244          $ch = $og->get_children();
 245          $this->assertTrue(array_key_exists('optigroup_workshopform_accumulative_assessment', $ch));
 246          $this->assertTrue($ch['optigroup_workshopform_accumulative_assessment'] instanceof backup_subplugin_element);
 247  
 248          // TODO: Add some test covering a non-mod subplugin once we have some implemented in core.
 249      }
 250  
 251      /**
 252       * Verify the add_plugin_structure() restore method behavior and created structures.
 253       */
 254      public function test_restore_structure_step_add_plugin_structure() {
 255          // Create mocked task, step and element.
 256          $bt = new mock_restore_task_basepath('taskname');
 257          $bs = new mock_restore_structure_step('steptest', null, $bt);
 258          $el = new restore_path_element('question', '/some/path/to/question');
 259          // Wrong plugintype.
 260          try {
 261              $bs->add_plugin_structure('fakeplugin', $el);
 262              $this->assertTrue(false, 'base_step_exception expected');
 263          } catch (exception $e) {
 264              $this->assertTrue($e instanceof restore_step_exception);
 265              $this->assertEquals('incorrect_plugin_type', $e->errorcode);
 266          }
 267          // Correct plugintype qtype call (@ 'question' level).
 268          $bs->add_plugin_structure('qtype', $el);
 269          $patheles = $bs->get_pathelements();
 270          // Verify some well-known qtype plugin restore_path_elements have been added.
 271          $keys = array(
 272              '/some/path/to/question/plugin_qtype_calculated_question/answers/answer',
 273              '/some/path/to/question/plugin_qtype_calculated_question/dataset_definitions/dataset_definition',
 274              '/some/path/to/question/plugin_qtype_calculated_question/calculated_options/calculated_option',
 275              '/some/path/to/question/plugin_qtype_essay_question/essay',
 276              '/some/path/to/question/plugin_qtype_random_question',
 277              '/some/path/to/question/plugin_qtype_truefalse_question/answers/answer');
 278          foreach ($keys as $key) {
 279              // Verify the element exists.
 280              $this->assertArrayHasKey($key, $patheles);
 281              // Verify the element is a restore_path_element.
 282              $this->assertTrue($patheles[$key] instanceof restore_path_element);
 283              // Check it has a processing object.
 284              $po = $patheles[$key]->get_processing_object();
 285              $this->assertTrue($po instanceof restore_plugin);
 286          }
 287      }
 288  
 289      /**
 290       * Verify the add_subplugin_structure() restore method behavior and created structures.
 291       */
 292      public function test_restore_structure_step_add_subplugin_structure() {
 293          // Create mocked task, step and element.
 294          $bt = new mock_restore_task_basepath('taskname');
 295          $bs = new mock_restore_structure_step('steptest', null, $bt);
 296          $el = new restore_path_element('workshop', '/path/to/workshop');
 297          // Wrong plugin type.
 298          try {
 299              $bs->add_subplugin_structure('fakesubplugin', $el, 'fakeplugintype', 'fakepluginname');
 300              $this->assertTrue(false, 'base_step_exception expected');
 301          } catch (exception $e) {
 302              $this->assertTrue($e instanceof restore_step_exception);
 303              $this->assertEquals('incorrect_plugin_type', $e->errorcode);
 304          }
 305          // Wrong plugin type.
 306          try {
 307              $bs->add_subplugin_structure('fakesubplugin', $el, 'mod', 'fakepluginname');
 308              $this->assertTrue(false, 'base_step_exception expected');
 309          } catch (exception $e) {
 310              $this->assertTrue($e instanceof restore_step_exception);
 311              $this->assertEquals('incorrect_plugin_name', $e->errorcode);
 312          }
 313          // Wrong plugin not having subplugins.
 314          try {
 315              $bs->add_subplugin_structure('fakesubplugin', $el, 'mod', 'page');
 316              $this->assertTrue(false, 'base_step_exception expected');
 317          } catch (exception $e) {
 318              $this->assertTrue($e instanceof restore_step_exception);
 319              $this->assertEquals('plugin_missing_subplugins_php_file', $e->errorcode);
 320          }
 321          // Wrong BC (defaulting to mod and modulename) use not having subplugins.
 322          try {
 323              $bt->set_modulename('page');
 324              $bs->add_subplugin_structure('fakesubplugin', $el);
 325              $this->assertTrue(false, 'base_step_exception expected');
 326          } catch (exception $e) {
 327              $this->assertTrue($e instanceof restore_step_exception);
 328              $this->assertEquals('plugin_missing_subplugins_php_file', $e->errorcode);
 329          }
 330          // Wrong subplugin type.
 331          try {
 332              $bs->add_subplugin_structure('fakesubplugin', $el, 'mod', 'workshop');
 333              $this->assertTrue(false, 'base_step_exception expected');
 334          } catch (exception $e) {
 335              $this->assertTrue($e instanceof restore_step_exception);
 336              $this->assertEquals('incorrect_subplugin_type', $e->errorcode);
 337          }
 338          // Wrong BC subplugin type.
 339          try {
 340              $bt->set_modulename('workshop');
 341              $bs->add_subplugin_structure('fakesubplugin', $el);
 342              $this->assertTrue(false, 'base_step_exception expected');
 343          } catch (exception $e) {
 344              $this->assertTrue($e instanceof restore_step_exception);
 345              $this->assertEquals('incorrect_subplugin_type', $e->errorcode);
 346          }
 347          // Correct call to workshopform subplugin (@ 'workshop' level).
 348          $bt = new mock_restore_task_basepath('taskname');
 349          $bs = new mock_restore_structure_step('steptest', null, $bt);
 350          $el = new restore_path_element('workshop', '/path/to/workshop');
 351          $bs->add_subplugin_structure('workshopform', $el, 'mod', 'workshop');
 352          $patheles = $bs->get_pathelements();
 353          // Verify some well-known workshopform subplugin restore_path_elements have been added.
 354          $keys = array(
 355              '/path/to/workshop/subplugin_workshopform_accumulative_workshop/workshopform_accumulative_dimension',
 356              '/path/to/workshop/subplugin_workshopform_comments_workshop/workshopform_comments_dimension',
 357              '/path/to/workshop/subplugin_workshopform_numerrors_workshop/workshopform_numerrors_map',
 358              '/path/to/workshop/subplugin_workshopform_rubric_workshop/workshopform_rubric_config');
 359          foreach ($keys as $key) {
 360              // Verify the element exists.
 361              $this->assertArrayHasKey($key, $patheles);
 362              // Verify the element is a restore_path_element.
 363              $this->assertTrue($patheles[$key] instanceof restore_path_element);
 364              // Check it has a processing object.
 365              $po = $patheles[$key]->get_processing_object();
 366              $this->assertTrue($po instanceof restore_subplugin);
 367          }
 368  
 369          // Correct BC call to workshopform subplugin (@ 'assessment' level).
 370          $bt = new mock_restore_task_basepath('taskname');
 371          $bs = new mock_restore_structure_step('steptest', null, $bt);
 372          $el = new restore_path_element('assessment', '/a/assessment');
 373          $bt->set_modulename('workshop');
 374          $bs->add_subplugin_structure('workshopform', $el);
 375          $patheles = $bs->get_pathelements();
 376          // Verify some well-known workshopform subplugin restore_path_elements have been added.
 377          $keys = array(
 378              '/a/assessment/subplugin_workshopform_accumulative_assessment/workshopform_accumulative_grade',
 379              '/a/assessment/subplugin_workshopform_comments_assessment/workshopform_comments_grade',
 380              '/a/assessment/subplugin_workshopform_numerrors_assessment/workshopform_numerrors_grade',
 381              '/a/assessment/subplugin_workshopform_rubric_assessment/workshopform_rubric_grade');
 382          foreach ($keys as $key) {
 383              // Verify the element exists.
 384              $this->assertArrayHasKey($key, $patheles);
 385              // Verify the element is a restore_path_element.
 386              $this->assertTrue($patheles[$key] instanceof restore_path_element);
 387              // Check it has a processing object.
 388              $po = $patheles[$key]->get_processing_object();
 389              $this->assertTrue($po instanceof restore_subplugin);
 390          }
 391  
 392          // TODO: Add some test covering a non-mod subplugin once we have some implemented in core.
 393      }
 394  
 395      /**
 396       * wrong base_step class tests
 397       */
 398      function test_base_step_wrong() {
 399  
 400          // Try to pass one wrong task
 401          try {
 402              $bt = new mock_base_step('teststep', new stdclass());
 403              $this->assertTrue(false, 'base_step_exception expected');
 404          } catch (exception $e) {
 405              $this->assertTrue($e instanceof base_step_exception);
 406              $this->assertEquals($e->errorcode, 'wrong_base_task_specified');
 407          }
 408      }
 409  
 410      /**
 411       * wrong backup_step class tests
 412       */
 413      function test_backup_test_wrong() {
 414  
 415          // Try to pass one wrong task
 416          try {
 417              $bt = new mock_backup_step('teststep', new stdclass());
 418              $this->assertTrue(false, 'backup_step_exception expected');
 419          } catch (exception $e) {
 420              $this->assertTrue($e instanceof backup_step_exception);
 421              $this->assertEquals($e->errorcode, 'wrong_backup_task_specified');
 422          }
 423      }
 424  }


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