Exemple #1
0
 /**
  * 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)));
 }
Exemple #2
0
/**
 * Clean up the tag tables, making sure all tagged object still exists.
 *
 * This should normally not be necessary, but in case related tags are not deleted when the tagged record is removed, this should be
 * done once in a while, perhaps on an occasional cron run.  On a site with lots of tags, this could become an expensive function to
 * call: don't run at peak time.
 *
 * @package core_tag
 * @deprecated since 3.1
 */
function tag_cleanup()
{
    debugging('Method tag_cleanup() is deprecated, use \\core\\task\\tag_cron_task::cleanup()', DEBUG_DEVELOPER);
    $task = new \core\task\tag_cron_task();
    return $task->cleanup();
}