/** * Tasks that should be performed at cron time */ function tag_cron() { tag_compute_correlations(); tag_cleanup(); }
/** * 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)); }