Skip to content

Commit

Permalink
MDL-27859 tags: unit test for correlated tags
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Aug 21, 2015
1 parent 6d392b3 commit a196f37
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 12 deletions.
25 changes: 13 additions & 12 deletions tag/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,8 @@ function tag_get_related_tags($tagid, $type=TAG_RELATED_ALL, $limitnum=10) {

if ( $type == TAG_RELATED_ALL || $type == TAG_RELATED_CORRELATED ) {
//gets the correlated tags
$automatic_related_tags = tag_get_correlated($tagid, $limitnum);
if (is_array($automatic_related_tags)) {
$related_tags = array_merge($related_tags, $automatic_related_tags);
}
$automatic_related_tags = tag_get_correlated($tagid);
$related_tags = array_merge($related_tags, $automatic_related_tags);
}

// Remove duplicated tags (multiple instances of the same tag).
Expand Down Expand Up @@ -1378,13 +1376,21 @@ function tag_get_name($tagids) {
* Returns the correlated tags of a tag, retrieved from the tag_correlation table. Make sure cron runs, otherwise the table will be
* empty and this function won't return anything.
*
* Correlated tags are calculated in cron based on existing tag instances.
*
* This function will return as many entries as there are existing tag instances,
* which means that there will be duplicates for each tag.
*
* If you need only one record for each correlated tag please call:
* tag_get_related_tags($tag_id, TAG_RELATED_CORRELATED);
*
* @package core_tag
* @access private
* @param int $tag_id is a single tag id
* @param int $limitnum this parameter does not appear to have any function???
* @param int $notused this argument is no longer used
* @return array an array of tag objects or an empty if no correlated tags are found
*/
function tag_get_correlated($tag_id, $limitnum=null) {
function tag_get_correlated($tag_id, $notused = null) {
global $DB;

$tag_correlation = $DB->get_record('tag_correlation', array('tagid'=>$tag_id));
Expand All @@ -1399,12 +1405,7 @@ function tag_get_correlated($tag_id, $limitnum=null) {
INNER JOIN {tag_instance} ti ON tg.id = ti.tagid
WHERE tg.id IN ({$tag_correlation->correlatedtags})
ORDER BY ti.ordering ASC";
$result = $DB->get_records_sql($sql);
if (!$result) {
return array();
}

return $result;
return $DB->get_records_sql($sql);
}

/**
Expand Down
96 changes: 96 additions & 0 deletions tag/tests/taglib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,100 @@ public function test_tag_bulk_delete_instances() {
$instancecount = $DB->count_records('tag_instance');
$this->assertEquals(0, $instancecount);
}

/**
* Test for function tag_compute_correlations() that is part of tag cron
*/
public function test_correlations() {
global $DB;
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$user5 = $this->getDataGenerator()->create_user();
$user6 = $this->getDataGenerator()->create_user();

// Several records have both 'cat' and 'cats' tags attached to them.
// This will make those tags automatically correlated.
// Same with 'dog', 'dogs' and 'puppy.
tag_set('user', $user1->id, array('cat', 'cats'),
'core', context_user::instance($user1->id)->id);
tag_set('user', $user2->id, array('cat', 'cats', 'kitten'),
'core', context_user::instance($user2->id)->id);
tag_set('user', $user3->id, array('cat', 'cats'),
'core', context_user::instance($user3->id)->id);
tag_set('user', $user4->id, array('dog', 'dogs', 'puppy'),
'core', context_user::instance($user4->id)->id);
tag_set('user', $user5->id, array('dog', 'dogs', 'puppy'),
'core', context_user::instance($user5->id)->id);
tag_set('user', $user6->id, array('dog', 'dogs', 'puppy'),
'core', context_user::instance($user6->id)->id);

$tags = tag_get_id(array('cat', 'cats', 'dog', 'dogs', 'kitten', 'puppy'));

// Add manual relation between tags 'cat' and 'kitten'.
tag_set('tag', $tags['cat'], array('kitten'), 'core', context_system::instance()->id);

tag_compute_correlations();

$this->assertEquals($tags['cats'],
$DB->get_field_select('tag_correlation', 'correlatedtags',
'tagid = ?', array($tags['cat'])));
$this->assertEquals($tags['cat'],
$DB->get_field_select('tag_correlation', 'correlatedtags',
'tagid = ?', array($tags['cats'])));
$this->assertEquals($tags['dogs'] . ',' . $tags['puppy'],
$DB->get_field_select('tag_correlation', 'correlatedtags',
'tagid = ?', array($tags['dog'])));
$this->assertEquals($tags['dog'] . ',' . $tags['puppy'],
$DB->get_field_select('tag_correlation', 'correlatedtags',
'tagid = ?', array($tags['dogs'])));
$this->assertEquals($tags['dog'] . ',' . $tags['dogs'],
$DB->get_field_select('tag_correlation', 'correlatedtags',
'tagid = ?', array($tags['puppy'])));

// Make sure tag_get_correlated() returns 'cats' as the only correlated tag to the 'cat'.
$correlatedtags = array_values(tag_get_correlated($tags['cat']));
$this->assertCount(3, $correlatedtags); // This will return all existing instances but they all point to the same tag.
$this->assertEquals('cats', $correlatedtags[0]->rawname);
$this->assertEquals('cats', $correlatedtags[1]->rawname);
$this->assertEquals('cats', $correlatedtags[2]->rawname);

$correlatedtags = array_values(tag_get_related_tags($tags['cat'], TAG_RELATED_CORRELATED));
$this->assertCount(1, $correlatedtags); // Duplicates are filtered out here.
$this->assertEquals('cats', $correlatedtags[0]->rawname);

// Make sure tag_get_correlated() returns 'dogs' and 'puppy' as the correlated tags to the 'dog'.
$correlatedtags = array_values(tag_get_correlated($tags['dog']));
$this->assertCount(6, $correlatedtags); // 2 tags times 3 instances.

$correlatedtags = array_values(tag_get_related_tags($tags['dog'], TAG_RELATED_CORRELATED));
$this->assertCount(2, $correlatedtags);
$this->assertEquals('dogs', $correlatedtags[0]->rawname);
$this->assertEquals('puppy', $correlatedtags[1]->rawname);

// Function tag_get_related_tags() with default argument will return both related and correlated tags.
$relatedtags = array_values(tag_get_related_tags($tags['cat']));
$this->assertCount(2, $relatedtags);
$this->assertEquals('kitten', $relatedtags[0]->rawname);
$this->assertEquals('cats', $relatedtags[1]->rawname);

// If we then manually set 'cat' and 'cats' as related, tag_get_related_tags() will filter out duplicates.
tag_set('tag', $tags['cat'], array('kitten', 'cats'), 'core', context_system::instance()->id);

$relatedtags = array_values(tag_get_related_tags($tags['cat']));
$this->assertCount(2, $relatedtags);
$this->assertEquals('kitten', $relatedtags[0]->rawname);
$this->assertEquals('cats', $relatedtags[1]->rawname);

// Make sure tag_get_correlated() and tag_get_tags() return the same set of fields.
$relatedtags = tag_get_tags('tag', $tags['cat']);
$relatedtag = reset($relatedtags);
$correlatedtags = tag_get_correlated($tags['cat']);
$correlatedtag = reset($correlatedtags);
$this->assertEquals(array_keys((array)$relatedtag), array_keys((array)$correlatedtag));
}
}

0 comments on commit a196f37

Please sign in to comment.