Merge branch 'master' into backup-convert
[moodle.git] / backup / util / helper / simpletest / testconverthelper.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Unit tests for backup/util/helper/convert_helper.class.php
20  *
21  * @package    core
22  * @subpackage backup-convert
23  * @copyright  2011 David Mudrak <david@moodle.com>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php');
31 /**
32  * Provides access to the protected methods we need to test
33  */
34 class testable_convert_helper extends convert_helper {
36     public static function choose_conversion_path($format, array $descriptions) {
37         return parent::choose_conversion_path($format, $descriptions);
38     }
39 }
41 /**
42  * Defines the test methods
43  */
44 class convert_helper_test extends UnitTestCase {
46     public static $includecoverage = array();
48     public function test_choose_conversion_path() {
50         // no converters available
51         $descriptions = array();
52         $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
53         $this->assertEqual($path, array());
55         // missing source and/or targets
56         $descriptions = array(
57             // some custom converter
58             'exporter' => array(
59                 'from'  => backup::FORMAT_MOODLE1,
60                 'to'    => 'some_custom_format',
61                 'cost'  => 10,
62             ),
63             // another custom converter
64             'converter' => array(
65                 'from'  => 'yet_another_crazy_custom_format',
66                 'to'    => backup::FORMAT_MOODLE,
67                 'cost'  => 10,
68             ),
69         );
70         $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
71         $this->assertEqual($path, array());
73         $path = testable_convert_helper::choose_conversion_path('some_other_custom_format', $descriptions);
74         $this->assertEqual($path, array());
76         // single step conversion
77         $path = testable_convert_helper::choose_conversion_path('yet_another_crazy_custom_format', $descriptions);
78         $this->assertEqual($path, array('converter'));
80         // no conversion needed - this is supposed to be detected by the caller
81         $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE, $descriptions);
82         $this->assertEqual($path, array());
84         // two alternatives
85         $descriptions = array(
86             // standard moodle 1.9 -> 2.x converter
87             'moodle1' => array(
88                 'from'  => backup::FORMAT_MOODLE1,
89                 'to'    => backup::FORMAT_MOODLE,
90                 'cost'  => 10,
91             ),
92             // alternative moodle 1.9 -> 2.x converter
93             'alternative' => array(
94                 'from'  => backup::FORMAT_MOODLE1,
95                 'to'    => backup::FORMAT_MOODLE,
96                 'cost'  => 8,
97             )
98         );
99         $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
100         $this->assertEqual($path, array('alternative'));
102         // complex case
103         $descriptions = array(
104             // standard moodle 1.9 -> 2.x converter
105             'moodle1' => array(
106                 'from'  => backup::FORMAT_MOODLE1,
107                 'to'    => backup::FORMAT_MOODLE,
108                 'cost'  => 10,
109             ),
110             // alternative moodle 1.9 -> 2.x converter
111             'alternative' => array(
112                 'from'  => backup::FORMAT_MOODLE1,
113                 'to'    => backup::FORMAT_MOODLE,
114                 'cost'  => 8,
115             ),
116             // custom converter from 1.9 -> custom 'CFv1' format
117             'cc1' => array(
118                 'from'  => backup::FORMAT_MOODLE1,
119                 'to'    => 'CFv1',
120                 'cost'  => 2,
121             ),
122             // custom converter from custom 'CFv1' format -> moodle 2.0 format
123             'cc2' => array(
124                 'from'  => 'CFv1',
125                 'to'    => backup::FORMAT_MOODLE,
126                 'cost'  => 5,
127             ),
128             // custom converter from CFv1 -> CFv2 format
129             'cc3' => array(
130                 'from'  => 'CFv1',
131                 'to'    => 'CFv2',
132                 'cost'  => 2,
133             ),
134             // custom converter from CFv2 -> moodle 2.0 format
135             'cc4' => array(
136                 'from'  => 'CFv2',
137                 'to'    => backup::FORMAT_MOODLE,
138                 'cost'  => 2,
139             ),
140         );
142         // ask the helper to find the most effective way
143         $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions);
144         $this->assertEqual($path, array('cc1', 'cc3', 'cc4'));
145     }