[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/util/dbops/tests/ -> restore_dbops_test.php (source)

   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   * @package    core_backup
  19   * @category   phpunit
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  // Include all the needed stuff
  27  global $CFG;
  28  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  29  
  30  /**
  31   * Restore dbops tests (all).
  32   */
  33  class restore_dbops_testcase extends advanced_testcase {
  34  
  35      /**
  36       * Verify the xxx_ids_cached (in-memory backup_ids cache) stuff works as expected.
  37       *
  38       * Note that those private implementations are tested here by using the public
  39       * backup_ids API and later performing low-level tests.
  40       */
  41      public function test_backup_ids_cached() {
  42          global $DB;
  43          $dbman = $DB->get_manager(); // We are going to use database_manager services.
  44  
  45          $this->resetAfterTest(true); // Playing with temp tables, better reset once finished.
  46  
  47          // Some variables and objects for testing.
  48          $restoreid = 'testrestoreid';
  49  
  50          $mapping = new stdClass();
  51          $mapping->itemname = 'user';
  52          $mapping->itemid = 1;
  53          $mapping->newitemid = 2;
  54          $mapping->parentitemid = 3;
  55          $mapping->info = 'info';
  56  
  57          // Create the backup_ids temp tables used by restore.
  58          restore_controller_dbops::create_restore_temp_tables($restoreid);
  59  
  60          // Send one mapping using the public api with defaults.
  61          restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  62          // Get that mapping and verify everything is returned as expected.
  63          $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  64          $this->assertSame($mapping->itemname, $result->itemname);
  65          $this->assertSame($mapping->itemid, $result->itemid);
  66          $this->assertSame(0, $result->newitemid);
  67          $this->assertSame(null, $result->parentitemid);
  68          $this->assertSame(null, $result->info);
  69  
  70          // Drop the backup_xxx_temp temptables manually, so memory cache won't be invalidated.
  71          $dbman->drop_table(new xmldb_table('backup_ids_temp'));
  72          $dbman->drop_table(new xmldb_table('backup_files_temp'));
  73  
  74          // Verify the mapping continues returning the same info,
  75          // now from cache (the table does not exist).
  76          $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  77          $this->assertSame($mapping->itemname, $result->itemname);
  78          $this->assertSame($mapping->itemid, $result->itemid);
  79          $this->assertSame(0, $result->newitemid);
  80          $this->assertSame(null, $result->parentitemid);
  81          $this->assertSame(null, $result->info);
  82  
  83          // Recreate the temp table, just to drop it using the restore API in
  84          // order to check that, then, the cache becomes invalid for the same request.
  85          restore_controller_dbops::create_restore_temp_tables($restoreid);
  86          restore_controller_dbops::drop_restore_temp_tables($restoreid);
  87  
  88          // No cached info anymore, so the mapping request will arrive to
  89          // DB leading to error (temp table does not exist).
  90          try {
  91              $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
  92              $this->fail('Expecting an exception, none occurred');
  93          } catch (Exception $e) {
  94              $this->assertTrue($e instanceof dml_exception);
  95              $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
  96          }
  97  
  98          // Create the backup_ids temp tables once more.
  99          restore_controller_dbops::create_restore_temp_tables($restoreid);
 100  
 101          // Send one mapping using the public api with complete values.
 102          restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid,
 103                  $mapping->newitemid, $mapping->parentitemid, $mapping->info);
 104          // Get that mapping and verify everything is returned as expected.
 105          $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
 106          $this->assertSame($mapping->itemname, $result->itemname);
 107          $this->assertSame($mapping->itemid, $result->itemid);
 108          $this->assertSame($mapping->newitemid, $result->newitemid);
 109          $this->assertSame($mapping->parentitemid, $result->parentitemid);
 110          $this->assertSame($mapping->info, $result->info);
 111  
 112          // Finally, drop the temp tables properly and get the DB error again (memory caches empty).
 113          restore_controller_dbops::drop_restore_temp_tables($restoreid);
 114          try {
 115              $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
 116              $this->fail('Expecting an exception, none occurred');
 117          } catch (Exception $e) {
 118              $this->assertTrue($e instanceof dml_exception);
 119              $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
 120          }
 121      }
 122  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1