/** * Test the cleanup task. */ public function test_cleanup_task() { global $DB; // Set the expiry to 1 week. set_config('categorybinexpiry', WEEKSECS, 'tool_recyclebin'); delete_course($this->course, false); $recyclebin = new \tool_recyclebin\category_bin($this->course->category); // Set deleted date to the distant past. foreach ($recyclebin->get_items() as $item) { $item->timecreated = time() - WEEKSECS; $DB->update_record('tool_recyclebin_category', $item); } // Create another course to delete. $course = $this->getDataGenerator()->create_course(); delete_course($course, false); // Should now be two courses in the recycle bin. $this->assertEquals(2, count($recyclebin->get_items())); // Execute cleanup task. $this->expectOutputRegex("/\\[tool_recyclebin\\] Deleting item '\\d+' from the category recycle bin/"); $task = new \tool_recyclebin\task\cleanup_category_bin(); $task->execute(); // Task should only have deleted the course where we updated the time. $courses = $recyclebin->get_items(); $this->assertEquals(1, count($courses)); $course = reset($courses); $this->assertEquals('Test course 2', $course->fullname); }
// Empty it. case 'empty': $recyclebin->delete_all_items(); redirect($PAGE->url, get_string('alertemptied', 'tool_recyclebin'), 2); break; } } // Add a "Go Back" button. $goback = html_writer::start_tag('div', array('class' => 'backlink')); $goback .= html_writer::link($context->get_url(), get_string('backto', '', $context->get_context_name())); $goback .= html_writer::end_tag('div'); // Output header. echo $OUTPUT->header(); echo $OUTPUT->heading($heading); // Grab our items, check there is actually something to display. $items = $recyclebin->get_items(); // Nothing to show? Bail out early. if (empty($items)) { echo $OUTPUT->box(get_string('noitemsinbin', 'tool_recyclebin')); echo $goback; echo $OUTPUT->footer(); die; } // Start with a description. if ($expiry > 0) { $expirydisplay = format_time($expiry); echo '<div class=\'alert\'>' . get_string('deleteexpirywarning', 'tool_recyclebin', $expirydisplay) . '</div>'; } // Define columns and headers. $firstcolstr = $context->contextlevel == CONTEXT_COURSE ? 'activity' : 'course'; $columns = array($firstcolstr, 'date', 'restore', 'delete');
/** * Adds a recycle bin link to the course admin menu. * * @param navigation_node $navigation The navigation node to extend * @param context $context The context of the course * @return void|null return null if we don't want to display the node. */ function tool_recyclebin_extend_navigation_category_settings($navigation, $context) { global $PAGE; // Check if it is enabled. if (!\tool_recyclebin\category_bin::is_enabled()) { return null; } $categorybin = new \tool_recyclebin\category_bin($context->instanceid); // Check we can view the recycle bin. if (!$categorybin->can_view()) { return null; } $url = null; $settingnode = null; // Add a link to the category recyclebin. $url = new moodle_url('/admin/tool/recyclebin/index.php', array('contextid' => $context->id)); // If we are set to auto-hide, check the number of items. $autohide = get_config('tool_recyclebin', 'autohide'); if ($autohide) { $items = $categorybin->get_items(); if (empty($items)) { return null; } } // Add the recyclebin link. $pluginname = get_string('pluginname', 'tool_recyclebin'); $node = navigation_node::create($pluginname, $url, navigation_node::NODETYPE_LEAF, 'tool_recyclebin', 'tool_recyclebin', new pix_icon('trash', $pluginname, 'tool_recyclebin')); if ($PAGE->url->compare($url, URL_MATCH_BASE)) { $node->make_active(); } $navigation->add_node($node); }
/** * 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); }