[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tool/uploadcourse/tests/ -> helper_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   * File containing tests for the helper.
  19   *
  20   * @package    tool_uploadcourse
  21   * @copyright  2013 Frédéric Massart
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  
  29  /**
  30   * Helper test case.
  31   *
  32   * @package    tool_uploadcourse
  33   * @copyright  2013 Frédéric Massart
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class tool_uploadcourse_helper_testcase extends advanced_testcase {
  37  
  38      public function test_generate_shortname() {
  39          $data = (object) array('fullname' => 'Ah bh Ch 01 02 03', 'idnumber' => 'ID123');
  40  
  41          $this->assertSame($data->fullname, tool_uploadcourse_helper::generate_shortname($data, '%f'));
  42          $this->assertSame($data->idnumber, tool_uploadcourse_helper::generate_shortname($data, '%i'));
  43          $this->assertSame('Ah Bh Ch', tool_uploadcourse_helper::generate_shortname($data, '%~8f'));
  44          $this->assertSame('AH BH CH', tool_uploadcourse_helper::generate_shortname($data, '%+8f'));
  45          $this->assertSame('id123', tool_uploadcourse_helper::generate_shortname($data, '%-i'));
  46          $this->assertSame('[Ah bh Ch] = ID123', tool_uploadcourse_helper::generate_shortname($data, '[%8f] = %i'));
  47          $this->assertSame('0', tool_uploadcourse_helper::generate_shortname($data, '0'));
  48          $this->assertSame('%unknown', tool_uploadcourse_helper::generate_shortname($data, '%unknown'));
  49  
  50          $this->assertNull(tool_uploadcourse_helper::generate_shortname($data, ''));
  51          $this->assertNull(tool_uploadcourse_helper::generate_shortname(array(), '%f'));
  52      }
  53  
  54      public function test_get_course_formats() {
  55          $result = tool_uploadcourse_helper::get_course_formats();
  56          $this->assertSame(array_keys(core_component::get_plugin_list('format')), $result);
  57          // Should be similar as first result, as cached.
  58          $this->assertSame($result, tool_uploadcourse_helper::get_course_formats());
  59      }
  60  
  61      public function test_get_enrolment_data() {
  62          $this->resetAfterTest(true);
  63          $data = array(
  64              'enrolment_1' => 'unknown',
  65              'enrolment_1_foo' => '1',
  66              'enrolment_1_bar' => '2',
  67              'enrolment_2' => 'self',
  68              'enrolment_2_delete' => '1',
  69              'enrolment_2_foo' => 'a',
  70              'enrolment_2_bar' => '1',
  71              'enrolment_3' => 'manual',
  72              'enrolment_3_disable' => '2',
  73              'enrolment_3_foo' => 'b',
  74              'enrolment_3_bar' => '2',
  75              'enrolment_4' => 'database',
  76              'enrolment_4_foo' => 'x',
  77              'enrolment_4_bar' => '3',
  78              'enrolment_5_test3' => 'test3',
  79              'enrolment_5_test2' => 'test2',
  80              'enrolment_5_test1' => 'test1',
  81              'enrolment_5' => 'flatfile',
  82          );
  83          $expected = array(
  84              'self' => array(
  85                  'delete' => '1',
  86                  'foo' => 'a',
  87                  'bar' => '1',
  88              ),
  89              'manual' => array(
  90                  'disable' => '2',
  91                  'foo' => 'b',
  92                  'bar' => '2',
  93              ),
  94              'database' => array(
  95                  'foo' => 'x',
  96                  'bar' => '3',
  97              ),
  98              'flatfile' => array(
  99                  'test3' => 'test3',
 100                  'test2' => 'test2',
 101                  'test1' => 'test1',
 102              )
 103          );
 104          $this->assertSame(tool_uploadcourse_helper::get_enrolment_data($data), $expected);
 105      }
 106  
 107      public function test_get_enrolment_plugins() {
 108          $this->resetAfterTest(true);
 109          $actual = tool_uploadcourse_helper::get_enrolment_plugins();
 110          $this->assertSame(array_keys(enrol_get_plugins(false)), array_keys($actual));
 111          // This should be identical as cached.
 112          $secondactual = tool_uploadcourse_helper::get_enrolment_plugins();
 113          $this->assertEquals($actual, $secondactual);
 114      }
 115  
 116      public function test_get_restore_content_dir() {
 117          global $CFG;
 118          $this->resetAfterTest(true);
 119          $this->setAdminUser();
 120  
 121          $c1 = $this->getDataGenerator()->create_course();
 122          $c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay'));
 123  
 124          // Creating backup file.
 125          $bc = new backup_controller(backup::TYPE_1COURSE, $c1->id, backup::FORMAT_MOODLE,
 126              backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
 127          $bc->execute_plan();
 128          $result = $bc->get_results();
 129          $this->assertTrue(isset($result['backup_destination']));
 130          $c1backupfile = $result['backup_destination']->copy_content_to_temp();
 131          $bc->destroy();
 132          unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
 133  
 134          // Creating backup file.
 135          $bc = new backup_controller(backup::TYPE_1COURSE, $c2->id, backup::FORMAT_MOODLE,
 136              backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
 137          $bc->execute_plan();
 138          $result = $bc->get_results();
 139          $this->assertTrue(isset($result['backup_destination']));
 140          $c2backupfile = $result['backup_destination']->copy_content_to_temp();
 141          $bc->destroy();
 142          unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
 143  
 144          $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
 145          $CFG->keeptempdirectoriesonbackup = true;
 146  
 147          // Checking restore dir.
 148          $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
 149          $bcinfo = backup_general_helper::get_backup_information($dir);
 150          $this->assertEquals($bcinfo->original_course_id, $c1->id);
 151          $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
 152  
 153          // Do it again, it should be the same directory.
 154          $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
 155          $this->assertEquals($dir, $dir2);
 156  
 157          // Get the second course.
 158          $dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null);
 159          $bcinfo = backup_general_helper::get_backup_information($dir);
 160          $this->assertEquals($bcinfo->original_course_id, $c2->id);
 161          $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
 162  
 163          // Checking with a shortname.
 164          $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
 165          $bcinfo = backup_general_helper::get_backup_information($dir);
 166          $this->assertEquals($bcinfo->original_course_id, $c1->id);
 167          $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
 168  
 169          // Do it again, it should be the same directory.
 170          $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
 171          $this->assertEquals($dir, $dir2);
 172  
 173          // Get the second course.
 174          $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname);
 175          $bcinfo = backup_general_helper::get_backup_information($dir);
 176          $this->assertEquals($bcinfo->original_course_id, $c2->id);
 177          $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
 178  
 179          // Get a course that does not exist.
 180          $errors = array();
 181          $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
 182          $this->assertFalse($dir);
 183          $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
 184  
 185          // Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching.
 186          $CFG->keeptempdirectoriesonbackup = false;
 187  
 188          // Checking restore dir.
 189          $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
 190          $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
 191          $this->assertNotEquals($dir, $dir2);
 192  
 193          // Checking with a shortname.
 194          $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
 195          $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
 196          $this->assertNotEquals($dir, $dir2);
 197  
 198          // Get a course that does not exist.
 199          $errors = array();
 200          $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
 201          $this->assertFalse($dir);
 202          $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
 203          $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
 204          $this->assertEquals($dir, $dir2);
 205  
 206          $CFG->keeptempdirectoriesonbackup = $oldcfg;
 207      }
 208  
 209      public function test_get_role_ids() {
 210          $this->getDataGenerator();
 211          // Mimic function result.
 212          $expected = array();
 213          $roles = get_all_roles();
 214          foreach ($roles as $role) {
 215              $expected[$role->shortname] = $role->id;
 216          }
 217  
 218          $actual = tool_uploadcourse_helper::get_role_ids();
 219          $this->assertSame($actual, $expected);
 220  
 221          // Check cache.
 222          $this->assertSame($actual, tool_uploadcourse_helper::get_role_ids());
 223      }
 224  
 225      public function test_get_role_names() {
 226          $this->resetAfterTest(true);
 227  
 228          create_role('Villain', 'villain', 'The bad guys');
 229          $data = array(
 230              'role_student' => 'Padawan',
 231              'role_teacher' => 'Guardian',
 232              'role_editingteacher' => 'Knight',
 233              'role_manager' => 'Master',
 234              'role_villain' => 'Jabba the Hutt',
 235              'role_android' => 'R2D2',
 236          );
 237  
 238          // Get the role IDs, but need to force the cache reset as a new role is defined.
 239          $roleids = tool_uploadcourse_helper::get_role_ids(true);
 240  
 241          $expected = array(
 242              'role_' . $roleids['student'] => 'Padawan',
 243              'role_' . $roleids['teacher'] => 'Guardian',
 244              'role_' . $roleids['editingteacher'] => 'Knight',
 245              'role_' . $roleids['manager'] => 'Master',
 246              'role_' . $roleids['villain'] => 'Jabba the Hutt',
 247          );
 248  
 249          $errors = array();
 250          $actual = tool_uploadcourse_helper::get_role_names($data, $errors);
 251          $this->assertSame($actual, $expected);
 252          $this->assertArrayHasKey('invalidroles', $errors);
 253      }
 254  
 255      public function test_increment_idnumber() {
 256          $this->resetAfterTest(true);
 257  
 258          $c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'C1'));
 259          $c2 = $this->getDataGenerator()->create_course(array('idnumber' => 'C2'));
 260          $c3 = $this->getDataGenerator()->create_course(array('idnumber' => 'Yo'));
 261  
 262          $this->assertEquals('C3', tool_uploadcourse_helper::increment_idnumber('C1'));
 263          $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_idnumber('Yo'));
 264          $this->assertEquals('DoesNotExist', tool_uploadcourse_helper::increment_idnumber('DoesNotExist'));
 265      }
 266  
 267      public function test_increment_shortname() {
 268          $this->resetAfterTest(true);
 269  
 270          $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'C1'));
 271          $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'C2'));
 272          $c3 = $this->getDataGenerator()->create_course(array('shortname' => 'Yo'));
 273  
 274          // FYI: increment_shortname assumes that the course exists, and so increment the shortname immediately.
 275          $this->assertEquals('C3', tool_uploadcourse_helper::increment_shortname('C1'));
 276          $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_shortname('Yo'));
 277          $this->assertEquals('DoesNotExist_2', tool_uploadcourse_helper::increment_shortname('DoesNotExist'));
 278      }
 279  
 280      public function test_resolve_category() {
 281          $this->resetAfterTest(true);
 282  
 283          $c1 = $this->getDataGenerator()->create_category(array('name' => 'First level'));
 284          $c2 = $this->getDataGenerator()->create_category(array('name' => 'Second level', 'parent' => $c1->id));
 285          $c3 = $this->getDataGenerator()->create_category(array('idnumber' => 'C3'));
 286  
 287          $data = array(
 288              'category' => $c1->id,
 289              'category_path' => $c1->name . ' / ' . $c2->name,
 290              'category_idnumber' => $c3->idnumber,
 291          );
 292  
 293          $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category($data));
 294          unset($data['category']);
 295          $this->assertEquals($c3->id, tool_uploadcourse_helper::resolve_category($data));
 296          unset($data['category_idnumber']);
 297          $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data));
 298  
 299          // Adding unexisting data.
 300          $errors = array();
 301          $data['category_idnumber'] = 1234;
 302          $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
 303          $this->assertArrayHasKey('couldnotresolvecatgorybyidnumber', $errors);
 304          $errors = array();
 305          $data['category'] = 1234;
 306          $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
 307          $this->assertArrayHasKey('couldnotresolvecatgorybyid', $errors);
 308          $errors = array();
 309          $data['category_path'] = 'Not exist';
 310          $this->assertEmpty(tool_uploadcourse_helper::resolve_category($data, $errors));
 311          $this->assertArrayHasKey('couldnotresolvecatgorybypath', $errors);
 312      }
 313  
 314      public function test_resolve_category_by_idnumber() {
 315          $this->resetAfterTest(true);
 316  
 317          $c1 = $this->getDataGenerator()->create_category(array('idnumber' => 'C1'));
 318          $c2 = $this->getDataGenerator()->create_category(array('idnumber' => 'C2'));
 319  
 320          // Doubled for cache check.
 321          $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
 322          $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
 323          $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
 324          $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
 325          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
 326          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
 327      }
 328  
 329      public function test_resolve_category_by_path() {
 330          $this->resetAfterTest(true);
 331  
 332          $cat1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1'));
 333          $cat1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1', 'parent' => $cat1->id));
 334          $cat1_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.1', 'parent' => $cat1_1->id));
 335          $cat1_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.2', 'parent' => $cat1_1->id));
 336          $cat1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.2', 'parent' => $cat1->id));
 337  
 338          $cat2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2'));
 339          $cat2_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1', 'parent' => $cat2->id, 'visible' => false));
 340          $cat2_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.1', 'parent' => $cat2_1->id));
 341          $cat2_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.2', 'parent' => $cat2_1->id));
 342          $cat2_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.2', 'parent' => $cat2->id));
 343  
 344          $cat3 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3'));
 345          $cat3_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
 346          $cat3_1b = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
 347          $cat3_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3_1->id));
 348          $cat3_fakedouble = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3->id));
 349  
 350          // Existing categories. Doubled for cache testing.
 351          $path = array('Cat 1');
 352          $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 353          $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 354  
 355          $path = array('Cat 1', 'Cat 1.1', 'Cat 1.1.2');
 356          $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 357          $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 358  
 359          $path = array('Cat 1', 'Cat 1.2');
 360          $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 361          $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 362  
 363          $path = array('Cat 2');
 364          $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 365          $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 366  
 367          // Hidden category.
 368          $path = array('Cat 2', 'Cat 2.1');
 369          $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 370          $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 371  
 372          // Hidden parent.
 373          $path = array('Cat 2', 'Cat 2.1', 'Cat 2.1.2');
 374          $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 375          $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 376  
 377          // Does not exist.
 378          $path = array('No cat 3', 'Cat 1.2');
 379          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 380          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 381  
 382          $path = array('Cat 2', 'Cat 2.x');
 383          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 384          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 385  
 386          // Name conflict.
 387          $path = array('Cat 3', 'Cat 3.1 Doubled');
 388          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 389          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 390  
 391          $path = array('Cat 3', 'Cat 3.1 Doubled', 'Cat 3.1.1');
 392          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 393          $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
 394  
 395          $path = array('Cat 3', 'Cat 3.1.1');
 396          $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 397          $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
 398      }
 399  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1