Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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