From cb04c9cb1e5f7d9f499ef2eb69c8ad2fed5aa65b Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 6 Jan 2015 15:02:25 +1300 Subject: [PATCH] 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. --- badges/tests/badgeslib_test.php | 89 +++++++++++++++++++++++++++++++++ lib/badgeslib.php | 14 +++--- 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index aa074b47d8bd9..01332a659d39f 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -180,6 +180,95 @@ public function test_badge_awards() { $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( diff --git a/lib/badgeslib.php b/lib/badgeslib.php index f2d397c4625d9..57aca65d80676 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -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);