[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tool/installaddon/tests/ -> installer_test.php (source)

   1  <?php
   2  
   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/>.
  17  
  18  /**
  19   * Provides the unit tests class and some helper classes
  20   *
  21   * @package     tool_installaddon
  22   * @category    test
  23   * @copyright   2013 David Mudrak <[email protected]>
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Unit tests for the {@link tool_installaddon_installer} class
  32   *
  33   * @copyright 2013 David Mudrak <[email protected]>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class tool_installaddon_installer_testcase extends advanced_testcase {
  37  
  38      public function test_get_addons_repository_url() {
  39          $installer = testable_tool_installaddon_installer::instance();
  40          $url = $installer->get_addons_repository_url();
  41          $query = parse_url($url, PHP_URL_QUERY);
  42          $this->assertEquals(1, preg_match('~^site=(.+)$~', $query, $matches));
  43          $site = rawurldecode($matches[1]);
  44          $site = json_decode(base64_decode($site), true);
  45          $this->assertInternalType('array', $site);
  46          $this->assertEquals(3, count($site));
  47          $this->assertSame('Nasty site', $site['fullname']);
  48          $this->assertSame('file:///etc/passwd', $site['url']);
  49          $this->assertSame("2.5'; DROP TABLE mdl_user; --", $site['majorversion']);
  50      }
  51  
  52      public function test_extract_installfromzip_file() {
  53          $jobid = md5(rand().uniqid('test_', true));
  54          $sourcedir = make_temp_directory('tool_installaddon/'.$jobid.'/source');
  55          $contentsdir = make_temp_directory('tool_installaddon/'.$jobid.'/contents');
  56          copy(dirname(__FILE__).'/fixtures/zips/invalidroot.zip', $sourcedir.'/testinvalidroot.zip');
  57  
  58          $installer = tool_installaddon_installer::instance();
  59          $files = $installer->extract_installfromzip_file($sourcedir.'/testinvalidroot.zip', $contentsdir, 'fixed_root');
  60          $this->assertInternalType('array', $files);
  61          $this->assertCount(4, $files);
  62          $this->assertSame(true, $files['fixed_root/']);
  63          $this->assertSame(true, $files['fixed_root/lang/']);
  64          $this->assertSame(true, $files['fixed_root/lang/en/']);
  65          $this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
  66          foreach ($files as $file => $status) {
  67              if (substr($file, -1) === '/') {
  68                  $this->assertTrue(is_dir($contentsdir.'/'.$file));
  69              } else {
  70                  $this->assertTrue(is_file($contentsdir.'/'.$file));
  71              }
  72          }
  73      }
  74  
  75      public function test_decode_remote_request() {
  76          $installer = testable_tool_installaddon_installer::instance();
  77  
  78          $request = base64_encode(json_encode(array(
  79              'name' => '<h1>Stamp collection</h1>"; DELETE FROM mdl_users; --',
  80              'component' => 'mod_stampcoll',
  81              'version' => 2013032800,
  82          )));
  83          $request = $installer->testable_decode_remote_request($request);
  84          $this->assertTrue(is_object($request));
  85          // One, my little hobbit, never trusts the input parameters!
  86          $this->assertEquals('Stamp collection&quot;; DELETE FROM mdl_users; --', $request->name);
  87          $this->assertEquals('mod_stampcoll', $request->component);
  88          $this->assertEquals(2013032800, $request->version);
  89  
  90          $request = base64_encode(json_encode(array(
  91              'name' => 'Theme with invalid version number',
  92              'component' => 'theme_invalid',
  93              'version' => '1.0',
  94          )));
  95          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  96  
  97          $request = base64_encode(json_encode(array(
  98              'name' => 'Invalid activity name',
  99              'component' => 'mod_invalid_activity',
 100              'version' => 2013032800,
 101          )));
 102          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 103  
 104          $request = base64_encode(json_encode(array(
 105              'name' => 'Moodle 3.0',
 106              'component' => 'core',
 107              'version' => 2022010100,
 108          )));
 109          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 110  
 111          $request = base64_encode(json_encode(array(
 112              'name' => 'Invalid core subsystem',
 113              'component' => 'core_cache',
 114              'version' => 2014123400,
 115          )));
 116          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 117  
 118          $request = base64_encode(json_encode(array(
 119              'name' => 'Non-existing plugintype',
 120              'component' => 'david_mudrak',
 121              'version' => 2012123199,
 122          )));
 123          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 124  
 125          $request = base64_encode(json_encode(array(
 126              'name' => 'Bogus module name',
 127              'component' => 'mod_xxx_yyy',
 128              'version' => 2012123190,
 129          )));
 130          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 131      }
 132  
 133      public function test_move_directory() {
 134          $jobid = md5(rand().uniqid('test_', true));
 135          $jobroot = make_temp_directory('tool_installaddon/'.$jobid);
 136          $contentsdir = make_temp_directory('tool_installaddon/'.$jobid.'/contents/sub/folder');
 137          file_put_contents($contentsdir.'/readme.txt', 'Hello world!');
 138  
 139          $installer = tool_installaddon_installer::instance();
 140          $installer->move_directory($jobroot.'/contents', $jobroot.'/moved', 0777, 0666);
 141  
 142          $this->assertFalse(is_dir($jobroot.'/contents'));
 143          $this->assertTrue(is_file($jobroot.'/moved/sub/folder/readme.txt'));
 144          $this->assertSame('Hello world!', file_get_contents($jobroot.'/moved/sub/folder/readme.txt'));
 145      }
 146  }
 147  
 148  
 149  /**
 150   * Testable subclass of the tested class
 151   *
 152   * @copyright 2013 David Mudrak <[email protected]>
 153   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 154   */
 155  class testable_tool_installaddon_installer extends tool_installaddon_installer {
 156  
 157      public function get_site_fullname() {
 158          return strip_tags('<h1 onmouseover="alert(\'Hello Moodle.org!\');">Nasty site</h1>');
 159      }
 160  
 161      public function get_site_url() {
 162          return 'file:///etc/passwd';
 163      }
 164  
 165      public function get_site_major_version() {
 166          return "2.5'; DROP TABLE mdl_user; --";
 167      }
 168  
 169      public function testable_decode_remote_request($request) {
 170          return parent::decode_remote_request($request);
 171      }
 172  
 173      protected function should_send_site_info() {
 174          return true;
 175      }
 176  }


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