/** * Tasks that should be performed at cron time * * @package core_tag * @deprecated since 3.1 */ function tag_cron() { debugging('Method tag_cron() is deprecated, use \\core\\task\\tag_cron_task::execute()', DEBUG_DEVELOPER); $task = new \core\task\tag_cron_task(); $task->execute(); }
/** * Test for function tag_cleanup() that is part of tag cron */ public function test_cleanup() { global $DB; $task = new \core\task\tag_cron_task(); $user = $this->getDataGenerator()->create_user(); $defaultcoll = core_tag_collection::get_default(); // Setting tags will create non-official tags 'cat', 'dog' and 'fish'. core_tag_tag::set_item_tags('core', 'user', $user->id, context_user::instance($user->id), array('cat', 'dog', 'fish')); $this->assertTrue($DB->record_exists('tag', array('name' => 'cat'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'dog'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'fish'))); // Make tag 'dog' official. $dogtag = core_tag_tag::get_by_name($defaultcoll, 'dog', '*'); $fishtag = core_tag_tag::get_by_name($defaultcoll, 'fish'); $dogtag->update(array('tagtype' => 'official')); // Manually remove the instances pointing on tags 'dog' and 'fish'. $DB->execute('DELETE FROM {tag_instance} WHERE tagid in (?,?)', array($dogtag->id, $fishtag->id)); // Call tag_cleanup(). $task->cleanup(); // Tag 'cat' is still present because it's used. Tag 'dog' is present because it's official. // Tag 'fish' was removed because it is not official and it is no longer used by anybody. $this->assertTrue($DB->record_exists('tag', array('name' => 'cat'))); $this->assertTrue($DB->record_exists('tag', array('name' => 'dog'))); $this->assertFalse($DB->record_exists('tag', array('name' => 'fish'))); // Delete user without using API function. $DB->update_record('user', array('id' => $user->id, 'deleted' => 1)); // Call tag_cleanup(). $task->cleanup(); // Tag 'cat' was now deleted too. $this->assertFalse($DB->record_exists('tag', array('name' => 'cat'))); // Assign tag to non-existing record. Make sure tag was created in the DB. core_tag_tag::set_item_tags('core', 'course', 1231231, context_system::instance(), array('bird')); $this->assertTrue($DB->record_exists('tag', array('name' => 'bird'))); // Call tag_cleanup(). $task->cleanup(); // Tag 'bird' was now deleted because the related record does not exist in the DB. $this->assertFalse($DB->record_exists('tag', array('name' => 'bird'))); // Now we have a tag instance pointing on 'sometag' tag. $user = $this->getDataGenerator()->create_user(); core_tag_tag::set_item_tags('core', 'user', $user->id, context_user::instance($user->id), array('sometag')); $sometag = core_tag_tag::get_by_name($defaultcoll, 'sometag'); $this->assertTrue($DB->record_exists('tag_instance', array('tagid' => $sometag->id))); // Some hacker removes the tag without using API. $DB->delete_records('tag', array('id' => $sometag->id)); // Call tag_cleanup(). $task->cleanup(); // The tag instances were also removed. $this->assertFalse($DB->record_exists('tag_instance', array('tagid' => $sometag->id))); }
/** * Testing function core_tag_tag::combine_tags() when correlated tags are present. */ public function test_combine_tags_with_correlated() { $task = new \core\task\tag_cron_task(); $tags = $this->prepare_correlated(); $task->compute_correlations(); // Now 'cat' is correlated with 'cats'. // Also 'dog', 'dogs' and 'puppy' are correlated. // There is a manual relation between 'cat' and 'kitten'. // See function test_correlations() for assertions. // Combine tags 'dog' and 'kitten' into 'cat' and make sure that cat is now correlated with dogs and puppy. $tags['cat']->combine_tags(array($tags['dog'], $tags['kitten'])); $correlatedtags = $this->get_correlated_tags_names($tags['cat']); $this->assertEquals(['cats', 'dogs', 'puppy'], $correlatedtags); $correlatedtags = $this->get_correlated_tags_names($tags['dogs']); $this->assertEquals(['cat', 'puppy'], $correlatedtags); $correlatedtags = $this->get_correlated_tags_names($tags['puppy']); $this->assertEquals(['cat', 'dogs'], $correlatedtags); // Add tag that does not have any correlations. $user7 = $this->getDataGenerator()->create_user(); core_tag_tag::set_item_tags('core', 'user', $user7->id, context_user::instance($user7->id), array('hippo')); $tags['hippo'] = core_tag_tag::get_by_name(core_tag_collection::get_default(), 'hippo', '*'); // Combine tag 'cat' into 'hippo'. Now 'hippo' should have the same correlations 'cat' used to have and also // tags 'dogs' and 'puppy' should have 'hippo' in correlations. $tags['hippo']->combine_tags(array($tags['cat'])); $correlatedtags = $this->get_correlated_tags_names($tags['hippo']); $this->assertEquals(['cats', 'dogs', 'puppy'], $correlatedtags); $correlatedtags = $this->get_correlated_tags_names($tags['dogs']); $this->assertEquals(['hippo', 'puppy'], $correlatedtags); $correlatedtags = $this->get_correlated_tags_names($tags['puppy']); $this->assertEquals(['dogs', 'hippo'], $correlatedtags); }