Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   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   * DML read/read-write database handle tests for mysqli_native_moodle_database
  19   *
  20   * @package    core
  21   * @category   dml
  22   * @copyright  2018 Srdjan Janković, Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once (__DIR__.'/fixtures/read_slave_moodle_database_mock_mysqli.php');
  31  
  32  /**
  33   * DML mysqli_native_moodle_database read slave specific tests
  34   *
  35   * @package    core
  36   * @category   dml
  37   * @copyright  2018 Catalyst IT
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @covers     \mysqli_native_moodle_database
  40   */
  41  class dml_mysqli_read_slave_test extends \base_testcase {
  42      /**
  43       * Test readonly handle is not used for reading from special pg_*() call queries,
  44       * pg_try_advisory_lock and pg_advisory_unlock.
  45       *
  46       * @return void
  47       */
  48      public function test_lock() : void {
  49          $DB = new read_slave_moodle_database_mock_mysqli();
  50  
  51          $this->assertEquals(0, $DB->perf_get_reads_slave());
  52  
  53          $DB->query_start("SELECT GET_LOCK('lock',1)", null, SQL_QUERY_SELECT);
  54          $this->assertTrue($DB->db_handle_is_rw());
  55          $DB->query_end(null);
  56          $this->assertEquals(0, $DB->perf_get_reads_slave());
  57  
  58          $DB->query_start("SELECT RELEASE_LOCK('lock',1)", null, SQL_QUERY_SELECT);
  59          $this->assertTrue($DB->db_handle_is_rw());
  60          $DB->query_end(null);
  61          $this->assertEquals(0, $DB->perf_get_reads_slave());
  62      }
  63  
  64      /**
  65       * Test readonly connection failure with real mysqli connection
  66       *
  67       * @return void
  68       */
  69      public function test_real_readslave_connect_fail() : void {
  70          global $DB;
  71  
  72          if ($DB->get_dbfamily() != 'mysql') {
  73              $this->markTestSkipped("Not mysql");
  74          }
  75  
  76          // Open second connection.
  77          $cfg = $DB->export_dbconfig();
  78          if (!isset($cfg->dboptions)) {
  79              $cfg->dboptions = array();
  80          }
  81          $cfg->dboptions['readonly'] = [
  82              'instance' => ['host.that.is.not'],
  83              'connecttimeout' => 1
  84          ];
  85  
  86          $db2 = \moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
  87          $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
  88          $this->assertTrue(count($db2->get_records('user')) > 0);
  89      }
  90  }