PageRenderTime 53ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/util/helper/tests/converterhelper_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 145 lines | 84 code | 17 blank | 44 comment | 1 complexity | 95db547c8b1608a914e6c627ce11abcb 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/helper/convert_helper.class.php');
  26. /**
  27. * Provides access to the protected methods we need to test
  28. */
  29. class testable_convert_helper extends convert_helper {
  30. public static function choose_conversion_path($format, array $descriptions) {
  31. return parent::choose_conversion_path($format, $descriptions);
  32. }
  33. }
  34. /**
  35. * Defines the test methods
  36. */
  37. class backup_convert_helper_testcase extends basic_testcase {
  38. public function test_choose_conversion_path() {
  39. // no converters available
  40. $descriptions = array();
  41. $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
  42. $this->assertEquals($path, array());
  43. // missing source and/or targets
  44. $descriptions = array(
  45. // some custom converter
  46. 'exporter' => array(
  47. 'from' => backup::FORMAT_MOODLE1,
  48. 'to' => 'some_custom_format',
  49. 'cost' => 10,
  50. ),
  51. // another custom converter
  52. 'converter' => array(
  53. 'from' => 'yet_another_crazy_custom_format',
  54. 'to' => backup::FORMAT_MOODLE,
  55. 'cost' => 10,
  56. ),
  57. );
  58. $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
  59. $this->assertEquals($path, array());
  60. $path = testable_convert_helper::choose_conversion_path('some_other_custom_format', $descriptions);
  61. $this->assertEquals($path, array());
  62. // single step conversion
  63. $path = testable_convert_helper::choose_conversion_path('yet_another_crazy_custom_format', $descriptions);
  64. $this->assertEquals($path, array('converter'));
  65. // no conversion needed - this is supposed to be detected by the caller
  66. $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE, $descriptions);
  67. $this->assertEquals($path, array());
  68. // two alternatives
  69. $descriptions = array(
  70. // standard moodle 1.9 -> 2.x converter
  71. 'moodle1' => array(
  72. 'from' => backup::FORMAT_MOODLE1,
  73. 'to' => backup::FORMAT_MOODLE,
  74. 'cost' => 10,
  75. ),
  76. // alternative moodle 1.9 -> 2.x converter
  77. 'alternative' => array(
  78. 'from' => backup::FORMAT_MOODLE1,
  79. 'to' => backup::FORMAT_MOODLE,
  80. 'cost' => 8,
  81. )
  82. );
  83. $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
  84. $this->assertEquals($path, array('alternative'));
  85. // complex case
  86. $descriptions = array(
  87. // standard moodle 1.9 -> 2.x converter
  88. 'moodle1' => array(
  89. 'from' => backup::FORMAT_MOODLE1,
  90. 'to' => backup::FORMAT_MOODLE,
  91. 'cost' => 10,
  92. ),
  93. // alternative moodle 1.9 -> 2.x converter
  94. 'alternative' => array(
  95. 'from' => backup::FORMAT_MOODLE1,
  96. 'to' => backup::FORMAT_MOODLE,
  97. 'cost' => 8,
  98. ),
  99. // custom converter from 1.9 -> custom 'CFv1' format
  100. 'cc1' => array(
  101. 'from' => backup::FORMAT_MOODLE1,
  102. 'to' => 'CFv1',
  103. 'cost' => 2,
  104. ),
  105. // custom converter from custom 'CFv1' format -> moodle 2.0 format
  106. 'cc2' => array(
  107. 'from' => 'CFv1',
  108. 'to' => backup::FORMAT_MOODLE,
  109. 'cost' => 5,
  110. ),
  111. // custom converter from CFv1 -> CFv2 format
  112. 'cc3' => array(
  113. 'from' => 'CFv1',
  114. 'to' => 'CFv2',
  115. 'cost' => 2,
  116. ),
  117. // custom converter from CFv2 -> moodle 2.0 format
  118. 'cc4' => array(
  119. 'from' => 'CFv2',
  120. 'to' => backup::FORMAT_MOODLE,
  121. 'cost' => 2,
  122. ),
  123. );
  124. // ask the helper to find the most effective way
  125. $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
  126. $this->assertEquals($path, array('cc1', 'cc3', 'cc4'));
  127. }
  128. }