mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
MDL-48753 badges: converted func to named params
Converted the SQL params within badges_get_user_badges to named params. Unit tests added for this function at the same time. UI covered by behat already.
This commit is contained in:
parent
eb1dc9fab9
commit
d237385e8a
@ -178,6 +178,95 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
|
||||
$this->assertCount(2, $badge->get_awards());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the {@link badges_get_user_badges()} function in lib/badgeslib.php
|
||||
*/
|
||||
public function test_badges_get_user_badges() {
|
||||
global $DB;
|
||||
|
||||
// Messaging is not compatible with transactions.
|
||||
$this->preventResetByRollback();
|
||||
|
||||
$badges = array();
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
// Record the current time, we need to be precise about a couple of things.
|
||||
$now = time();
|
||||
// Create 11 badges with which to test.
|
||||
for ($i = 1; $i <= 11; $i++) {
|
||||
// Mock up a badge.
|
||||
$badge = new stdClass();
|
||||
$badge->id = null;
|
||||
$badge->name = "Test badge $i";
|
||||
$badge->description = "Testing badges $i";
|
||||
$badge->timecreated = $now - 12;
|
||||
$badge->timemodified = $now - 12;
|
||||
$badge->usercreated = $user1->id;
|
||||
$badge->usermodified = $user1->id;
|
||||
$badge->issuername = "Test issuer";
|
||||
$badge->issuerurl = "http://issuer-url.domain.co.nz";
|
||||
$badge->issuercontact = "issuer@example.com";
|
||||
$badge->expiredate = null;
|
||||
$badge->expireperiod = null;
|
||||
$badge->type = BADGE_TYPE_SITE;
|
||||
$badge->courseid = null;
|
||||
$badge->messagesubject = "Test message subject for badge $i";
|
||||
$badge->message = "Test message body for badge $i";
|
||||
$badge->attachment = 1;
|
||||
$badge->notification = 0;
|
||||
$badge->status = BADGE_STATUS_INACTIVE;
|
||||
|
||||
$badgeid = $DB->insert_record('badge', $badge, true);
|
||||
$badges[$badgeid] = new badge($badgeid);
|
||||
$badges[$badgeid]->issue($user2->id, true);
|
||||
// Check it all actually worked.
|
||||
$this->assertCount(1, $badges[$badgeid]->get_awards());
|
||||
|
||||
// Hack the database to adjust the time each badge was issued.
|
||||
// The alternative to this is sleep which is a no-no in unit tests.
|
||||
$DB->set_field('badge_issued', 'dateissued', $now - 11 + $i, array('userid' => $user2->id, 'badgeid' => $badgeid));
|
||||
}
|
||||
|
||||
// Make sure the first user has no badges.
|
||||
$result = badges_get_user_badges($user1->id);
|
||||
$this->assertInternalType('array', $result);
|
||||
$this->assertCount(0, $result);
|
||||
|
||||
// Check that the second user has the expected 11 badges.
|
||||
$result = badges_get_user_badges($user2->id);
|
||||
$this->assertCount(11, $result);
|
||||
|
||||
// Test pagination.
|
||||
// Ordering is by time issued desc, so things will come out with the last awarded badge first.
|
||||
$result = badges_get_user_badges($user2->id, 0, 0, 4);
|
||||
$this->assertCount(4, $result);
|
||||
$lastbadgeissued = reset($result);
|
||||
$this->assertSame('Test badge 11', $lastbadgeissued->name);
|
||||
// Page 2. Expecting 4 results again.
|
||||
$result = badges_get_user_badges($user2->id, 0, 1, 4);
|
||||
$this->assertCount(4, $result);
|
||||
$lastbadgeissued = reset($result);
|
||||
$this->assertSame('Test badge 7', $lastbadgeissued->name);
|
||||
// Page 3. Expecting just three results here.
|
||||
$result = badges_get_user_badges($user2->id, 0, 2, 4);
|
||||
$this->assertCount(3, $result);
|
||||
$lastbadgeissued = reset($result);
|
||||
$this->assertSame('Test badge 3', $lastbadgeissued->name);
|
||||
// Page 4.... there is no page 4.
|
||||
$result = badges_get_user_badges($user2->id, 0, 3, 4);
|
||||
$this->assertCount(0, $result);
|
||||
|
||||
// Test search.
|
||||
$result = badges_get_user_badges($user2->id, 0, 0, 0, 'badge 1');
|
||||
$this->assertCount(3, $result);
|
||||
$lastbadgeissued = reset($result);
|
||||
$this->assertSame('Test badge 11', $lastbadgeissued->name);
|
||||
// The term Totara doesn't appear anywhere in the badges.
|
||||
$result = badges_get_user_badges($user2->id, 0, 0, 0, 'Totara');
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
public function data_for_message_from_template() {
|
||||
return array(
|
||||
array(
|
||||
|
@ -843,9 +843,10 @@ function badges_get_badges($type, $courseid = 0, $sort = '', $dir = '', $page =
|
||||
*/
|
||||
function badges_get_user_badges($userid, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) {
|
||||
global $DB;
|
||||
$badges = array();
|
||||
|
||||
$params[] = $userid;
|
||||
$params = array(
|
||||
'userid' => $userid
|
||||
);
|
||||
$sql = 'SELECT
|
||||
bi.uniquehash,
|
||||
bi.dateissued,
|
||||
@ -860,18 +861,19 @@ function badges_get_user_badges($userid, $courseid = 0, $page = 0, $perpage = 0,
|
||||
{user} u
|
||||
WHERE b.id = bi.badgeid
|
||||
AND u.id = bi.userid
|
||||
AND bi.userid = ?';
|
||||
AND bi.userid = :userid';
|
||||
|
||||
if (!empty($search)) {
|
||||
$sql .= ' AND (' . $DB->sql_like('b.name', '?', false) . ') ';
|
||||
$params[] = "%$search%";
|
||||
$sql .= ' AND (' . $DB->sql_like('b.name', ':search', false) . ') ';
|
||||
$params['search'] = '%'.$DB->sql_like_escape($search).'%';
|
||||
}
|
||||
if ($onlypublic) {
|
||||
$sql .= ' AND (bi.visible = 1) ';
|
||||
}
|
||||
|
||||
if ($courseid != 0) {
|
||||
$sql .= ' AND (b.courseid = ' . $courseid . ') ';
|
||||
$sql .= ' AND (b.courseid = :courseid) ';
|
||||
$params['courseid'] = $courseid;
|
||||
}
|
||||
$sql .= ' ORDER BY bi.dateissued DESC';
|
||||
$badges = $DB->get_records_sql($sql, $params, $page * $perpage, $perpage);
|
||||
|
Loading…
x
Reference in New Issue
Block a user