[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/recyclebin/tests/ -> course_bin_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   * Recycle bin tests.
  19   *
  20   * @package    tool_recyclebin
  21   * @copyright  2015 University of Kent
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Recycle bin course tests.
  29   *
  30   * @package    tool_recyclebin
  31   * @copyright  2015 University of Kent
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class tool_recyclebin_course_bin_tests extends advanced_testcase {
  35  
  36      /**
  37       * @var stdClass $course
  38       */
  39      protected $course;
  40  
  41      /**
  42       * @var stdClass the quiz record
  43       */
  44      protected $quiz;
  45  
  46      /**
  47       * Setup for each test.
  48       */
  49      protected function setUp() {
  50          $this->resetAfterTest(true);
  51          $this->setAdminUser();
  52  
  53          // We want the course bin to be enabled.
  54          set_config('coursebinenable', 1, 'tool_recyclebin');
  55  
  56          $this->course = $this->getDataGenerator()->create_course();
  57          $this->quiz = $this->getDataGenerator()->get_plugin_generator('mod_quiz')->create_instance(array(
  58              'course' => $this->course->id
  59          ));
  60      }
  61  
  62      /**
  63       * Check that our hook is called when an activity is deleted.
  64       */
  65      public function test_pre_course_module_delete_hook() {
  66          global $DB;
  67  
  68          // Should have nothing in the recycle bin.
  69          $this->assertEquals(0, $DB->count_records('tool_recyclebin_course'));
  70  
  71          // Delete the course module.
  72          course_delete_module($this->quiz->cmid);
  73  
  74          // Check the course module is now in the recycle bin.
  75          $this->assertEquals(1, $DB->count_records('tool_recyclebin_course'));
  76  
  77          // Try with the API.
  78          $recyclebin = new \tool_recyclebin\course_bin($this->course->id);
  79          $this->assertEquals(1, count($recyclebin->get_items()));
  80      }
  81  
  82      /**
  83       * Test that we can restore recycle bin items.
  84       */
  85      public function test_restore() {
  86          global $DB;
  87  
  88          $startcount = $DB->count_records('course_modules');
  89  
  90          // Delete the course module.
  91          course_delete_module($this->quiz->cmid);
  92  
  93          // Try restoring.
  94          $recyclebin = new \tool_recyclebin\course_bin($this->course->id);
  95          foreach ($recyclebin->get_items() as $item) {
  96              $recyclebin->restore_item($item);
  97          }
  98  
  99          // Check that it was restored and removed from the recycle bin.
 100          $this->assertEquals($startcount, $DB->count_records('course_modules'));
 101          $this->assertEquals(0, count($recyclebin->get_items()));
 102      }
 103  
 104      /**
 105       * Test that we can delete recycle bin items.
 106       */
 107      public function test_delete() {
 108          global $DB;
 109  
 110          $startcount = $DB->count_records('course_modules');
 111  
 112          // Delete the course module.
 113          course_delete_module($this->quiz->cmid);
 114  
 115          // Try purging.
 116          $recyclebin = new \tool_recyclebin\course_bin($this->course->id);
 117          foreach ($recyclebin->get_items() as $item) {
 118              $recyclebin->delete_item($item);
 119          }
 120  
 121          // Item was deleted, so no course module was restored.
 122          $this->assertEquals($startcount - 1, $DB->count_records('course_modules'));
 123          $this->assertEquals(0, count($recyclebin->get_items()));
 124      }
 125  
 126      /**
 127       * Test the cleanup task.
 128       */
 129      public function test_cleanup_task() {
 130          global $DB;
 131  
 132          set_config('coursebinexpiry', WEEKSECS, 'tool_recyclebin');
 133  
 134          // Delete the quiz.
 135          course_delete_module($this->quiz->cmid);
 136  
 137          // Set deleted date to the distant past.
 138          $recyclebin = new \tool_recyclebin\course_bin($this->course->id);
 139          foreach ($recyclebin->get_items() as $item) {
 140              $item->timecreated = time() - WEEKSECS;
 141              $DB->update_record('tool_recyclebin_course', $item);
 142          }
 143  
 144          // Create another module we are going to delete, but not alter the time it was placed in the recycle bin.
 145          $book = $this->getDataGenerator()->get_plugin_generator('mod_book')->create_instance(array(
 146              'course' => $this->course->id));
 147  
 148          course_delete_module($book->cmid);
 149  
 150          // Should have 2 items now.
 151          $this->assertEquals(2, count($recyclebin->get_items()));
 152  
 153          // Execute cleanup task.
 154          $this->expectOutputRegex("/\[tool_recyclebin\] Deleting item '\d+' from the course recycle bin/");
 155          $task = new \tool_recyclebin\task\cleanup_course_bin();
 156          $task->execute();
 157  
 158          // Should only have the book as it was not due to be deleted.
 159          $items = $recyclebin->get_items();
 160          $this->assertEquals(1, count($items));
 161          $deletedbook = reset($items);
 162          $this->assertEquals($book->name, $deletedbook->name);
 163      }
 164  }


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