Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  namespace core;
  18  
  19  /**
  20   * Test basic_testcase extra features and PHPUnit Moodle integration.
  21   *
  22   * @package    core
  23   * @category   test
  24   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class basic_test extends \basic_testcase {
  28      protected $testassertexecuted = false;
  29  
  30      protected function setUp(): void {
  31          parent::setUp();
  32          if ($this->getName() === 'test_setup_assert') {
  33              $this->assertTrue(true);
  34              $this->testassertexecuted = true;
  35              return;
  36          }
  37      }
  38  
  39      /**
  40       * Tests that bootstrapping has occurred correctly
  41       * @return void
  42       */
  43      public function test_bootstrap() {
  44          global $CFG;
  45  
  46          // The httpswwwroot has been deprecated, we keep it as an alias for backwards compatibility with plugins only.
  47          $this->assertTrue(isset($CFG->httpswwwroot));
  48          $this->assertEquals($CFG->httpswwwroot, $CFG->wwwroot);
  49          $this->assertEquals($CFG->prefix, $CFG->phpunit_prefix);
  50      }
  51  
  52      /**
  53       * This is just a verification if I understand the PHPUnit assert docs right --skodak
  54       * @return void
  55       */
  56      public function test_assert_behaviour() {
  57          // Arrays.
  58          $a = array('a', 'b', 'c');
  59          $b = array('a', 'c', 'b');
  60          $c = array('a', 'b', 'c');
  61          $d = array('a', 'b', 'C');
  62          $this->assertNotEquals($a, $b);
  63          $this->assertNotEquals($a, $d);
  64          $this->assertEquals($a, $c);
  65          $this->assertEqualsCanonicalizing($a, $b);
  66  
  67          // Objects.
  68          $a = new \stdClass();
  69          $a->x = 'x';
  70          $a->y = 'y';
  71          $b = new \stdClass(); // Switched order.
  72          $b->y = 'y';
  73          $b->x = 'x';
  74          $c = $a;
  75          $d = new \stdClass();
  76          $d->x = 'x';
  77          $d->y = 'y';
  78          $d->z = 'z';
  79          $this->assertEquals($a, $b);
  80          $this->assertNotSame($a, $b);
  81          $this->assertEquals($a, $c);
  82          $this->assertSame($a, $c);
  83          $this->assertNotEquals($a, $d);
  84  
  85          // String comparison.
  86          $this->assertEquals(1, '1');
  87          $this->assertEquals(null, '');
  88  
  89          $this->assertNotEquals(0, '');
  90          $this->assertNotEquals(null, '0');
  91          $this->assertNotEquals(array(), '');
  92  
  93          // Other comparison.
  94          $this->assertEquals(null, null);
  95          $this->assertEquals(false, null);
  96          $this->assertEquals(0, null);
  97  
  98          // Emptiness.
  99          $this->assertEmpty(0);
 100          $this->assertEmpty(0.0);
 101          $this->assertEmpty('');
 102          $this->assertEmpty('0');
 103          $this->assertEmpty(false);
 104          $this->assertEmpty(null);
 105          $this->assertEmpty(array());
 106  
 107          $this->assertNotEmpty(1);
 108          $this->assertNotEmpty(0.1);
 109          $this->assertNotEmpty(-1);
 110          $this->assertNotEmpty(' ');
 111          $this->assertNotEmpty('0 ');
 112          $this->assertNotEmpty(true);
 113          $this->assertNotEmpty(array(null));
 114          $this->assertNotEmpty(new \stdClass());
 115      }
 116  
 117      /**
 118       * Make sure there are no sloppy Windows line endings
 119       * that would break our tests.
 120       */
 121      public function test_lineendings() {
 122          $string = <<<STRING
 123  a
 124  b
 125  STRING;
 126          $this->assertSame("a\nb", $string, 'Make sure all project files are checked out with unix line endings.');
 127  
 128      }
 129  
 130      /**
 131       * Make sure asserts in setUp() do not create problems.
 132       */
 133      public function test_setup_assert() {
 134          $this->assertTrue($this->testassertexecuted);
 135          $this->testassertexecuted = false;
 136      }
 137  
 138      /**
 139       * Test assert Tag
 140       */
 141      public function test_assert_tag() {
 142          // This should succeed.
 143          self::assertTag(['id' => 'testid'], "<div><span id='testid'></span></div>");
 144          $this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
 145          self::assertTag(['id' => 'testid'], "<div><div>");
 146      }
 147  
 148      // Uncomment following tests to see logging of unexpected changes in global state and database.
 149      /*
 150          public function test_db_modification() {
 151              global $DB;
 152              $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
 153          }
 154  
 155          public function test_cfg_modification() {
 156              global $CFG;
 157              $CFG->xx = 'yy';
 158              unset($CFG->admin);
 159              $CFG->rolesactive = 0;
 160          }
 161  
 162          public function test_user_modification() {
 163              global $USER;
 164              $USER->id = 10;
 165          }
 166  
 167          public function test_course_modification() {
 168              global $COURSE;
 169              $COURSE->id = 10;
 170          }
 171  
 172          public function test_all_modifications() {
 173              global $DB, $CFG, $USER, $COURSE;
 174              $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
 175              $CFG->xx = 'yy';
 176              unset($CFG->admin);
 177              $CFG->rolesactive = 0;
 178              $USER->id = 10;
 179              $COURSE->id = 10;
 180          }
 181  
 182          public function test_transaction_problem() {
 183              global $DB;
 184              $DB->start_delegated_transaction();
 185          }
 186      */
 187  }