[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/badges/tests/ -> badgeslib_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   * Unit tests for badges
  19   *
  20   * @package    core
  21   * @subpackage badges
  22   * @copyright  2013 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Yuliya Bozhko <[email protected]>
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->libdir . '/badgeslib.php');
  31  
  32  class core_badges_badgeslib_testcase extends advanced_testcase {
  33      protected $badgeid;
  34      protected $course;
  35      protected $user;
  36      protected $module;
  37      protected $coursebadge;
  38      protected $assertion;
  39  
  40      protected function setUp() {
  41          global $DB, $CFG;
  42          $this->resetAfterTest(true);
  43  
  44          unset_config('noemailever');
  45  
  46          $CFG->enablecompletion = true;
  47  
  48          $user = $this->getDataGenerator()->create_user();
  49  
  50          $fordb = new stdClass();
  51          $fordb->id = null;
  52          $fordb->name = "Test badge";
  53          $fordb->description = "Testing badges";
  54          $fordb->timecreated = time();
  55          $fordb->timemodified = time();
  56          $fordb->usercreated = $user->id;
  57          $fordb->usermodified = $user->id;
  58          $fordb->issuername = "Test issuer";
  59          $fordb->issuerurl = "http://issuer-url.domain.co.nz";
  60          $fordb->issuercontact = "[email protected]";
  61          $fordb->expiredate = null;
  62          $fordb->expireperiod = null;
  63          $fordb->type = BADGE_TYPE_SITE;
  64          $fordb->courseid = null;
  65          $fordb->messagesubject = "Test message subject";
  66          $fordb->message = "Test message body";
  67          $fordb->attachment = 1;
  68          $fordb->notification = 0;
  69          $fordb->status = BADGE_STATUS_INACTIVE;
  70  
  71          $this->badgeid = $DB->insert_record('badge', $fordb, true);
  72  
  73          // Create a course with activity and auto completion tracking.
  74          $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
  75          $this->user = $this->getDataGenerator()->create_user();
  76          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  77          $this->assertNotEmpty($studentrole);
  78  
  79          // Get manual enrolment plugin and enrol user.
  80          require_once($CFG->dirroot.'/enrol/manual/locallib.php');
  81          $manplugin = enrol_get_plugin('manual');
  82          $maninstance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  83          $manplugin->enrol_user($maninstance, $this->user->id, $studentrole->id);
  84          $this->assertEquals(1, $DB->count_records('user_enrolments'));
  85  
  86          $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
  87          $this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
  88  
  89          // Build badge and criteria.
  90          $fordb->type = BADGE_TYPE_COURSE;
  91          $fordb->courseid = $this->course->id;
  92          $fordb->status = BADGE_STATUS_ACTIVE;
  93  
  94          $this->coursebadge = $DB->insert_record('badge', $fordb, true);
  95          $this->assertion = new stdClass();
  96          $this->assertion->badge = '{"uid":"%s","recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},"badge":"%s","verify":{"type":"hosted","url":"%s"},"issuedOn":"%d","evidence":"%s"}';
  97          $this->assertion->class = '{"name":"%s","description":"%s","image":"%s","criteria":"%s","issuer":"%s"}';
  98          $this->assertion->issuer = '{"name":"%s","url":"%s","email":"%s"}';
  99      }
 100  
 101      public function test_create_badge() {
 102          $badge = new badge($this->badgeid);
 103  
 104          $this->assertInstanceOf('badge', $badge);
 105          $this->assertEquals($this->badgeid, $badge->id);
 106      }
 107  
 108      public function test_clone_badge() {
 109          $badge = new badge($this->badgeid);
 110          $newid = $badge->make_clone();
 111          $cloned_badge = new badge($newid);
 112  
 113          $this->assertEquals($badge->description, $cloned_badge->description);
 114          $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
 115          $this->assertEquals($badge->issuername, $cloned_badge->issuername);
 116          $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
 117          $this->assertEquals($badge->issuerurl, $cloned_badge->issuerurl);
 118          $this->assertEquals($badge->expiredate, $cloned_badge->expiredate);
 119          $this->assertEquals($badge->expireperiod, $cloned_badge->expireperiod);
 120          $this->assertEquals($badge->type, $cloned_badge->type);
 121          $this->assertEquals($badge->courseid, $cloned_badge->courseid);
 122          $this->assertEquals($badge->message, $cloned_badge->message);
 123          $this->assertEquals($badge->messagesubject, $cloned_badge->messagesubject);
 124          $this->assertEquals($badge->attachment, $cloned_badge->attachment);
 125          $this->assertEquals($badge->notification, $cloned_badge->notification);
 126      }
 127  
 128      public function test_badge_status() {
 129          $badge = new badge($this->badgeid);
 130          $old_status = $badge->status;
 131          $badge->set_status(BADGE_STATUS_ACTIVE);
 132          $this->assertAttributeNotEquals($old_status, 'status', $badge);
 133          $this->assertAttributeEquals(BADGE_STATUS_ACTIVE, 'status', $badge);
 134      }
 135  
 136      public function test_delete_badge() {
 137          $badge = new badge($this->badgeid);
 138          $badge->delete();
 139          // We don't actually delete badges. We archive them.
 140          $this->assertAttributeEquals(BADGE_STATUS_ARCHIVED, 'status', $badge);
 141      }
 142  
 143      public function test_create_badge_criteria() {
 144          $badge = new badge($this->badgeid);
 145          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 146          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 147  
 148          $this->assertCount(1, $badge->get_criteria());
 149  
 150          $criteria_profile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
 151          $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
 152          $criteria_profile->save($params);
 153  
 154          $this->assertCount(2, $badge->get_criteria());
 155      }
 156  
 157      public function test_delete_badge_criteria() {
 158          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
 159          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 160          $badge = new badge($this->badgeid);
 161  
 162          $this->assertInstanceOf('award_criteria_overall', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
 163  
 164          $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
 165          $this->assertEmpty($badge->get_criteria());
 166      }
 167  
 168      public function test_badge_awards() {
 169          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 170          $badge = new badge($this->badgeid);
 171          $user1 = $this->getDataGenerator()->create_user();
 172  
 173          $badge->issue($user1->id, true);
 174          $this->assertTrue($badge->is_issued($user1->id));
 175  
 176          $user2 = $this->getDataGenerator()->create_user();
 177          $badge->issue($user2->id, true);
 178          $this->assertTrue($badge->is_issued($user2->id));
 179  
 180          $this->assertCount(2, $badge->get_awards());
 181      }
 182  
 183      public function data_for_message_from_template() {
 184          return array(
 185              array(
 186                  'This is a message with no variables',
 187                  array(), // no params
 188                  'This is a message with no variables'
 189              ),
 190              array(
 191                  'This is a message with %amissing% variables',
 192                  array(), // no params
 193                  'This is a message with %amissing% variables'
 194              ),
 195              array(
 196                  'This is a message with %one% variable',
 197                  array('one' => 'a single'),
 198                  'This is a message with a single variable'
 199              ),
 200              array(
 201                  'This is a message with %one% %two% %three% variables',
 202                  array('one' => 'more', 'two' => 'than', 'three' => 'one'),
 203                  'This is a message with more than one variables'
 204              ),
 205              array(
 206                  'This is a message with %three% %two% %one%',
 207                  array('one' => 'variables', 'two' => 'ordered', 'three' => 'randomly'),
 208                  'This is a message with randomly ordered variables'
 209              ),
 210              array(
 211                  'This is a message with %repeated% %one% %repeated% of variables',
 212                  array('one' => 'and', 'repeated' => 'lots'),
 213                  'This is a message with lots and lots of variables'
 214              ),
 215          );
 216      }
 217  
 218      /**
 219       * @dataProvider data_for_message_from_template
 220       */
 221      public function test_badge_message_from_template($message, $params, $result) {
 222          $this->assertEquals(badge_message_from_template($message, $params), $result);
 223      }
 224  
 225      /**
 226       * Test badges observer when course module completion event id fired.
 227       */
 228      public function test_badges_observer_course_module_criteria_review() {
 229          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 230          $badge = new badge($this->coursebadge);
 231          $this->assertFalse($badge->is_issued($this->user->id));
 232  
 233          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 234          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 235          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
 236          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
 237  
 238          // Set completion for forum activity.
 239          $c = new completion_info($this->course);
 240          $activities = $c->get_activities();
 241          $this->assertEquals(1, count($activities));
 242          $this->assertTrue(isset($activities[$this->module->cmid]));
 243          $this->assertEquals($activities[$this->module->cmid]->name, $this->module->name);
 244  
 245          $current = $c->get_data($activities[$this->module->cmid], false, $this->user->id);
 246          $current->completionstate = COMPLETION_COMPLETE;
 247          $current->timemodified = time();
 248          $sink = $this->redirectEmails();
 249          $c->internal_set_data($activities[$this->module->cmid], $current);
 250          $this->assertCount(1, $sink->get_messages());
 251          $sink->close();
 252  
 253          // Check if badge is awarded.
 254          $this->assertDebuggingCalled('Error baking badge image!');
 255          $this->assertTrue($badge->is_issued($this->user->id));
 256      }
 257  
 258      /**
 259       * Test badges observer when course_completed event is fired.
 260       */
 261      public function test_badges_observer_course_criteria_review() {
 262          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 263          $badge = new badge($this->coursebadge);
 264          $this->assertFalse($badge->is_issued($this->user->id));
 265  
 266          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 267          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 268          $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
 269          $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
 270  
 271          $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
 272  
 273          // Mark course as complete.
 274          $sink = $this->redirectEmails();
 275          $ccompletion->mark_complete();
 276          $this->assertCount(1, $sink->get_messages());
 277          $sink->close();
 278  
 279          // Check if badge is awarded.
 280          $this->assertDebuggingCalled('Error baking badge image!');
 281          $this->assertTrue($badge->is_issued($this->user->id));
 282      }
 283  
 284      /**
 285       * Test badges observer when user_updated event is fired.
 286       */
 287      public function test_badges_observer_profile_criteria_review() {
 288          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 289          $badge = new badge($this->coursebadge);
 290          $this->assertFalse($badge->is_issued($this->user->id));
 291  
 292          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 293          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 294          $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
 295          $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim'));
 296  
 297          $this->user->address = 'Test address';
 298          $this->user->aim = '999999999';
 299          $sink = $this->redirectEmails();
 300          user_update_user($this->user, false);
 301          $this->assertCount(1, $sink->get_messages());
 302          $sink->close();
 303          // Check if badge is awarded.
 304          $this->assertDebuggingCalled('Error baking badge image!');
 305          $this->assertTrue($badge->is_issued($this->user->id));
 306      }
 307  
 308      /**
 309       * Test badges assertion generated when a badge is issued.
 310       */
 311      public function test_badges_assertion() {
 312          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 313          $badge = new badge($this->coursebadge);
 314          $this->assertFalse($badge->is_issued($this->user->id));
 315  
 316          $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 317          $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
 318          $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
 319          $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
 320  
 321          $this->user->address = 'Test address';
 322          $sink = $this->redirectEmails();
 323          user_update_user($this->user, false);
 324          $this->assertCount(1, $sink->get_messages());
 325          $sink->close();
 326          // Check if badge is awarded.
 327          $this->assertDebuggingCalled('Error baking badge image!');
 328          $awards = $badge->get_awards();
 329          $this->assertCount(1, $awards);
 330  
 331          // Get assertion.
 332          $award = reset($awards);
 333          $assertion = new core_badges_assertion($award->uniquehash);
 334          $testassertion = $this->assertion;
 335  
 336          // Make sure JSON strings have the same structure.
 337          $this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
 338          $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
 339          $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
 340      }
 341  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1