Skip to content

Commit

Permalink
MDL-39915 cleanup core_messagelib_testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 27, 2013
1 parent 8603c60 commit 2988b72
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/tests/messagelib_test.php
Expand Up @@ -18,22 +18,23 @@
* Tests for messagelib.php.
*
* @package core_message
* @category phpunit
* @copyright 2012 The Open Universtiy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

class messagelib_testcase extends advanced_testcase {
class core_messagelib_testcase extends advanced_testcase {

public function test_message_get_providers_for_user() {
global $CFG, $DB;

$this->resetAfterTest(true);
$this->resetAfterTest();

$generator = $this->getDataGenerator();

// Create a course category and course
// Create a course category and course.
$cat = $generator->create_category(array('parent' => 0));
$course = $generator->create_course(array('category' => $cat->id));
$quiz = $generator->create_module('quiz', array('course' => $course->id));
Expand Down Expand Up @@ -82,19 +83,19 @@ public function test_message_get_providers_for_user() {
public function test_message_get_providers_for_user_more() {
global $DB;

$this->resetAfterTest(true);
$this->resetAfterTest();

// Create a course
// Create a course.
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);

// It would probably be better to use a quiz instance as it has capability controlled messages
// however mod_quiz doesn't have a data generator
// Instead we're going to use backup notifications and give and take away the capability at various levels
// however mod_quiz doesn't have a data generator.
// Instead we're going to use backup notifications and give and take away the capability at various levels.
$assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
$modulecontext = context_module::instance($assign->id);

// Create and enrol a teacher
// Create and enrol a teacher.
$teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
$teacher = $this->getDataGenerator()->create_user();
role_assign($teacherrole->id, $teacher->id, $coursecontext);
Expand All @@ -108,16 +109,16 @@ public function test_message_get_providers_for_user_more() {
}
$enrolplugin->enrol_user($enrolinstance, $teacher->id);

// Make the teacher the current user
// Make the teacher the current user.
$this->setUser($teacher);

// Teacher shouldn't have the required capability so they shouldn't be able to see the backup message
// Teacher shouldn't have the required capability so they shouldn't be able to see the backup message.
$this->assertFalse(has_capability('moodle/site:config', $modulecontext));
$providers = message_get_providers_for_user($teacher->id);
$this->assertFalse($this->message_type_present('moodle', 'backup', $providers));

// Give the user the required capability in an activity module
// They should now be able to see the backup message
// Give the user the required capability in an activity module.
// They should now be able to see the backup message.
assign_capability('moodle/site:config', CAP_ALLOW, $teacherrole->id, $modulecontext->id, true);
accesslib_clear_all_caches_for_unit_testing();
$modulecontext = context_module::instance($assign->id);
Expand All @@ -126,9 +127,9 @@ public function test_message_get_providers_for_user_more() {
$providers = message_get_providers_for_user($teacher->id);
$this->assertTrue($this->message_type_present('moodle', 'backup', $providers));

// Prohibit the capability for the user at the course level
// This overrules the CAP_ALLOW at the module level
// They should not be able to see the backup message
// Prohibit the capability for the user at the course level.
// This overrules the CAP_ALLOW at the module level.
// They should not be able to see the backup message.
assign_capability('moodle/site:config', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id, true);
accesslib_clear_all_caches_for_unit_testing();
$modulecontext = context_module::instance($assign->id);
Expand All @@ -142,6 +143,7 @@ public function test_message_get_providers_for_user_more() {

/**
* Is a particular message type in the list of message types.
* @param string $component
* @param string $name a message name.
* @param array $providers as returned by message_get_providers_for_user.
* @return bool whether the message type is present.
Expand Down

0 comments on commit 2988b72

Please sign in to comment.