[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tool/installaddon/tests/ -> validator_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_validator_testcase extends basic_testcase {
  37  
  38      public function test_validate_files_layout() {
  39          $fixtures = dirname(__FILE__).'/fixtures';
  40  
  41          // Non-existing directory.
  42          $validator = testable_tool_installaddon_validator::instance($fixtures.'/nulldir', array(
  43              'null/' => true,
  44              'null/lang/' => true,
  45              'null/lang/en/' => true,
  46              'null/lang/en/null.php' => true));
  47          $this->assertEquals('testable_tool_installaddon_validator', get_class($validator));
  48          $this->assertFalse($validator->execute());
  49          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR,
  50              'filenotexists', array('file' => 'null/')));
  51  
  52          // Missing expected file
  53          $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
  54              'foobar/' => true,
  55              'foobar/version.php' => true,
  56              'foobar/index.php' => true,
  57              'foobar/lang/' => true,
  58              'foobar/lang/en/' => true,
  59              'foobar/lang/en/local_foobar.php' => true,
  60              'foobar/NOTEXISTS.txt' => true));
  61          $this->assertFalse($validator->execute());
  62          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR,
  63              'filenotexists', array('file' => 'foobar/NOTEXISTS.txt')));
  64  
  65          // Errors during ZIP extraction
  66          $validator = testable_tool_installaddon_validator::instance($fixtures.'/multidir', array(
  67              'one/' => true,
  68              'one/version.php' => 'Can not write target file',
  69              'two/' => true,
  70              'two/README.txt' => true));
  71          $this->assertFalse($validator->execute());
  72          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'filestatus',
  73              array('file' => 'one/version.php', 'status' => 'Can not write target file')));
  74  
  75          // Insufficient number of extracted files
  76          $validator = testable_tool_installaddon_validator::instance($fixtures.'/emptydir', array(
  77              'emptydir/' => true,
  78              'emptydir/README.txt' => true));
  79          $this->assertFalse($validator->execute());
  80          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'filesnumber'));
  81  
  82          // No wrapping directory
  83          $validator = testable_tool_installaddon_validator::instance($fixtures.'/nowrapdir', array(
  84              'version.php' => true,
  85              'index.php' => true,
  86              'lang/' => true,
  87              'lang/en/' => true,
  88              'lang/en/foo.php' => true));
  89          $this->assertFalse($validator->execute());
  90          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'onedir'));
  91  
  92          // Multiple directories
  93          $validator = testable_tool_installaddon_validator::instance($fixtures.'/multidir', array(
  94              'one/' => true,
  95              'one/version.php' => true,
  96              'two/' => true,
  97              'two/README.txt' => true));
  98          $this->assertFalse($validator->execute());
  99          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'onedir'));
 100  
 101          // Invalid root directory name
 102          $validator = testable_tool_installaddon_validator::instance($fixtures.'/github', array(
 103              'moodle-repository_mahara-master/' => true,
 104              'moodle-repository_mahara-master/lang/' => true,
 105              'moodle-repository_mahara-master/lang/en/' => true,
 106              'moodle-repository_mahara-master/lang/en/repository_mahara.php' => true,
 107              'moodle-repository_mahara-master/version.php' => true));
 108          $this->assertFalse($validator->execute());
 109          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'rootdirinvalid',
 110              'moodle-repository_mahara-master'));
 111      }
 112  
 113      public function test_validate_version_php() {
 114          $fixtures = dirname(__FILE__).'/fixtures';
 115  
 116          $validator = testable_tool_installaddon_validator::instance($fixtures.'/noversiontheme', array(
 117              'noversion/' => true,
 118              'noversion/lang/' => true,
 119              'noversion/lang/en/' => true,
 120              'noversion/lang/en/theme_noversion.php' => true));
 121          $validator->assert_plugin_type('theme');
 122          $validator->assert_moodle_version(0);
 123          $this->assertTrue($validator->execute());
 124          $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'missingversionphp'));
 125          $this->assertTrue(is_null($validator->get_versionphp_info()));
 126  
 127          $validator = testable_tool_installaddon_validator::instance($fixtures.'/noversionmod', array(
 128              'noversion/' => true,
 129              'noversion/lang/' => true,
 130              'noversion/lang/en/' => true,
 131              'noversion/lang/en/noversion.php' => true));
 132          $validator->assert_plugin_type('mod');
 133          $validator->assert_moodle_version(0);
 134          $this->assertFalse($validator->execute());
 135          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missingversionphp'));
 136  
 137          $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
 138              'foobar/' => true,
 139              'foobar/version.php' => true,
 140              'foobar/index.php' => true,
 141              'foobar/lang/' => true));
 142          $validator->assert_plugin_type('block');
 143          $validator->assert_moodle_version('2013031400.00');
 144          $this->assertFalse($validator->execute());
 145          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'componentmismatchtype',
 146              array('expected' => 'block', 'found' => 'local')));
 147  
 148          $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
 149              'foobar/' => true,
 150              'foobar/version.php' => true,
 151              'foobar/index.php' => true,
 152              'foobar/lang/' => true,
 153              'foobar/lang/en/' => true,
 154              'foobar/lang/en/local_foobar.php' => true));
 155          $validator->assert_plugin_type('local');
 156          $validator->assert_moodle_version('2013031400.00');
 157          $this->assertTrue($validator->execute());
 158          $this->assertTrue($validator->get_result());
 159          $this->assertEquals('foobar', $validator->get_rootdir());
 160          $this->assertTrue($this->has_message($validator->get_messages(), $validator::INFO, 'rootdir', 'foobar'));
 161          $versionphpinfo = $validator->get_versionphp_info();
 162          $this->assertInternalType('array', $versionphpinfo);
 163          $this->assertCount(4, $versionphpinfo);
 164          $this->assertEquals(2013031900, $versionphpinfo['version']);
 165          $this->assertEquals(2013031200, $versionphpinfo['requires']);
 166          $this->assertEquals('local_foobar', $versionphpinfo['component']);
 167          $this->assertEquals('MATURITY_ALPHA', $versionphpinfo['maturity']); // Note we get the constant name here.
 168          $this->assertEquals(MATURITY_ALPHA, constant($versionphpinfo['maturity'])); // This is how to get the real value.
 169          $this->assertTrue($this->has_message($validator->get_messages(), $validator::WARNING, 'maturity', 'MATURITY_ALPHA'));
 170      }
 171  
 172      public function test_validate_language_pack() {
 173          $fixtures = dirname(__FILE__).'/fixtures';
 174  
 175          $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
 176              'bah/' => true,
 177              'bah/index.php' => true,
 178              'bah/view.php' => true,
 179              'bah/version.php' => true));
 180          $validator->assert_plugin_type('mod');
 181          $validator->assert_moodle_version(0);
 182          $this->assertFalse($validator->execute());
 183          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missinglangenfolder'));
 184  
 185          $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
 186              'bah/' => true,
 187              'bah/version.php' => true,
 188              'bah/lang/' => true,
 189              'bah/lang/en/' => true));
 190          $validator->assert_plugin_type('mod');
 191          $validator->assert_moodle_version(0);
 192          $this->assertFalse($validator->execute());
 193          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missinglangenfile'));
 194  
 195          $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
 196              'bah/' => true,
 197              'bah/version.php' => true,
 198              'bah/lang/' => true,
 199              'bah/lang/en/' => true,
 200              'bah/lang/en/bleh.php' => true,
 201              'bah/lang/en/bah.php' => true));
 202          $validator->assert_plugin_type('mod');
 203          $validator->assert_moodle_version(0);
 204          $this->assertTrue($validator->execute());
 205          $this->assertTrue($this->has_message($validator->get_messages(), $validator::WARNING, 'multiplelangenfiles'));
 206          $this->assertTrue(is_null($validator->get_language_file_name()));
 207  
 208          $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
 209              'bah/' => true,
 210              'bah/version.php' => true,
 211              'bah/lang/' => true,
 212              'bah/lang/en/' => true,
 213              'bah/lang/en/bah.php' => true));
 214          $validator->assert_plugin_type('block');
 215          $validator->assert_moodle_version(0);
 216          $this->assertFalse($validator->execute());
 217          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missingexpectedlangenfile', 'block_bah.php'));
 218          $this->assertEquals('bah', $validator->get_language_file_name());
 219  
 220          $validator = testable_tool_installaddon_validator::instance($fixtures.'/noversiontheme', array(
 221              'noversion/' => true,
 222              'noversion/lang/' => true,
 223              'noversion/lang/en/' => true,
 224              'noversion/lang/en/theme_noversion.php' => true));
 225          $validator->assert_plugin_type('theme');
 226          $validator->assert_moodle_version(0);
 227          $this->assertTrue($validator->execute());
 228          $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'foundlangfile', 'theme_noversion'));
 229          $this->assertEquals('theme_noversion', $validator->get_language_file_name());
 230  
 231          $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
 232              'foobar/' => true,
 233              'foobar/version.php' => true,
 234              'foobar/index.php' => true,
 235              'foobar/lang/' => true,
 236              'foobar/lang/en/' => true,
 237              'foobar/lang/en/local_foobar.php' => true));
 238          $validator->assert_plugin_type('local');
 239          $validator->assert_moodle_version('2013031400.00');
 240          $this->assertTrue($validator->execute());
 241          $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'foundlangfile', 'local_foobar'));
 242          $this->assertEquals('local_foobar', $validator->get_language_file_name());
 243      }
 244  
 245      public function test_validate_target_location() {
 246          $fixtures = dirname(__FILE__).'/fixtures';
 247  
 248          $validator = testable_tool_installaddon_validator::instance($fixtures.'/installed', array(
 249              'greenbar/' => true,
 250              'greenbar/version.php' => true,
 251              'greenbar/index.php' => true,
 252              'greenbar/lang/' => true,
 253              'greenbar/lang/en/' => true,
 254              'greenbar/lang/en/local_greenbar.php' => true));
 255          $validator->assert_plugin_type('local');
 256          $validator->assert_moodle_version('2013031400.00');
 257          $this->assertFalse($validator->execute());
 258          $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'targetexists',
 259              $validator->get_plugintype_location('local').'/greenbar'));
 260  
 261          $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
 262              'foobar/' => true,
 263              'foobar/version.php' => true,
 264              'foobar/index.php' => true,
 265              'foobar/lang/' => true,
 266              'foobar/lang/en/' => true,
 267              'foobar/lang/en/local_foobar.php' => true));
 268          $validator->assert_plugin_type('local');
 269          $validator->assert_moodle_version('2013031400.00');
 270          $this->assertTrue($validator->execute());
 271          $this->assertTrue($this->has_message($validator->get_messages(), $validator::INFO, 'pathwritable',
 272              $validator->get_plugintype_location('local')));
 273      }
 274  
 275      public function test_parse_version_php() {
 276          $fixtures = dirname(__FILE__).'/fixtures/versionphp';
 277  
 278          $validator = testable_tool_installaddon_validator::instance($fixtures, array());
 279          $this->assertEquals('testable_tool_installaddon_validator', get_class($validator));
 280  
 281          $info = $validator->testable_parse_version_php($fixtures.'/version1.php');
 282          $this->assertInternalType('array', $info);
 283          $this->assertCount(7, $info);
 284          $this->assertEquals('block_foobar', $info['plugin->component']);    // Later in the file.
 285          $this->assertEquals('2013010100', $info['plugin->version']);        // Numeric wins over strings.
 286          $this->assertEquals('2012122401', $info['plugin->requires']);       // Commented.
 287          $this->assertEquals('MATURITY_STABLE', $info['module->maturity']);  // Constant wins regardless the order (non-PHP behaviour).
 288          $this->assertEquals('MATURITY_ALPHA', $info['plugin->maturity']);   // Constant wins regardless the order (non-PHP behaviour).
 289          $this->assertEquals('v2.3', $info['module->release']);              // String wins over numeric (non-PHP behaviour).
 290          $this->assertEquals('v2.4', $info['plugin->release']);              // String wins over numeric (non-PHP behaviour).
 291      }
 292  
 293      // Helper methods //////////////////////////////////////////////////////////
 294  
 295      protected function has_message(array $messages, $level, $msgcode, $addinfo = null) {
 296          foreach ($messages as $message) {
 297              if ($message->level === $level and $message->msgcode === $msgcode and $message->addinfo === $addinfo) {
 298                  return true;
 299              }
 300          }
 301          return false;
 302      }
 303  }
 304  
 305  
 306  /**
 307   * Provides access to protected methods we want to explicitly test
 308   *
 309   * @copyright 2013 David Mudrak <[email protected]>
 310   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 311   */
 312  class testable_tool_installaddon_validator extends tool_installaddon_validator {
 313  
 314      public function testable_parse_version_php($fullpath) {
 315          return parent::parse_version_php($fullpath);
 316      }
 317  
 318      public function get_plugintype_location($plugintype) {
 319  
 320          $testableroot = make_temp_directory('testable_tool_installaddon_validator/plugintypes');
 321          if (!is_dir($testableroot.'/'.$plugintype)) {
 322              make_temp_directory('testable_tool_installaddon_validator/plugintypes/'.$plugintype);
 323          }
 324  
 325          if ($plugintype === 'local') {
 326              // We need the following for the test_validate_target_location() method
 327              make_temp_directory('testable_tool_installaddon_validator/plugintypes/local/greenbar');
 328          }
 329  
 330          return $testableroot.'/'.$plugintype;
 331      }
 332  }


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