Files
moodle/admin/tool/recyclebin/tests/events_test.php
T
Mark Nelson 93af6a610e MDL-48012 tool_recyclebin: multiple changes before integration
- Added new icon for the recycle bin.
- Fixed issue where the course expiry setting was being used to display
  the expiry messages in the category bin.
- Fixed failing Behat tests.
- Tidied Behat tests.
  - General tidy up.
  - Deleted three Behat feature files.
    - Deleted 'delete_confirmation.feature' as it's testing JS functionality
      that was removed (see comment further down). The 'basic_functionality.feature'
      now tests deleting an activity and a course.
    - Deleted 'description.feature' and added the tested functionality to
      'basic_functionality.feature'.
    - Deleted 'logs_test.feature' and moved testing to PHPUnit tests.
  - Added another scenario for restoring a course.
- Removed protected mod settings - it was discussed that this was not a necessary
  feature. We should not be treating the recycle bin as a backup.
- Renamed events to conform to guidelines.
- Wrote new PHPUnit tests for events.
- Tidied up existing PHPUnit tests and extended them.
- Alphabetised lang file.
- Made changes to strings and string identifiers in the lang file and changed usages in code.
- Changed setting names to better identify their purpose.
- Renamed classes and file names so it's easier to identify their purpose.
- Renamed the 'deleted' column to 'timecreated' for consistency with other
  Moodle tables, and to easily identify what the value represents.
- Changed the columns 'category' and 'course' to 'categoryid' and
  'courseid' respectively.
- Changed the name of the table indexes for consistency with core.
- Removed module.js and any use of it and replaced with Moodle core libraries
  for JS confirmation.
- Removed 'noevent' argument in delete_item().
- Removed unnecessary capabilities - we can use the same capabilties for both
  recycle bins based on the context the capability was given.
- Removed '_' in the capability definitions to keep it consistent with core.
- Set page layout and headings for index.php - varies between the course and
  category recycle bins.
- Deleted styles.css and centred columns using the flexible_table API.
- Fixed IDE and codechecker complaints.
- Made use of the files API, rather than writing directly to
  "$CFG->dataroot . '/recyclebin'".
- Used make_temp_directory rather than expecting it to exist.
- Replaced debugging function calls with calls to the backup API to
  display errors.
- Deleted the temporary backup file after a successful restore, or if it fails.
- If the restore fails for a course, delete the course we created.
- Removed unnecessary '\' characters when not in namespace.
- Used configduration class for expiry times in the settings.
- Cleanup course bin when a course is deleted.
- Cleanup a category bin when a category is deleted.
- Removed unnecessary throw tags.
- Changed default settings.
2016-03-18 14:12:08 +08:00

226 lines
8.0 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package tool_recyclebin
* @category test
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Events tests class.
*
* @package tool_recyclebin
* @category test
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_recyclebin_events_testcase extends advanced_testcase {
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp() {
$this->resetAfterTest();
// We want the category and course bin to be enabled.
set_config('categorybinenable', 1, 'tool_recyclebin');
set_config('coursebinenable', 1, 'tool_recyclebin');
}
/**
* Test the category bin item created event.
*/
public function test_category_bin_item_created() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Trigger and capture the event.
$sink = $this->redirectEvents();
delete_course($course, false);
$events = $sink->get_events();
$event = reset($events);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\category_bin($course->category);
$items = $rb->get_items();
$item = reset($items);
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_created', $event);
$this->assertEquals(context_coursecat::instance($course->category), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
/**
* Test the category bin item deleted event.
*/
public function test_category_bin_item_deleted() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Delete the course.
delete_course($course, false);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\category_bin($course->category);
$items = $rb->get_items();
$item = reset($items);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$rb->delete_item($item);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_deleted', $event);
$this->assertEquals(context_coursecat::instance($course->category), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
/**
* Test the category bin item restored event.
*/
public function test_category_bin_item_restored() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Delete the course.
delete_course($course, false);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\category_bin($course->category);
$items = $rb->get_items();
$item = reset($items);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$rb->restore_item($item);
$events = $sink->get_events();
$event = $events[6];
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_restored', $event);
$this->assertEquals(context_coursecat::instance($course->category), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
/**
* Test the course bin item created event.
*/
public function test_course_bin_item_created() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create the assignment.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$instance = $generator->create_instance(array('course' => $course->id));
// Trigger and capture the event.
$sink = $this->redirectEvents();
course_delete_module($instance->cmid);
$events = $sink->get_events();
$event = reset($events);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\course_bin($course->id);
$items = $rb->get_items();
$item = reset($items);
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_created', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
/**
* Test the course bin item deleted event.
*/
public function test_course_bin_item_deleted() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create the assignment.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$instance = $generator->create_instance(array('course' => $course->id));
// Delete the module.
course_delete_module($instance->cmid);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\course_bin($course->id);
$items = $rb->get_items();
$item = reset($items);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$rb->delete_item($item);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_deleted', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
/**
* Test the course bin item restored event.
*/
public function test_course_bin_item_restored() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create the assignment.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$instance = $generator->create_instance(array('course' => $course->id));
course_delete_module($instance->cmid);
// Get the item from the recycle bin.
$rb = new \tool_recyclebin\course_bin($course->id);
$items = $rb->get_items();
$item = reset($items);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$rb->restore_item($item);
$events = $sink->get_events();
$event = reset($events);
// Check that the event contains the expected values.
$this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_restored', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals($item->id, $event->objectid);
$this->assertEventContextNotUsed($event);
}
}